TicTacToe.pm

filter: Statementstmt 50.0% 54 / 108 Branchbran 15.3% 4 / 26 Conditioncond 12.1% 4 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 64.0% 16 / 25 Podpod 92.3% 12 / 13 totaltotal 40.1% 90 / 224 CC 27 SCAR 24.0
CC
27
Cov
37%
isValidMove
30.0
play
25.7
isGameOver
13.0
CRAP 11.0
« TicTacToe/Move.pm ↑ Summary TicTacToe/Board.pm »
1 package Games::TicTacToe;
2  
3 10 $Games::TicTacToe::VERSION = '0.25';
4 10 $Games::TicTacToe::AUTHOR = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 Games::TicTacToe - Interface to the TicTacToe (nxn) game.
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 Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Games::TicTacToe::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Games::TicTacToe::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Games::TicTacToe::Player;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Games::TicTacToe::Params qw(Board PlayerType Players);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 has 'board' => (is => 'rw', isa => Board);
27 7 ▶has 'current' => (is => 'rw', isa => PlayerType, default => sub { return 'H'; });
Subroutine __ANON__: called CC=1 SCAR=0.0
28 10 has 'players' => (is => 'rw', isa => Players, predicate => 1);
29 6 ▶has 'size' => (is => 'ro', default => sub { return 3 });
Subroutine __ANON__: called CC=1 SCAR=0.0
30 10 has 'winner' => (is => 'rw', predicate => 1, clearer => 1);
31  
32 =head1 DESCRIPTION
33  
34 A console based TicTacToe game to play against the computer. A simple TicTacToe
35 layer supplied with the distribution in the script sub folder. Board arranged as
36 nxn, where n>=3. Default size is 3,For example 5x5 would be something like below:
37  
38 +------------------------+
39 | TicTacToe |
40 +----+----+----+----+----+
41 | 1 | 2 | 3 | 4 | 5 |
42 +----+----+----+----+----+
43 | 6 | 7 | 8 | 9 | 10 |
44 +----+----+----+----+----+
45 | 11 | 12 | 13 | 14 | 15 |
46 +----+----+----+----+----+
47 | 16 | 17 | 18 | 19 | 20 |
48 +----+----+----+----+----+
49 | 21 | 22 | 23 | 24 | 25 |
50 +----+----+----+----+----+
51  
52 The game script C<play-tictactoe> is supplied with the distribution and on install
53 is available to play with.
54  
55 USAGE: play-tictactoe [-h] [long options...]
56  
57 --size=Int TicTacToe board size. Default is 3.
58 --symbol=String User preferred symbol. Default is X. The other possible
59 value is O.
60  
61 --usage show a short help message
62 -h show a compact help message
63 --help show a long help message
64 --man show the manual
65  
66 =cut
67  
68 sub BUILD {
69 6 ▶ my ($self) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
70  
71 6 $self->setGameBoard($self->size);
72 }
73  
74 =head1 METHODS
75  
76 =head2 setGameBoard($size)
77  
78 It sets up the game board of the given C<$size>.
79  
80 =cut
81  
82 sub setGameBoard {
83 6 ▶ my ($self, $size) = @_;
Subroutine setGameBoard: called CC=1 SCAR=0.0
Pod documented
84  
85 6 my $cell = [ map { $_ } (1..($size * $size)) ];
86 6 $self->board(Games::TicTacToe::Board->new(cell => $cell));
87 }
88  
89 =head2 getGameBoard()
90  
91 Returns game board for TicTacToe (3x3) by default.
92  
93 =cut
94  
95 sub getGameBoard {
96 0 ▶ my ($self) = @_;
Subroutine getGameBoard: not called CC=1 SCAR=6.9
Pod documented
97  
98 0 return $self->board->as_string;
99 }
100  
101 =head2 setPlayers($symbol)
102  
103 Adds a player with the given C<$symbol>. The other symbol would be given to the
104 opposite player i.e. Computer.
105  
106 =cut
107  
108 sub setPlayers {
109 1 ▶ my ($self, $symbol) = @_;
Subroutine setPlayers: called CC=5 SCAR=24.7
Pod documented
110  
111 1 ▶ if (($self->has_players) && (scalar(@{$self->players}) == 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_players and scalar @{$self->players;} == 2) 1 0 1
Condition33%1/3
$self->has_players and scalar @{$self->players;} == 2
!ll&&!rl&&r
001
MC/DC0%0/2
$self->has_players and scalar @{$self->players;} == 2
$self->has_players scalar @{$self->players;} == 2
Truth table33%1/3
A and B
A= $self->has_playersB= scalar @{$self->players;} == 2
ABresult
0X0
100
111
112 1 warn("WARNING: We already have 2 players to play the TicTacToe game.");
113 1 return;
114 }
115  
116 0 ▶ die "ERROR: Missing symbol for the player.\n" unless defined $symbol;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $symbol 0 0 0
117  
118 # Player 1
119 0 push @{$self->{players}}, Games::TicTacToe::Player->new(type => 'H', symbol => uc($symbol));
120  
121 # Player 2
122 0 ▶ $symbol = (uc($symbol) eq 'X')?('O'):('X');
Branch: 1 branch
BranchTrueFalseTotal
uc $symbol eq 'X' ? : 0 0 0
123 0 push @{$self->{players}}, Games::TicTacToe::Player->new(type => 'C', symbol => $symbol);
124 }
125  
126 =head2 getPlayers()
127  
128 Returns the players information with their symbol.
129  
130 =cut
131  
132 sub getPlayers {
133 1 ▶ my ($self) = @_;
Subroutine getPlayers: called CC=4 SCAR=20.7
Pod documented
134  
135 1 ▶ if (!($self->has_players) || scalar(@{$self->players}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_players or scalar @{$self->players;} == 0) 1 0 1
Condition33%1/3
not $self->has_players or scalar @{$self->players;} == 0
l!l&&r!l&&!r
100
MC/DC0%0/2
not $self->has_players or scalar @{$self->players;} == 0
not $self->has_players scalar @{$self->players;} == 0
Truth table33%1/3
not(A) or B
A= not $self->has_playersB= scalar @{$self->players;} == 0
ABresult
1X1
011
000
136 1 warn("WARNING: No player found to play the TicTacToe game.");
137 1 return;
138 }
139  
140 0 my $players = sprintf("+-------------+\n");
141 0 foreach (@{$self->{players}}) {
142 0 $players .= sprintf("|%9s: %s |\n", $_->desc, $_->symbol);
143 }
144 0 $players .= sprintf("+-------------+\n");
145  
146 0 return $players;
147 }
148  
149 =head2 play($move)
150  
151 Makes the given C<$move>, if provided, otherwise make next best possible moves on
152 behalf of opponent.
153  
154 =cut
155  
156 sub play {
157 1 ▶ my ($self, $move) = @_;
Subroutine play: called CC=6 SCAR=32.5
Pod documented
158  
159 die("ERROR: Please add player before you start the game.\n")
160 1 ▶ unless (($self->has_players) && (scalar(@{$self->players}) == 2));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_players and scalar @{$self->players;} == 2 1 0 1
Condition33%1/3
$self->has_players and scalar @{$self->players;} == 2
!ll&&!rl&&r
100
MC/DC0%0/2
$self->has_players and scalar @{$self->players;} == 2
$self->has_players scalar @{$self->players;} == 2
Truth table33%1/3
A and B
A= $self->has_playersB= scalar @{$self->players;} == 2
ABresult
0X0
100
111
161  
162 0 my $player = $self->_getCurrentPlayer;
163 0 my $board = $self->board;
164 0 ▶ if (defined $move && ($self->_getCurrentPlayer->type eq 'H')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $move and $self->_getCurrentPlayer->type eq 'H') { } 0 0 0
Condition0%0/3
defined $move and $self->_getCurrentPlayer->type eq "H"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $move and $self->_getCurrentPlayer->type eq "H"
defined $move $self->_getCurrentPlayer->type eq "H"
Truth table0%0/3
A and B
A= defined $moveB= $self->_getCurrentPlayer->type eq "H"
ABresult
0X0
100
111
165 0 --$move;
166 }
167 else {
168 0 $move = Games::TicTacToe::Move::now($player, $board);
169 }
170  
171 0 $board->setCell($move, $player->symbol);
172 0 ▶ $self->_resetCurrentPlayer unless ($self->isGameOver);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isGameOver 0 0 0
173 }
174  
175 =head2 getResult()
176  
177 Returns the result message.
178  
179 =cut
180  
181 sub getResult {
182 0 ▶ my ($self) = @_;
Subroutine getResult: not called CC=3 SCAR=24.8
Pod documented
183  
184 0 my $result;
185 0 ▶ if ($self->has_winner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_winner) { } 0 0 0
186 0 $result = $self->winner->getMessage;
187 }
188 else {
189 0 ▶ die "ERROR: Game is not finished yet.\n" unless $self->board->isFull;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board->isFull 0 0 0
190 0 $result = "<cyan><bold>Game drawn, better luck next time.</bold></cyan>\n";
191 }
192  
193 0 $self->clear_winner;
194 0 $self->current('H');
195  
196 0 return Term::ANSIColor::Markup->colorize($result);
197 }
198  
199 =head2 needNextMove()
200  
201 Returns 0 or 1 depending on whether it needs to prompt for next move.
202  
203 =cut
204  
205 sub needNextMove {
206 0 ▶ my ($self) = @_;
Subroutine needNextMove: not called CC=1 SCAR=6.9
Pod documented
207  
208 0 return ($self->_getCurrentPlayer->type eq 'H');
209 }
210  
211 =head2 isLastMove()
212  
213 Returns 0 or 1 depending on whether it is the last move.
214  
215 =cut
216  
217 sub isLastMove {
218 0 ▶ my ($self) = @_;
Subroutine isLastMove: not called CC=1 SCAR=6.9
Pod documented
219  
220 0 return ($self->board->availableIndex !~ /\,/);
221 }
222  
223 =head2 isGameOver()
224  
225 Returns 0 or 1 depending whether the TicTacToe game is over or not.
226  
227 =cut
228  
229 sub isGameOver {
230 1 ▶ my ($self) = @_;
Subroutine isGameOver: called CC=5 SCAR=25.7
Pod documented
231  
232 1 ▶ if (!($self->has_players) || scalar(@{$self->players}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->has_players or scalar @{$self->players;} == 0) 1 0 1
Condition33%1/3
not $self->has_players or scalar @{$self->players;} == 0
l!l&&r!l&&!r
100
MC/DC0%0/2
not $self->has_players or scalar @{$self->players;} == 0
not $self->has_players scalar @{$self->players;} == 0
Truth table33%1/3
not(A) or B
A= not $self->has_playersB= scalar @{$self->players;} == 0
ABresult
1X1
011
000
233 1 warn("WARNING: No player found to play the TicTacToe game.");
234 1 return;
235 }
236  
237 0 my $board = $self->board;
238 0 foreach my $player (@{$self->players}) {
239 0 ▶ if (Games::TicTacToe::Move::foundWinner($player, $board)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::TicTacToe::Move::foundWinner($player, $board)) 0 0 0
240 0 $self->winner($player);
241 0 return 1;
242 }
243 }
244  
245 0 return $board->isFull;
246 }
247  
248 =head2 isValidMove($move)
249  
250 Returns 0 or 1 depending on whether the given C<$move> is valid or not.
251  
252 =cut
253  
254 sub isValidMove {
255 0 ▶ my ($self, $move) = @_;
Subroutine isValidMove: not called CC=5 SCAR=34.0
Pod documented
256  
257 0 ▶ return (defined($move)
Condition0%0/12
defined $move && $move =~ /^\d+$/ && $move >= 1 && $move <= $self->board->getSize && $self->board->isCellEmpty($move - 1)
!ll&&!rl&&r
000
defined $move and $move =~ /^\d+$/ and $move >= 1 and $move <= $self->board->getSize
!ll&&!rl&&r
000
defined $move and $move =~ /^\d+$/ and $move >= 1
!ll&&!rl&&r
000
defined $move and $move =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/5
defined $move && $move =~ /^\d+$/ && $move >= 1 && $move <= $self->board->getSize && $self->board->isCellEmpty($move - 1)
defined $move $move =~ /^\d+$/ $move >= 1 $move <= $self->board->getSize $self->board->isCellEmpty($move - 1)
Truth table0%0/6
A and B and C and D && E
A= defined $moveB= $move =~ /^\d+$/C= $move >= 1D= $move <= $self->board->getSizeE= $self->board->isCellEmpty($move - 1)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
258 && ($move =~ /^\d+$/)
259 && ($move >= 1) && ($move <= $self->board->getSize)
260 && ($self->board->isCellEmpty($move-1)));
261 }
262  
263 =head2 isValidSymbol($symbol)
264  
265 Returns 0 or 1 depending on whether the given C<$symbol> is valid or not.
266  
267 =cut
268  
269 sub isValidSymbol {
270 0 ▶ my ($self, $symbol) = @_;
Subroutine isValidSymbol: not called CC=2 SCAR=17.9
Pod documented
271  
272 0 ▶ return (defined $symbol && ($symbol =~ /^[X|O]$/i));
Condition0%0/3
defined $symbol && $symbol =~ /^[X|O]$/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $symbol && $symbol =~ /^[X|O]$/i
defined $symbol $symbol =~ /^[X|O]$/i
Truth table0%0/3
A && B
A= defined $symbolB= $symbol =~ /^[X|O]$/i
ABresult
0X0
100
111
273 }
274  
275 =head2 isValidGameBoardSize($size)
276  
277 Returns 0 or 1 depending on whether the given C<$size> is valid or not.
278  
279 =cut
280  
281 sub isValidGameBoardSize {
282 0 ▶ my ($self, $size) = @_;
Subroutine isValidGameBoardSize: not called CC=2 SCAR=17.9
Pod documented
283  
284 0 ▶ return (defined $size && ($size >= 3));
Condition0%0/3
defined $size && $size >= 3
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size && $size >= 3
defined $size $size >= 3
Truth table0%0/3
A && B
A= defined $sizeB= $size >= 3
ABresult
0X0
100
111
285 }
286  
287 #
288 #
289 # PRIVATE METHODS
290  
291 sub _getCurrentPlayer {
292 0 ▶ my ($self) = @_;
Subroutine _getCurrentPlayer: not called CC=2 SCAR=17.9
293  
294 ($self->{players}->[0]->type eq $self->current)
295 ?
296 (return $self->{players}->[0])
297 :
298 0 ▶ (return $self->{players}->[1]);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'players'}[0]->type eq $self->current ? : 0 0 0
299 }
300  
301 sub _resetCurrentPlayer {
302 0 ▶ my ($self) = @_;
Subroutine _resetCurrentPlayer: not called CC=2 SCAR=17.9
303  
304 ($self->{players}->[0]->type eq $self->current)
305 ?
306 ($self->current($self->{players}->[1]->type))
307 :
308 0 ▶ ($self->current($self->{players}->[0]->type));
Branch: 1 branch
BranchTrueFalseTotal
$self->{'players'}[0]->type eq $self->current ? : 0 0 0
309 }
310  
311 =head1 AUTHOR
312  
313 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
314  
315 =head1 REPOSITORY
316  
317 L<https://github.com/manwar/Games-TicTacToe>
318  
319 =head1 BUGS
320  
321 Please report any bugs / feature requests to C<bug-games-tictactoe at rt.cpan.org>
322 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-TicTacToe>.
323 I will be notified & then you'll automatically be notified of progress on your bug
324 as I make changes.
325  
326 =head1 SUPPORT
327  
328 You can find documentation for this module with the perldoc command.
329  
330 perldoc Games::TicTacToe
331  
332 You can also look for information at:
333  
334 =over 4
335  
336 =item * RT: CPAN's request tracker
337  
338 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-TicTacToe>
339  
340 =item * AnnoCPAN: Annotated CPAN documentation
341  
342 L<http://annocpan.org/dist/Games-TicTacToe>
343  
344 =item * CPAN Ratings
345  
346 L<http://cpanratings.perl.org/d/Games-TicTacToe>
347  
348 =item * Search CPAN
349  
350 L<http://search.cpan.org/dist/Games-TicTacToe/>
351  
352 =back
353  
354 =head1 LICENSE AND COPYRIGHT
355  
356 Copyright (C) 2011 - 2016 Mohammad S Anwar.
357  
358 This program is free software; you can redistribute it and/or modify it under
359 the terms of the the Artistic License (2.0). You may obtain a copy of the full
360 license at:
361  
362 L<http://www.perlfoundation.org/artistic_license_2_0>
363  
364 Any use, modification, and distribution of the Standard or Modified Versions is
365 governed by this Artistic License.By using, modifying or distributing the Package,
366 you accept this license. Do not use, modify, or distribute the Package, if you do
367 not accept this license.
368  
369 If your Modified Version has been derived from a Modified Version made by someone
370 other than you,you are nevertheless required to ensure that your Modified Version
371 complies with the requirements of this license.
372  
373 This license does not grant you the right to use any trademark, service mark,
374 tradename, or logo of the Copyright Holder.
375  
376 This license includes the non-exclusive, worldwide, free-of-charge patent license
377 to make, have made, use, offer to sell, sell, import and otherwise transfer the
378 Package with respect to any patent claims licensable by the Copyright Holder that
379 are necessarily infringed by the Package. If you institute patent litigation
380 (including a cross-claim or counterclaim) against any party alleging that the
381 Package constitutes direct or contributory patent infringement,then this Artistic
382 License to you shall terminate on the date that such litigation is filed.
383  
384 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
385 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
386 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
387 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
388 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
389 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
390 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
391  
392 =cut
393  
394 10 1; # End of Games::TicTacToe
« TicTacToe/Move.pm ↑ Summary TicTacToe/Board.pm »