Autooooooooobox
[gitmo/Moose-Autobox.git] / examples / tic_tac_toe.t
CommitLineData
7fc99864 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Moose::Autobox;
7use Moose::Autobox::Undef;
8
9use autobox UNDEF => 'Moose::Autobox::Undef';
10
11sub ARRAY::print_board {
12 my ($b) = @_;
13 my $count = 0;
14 $b->map(sub {
15 print("$_ \t");
16 print("\n") unless ((++$count) % 3);
17 });
18}
19
20my $g = [ ('.') x 9 ];
21
22$g->print_board();
23
24my $players = { 'X' => 'Player 1', 'O' => 'Player 2' };
25
26my $player_character = [ 'X', 'O' ]->any;
27
28my $entered = {};
29my $choice = [ 1 .. 9 ]->any;
30
31my $player = 'X';
32while ($g->all == '.') {
33
34 INPUT: {
35 print($players->{$player} . " Enter the Position [1-9]: ");
36 my $in = <>;
37
38 unless ($in == $choice) {
39 print "\n\tPlease enter a value within 1-9\n\n";
40 redo INPUT;
41 }
42
43 my $idx = $in - 1;
44 if ($entered->exists($idx)) {
45 print "\n\tElement already entered at $in\n";
46 redo INPUT;
47 }
48
49 $g->[$idx] = $player;
50 $entered->{$idx}++;
51 }
52
53 $g->print_board;
54
55 [
56 [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ],
57 [ 0, 3, 6 ], [ 1, 4, 7 ], [ 2, 5, 8 ],
58 [ 0, 4, 8 ], [ 2, 4, 6 ],
59 ]->map(sub {
60
61 #my @row = @{$g}[$_->[0], $_->[1], $_->[2]];
62 #my $row = \@row;
63 #warn $row->dump;
64 # FIX ME
65 #(($row->all == 'X') || ($row->all == 'O'))&& warn "Wow, this worked";
66
67 if ( ( $players->exists($g->[$_->[0]]) &&
68 $players->exists($g->[$_->[1]]) &&
69 $players->exists($g->[$_->[2]]) )
70 &&
71 ( ( $g->[$_->[0]] eq $g->[$_->[1]] ) &&
72 ( $g->[$_->[1]] eq $g->[$_->[2]] ) ) )
73 {
74 print("\n\t$players->{$player} Wins\n");
75 exit;
76 }
77 });
78
79 $player = $player eq 'X' ? 'O' : 'X';
80}
81
82
83=pod
84
85=head1 NAME
86
87tic_tac_toe.p6 - Tic-Tac-Toe
88
89=head1 DESCRIPTION
90
91This is a Moose::Autobox port of a perl6 implementation
92of the classic Tic-Tac-Toe game.
93
94This uses a modified version of the one Rob Kinyon created
95L<http://www.perlmonks.org/index.pl?node_id=451302>.
96
97=head1 AUTHORS
98
99mkirank L<http://www.perlmonks.org/index.pl?node_id=451261>
100
101Rob Kinyon L<http://www.perlmonks.org/index.pl?node_id=451302>
102
103Stevan Little, E<lt>stevan@iinteractive.comE<gt>
104
105Audrey Tang
106
107=cut
108