File Coverage

lib/Games/Checkers/CreateMoveList.pm
Criterion Covered Total %
statement 42 114 36.8
branch 0 38 0.0
condition 0 6 0.0
subroutine 14 25 56.0
pod 0 2 0.0
total 56 185 30.2


line stmt bran cond sub pod time code
1             # Games::Checkers, Copyright (C) 1996-2012 Mikhael Goikhman, migo@cpan.org
2             #
3             # This program is free software: you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by
5             # the Free Software Foundation, either version 3 of the License, or
6             # (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program. If not, see .
15              
16 1     1   6 use strict;
  1         2  
  1         100  
17 1     1   5 use warnings;
  1         2  
  1         36  
18              
19             # ----------------------------------------------------------------------------
20              
21             package Games::Checkers::CreateMoveList;
22              
23 1     1   5 use base 'Games::Checkers::ExpandMoveList';
  1         1  
  1         504  
24              
25 1     1   6 use Games::Checkers::Constants;
  1         2  
  1         4  
26 1     1   4 use Games::Checkers::MoveConstants;
  1         1  
  1         5  
27              
28             sub new ($$$) {
29 0     0 0   my $class = shift;
30 0           my $board_tree_node = shift;
31 0           my $color = shift;
32 0           my $self = $class->SUPER::new($board_tree_node, $color);
33              
34 0           $self->{board_tree_node} = $board_tree_node;
35              
36 0           $self->build;
37 0           return $self;
38             }
39              
40             sub add_move ($) {
41 0     0 0   my $self = shift;
42 0           my $move = $self->create_move;
43 0 0         return Err unless $move; ### not needed
44 0 0         die "Internal Error" if $move == NO_MOVE;
45 0           my $new_board_tree_node = Games::Checkers::BoardTreeNode->new($self, $move);
46 0 0         return Err unless $new_board_tree_node; ### not needed
47 0           push @{$self->{board_tree_node}->{sons}}, $new_board_tree_node;
  0            
48 0           return Ok;
49             }
50              
51             # ----------------------------------------------------------------------------
52              
53             package Games::Checkers::CountMoveList;
54              
55 1     1   5 use base 'Games::Checkers::ExpandMoveList';
  1         1  
  1         73  
56              
57 1     1   4 use Games::Checkers::Constants;
  1         2  
  1         3  
58              
59             sub new ($$$) {
60 0     0     my $class = shift;
61 0           my $board = shift;
62 0           my $color = shift;
63 0           my $self = $class->SUPER::new($board, $color);
64              
65 0           $self->{count} = 0;
66              
67 0           $self->build;
68 0           return $self;
69             }
70              
71             sub add_move ($) {
72 0     0     my $self = shift;
73 0           $self->{count}++;
74 0           return Ok;
75             }
76              
77             sub get_count ($) {
78 0     0     my $self = shift;
79 0 0         return $self->{status} == Ok ? $self->{count} : 0;
80             }
81              
82             # ----------------------------------------------------------------------------
83              
84             package Games::Checkers::CreateUniqueMove;
85              
86 1     1   6 use base 'Games::Checkers::ExpandMoveList';
  1         1  
  1         69  
87              
88 1     1   5 use Games::Checkers::Constants;
  1         1  
  1         5  
89 1     1   6 use Games::Checkers::MoveConstants;
  1         2  
  1         5  
90              
91             sub new ($$$) {
92 0     0     my $class = shift;
93 0           my $board = shift;
94 0           my $color = shift;
95 0           my $self = $class->SUPER::new($board, $color);
96              
97 0           $self->{move} = NO_MOVE;
98              
99 0           $self->build;
100 0           return $self;
101             }
102              
103             sub add_move ($) {
104 0     0     my $self = shift;
105 0 0         return Err if $self->{move} != NO_MOVE;
106 0           $self->{move} = $self->create_move;
107 0           return Ok;
108             }
109              
110             sub get_move ($) {
111 0     0     my $self = shift;
112 0 0         return $self->{status} == Ok ? $self->{move} : NO_MOVE;
113             }
114              
115             # ----------------------------------------------------------------------------
116              
117             package Games::Checkers::CreateVergeMove;
118              
119 1     1   5 use base 'Games::Checkers::ExpandMoveList';
  1         1  
  1         80  
120              
121 1     1   5 use Games::Checkers::Constants;
  1         2  
  1         3  
122 1     1   5 use Games::Checkers::MoveConstants;
  1         2  
  1         5  
123 1     1   4 use Games::Checkers::Move;
  1         2  
  1         433  
124              
125             sub new ($$$$$$) {
126 0     0     my $class = shift;
127 0           my $board = shift;
128 0           my $color = shift;
129 0           my $self = $class->SUPER::new($board, $color);
130              
131 0 0         die "Not enough arguments in constructor" unless @_ >= 3;
132 0           my $is_beat = $self->{is_beat} = shift;
133 0           my $src = $self->{src0} = shift;
134 0           my $dst = $self->{dst0} = shift;
135              
136 0 0         die "Bad verge move source location ($src): not occupied\n"
137             unless $board->occup($src);
138 0 0         die "Bad verge move source location ($src): incorrect color\n"
139             unless $board->color($src) == $color;
140 0 0 0       die "Bad verge move source location ($src): can't beat\n"
141             unless !$is_beat || $board->can_piece_beat($src);
142             # die "Bad verge move source location ($src): can't step\n"
143             # unless $is_beat || $board->can_piece_step($src);
144              
145 0 0         if (!$is_beat) {
146 0 0         if ($board->can_piece_step($src, $dst)) {
147 0           $self->{move} = new Games::Checkers::Move($is_beat, $src, [$dst]);
148 0           return $self;
149             }
150             # give it the last chance
151 0 0         $board->can_piece_beat($src) ? $is_beat = 1 :
152             die "Bad verge move ($src-$dst): can't step\n";
153             }
154              
155             # support British rules
156 0 0         if ($is_beat) {
157 0 0         if ($board->can_piece_beat($src, $dst)) {
158 0           $self->{move} = new Games::Checkers::Move($is_beat, $src, [$dst]);
159 0           return $self;
160             }
161             }
162 0           $self->{move} = NO_MOVE;
163              
164 0           $self->build;
165 0           return $self;
166             }
167              
168             sub add_move ($) {
169 0     0     my $self = shift;
170              
171 0 0         return Err if !$self->{must_beat};
172 0 0 0       return Ok if $self->{src} != $self->{src0} || $self->dst_1 != $self->{dst0};
173              
174 0 0         return Err if $self->{move} != NO_MOVE;
175 0           $self->{move} = $self->create_move;
176 0           return Ok;
177             }
178              
179             sub get_move ($) {
180 0     0     my $self = shift;
181 0 0         return $self->{status} == Ok ? $self->{move} : NO_MOVE;
182             }
183              
184             1;