http://www.perlmonks.org?node_id=456587

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

Replies are listed 'Best First'.
Re: P6: OO Wizard Game (RPG kinda, sorta)
by Juerd (Abbot) on May 13, 2005 at 13:09 UTC

    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) );
    I suggest:
    given my $person = Person.new(...) { given .weapons { .<a> = Weapon.new(...); .<b> = Weapon.new(...); } }
    Or simply:
    given my $person = Person.new(...) { .weapons = { a => Weapon.new(...), b => Weapon.new(...) }; }
    Or maybe just
    my $person = Person.new( ..., weapons => { a => Weapon.new(...), b => Weapon.new(...) } };
    I personally think :pair() style should not be used for setting anything that's clearly key and value in a hash or object. :foo() is very nice for things that alter behaviour:
    my $socket = Socket.new( localport => 80, localaddr => '127.0.0.1', :proto<tcp> :listen(5) :timeout(500) :reuse ); my @users = slurp '/etc/passwd' :chomp; my @even = 2..10 :by(2);

    All code is untested.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: P6: OO Wizard Game (RPG kinda, sorta)
by dragonchild (Archbishop) on May 13, 2005 at 02:25 UTC
    I haven't kept up with Pugs OO (cause cygwin sucks rocks), but can't you create a base class for Person and Monster? Something along the lines of:
    class Baseclass { has Int $.life is rw; has Weapon $.weapon is rw; method hit (Int $power) { $.life -= $power; }; method dead () { $.life <= 0 }; method attack() { $.weapon.damage } } class Monster is Baseclass { }; class Person is Baseclass { };

    Where $.weapon is the active weapon and the rest stay in the inventory.

    Also, note the variable types. :-)


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

      I'm fairly certain that inheritance isn't implemented yet. As soon as it is or at least i find out it is or get a minute to check i will be updating it.


      ___________
      Eric Hodges
      I haven't kept up with Pugs OO (cause cygwin sucks rocks)
      If you're running Windows you can still check out Pugs without the help of Cygwin - all you need is GHC 6.4, Perl (not sure the lower version limit) and nmake. If you also want Rules support you'll also need Parrot, which can be slightly trickier to compile, depending on your setup.
      HTH

      _________
      broquaint

        Cool thanks for the info, that will be useful for me too, i am on windows :)
        rpg game
Re: P6: OO Wizard Game (RPG kinda, sorta)
by Juerd (Abbot) on May 13, 2005 at 12:57 UTC

    I think that

    when 'f' { say "You ran away from the $enemy.name()!" } if ($.weapons.exists($_)) { .attack($enemy, $.weapons{$_}); } else { say "Please enter a valid command!" }
    Is much prettier when written as
    when 'f' { say "You ran away from the { $enemy.name }!" } when $.weapons { .attack($enemy, $.weapons{$_}); } default { say "Please enter a valid command!" }
    "when" combined with "if"/"else" just doesn't feel right. It's better style to write %.weapons, by the way. (Why would $.weapons even work? It's not declared!)

    Note that my comments are not tested against the reality of Pugs.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Don't you have to say .attack($enemy, %.weapons{$_}); }? $.weapons doesn't exist, only %.weapons does.

      • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
      • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

        Don't you have to say .attack($enemy, %.weapons{$_}); }?

        Yes.

      The if isn't all that ugly ;). Either way the code you suggested doesn't work yet. I didn't even know that smart matching would do a hash key lookup...or what yours suppose to do? On the $ versus % note, I have updated my code with that, once I get some more updates I will probably recommit it.


      ___________
      Eric Hodges

        I didn't even know that smart matching would do a hash key lookup

        See S04. Hash ~~ Any has no *, so it is reversible: Any ~~ Hash.

        I have updated my code with that, once I get some more updates I will probably recommit it.

        Why wait?

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }