| 1 | package Games::TicTacToe::Move; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 10 | $Games::TicTacToe::Move::VERSION = '0.25'; | |||||||||||||
| 4 | 10 | $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 | ▶ | use 5.006; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 10 | ▶ | use strict; use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 10 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||
| 21 | |||||||||||||||
| 22 | It is used internally by L<Games::TicTacToe>. | ||||||||||||||
| 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 | ▶ | my ($player, $board) = @_; | ||||||||||||
|
Subroutine
foundWinner: called CC=5 SCAR=22.7
Pod
documented
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 2 | ▶ | die("ERROR: Player not defined.\n") unless defined $player; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 1 | ▶ | die("ERROR: Board not defined.\n") unless defined $board; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | 0 | my $size = sqrt($board->getSize); | |||||||||||||
| 39 | 0 | my $winning_moves = ___winningMoves($size); | |||||||||||||
| 40 | 0 | foreach my $move (@$winning_moves) { | |||||||||||||
| 41 | 0 | ▶ | return 1 if $board->belongsToPlayer($move, $player); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 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 | ▶ | my ($player, $board) = @_; | ||||||||||||
|
Subroutine
now: called CC=8 SCAR=34.4
Pod
documented
| |||||||||||||||
| 55 | |||||||||||||||
| 56 | 2 | ▶ | die("ERROR: Player not defined.\n") unless defined $player; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 1 | ▶ | die("ERROR: Board not defined.\n") unless defined $board; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | my $move = _getBestMove($board, $player); | |||||||||||||
| 60 | 0 | ▶ | return $move unless ($move == -1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | |||||||||||||||
| 62 | 0 | my $size = $board->getSize; | |||||||||||||
| 63 | 0 | my $best_moves = ___bestMoves(sqrt($size)); | |||||||||||||
| 64 | 0 | foreach my $i (@$best_moves) { | |||||||||||||
| 65 | 0 | ▶ | return $i if $board->isCellEmpty($i); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | foreach my $i (0..($size-1)) { | |||||||||||||
| 69 | 0 | ▶ | return $i if $board->isCellEmpty($i); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | # | ||||||||||||||
| 74 | # | ||||||||||||||
| 75 | # PRIVATE METHODS | ||||||||||||||
| 76 | |||||||||||||||
| 77 | sub _getBestMove { | ||||||||||||||
| 78 | 0 | ▶ | my ($board, $player) = @_; | ||||||||||||
|
Subroutine
_getBestMove: not called CC=2 SCAR=17.9
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | 0 | my $move = _isWinningMove($board, $player->symbol); | |||||||||||||
| 81 | 0 | ▶ | return $move unless ($move == -1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 0 | return _isWinningMove($board, $player->otherSymbol()); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | sub _isWinningMove { | ||||||||||||||
| 86 | 0 | ▶ | my ($board, $symbol) = @_; | ||||||||||||
|
Subroutine
_isWinningMove: not called CC=8 SCAR=42.8
| |||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | my $size = sqrt($board->getSize); | |||||||||||||
| 89 | 0 | my $winning_moves = ___winningMoves($size); | |||||||||||||
| 90 | 0 | foreach my $m (@{$winning_moves}) { | |||||||||||||
| 91 | 0 | foreach my $i (0..($size-1)) { | |||||||||||||
| 92 | 0 | ▶ | if ($board->isCellEmpty($m->[$i])) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | my $matched = 1; | |||||||||||||
| 94 | 0 | foreach my $j (0..($size-1)) { | |||||||||||||
| 95 | 0 | ▶ | next if ($i == $j); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | 0 | ▶ | $matched = 0 unless ($board->cellContains($m->[$j], $symbol)); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | } | ||||||||||||||
| 98 | 0 | ▶ | return $m->[$i] if ($matched); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | } | ||||||||||||||
| 100 | } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | return -1; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | sub ___bestMoves { | ||||||||||||||
| 107 | 0 | ▶ | my ($size) = @_; | ||||||||||||
|
Subroutine
___bestMoves: not called CC=8 SCAR=42.8
| |||||||||||||||
| 108 | |||||||||||||||
| 109 | 0 | my $moves = []; | |||||||||||||
| 110 | 0 | ▶ | if ($size % 2 == 0) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | ▶ | if ($size > 3) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | ▶ | my ($size) = @_; | ||||||||||||
|
Subroutine
___winningMoves: not called CC=7 SCAR=40.3
| |||||||||||||||
| 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<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =head1 REPOSITORY | ||||||||||||||
| 208 | |||||||||||||||
| 209 | L<https://github.com/manwar/Games-TicTacToe> | ||||||||||||||
| 210 | |||||||||||||||
| 211 | =head1 BUGS | ||||||||||||||
| 212 | |||||||||||||||
| 213 | Please report any bugs or feature requests to C<bug-games-tictactoe at rt.cpan.org> | ||||||||||||||
| 214 | or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-TicTacToe>. | ||||||||||||||
| 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-TicTacToe> | ||||||||||||||
| 231 | |||||||||||||||
| 232 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||
| 233 | |||||||||||||||
| 234 | L<http://annocpan.org/dist/Games-TicTacToe> | ||||||||||||||
| 235 | |||||||||||||||
| 236 | =item * CPAN Ratings | ||||||||||||||
| 237 | |||||||||||||||
| 238 | L<http://cpanratings.perl.org/d/Games-TicTacToe> | ||||||||||||||
| 239 | |||||||||||||||
| 240 | =item * Search CPAN | ||||||||||||||
| 241 | |||||||||||||||
| 242 | L<http://search.cpan.org/dist/Games-TicTacToe/> | ||||||||||||||
| 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<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||
| 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 | 10 | 1; # End of Games::TicTacToe::Move | |||||||||||||