File Coverage

lib/Games/Checkers/Rules.pm
Criterion Covered Total %
statement 25 28 89.2
branch 8 22 36.3
condition 5 16 31.2
subroutine 3 4 75.0
pod 0 2 0.0
total 41 72 56.9


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         2  
  1         23  
17 1     1   3 use warnings;
  1         2  
  1         671  
18              
19             package main;
20              
21             our %RULES;
22              
23             package Games::Checkers::Rules;
24              
25             our %variant_rules = (
26             international => {
27             BOARD_SIZE => '10x10',
28             BOARD_NOTATION => 'TL',
29             CAPTURE_SEPARATOR => 'x',
30             STARTING_ROWS => 4,
31             BOTTOM_LEFT_CELL => 1,
32             PDN_GAME_TYPE => 20,
33             WHITE_STARTS => 1,
34             KINGS_LONG_RANGED => 1,
35             PAWNS_CAPTURING_BACKWARDS => 1,
36             CAPTURING_LARGEST => 1,
37             CAPTURING_POSTPONED => 1,
38             CROWNING_DURING_CAPTURE => 0,
39             PAWNS_CANT_CAPTURE_KINGS => 0,
40             CAPTURING_LEAVES_NO_GAP => 0,
41             GIVE_AWAY => 0,
42             CAPTURING_IN_8_DIRECTIONS => 0,
43             },
44             russian => {
45             base => 'international',
46             BOARD_SIZE => '8x8',
47             BOARD_NOTATION => 'A1',
48             CAPTURE_SEPARATOR => ':',
49             STARTING_ROWS => 3,
50             PDN_GAME_TYPE => 25,
51             CAPTURING_LARGEST => 0,
52             CROWNING_DURING_CAPTURE => 1,
53             },
54             russian_give_away => {
55             base => 'russian',
56             PDN_GAME_TYPE => 00,
57             GIVE_AWAY => 1,
58             },
59             russian_10x8 => {
60             base => 'russian',
61             BOARD_SIZE => '10x8',
62             PDN_GAME_TYPE => 41,
63             },
64             english => {
65             base => 'international',
66             BOARD_SIZE => '8x8',
67             BOARD_NOTATION => 'TL',
68             STARTING_ROWS => 3,
69             PDN_GAME_TYPE => 21,
70             WHITE_STARTS => 0,
71             KINGS_LONG_RANGED => 0,
72             PAWNS_CAPTURING_BACKWARDS => 0,
73             CAPTURING_LARGEST => 0,
74             CAPTURING_POSTPONED => 0,
75             CROWNING_DURING_CAPTURE => 'S',
76             },
77             english_give_away => {
78             base => 'english',
79             PDN_GAME_TYPE => 00,
80             GIVE_AWAY => 1,
81             },
82             italian => {
83             base => 'english',
84             BOARD_NOTATION => 'TR',
85             BOTTOM_LEFT_CELL => 0,
86             PDN_GAME_TYPE => 22,
87             WHITE_STARTS => 1,
88             CAPTURING_LARGEST => 2,
89             PAWNS_CANT_CAPTURE_KINGS => 1,
90             },
91             spanish => {
92             base => 'english',
93             BOARD_NOTATION => 'BR',
94             BOTTOM_LEFT_CELL => 0,
95             PDN_GAME_TYPE => 24,
96             WHITE_STARTS => 1,
97             KINGS_LONG_RANGED => 1,
98             CAPTURING_LARGEST => 3,
99             CAPTURING_POSTPONED => 1,
100             },
101             argentinian => {
102             base => 'spanish',
103             CAPTURING_LEAVES_NO_GAP => 1,
104             PDN_GAME_TYPE => 00,
105             },
106             portuguese => {
107             base => 'spanish',
108             BOTTOM_LEFT_CELL => 1,
109             PDN_GAME_TYPE => 28,
110             },
111             czech => {
112             base => 'portuguese',
113             BOARD_NOTATION => 'A1',
114             CAPTURING_LARGEST => -1,
115             PDN_GAME_TYPE => 29,
116             },
117             german => {
118             base => 'czech',
119             CAPTURING_LARGEST => 0,
120             PDN_GAME_TYPE => 00,
121             },
122             thai => {
123             base => 'english',
124             BOARD_NOTATION => 'BR',
125             CAPTURE_SEPARATOR => '-',
126             STARTING_ROWS => 2,
127             PDN_GAME_TYPE => 31,
128             KINGS_LONG_RANGED => 1,
129             CAPTURING_LEAVES_NO_GAP => 1,
130             },
131             pool => {
132             base => 'english',
133             BOARD_NOTATION => 'BR',
134             PDN_GAME_TYPE => 23,
135             KINGS_LONG_RANGED => 1,
136             CROWNING_DURING_CAPTURE => 0,
137             PAWNS_CAPTURING_BACKWARDS => 1,
138             },
139             brazilian => {
140             base => 'international',
141             BOARD_SIZE => '8x8',
142             BOARD_NOTATION => 'A1',
143             STARTING_ROWS => 3,
144             PDN_GAME_TYPE => 26,
145             },
146             frisian => {
147             base => 'international',
148             PDN_GAME_TYPE => 40,
149             CAPTURING_IN_8_DIRECTIONS => 1,
150             },
151             canadian => {
152             base => 'international',
153             BOARD_SIZE => '12x12',
154             STARTING_ROWS => 5,
155             PDN_GAME_TYPE => 27,
156             },
157             sri_lankian => {
158             base => 'canadian',
159             BOTTOM_LEFT_CELL => 0,
160             PDN_GAME_TYPE => 00,
161             },
162             default => 'russian',
163             british => 'english',
164             internt => 'international',
165             int => 'international',
166             polish => 'international',
167             '8x8' => 'brazilian',
168             '10x10' => 'international',
169             '12x12' => 'canadian',
170             shashki => 'russian',
171             poddavki => 'russian_give_away',
172             american => 'english',
173             give_away => 'english_give_away',
174             spantsiretti => 'russian_10x8',
175             american_pool => 'pool',
176             spanish_pool => 'spanish',
177             20 => 'international',
178             21 => 'english',
179             22 => 'italian',
180             23 => 'pool',
181             24 => 'spanish',
182             25 => 'russian',
183             26 => 'brazilian',
184             27 => 'canadian',
185             28 => 'portuguese',
186             29 => 'czech',
187             31 => 'thai',
188             40 => 'frisian',
189             41 => 'russian_10x8',
190             );
191              
192             sub set_variant ($%) {
193 1   50 1 0 5 my $name = shift || 'default';
194 1         2 my %params = @_;
195              
196             # support GameType specification from PDN
197 1         3 my ($base, $starting_color, $size_x, $size_y, $notation, $invert) =
198             split(',', $name);
199 1 0       4 $params{WHITE_STARTS} = $starting_color eq 'W' ? 1 : 0
    50          
200             if defined $starting_color;
201 1 0 33     2 $params{BOARD_SIZE} = "${size_x}x$size_y"
      33        
202             if $size_x && $size_y && "$size_x$size_y" =~ /^\d+$/;
203 1 0 33     15 $params{BOARD_NOTATION} = $1 eq 'N' ? [ qw(BL BR TL TR) ]->[$2] : "$1$2"
    50          
204             if defined $notation && $notation =~ /^([ANS])([0-3])$/;
205 1 0       3 $params{BOTTOM_LEFT_CELL} = $invert ? 0 : 1
    50          
206             if defined $invert;
207              
208 1         3 %::RULES = (variant => $base, base => $base);
209 1         1 for (keys %{$variant_rules{international}}) {
  1         5  
210 16 50       29 $::RULES{$_} = defined $params{$_} ? $params{$_} : $ENV{$_};
211             }
212              
213 1         4 while (my $base = delete $::RULES{base}) {
214 3         6 $base = lc($base); $base =~ s/[\s-]+/_/g;
  3         6  
215 3   50     7 my $rules = $variant_rules{$base} || die "Checkers variant '$base' is unknown\n";
216 3 100       6 $rules = { base => $rules } unless ref($rules);
217 3         14 for (keys %$rules) {
218 25 100       45 $::RULES{$_} = $rules->{$_} unless defined $::RULES{$_};
219             }
220             }
221              
222 1         3 return 1;
223             }
224              
225             sub get_main_variants () {
226             return sort map {
227 0     0 0   my $entry = $variant_rules{$_};
  0            
228             ref($entry) eq 'HASH' && defined $entry->{PDN_GAME_TYPE}
229 0 0 0       ? ($_) : ()
230             } keys %variant_rules;
231             }
232              
233             set_variant('default');
234              
235             1;