Commit | Line | Data |
7fc99864 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Moose::Autobox; |
7 | use Moose::Autobox::Undef; |
8 | |
9 | use autobox UNDEF => 'Moose::Autobox::Undef'; |
10 | |
11 | sub 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 | |
20 | my $g = [ ('.') x 9 ]; |
21 | |
22 | $g->print_board(); |
23 | |
24 | my $players = { 'X' => 'Player 1', 'O' => 'Player 2' }; |
25 | |
26 | my $player_character = [ 'X', 'O' ]->any; |
27 | |
28 | my $entered = {}; |
29 | my $choice = [ 1 .. 9 ]->any; |
30 | |
31 | my $player = 'X'; |
32 | while ($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 | |
87 | tic_tac_toe.p6 - Tic-Tac-Toe |
88 | |
89 | =head1 DESCRIPTION |
90 | |
91 | This is a Moose::Autobox port of a perl6 implementation |
92 | of the classic Tic-Tac-Toe game. |
93 | |
94 | This uses a modified version of the one Rob Kinyon created |
95 | L<http://www.perlmonks.org/index.pl?node_id=451302>. |
96 | |
97 | =head1 AUTHORS |
98 | |
99 | mkirank L<http://www.perlmonks.org/index.pl?node_id=451261> |
100 | |
101 | Rob Kinyon L<http://www.perlmonks.org/index.pl?node_id=451302> |
102 | |
103 | Stevan Little, E<lt>stevan@iinteractive.comE<gt> |
104 | |
105 | Audrey Tang |
106 | |
107 | =cut |
108 | |