File Coverage

lib/Games/Checkers/Iterators.pm
Criterion Covered Total %
statement 15 52 28.8
branch 0 8 0.0
condition 0 5 0.0
subroutine 5 14 35.7
pod n/a
total 20 79 25.3


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   5 use strict;
  1         3  
  1         23  
17 1     1   4 use warnings;
  1         1  
  1         27  
18              
19             # ----------------------------------------------------------------------------
20              
21             package Games::Checkers::LocationIterator;
22              
23 1     1   4 use Games::Checkers::Constants;
  1         1  
  1         4  
24              
25             sub new ($$%) {
26 0     0     my $class = shift;
27 0   0       my $board = shift || die "No board in constructor";
28              
29 0           my $self = { board => $board, loc => undef, @_ };
30              
31 0           bless $self, $class;
32 0           $self->restart;
33              
34 0           return $self;
35             }
36              
37             sub last ($) {
38 0     0     my $self = shift;
39 0           return $self->{loc};
40             }
41              
42             sub next ($) {
43 0     0     my $self = shift;
44 0           my $old = $self->{loc};
45 0           $self->increment;
46 0           return $old;
47             }
48              
49             sub left ($) {
50 0     0     my $self = shift;
51 0           return $self->{loc} != NL;
52             }
53              
54             sub increment ($) {
55 0     0     my $self = shift;
56 0 0         return NL if $self->{loc} == NL;
57             $self->{loc} = NL
58 0 0         unless ++$self->{loc} < $self->{board}->locs;
59 0           return $self->{loc};
60             }
61              
62             sub restart ($) {
63 0     0     my $self = shift;
64 0           $self->{loc} = -1;
65 0           $self->increment;
66             }
67              
68             sub all ($) {
69 0     0     my $self = shift;
70 0           my @locations = ();
71 0           push @locations, $self->next while $self->left;
72 0           return @locations;
73             }
74              
75             # ----------------------------------------------------------------------------
76              
77             package Games::Checkers::FigureIterator;
78              
79 1     1   7 use base 'Games::Checkers::LocationIterator';
  1         2  
  1         453  
80 1     1   7 use Games::Checkers::Constants;
  1         1  
  1         4  
81              
82             sub new ($$$) {
83 0     0     my $class = shift;
84 0           my $board = shift;
85 0           my $color = shift;
86              
87 0           return $class->SUPER::new($board, color => $color);
88             }
89              
90             sub increment ($) {
91 0     0     my $self = shift;
92 0           my $loc = $self->{loc};
93              
94 0 0         return NL if $loc == NL;
95              
96 0           my $board = $self->{board};
97              
98 0           while (++$loc < $board->locs) {
99 0 0 0       if (
100             $board->occup($loc) &&
101             $board->color($loc) == $self->{color}
102             ) {
103 0           return $self->{loc} = $loc;
104             }
105             }
106              
107 0           return $self->{loc} = NL;
108             }
109              
110             1;