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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.