line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::TicTacToe::Move; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$Games::TicTacToe::Move::VERSION = '0.25'; |
4
|
|
|
|
|
|
|
$Games::TicTacToe::Move::AUTHOR = 'cpan:MANWAR'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Games::TicTacToe::Move - Interface to the TicTacToe game's move. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.25 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
10
|
|
|
10
|
|
172
|
use 5.006; |
|
10
|
|
|
|
|
33
|
|
17
|
10
|
|
|
10
|
|
52
|
use strict; use warnings; |
|
10
|
|
|
10
|
|
19
|
|
|
10
|
|
|
|
|
185
|
|
|
10
|
|
|
|
|
61
|
|
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
244
|
|
18
|
10
|
|
|
10
|
|
48
|
use Data::Dumper; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
7838
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 DESCRIPTION |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
It is used internally by L. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 METHODS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 foundWinner($player, $board) |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Return 1 or 0 depending wether we have a winner or not. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub foundWinner { |
33
|
2
|
|
|
2
|
1
|
1565
|
my ($player, $board) = @_; |
34
|
|
|
|
|
|
|
|
35
|
2
|
100
|
|
|
|
15
|
die("ERROR: Player not defined.\n") unless defined $player; |
36
|
1
|
50
|
|
|
|
7
|
die("ERROR: Board not defined.\n") unless defined $board; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
0
|
my $size = sqrt($board->getSize); |
39
|
0
|
|
|
|
|
0
|
my $winning_moves = ___winningMoves($size); |
40
|
0
|
|
|
|
|
0
|
foreach my $move (@$winning_moves) { |
41
|
0
|
0
|
|
|
|
0
|
return 1 if $board->belongsToPlayer($move, $player); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
return 0; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 now($player, $board) |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Make a move now for computer. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub now { |
54
|
2
|
|
|
2
|
1
|
1905
|
my ($player, $board) = @_; |
55
|
|
|
|
|
|
|
|
56
|
2
|
100
|
|
|
|
13
|
die("ERROR: Player not defined.\n") unless defined $player; |
57
|
1
|
50
|
|
|
|
8
|
die("ERROR: Board not defined.\n") unless defined $board; |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
my $move = _getBestMove($board, $player); |
60
|
0
|
0
|
|
|
|
|
return $move unless ($move == -1); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $size = $board->getSize; |
63
|
0
|
|
|
|
|
|
my $best_moves = ___bestMoves(sqrt($size)); |
64
|
0
|
|
|
|
|
|
foreach my $i (@$best_moves) { |
65
|
0
|
0
|
|
|
|
|
return $i if $board->isCellEmpty($i); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
foreach my $i (0..($size-1)) { |
69
|
0
|
0
|
|
|
|
|
return $i if $board->isCellEmpty($i); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
# |
75
|
|
|
|
|
|
|
# PRIVATE METHODS |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _getBestMove { |
78
|
0
|
|
|
0
|
|
|
my ($board, $player) = @_; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $move = _isWinningMove($board, $player->symbol); |
81
|
0
|
0
|
|
|
|
|
return $move unless ($move == -1); |
82
|
0
|
|
|
|
|
|
return _isWinningMove($board, $player->otherSymbol()); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _isWinningMove { |
86
|
0
|
|
|
0
|
|
|
my ($board, $symbol) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
my $size = sqrt($board->getSize); |
89
|
0
|
|
|
|
|
|
my $winning_moves = ___winningMoves($size); |
90
|
0
|
|
|
|
|
|
foreach my $m (@{$winning_moves}) { |
|
0
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
foreach my $i (0..($size-1)) { |
92
|
0
|
0
|
|
|
|
|
if ($board->isCellEmpty($m->[$i])) { |
93
|
0
|
|
|
|
|
|
my $matched = 1; |
94
|
0
|
|
|
|
|
|
foreach my $j (0..($size-1)) { |
95
|
0
|
0
|
|
|
|
|
next if ($i == $j); |
96
|
0
|
0
|
|
|
|
|
$matched = 0 unless ($board->cellContains($m->[$j], $symbol)); |
97
|
|
|
|
|
|
|
} |
98
|
0
|
0
|
|
|
|
|
return $m->[$i] if ($matched); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
return -1; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub ___bestMoves { |
107
|
0
|
|
|
0
|
|
|
my ($size) = @_; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my $moves = []; |
110
|
0
|
0
|
|
|
|
|
if ($size % 2 == 0) { |
|
|
0
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# Around the center |
112
|
0
|
|
|
|
|
|
foreach my $i (1..($size-2)) { |
113
|
0
|
|
|
|
|
|
push @$moves, (($i*$size)+($i+1)-1); |
114
|
|
|
|
|
|
|
} |
115
|
0
|
|
|
|
|
|
foreach my $i (1..($size-2)) { |
116
|
0
|
|
|
|
|
|
push @$moves, ((($i+1)*$size)-$i)-1; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
# All edge corners |
119
|
0
|
|
|
|
|
|
push @$moves, (0, |
120
|
|
|
|
|
|
|
$size-1, |
121
|
|
|
|
|
|
|
(($size*($size-1)+1))-1, |
122
|
|
|
|
|
|
|
($size*$size)-1); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif ($size % 2 == 1) { |
125
|
0
|
|
|
|
|
|
my $c = int($size / 2) + 1; |
126
|
0
|
|
|
|
|
|
my $k = ($size*$c)-($c-1); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Center point |
129
|
0
|
|
|
|
|
|
push @$moves, ($k-1); |
130
|
|
|
|
|
|
|
# All edge corners |
131
|
0
|
|
|
|
|
|
push @$moves, (0, |
132
|
|
|
|
|
|
|
$size-1, |
133
|
|
|
|
|
|
|
(($size*$size)-($size-1))-1, |
134
|
|
|
|
|
|
|
($size*$size)-1); |
135
|
|
|
|
|
|
|
|
136
|
0
|
0
|
|
|
|
|
if ($size > 3) { |
137
|
|
|
|
|
|
|
# Diagonal (left to right) around the center |
138
|
0
|
|
|
|
|
|
foreach my $i (1..($c-2)) { |
139
|
0
|
|
|
|
|
|
push @$moves, ($k-(($i*$size)+$i))-1; |
140
|
0
|
|
|
|
|
|
push @$moves, ($k-(($i*$size)-$i))-1; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
# Diagonal (right to left) around the center |
143
|
0
|
|
|
|
|
|
my $j = 1; |
144
|
0
|
|
|
|
|
|
foreach my $i (($c+1)..($size-1)) { |
145
|
0
|
|
|
|
|
|
push @$moves, ($k+(($j*$size)+$j))-1; |
146
|
0
|
|
|
|
|
|
push @$moves, ($k+(($j*$size)-$j))-1; |
147
|
0
|
|
|
|
|
|
$j++; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
return $moves; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub ___winningMoves { |
156
|
0
|
|
|
0
|
|
|
my ($size) = @_; |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $moves = []; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# Horizontal |
161
|
0
|
|
|
|
|
|
my $_moves = []; |
162
|
0
|
|
|
|
|
|
my $k = 0; |
163
|
0
|
|
|
|
|
|
foreach my $i (1..$size) { |
164
|
0
|
|
|
|
|
|
$_moves = []; |
165
|
0
|
|
|
|
|
|
foreach my $j (1..$size) { |
166
|
0
|
|
|
|
|
|
push @$_moves, $k++; |
167
|
|
|
|
|
|
|
} |
168
|
0
|
|
|
|
|
|
push @$moves, $_moves; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# Vertical |
172
|
0
|
|
|
|
|
|
foreach my $i (1..$size) { |
173
|
0
|
|
|
|
|
|
$_moves = []; |
174
|
0
|
|
|
|
|
|
$k = 0; |
175
|
0
|
|
|
|
|
|
foreach my $j (1..$size) { |
176
|
0
|
|
|
|
|
|
push @$_moves, ($i+($size*$k))-1; |
177
|
0
|
|
|
|
|
|
$k++; |
178
|
|
|
|
|
|
|
} |
179
|
0
|
|
|
|
|
|
push @$moves, $_moves; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# Diagonal (left to right) |
183
|
0
|
|
|
|
|
|
$_moves = []; |
184
|
0
|
|
|
|
|
|
my $j = 1; |
185
|
0
|
|
|
|
|
|
foreach my $i (0..$size-1) { |
186
|
0
|
|
|
|
|
|
push @$_moves, ($j+($i*$size))-1; |
187
|
0
|
|
|
|
|
|
$j++; |
188
|
|
|
|
|
|
|
} |
189
|
0
|
|
|
|
|
|
push @$moves, $_moves; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Diagonal (right to left) |
192
|
0
|
|
|
|
|
|
$_moves = []; |
193
|
0
|
|
|
|
|
|
$j = 0; |
194
|
0
|
|
|
|
|
|
foreach my $i (1..$size) { |
195
|
0
|
|
|
|
|
|
push @$_moves, (($i*$size)-$j)-1; |
196
|
0
|
|
|
|
|
|
$j++; |
197
|
|
|
|
|
|
|
} |
198
|
0
|
|
|
|
|
|
push @$moves, $_moves; |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
|
|
|
return $moves; |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 AUTHOR |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Mohammad S Anwar, C<< >> |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 REPOSITORY |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
L |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 BUGS |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Please report any bugs or feature requests to C |
214
|
|
|
|
|
|
|
or through the web interface at L. |
215
|
|
|
|
|
|
|
I will be notified & then you'll automatically be notified of progress on your bug |
216
|
|
|
|
|
|
|
as I make changes. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 SUPPORT |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
perldoc Games::TicTacToe::Move |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
You can also look for information at: |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
=over 4 |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
L |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
L |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item * CPAN Ratings |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
L |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item * Search CPAN |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
L |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=back |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Copyright (C) 2011 - 2016 Mohammad S Anwar. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under |
251
|
|
|
|
|
|
|
the terms of the the Artistic License (2.0). You may obtain a copy of the full |
252
|
|
|
|
|
|
|
license at: |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
L |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified Versions is |
257
|
|
|
|
|
|
|
governed by this Artistic License.By using, modifying or distributing the Package, |
258
|
|
|
|
|
|
|
you accept this license. Do not use, modify, or distribute the Package, if you do |
259
|
|
|
|
|
|
|
not accept this license. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made by someone |
262
|
|
|
|
|
|
|
other than you,you are nevertheless required to ensure that your Modified Version |
263
|
|
|
|
|
|
|
complies with the requirements of this license. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service mark, |
266
|
|
|
|
|
|
|
tradename, or logo of the Copyright Holder. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge patent license |
269
|
|
|
|
|
|
|
to make, have made, use, offer to sell, sell, import and otherwise transfer the |
270
|
|
|
|
|
|
|
Package with respect to any patent claims licensable by the Copyright Holder that |
271
|
|
|
|
|
|
|
are necessarily infringed by the Package. If you institute patent litigation |
272
|
|
|
|
|
|
|
(including a cross-claim or counterclaim) against any party alleging that the |
273
|
|
|
|
|
|
|
Package constitutes direct or contributory patent infringement,then this Artistic |
274
|
|
|
|
|
|
|
License to you shall terminate on the date that such litigation is filed. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND |
277
|
|
|
|
|
|
|
CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED |
278
|
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR |
279
|
|
|
|
|
|
|
NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS |
280
|
|
|
|
|
|
|
REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, |
281
|
|
|
|
|
|
|
INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE |
282
|
|
|
|
|
|
|
OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=cut |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
1; # End of Games::TicTacToe::Move |