Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

So I decided to start learning. The past couple of days I got the hang of downloading the latest versions and compiling them. Then I saw object support and I had to give it a shot. So here is a very rough version of the Wizard game posted earlier. Okay it isn't much like that game at all but that is where I got the idea. So here it is. It makes use of classes, attributes, methods, given, when, junctions and that is probably all. I don't think object in heritance is working yet but when it is there are some obvious places to add it to this. There are some bugs that had to be worked around ('when' + 'junction' never returns true. Thanks to autrijus for help finding and working around the bugs and for commit access. I've already commited some tests and this game in the example directory. So without further blubering here it is. Any comments? Improvements? More Perl6ish ways of doing things? Enjoy!

use v6; sub prompt (?$prompt) { print $prompt; my $temp; ($temp= =<>).chomp; return $temp; }; class Weapon { has $.name is rw; has $.powerLow is rw; has $.powerHigh is rw; method damage { return int(rand($.powerHigh - $.powerLow + 1) + $.p +owerLow); }; } class Person { has $.location is rw; has $.name is rw; has $.life is rw; has $.attach is rw; has $.spell is rw; has %.weapons is rw; method where () { return "You are currently in the $.location"; }; method battle_choice (Monster $enemy) { my $choice; say $enemy.name, " is attatcking you! What will you do?"; until ($choice eq 'f' or $enemy.dead) { for $.weapons.kv -> $key, $wep { say "\t$key-attack with $wep.name()" } say "\tf-flee in terror!"; $choice = prompt("Your choice?"); given $choice { when 'f' { say "You ran away from the $enemy.name()!" } if ($.weapons.exists($_)) { .attack($enemy, $.weapons{$_}); } else { say "Please enter a valid command!" } } } say "Ths $enemy.name()", " is dead!" unless $choice eq 'f'; } method attack (Monster $enemy,Weapon $weapon) { say "You attack the $enemy.name()", " with your $weapon.name() +!"; $enemy.hit($weapon.damage); .hit($enemy.attack()); say "Your health: $.life\t$enemy.name(): $enemy.life()"; } method hit ($power) { $.life -= $power; $.life = 0 if $.life < 0; + } method dead () { $.life <= 0 } } class Monster { has $.name is rw; has $.gold is rw; has $.life is rw; has $.weapon is rw; method hit ($power) { $.life -= $power; $.life = 0 if $.life < 0; + } method dead () { $.life <= 0 }; method attack () { my $wep = $.weapon; $wep.damage; # $.weapon.damage # doesn't work for some reason... }; } my $person = Person.new(:life(100), :attack(1), :spell(2)); $person.weapons<a> = Weapon.new(:name<sword>, :powerLow(3), :powerHigh +(5) ); $person.weapons<s> = Weapon.new(:name<spell>, :powerLow(0), :powerHigh +(7) ); my $wep = Weapon.new(:powerLow(3), :powerHigh(5)); my $enemy = Monster.new(:name("Army of frogs"), :gold(int rand 100), +:life(50), :weapon($wep) ); $person.location = "Lobby"; $person.name = prompt("What is your name: "); say "Hello $person.name()"; say $person.where; $person.battle_choice($enemy);

___________
Eric Hodges

In reply to P6: OO Wizard Game (RPG kinda, sorta) by eric256

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-23 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found