eric256 has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|