From: Stevan Little Date: Sun, 25 Jun 2006 02:15:52 +0000 (+0000) Subject: fpp X-Git-Tag: 0_02^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c11e6a7483ba10a1f1d407db834163a0637e9ae8;p=gitmo%2FMoose-Autobox.git fpp --- diff --git a/Changes b/Changes index a559105..cca0a5e 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,9 @@ Revision history for Perl extension Moose::Autobox 0.02 - fixed Moose::Autobox so that PAUSE won't try to index SCALAR, ARRAY, HASH and CODE. + + - added examples/ directory + - ported tic-tac-toe from the Pugs examples * Moose::Autobox::Ref - added &dump method which will use Data::Dumper diff --git a/MANIFEST b/MANIFEST index 2e2f6ea..72a60a2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ META.yml MANIFEST MANIFEST.SKIP README +examples/tic_tac_toe.t lib/Moose/Autobox.pm lib/Moose/Autobox/Array.pm lib/Moose/Autobox/Code.pm diff --git a/examples/tic_tac_toe.t b/examples/tic_tac_toe.t index 18de440..24f872a 100644 --- a/examples/tic_tac_toe.t +++ b/examples/tic_tac_toe.t @@ -8,7 +8,7 @@ use Moose::Autobox::Undef; use autobox UNDEF => 'Moose::Autobox::Undef'; -sub ARRAY::print_board { +sub print_board { my ($b) = @_; my $count = 0; $b->map(sub { @@ -17,22 +17,17 @@ sub ARRAY::print_board { }); } -my $g = [ ('.') x 9 ]; +my $board = [ ('.') x 9 ]; -$g->print_board(); +print_board($board); -my $players = { 'X' => 'Player 1', 'O' => 'Player 2' }; - -my $player_character = [ 'X', 'O' ]->any; - -my $entered = {}; my $choice = [ 1 .. 9 ]->any; my $player = 'X'; -while ($g->all == '.') { +while ($board->any eq '.') { INPUT: { - print($players->{$player} . " Enter the Position [1-9]: "); + print("Player ($player), enter the Position [1-9]: "); my $in = <>; unless ($in == $choice) { @@ -41,39 +36,29 @@ while ($g->all == '.') { } my $idx = $in - 1; - if ($entered->exists($idx)) { + if ($board->[$idx] ne '.') { print "\n\tElement already entered at $in\n"; redo INPUT; } - $g->[$idx] = $player; - $entered->{$idx}++; + $board->[$idx] = $player; } - $g->print_board; + print_board($board); [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ], [ 0, 3, 6 ], [ 1, 4, 7 ], [ 2, 5, 8 ], [ 0, 4, 8 ], [ 2, 4, 6 ], - ]->map(sub { - - #my @row = @{$g}[$_->[0], $_->[1], $_->[2]]; - #my $row = \@row; - #warn $row->dump; - # FIX ME - #(($row->all == 'X') || ($row->all == 'O'))&& warn "Wow, this worked"; - - if ( ( $players->exists($g->[$_->[0]]) && - $players->exists($g->[$_->[1]]) && - $players->exists($g->[$_->[2]]) ) - && - ( ( $g->[$_->[0]] eq $g->[$_->[1]] ) && - ( $g->[$_->[1]] eq $g->[$_->[2]] ) ) ) - { - print("\n\t$players->{$player} Wins\n"); + ]->map(sub { + + my $row = $board->slice($_); + + if (($row->all eq 'X') || ($row->all eq 'O')) { + print("\n\tPlayer ($player) Wins\n"); exit; } + }); $player = $player eq 'X' ? 'O' : 'X'; @@ -94,7 +79,14 @@ of the classic Tic-Tac-Toe game. This uses a modified version of the one Rob Kinyon created L. -=head1 AUTHORS +=head1 AUTHOR + +Stevan Little, Estevan@iinteractive.comE + +=head1 ACKNOLEDGEMENTS + +This code was ported from the version in the Pugs examples/ +directory. The authors of that were: mkirank L diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 27680b5..03dc46b 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -36,7 +36,12 @@ sub delete { sub shift { my ($array) = @_; CORE::shift @$array; -} +} + +sub slice { + my ($array, $indicies) = @_; + [ @{$array}[ @{$indicies} ] ]; +} # NOTE: # sprintf args need to be reversed, @@ -179,6 +184,8 @@ This is a role to describe operations on the Array type. =item B +=item B + =back =head2 Indexed implementation diff --git a/t/001_basic.t b/t/001_basic.t index beb218c..fc0f00b 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 55; +use Test::More tests => 56; BEGIN { use_ok('Moose::Autobox'); @@ -93,6 +93,12 @@ is_deeply($a, [ 4, 2, 6, 78, 101, 2 ], '... original value is now changed'); is($a->shift(), 4, '... got the right unshift-ed value'); is_deeply($a, [ 2, 6, 78, 101, 2 ], '... original value is now changed'); + +is_deeply( +$a->slice([ 1, 2, 4 ]), +[ 6, 78, 2 ], +'... got the right sliced value'); + is_deeply( $a->unshift(10), [ 10, 2, 6, 78, 101, 2 ],