Mines/Play.pm

filter: Statementstmt 88.5% 193 / 218 Branchbran 73.8% 93 / 126 Conditioncond 51.7% 15 / 29 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 100% 26 / 26 Podpod 100% 8 / 8 totaltotal 79.8% 340 / 426 CC 76 SCAR 29.4
CC
76
Cov
81%
save_game
41.5
load_game
22.0
default
14.0
CRAP 18.9
↑ Summary Mines.pm »
1 package Games::Mines::Play;
2  
3 45 require 5.005_62;
4 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 45 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 45 ▶use Games::Mines;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 45 our $VERSION = sprintf("%01d.%02d.%02d", 0,q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/);
14  
15 =head1 NAME
16  
17 Games::Mines::Play;
18  
19 =head1 SYNOPSIS
20  
21 require Games::Mines::Play;
22  
23 # get new 30x40 mine field with 50 mines
24 my($game) = Games::Mines->new(30,40,50);
25  
26 # use color text
27 $game->set_ANSI_Color;
28  
29 # fill with mines, except at the four corners
30 $game->fill_mines([0,0],[0,40],[30,0],[30,40]);
31  
32 # print out playing field
33 $game->print_out("field");
34 $game->print_status_line;
35  
36  
37 =head1 DESCRIPTION
38  
39 This module is he basis for mine finding game. It builds on the
40 Games::Mines base class to with all the various methods needed to play
41 a text version of the game.
42  
43 =head2 Class and object Methods
44  
45  
46 =over 5
47  
48 =item $Class->new;
49  
50 The new method creates a new mine field object. It takes three
51 arguments: The width of the field, the height of the field, and the
52 number of mines.
53  
54 =cut
55  
56 58 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
Pod documented
57 58 my($base) = shift;
58
59 # Get class or object ref and construct object
60 58 ▶ my($class) = ref($base) || $base;
Condition33%1/3
ref $base || $base
l!l&&r!l&&!r
0580
MC/DC0%0/2
ref $base || $base
ref $base $base
Truth table33%1/3
A || B
A= ref $baseB= $base
ABresult
1X1
011
000
61
62 58 my($game) = 'Games::Mines';
63
64 58 my($w,$h,$m) = (shift,shift,shift);
65
66 58 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 58 58
67 0 $game=shift;
68 }
69
70 58 my($new_game) = $game->new($w,$h,$m);
71 58 ▶ return unless defined( $new_game);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_game 0 58 58
72
73 58 my($mine_field) = {
74 'GAME' => $new_game,
75 'map' =>'',
76 'game number' => 1,
77 };
78
79 58 bless $mine_field, $class;
80 58 $mine_field->set_ASCII;
81 58 return $mine_field;
82 }
83  
84 # class methodes
85  
86 =item $class->default(%opts)
87  
88 Returns an array of width, height, and number of mines, based on some
89 common arguments. It takes a has with the some combination of six
90 keys. The first three are B<small>, B<medium>, and B<large>. These
91 are boolean keys, who's value are only checked to see if they
92 contain a true value. The small field is 8x8 with 10 mines, the medium
93 field is 16x16 with 40 mines, and the large field is 16x30 with 99
94 mines. The other three are B<width>, B<height>, and B<mines>, which
95 sets the corresponding term. Note that this is designed to work with
96 Getopt::Long, so any other keys are ignored. The default is to return
97 a large field.
98  
99 =cut
100  
101 6 ▶sub default {
Subroutine default: called CC=13 SCAR=26.4
Pod documented
102 6 my($class) = shift; # don't really do anything with this.
103 6 my(%opt) = @_;
104
105 6 my (@defs) = (16,30,99);
106
107 6 ▶ if( exists ( $opt{small} ) && $opt{small} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opt{'small'} and $opt{'small'}) { } 2 4 6
elsif (exists $opt{'medium'} and $opt{'medium'}) { } 2 2 4
elsif (exists $opt{'large'} and $opt{'large'}) { } 2 0 2
Condition66%2/3
exists $opt{"small"} and $opt{"small"}
!ll&&!rl&&r
402
MC/DC50%1/2
exists $opt{"small"} and $opt{"small"}
exists $opt{"small"} $opt{"small"}
Truth table66%2/3
A and B
A= exists $opt{"small"}B= $opt{"small"}
ABresult
0X0
100
111
108 2 @defs = (8,8,10);
109 }
110 ▶ elsif( exists ( $opt{medium} )&& $opt{medium}) {
Condition66%2/3
exists $opt{"medium"} and $opt{"medium"}
!ll&&!rl&&r
202
MC/DC50%1/2
exists $opt{"medium"} and $opt{"medium"}
exists $opt{"medium"} $opt{"medium"}
Truth table66%2/3
A and B
A= exists $opt{"medium"}B= $opt{"medium"}
ABresult
0X0
100
111
111 2 @defs = (16,16,40);
112 }
113 ▶ elsif( exists ( $opt{large} ) && $opt{large} ) {
Condition33%1/3
exists $opt{"large"} and $opt{"large"}
!ll&&!rl&&r
002
MC/DC0%0/2
exists $opt{"large"} and $opt{"large"}
exists $opt{"large"} $opt{"large"}
Truth table33%1/3
A and B
A= exists $opt{"large"}B= $opt{"large"}
ABresult
0X0
100
111
114 2 @defs = (16,30,99);
115 }
116
117 6 ▶ if( defined( $opt{ height }) && ($opt{ height }>1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"height"} and $opt{"height"} > 1) 1 5 6
Condition66%2/3
defined $opt{"height"} and $opt{"height"} > 1
!ll&&!rl&&r
501
MC/DC50%1/2
defined $opt{"height"} and $opt{"height"} > 1
defined $opt{"height"} $opt{"height"} > 1
Truth table66%2/3
A and B
A= defined $opt{"height"}B= $opt{"height"} > 1
ABresult
0X0
100
111
118 1 $defs[0] = $opt{height};
119 }
120 6 ▶ if( defined( $opt{ width }) && ($opt{ width }>1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"width"} and $opt{"width"} > 1) 1 5 6
Condition66%2/3
defined $opt{"width"} and $opt{"width"} > 1
!ll&&!rl&&r
501
MC/DC50%1/2
defined $opt{"width"} and $opt{"width"} > 1
defined $opt{"width"} $opt{"width"} > 1
Truth table66%2/3
A and B
A= defined $opt{"width"}B= $opt{"width"} > 1
ABresult
0X0
100
111
121 1 $defs[1] = $opt{width};
122 }
123 6 ▶ if( defined( $opt{ mines }) && ($opt{ mines }>0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"mines"} and $opt{"mines"} > 0) 1 5 6
Condition66%2/3
defined $opt{"mines"} and $opt{"mines"} > 0
!ll&&!rl&&r
501
MC/DC50%1/2
defined $opt{"mines"} and $opt{"mines"} > 0
defined $opt{"mines"} $opt{"mines"} > 0
Truth table66%2/3
A and B
A= defined $opt{"mines"}B= $opt{"mines"} > 0
ABresult
0X0
100
111
124 1 $defs[2] = $opt{mines};
125 }
126 6 return @defs;
127 }
128  
129 =back
130  
131 =head2 Object Methods
132  
133 =over 5
134  
135 =item $obj->print_out($arg)
136  
137 Prints out the game field. It takes one argument, saying what to
138 print. The "field" argument prints out the current visible
139 field. The "solution" argument prints out the actual location of
140 the mines. The "check" argument prints out the field, marking any
141 mistakes that where made. Default is is to print a "field".
142  
143 =cut
144  
145 3 ▶sub print_out {
Subroutine print_out: called CC=7 SCAR=19.5
Pod documented
146 3 my($mine_field) = shift;
147
148 3 ▶ my($type) = shift ||"field";
Condition50%1/2
shift() || 'field'
l!l
30
MC/DC0%0/1
shift() || 'field'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
149 3 my($w,$h);
150
151 3 $mine_field->_start_field;
152
153 3 ▶ for($w = 0; $w <= $mine_field->width(); $w++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w <= $mine_field->width) 9 3 12
154 9 $mine_field->_start_line($w);
155
156 9 ▶ for($h = 0; $h<= $mine_field->height(); $h++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h <= $mine_field->height) 27 9 36
157 27 $mine_field->_start_cell($w,$h);
158 27 ▶ if($type eq "field") {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'field') { } 9 18 27
elsif ($type eq 'check') { } 9 9 18
elsif ($type eq 'solution') { } 9 0 9
159 9 $mine_field->_map( $mine_field->at($w,$h),$w,$h );
160 }
161 elsif($type eq "check") {
162 9 $mine_field->_map( $mine_field->_diff($w,$h),$w,$h );
163 }
164 elsif($type eq "solution") {
165 9 $mine_field->_map( $mine_field->_at($w,$h),$w,$h );
166 }
167 27 $mine_field->_end_cell($w,$h);
168 }
169 9 $mine_field->_end_line($w);
170 }
171 3 $mine_field->_end_field;
172 }
173  
174 =item $obj->print_status_line
175  
176 Prints out a status line of how many mines have been located. If the
177 game has ended, it also prints out the ending text saying why.
178  
179 =cut
180  
181 1 ▶sub print_status_line {
Subroutine print_status_line: called CC=2 SCAR=7.6
Pod documented
182 1 my($mine_field) = shift;
183 1 print "mines: ",$mine_field->flags," of ",
184 $mine_field->count,"\n";
185 1 ▶ unless($mine_field->running) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mine_field->running) 0 1 1
186 0 print $mine_field->why,"\n";
187 }
188 }
189  
190 =item $obj->set_ASCII
191  
192 Set the default mapping of the internal representation to the actual
193 characters printed out, to a plain ascii characters.
194  
195 =cut
196  
197 88 ▶sub set_ASCII {
Subroutine set_ASCII: called CC=1 SCAR=0.0
Pod documented
198 88 my($mine_field) = shift;
199 88 $mine_field->{'map'} = {
200 '*' => '*',
201 '.' => '.',
202 'F' => 'F',
203 'f' => 'f',
204 ' ' => ' ',
205 '1' => '1',
206 '2' => '2',
207 '3' => '3',
208 '4' => '4',
209 '5' => '5',
210 '6' => '6',
211 '7' => '7',
212 '8' => '8',
213 'X' => 'X',
214 };
215 }
216  
217  
218 =item $obj->set_ANSI_Color
219  
220 Set the default mapping of the internal representation to the actual
221 characters printed out, to ascii characters with ANSI colors. If
222 Term::ANSIColor is not installed on your machine, this will quietly
223 fail.
224  
225 =cut
226  
227 45 ▶use vars q($loaded_ansi_color);
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 45 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
230 45 ▶ eval 'use Term::ANSIColor; $loaded_ansi_color=1';
Subroutine BEGIN: called CC=1 SCAR=0.0
231 }
232  
233 17 ▶sub set_ANSI_Color {
Subroutine set_ANSI_Color: called CC=2 SCAR=6.9
Pod documented
234 17 my($mine_field) = shift;
235 17 ▶ return unless( $loaded_ansi_color );
Branch: 1 branch
BranchTrueFalseTotal
unless $Games::Mines::Play::loaded_ansi_color 1 16 17
236 16 $mine_field->{'map'} = {
237 '*' => colored('*',"black","on_white"),
238 '.' => colored('L',"black","on_blue","bold"),
239 'F' => colored('F',"red","on_blue","bold"),
240 'f' => colored('f',"black","on_red"),
241 ' ' => colored(' ',"on_white"),
242 '1' => colored('1',"blue","on_white"),
243 '2' => colored('2',"green","on_white"),
244 '3' => colored('3',"red","on_white"),
245 '4' => colored('4',"black","on_white"),
246 '5' => colored('5',"magenta","on_white"),
247 '6' => colored('6',"cyan","on_white"),
248 '7' => colored('7',"yellow","on_white"),
249 '8' => colored('8',"black","on_white"),
250 'X' => colored('*',"black","on_red","blink"),
251 };
252 }
253  
254  
255 =item $obj->save_game($filename,$number)
256  
257 Saves the current game. Takes two arguments: The filename to save it
258 to, and the game number to save it under. Note that if you give it
259 a game number that already exists within that file, that game will
260 get over written by this one. If no such game number exists, then
261 it is simply added to the end.
262  
263 =cut
264  
265 1 ▶sub save_game {
Subroutine save_game: called CC=30 SCAR=37.3
Pod documented
266 1 my($field) = shift;
267 1 my($file,$game) = @_;
268 1 my($reading)=1;
269
270 1 my($mine_field) = $field->{'GAME'};
271
272 1 ▶ $game ||=$mine_field->{'game number'};
Condition33%1/3
$game ||= $mine_field->{"game number"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$game ||= $mine_field->{"game number"}
$game $mine_field->{"game number"}
Truth table33%1/3
A ||= B
A= $gameB= $mine_field->{"game number"}
ABresult
1X1
011
000
273
274 1 ▶ unless( open(FILE, "$file") ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, "$file") 0 1 1
275 0 $reading=0;
276 }
277 1 ▶ unless( open(FILE_TO, "> $file.working") ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE_TO, "> $file.working") 0 1 1
278 0 warn("can't open file $file.working for temporary file: $!");
279 0 return;
280 }
281
282 1 my($line)="\n";
283 # skip games untill we find the right one
284 1 ▶ if($reading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reading) 1 0 1
285 1 ▶ while($line=<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 45 0 45
286 45 ▶ last if( $line =~/Game\s+$game\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /Game\s+$game\s*$/ 1 44 45
287 44 print FILE_TO $line;
288 }
289 }
290
291 1 print FILE_TO "Game $game\n";
292 1 print FILE_TO $mine_field->width+1,"x",$mine_field->height+1,"\n";
293  
294 1 my($w,$h);
295 1 ▶ for($w = 0; $w <= $mine_field->width(); $w++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w <= $mine_field->width) 3 1 4
296 3 ▶ for($h = 0; $h<= $mine_field->height(); $h++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h <= $mine_field->height) 9 3 12
297 9 ▶ if($mine_field->at($w,$h) eq ' ') {
Branch: 5 branches
BranchTrueFalseTotal
if ($mine_field->at($w, $h) eq ' ') { } 1 8 9
elsif ($mine_field->at($w, $h) =~ /\d/) { } 3 5 8
elsif ($mine_field->at($w, $h) eq '*') { } 0 5 5
elsif ($mine_field->at($w, $h) eq '.') { } 3 2 5
elsif ($mine_field->at($w, $h) eq 'F') { } 2 0 2
298 1 print FILE_TO ' ';
299 }
300 elsif($mine_field->at($w,$h) =~/\d/) {
301 3 print FILE_TO ' ';
302 }
303 elsif($mine_field->at($w,$h) eq '*') {
304 0 print FILE_TO '*';
305 }
306 elsif($mine_field->at($w,$h) eq '.') {
307 3 ▶ if($mine_field->_at($w,$h) eq ' ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mine_field->_at($w, $h) eq ' ') { } 0 3 3
elsif ($mine_field->_at($w, $h) =~ /\d/) { } 2 1 3
elsif ($mine_field->_at($w, $h) eq '*') { } 1 0 1
308 0 print FILE_TO '.';
309 }
310 elsif($mine_field->_at($w,$h) =~/\d/) {
311 2 print FILE_TO '.';
312 }
313 elsif($mine_field->_at($w,$h) eq '*') {
314 1 print FILE_TO ':';
315 }
316 }
317 elsif($mine_field->at($w,$h) eq 'F') {
318 2 ▶ if($mine_field->_at($w,$h) eq ' ') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mine_field->_at($w, $h) eq ' ') { } 0 2 2
elsif ($mine_field->_at($w, $h) =~ /\d/) { } 1 1 2
elsif ($mine_field->_at($w, $h) eq '*') { } 1 0 1
319 0 print FILE_TO 'f';
320 }
321 elsif($mine_field->_at($w,$h) =~/\d/) {
322 1 print FILE_TO 'f';
323 }
324 elsif($mine_field->_at($w,$h) eq '*') {
325 1 print FILE_TO 'F';
326 }
327 }
328 }
329 3 print FILE_TO "\n";
330 }
331 1 print FILE_TO"\n";
332
333 1 ▶ if($reading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reading) 1 0 1
334 1 ▶ while(not eof(FILE)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof FILE) 1 1 2
335 # dump old game number
336 1 ▶ while($line=<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 19 0 19
337 19 ▶ last if( $line =~/Game/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /Game/ 1 18 19
338 }
339 1 ▶ last if( eof(FILE));
Branch: 1 branch
BranchTrueFalseTotal
if eof FILE 0 1 1
340 # copy rest of games
341 1 print FILE_TO $line;
342 1 ▶ while($line=<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 18 1 19
343 18 print FILE_TO $line;
344 }
345 }
346 }
347
348 1 close(FILE_TO);
349 1 ▶ if($reading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reading) 1 0 1
350 1 close(FILE);
351 }
352 1 ▶ if($reading) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reading) 1 0 1
353 1 ▶ rename($file,"$file.bak") || die("Can't move $file to backup: $!");
Branch: 1 branch
BranchTrueFalseTotal
rename $file, "$file.bak" or die "Can't move $file to backup: $!" 1 0 1
354 }
355
356 1 ▶ rename("$file.working",$file) ||
Branch: 1 branch
BranchTrueFalseTotal
rename "$file.working", $file or die "Can't rename temporary file $file.working to ${file}: $!" 1 0 1
357 die("Can't rename temporary file $file.working to $file: $!");
358 }
359  
360 =item $obj->load_game($filename,$number)
361  
362 Loads a previously saved game to replace the current game. It takes
363 two arguments: the file name to get the game from and the game
364 number to load. If it can't open the file or find the given game
365 number will leave the current game unchanged, and return undefined.
366  
367 =cut
368  
369 2 ▶sub load_game {
Subroutine load_game: called CC=16 SCAR=30.9
Pod documented
370 2 my($field) = shift;
371 2 my($file,$game) = @_;
372
373 2 my($old_field) = $field->{'GAME'};
374
375 2 ▶ $game ||=$field->{'game number'};
Condition33%1/3
$game ||= $field->{"game number"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$game ||= $field->{"game number"}
$game $field->{"game number"}
Truth table33%1/3
A ||= B
A= $gameB= $field->{"game number"}
ABresult
1X1
011
000
376 2 ▶ unless( open(FILE, $file) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (open FILE, $file) 0 2 2
377 0 warn("can't open save file $file: $!");
378 0 return;
379 }
380
381 2 my($line);
382 # skip games untill we find the right one
383 2 ▶ while($line=<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline FILE)) 65 0 65
384 65 ▶ last if( $line =~/Game $game\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /Game $game\s*$/ 2 63 65
385 }
386
387 2 ▶ return if(eof(FILE));
Branch: 1 branch
BranchTrueFalseTotal
if eof FILE 0 2 2
388
389 # get the width and height and make new field
390 2 $line=<FILE>;
391 2 $line=~/\s*(\d+)x(\d+)/;
392 2 my($width,$height) = ($1,$2);
393
394 2 my($mine_field) = $old_field->new($width,$height,0);
395
396 2 my($w,$h);
397 2 my($error)=0;
398 # fill in playing field
399 2 ▶ for($w =0;$w<=$mine_field->width;$w++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w <= $mine_field->width) 6 2 8
400
401 6 $line=<FILE>;
402
403 6 my(@sq) = split('',$line);
404
405 6 my($cont,$vis);
406 6 ▶ for($h=0; $h<=$mine_field->height;$h++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h <= $mine_field->height) 18 6 24
407 18 ▶ if($sq[$h] eq '.') { #no mine/unstepped
Branch: 7 branches
BranchTrueFalseTotal
if ($sq[$h] eq '.') { } 4 14 18
elsif ($sq[$h] eq 'f') { } 2 12 14
elsif ($sq[$h] eq ' ') { } 8 4 12
elsif ($sq[$h] =~ /\d/) { } 0 4 4
elsif ($sq[$h] eq ':') { } 2 2 4
elsif ($sq[$h] eq 'F') { } 2 0 2
elsif ($sq[$h] eq 'X') { } 0 0 0
408 4 $mine_field->{field}[$w][$h]{visibility} = '.';
409 }
410 elsif($sq[$h] eq 'f') { #no mine/flagged
411 2 $mine_field->{field}[$w][$h]{visibility} = 'F';
412 2 $mine_field->{flags}++;
413 2 $mine_field->{unknown}--;
414 }
415 elsif($sq[$h] eq ' ') { #no mine/stepped
416 8 $mine_field->{field}[$w][$h]{visibility} = '',;
417 8 $mine_field->{unknown}--;
418 }
419 elsif($sq[$h] =~/\d/) { #no mine/stepped
420 0 $mine_field->{field}[$w][$h]{visibility} = '',;
421 0 $mine_field->{unknown}--;
422 }
423 elsif($sq[$h] eq ':') { # mine/unstepped
424 2 $mine_field->{field}[$w][$h] = {
425 contains => '*',
426 visibility => '.',
427 };
428 2 $mine_field->{count}++;
429 2 $mine_field->_fill_count($w,$h);
430 }
431 elsif($sq[$h] eq 'F') { #mine/flagged
432 2 $mine_field->{field}[$w][$h] = {
433 contains => '*',
434 visibility => 'F',
435 };
436 2 $mine_field->_fill_count($w,$h);
437 2 $mine_field->{flags}++;
438 2 $mine_field->{unknown}--;
439 2 $mine_field->{count}++;
440 }
441 elsif($sq[$h] eq 'X') { #mine/stepped : shouldn't happen
442 0 $mine_field->{field}[$w][$h] = {
443 contains => '*',
444 visibility => '',
445 };
446 0 $mine_field->_fill_count($w,$h);
447 0 $mine_field->{flags}++;
448 0 $mine_field->{unknown}--;
449 0 $mine_field->{count}++;
450 }
451 else { #got something totaly unknown
452 0 die("Don't know how to interpret $sq[$h] in Game $game at line $.\n");
453 0 $error=1;
454 0 last;
455 }
456 }
457 }
458
459 2 ▶ if($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 2 2
460 0 $field->{'GAME'} = $old_field;
461 0 return;
462 }
463
464 2 $mine_field->{on} = 1;
465 2 $field->{'GAME'} = $mine_field;
466
467 2 return 1;
468 }
469  
470 =begin for developers
471  
472 =cut
473  
474 4 ▶sub _start_field {
Subroutine _start_field: called CC=3 SCAR=11.0
475 4 my($mine_field) = shift;
476 4 my($w,$h);
477
478 4 my $cars= length($mine_field->height());
479 4 my($format) = "%0". length($mine_field->height()). "u";
480
481 4 foreach my $line (0.. $cars-1 ) {
482 4 print " "x(length($mine_field->width())+1);
483
484 4 foreach my $h (0.. $mine_field->height()) {
485 12 print substr(sprintf($format,$h),$line,1);
486 }
487 4 print "\n";
488 }
489
490 print
491 4 " "x(length($mine_field->width())),
492 "+",
493 "-"x($mine_field->height()+1),
494 "+\n";
495 }
496  
497 4 ▶sub _end_field {
Subroutine _end_field: called CC=1 SCAR=0.0
498 4 my($mine_field) = shift;
499 4 print
500 " "x(length($mine_field->width())),
501 "+",
502 "-"x($mine_field->height()+1),
503 "+\n";
504 }
505  
506 10 ▶sub _start_line {
Subroutine _start_line: called CC=1 SCAR=0.0
507 10 my($mine_field) = shift;
508 10 my($line) = shift;
509 10 my($format) = "%0". length($mine_field->width()). "u|";
510 10 printf($format,$line);
511 }
512  
513 10 ▶sub _end_line {
Subroutine _end_line: called CC=1 SCAR=0.0
514 10 print "|\n";
515 }
516  
517 55 ▶sub _map {
Subroutine _map: called CC=1 SCAR=0.0
518 55 my($mine_field) = shift;
519
520 55 my($value) = shift;
521 55 print $mine_field->{map}->{ $value };
522 }
523  
524 27 ▶sub _start_cell {1;}
Subroutine _start_cell: called CC=1 SCAR=0.0
525  
526 27 ▶sub _end_cell {1;}
Subroutine _end_cell: called CC=1 SCAR=0.0
527  
528 =item $obj->_diff
529  
530 Internal method used to print out the end game results, indicating any
531 wrongly marked or stepped fields.
532  
533 =cut
534  
535 9 ▶sub _diff {
Subroutine _diff: called CC=5 SCAR=16.6
536 9 my($field) = shift;
537 9 my($mine_field) = $field->{'GAME'};
538 9 my($w,$h) = @_;
539
540 9 ▶ if($mine_field->shown($w,$h)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mine_field->shown($w, $h)) { } 4 5 9
elsif ($mine_field->at($w, $h) eq 'X') { } 0 5 5
elsif ($mine_field->at($w, $h) eq 'F') { } 1 4 5
541 4 return $mine_field->_at($w,$h);
542 }
543 elsif($mine_field->at($w,$h) eq 'X') {
544 0 return 'X';
545 }
546 elsif($mine_field->at($w,$h) eq 'F') {
547 1 ▶ if($mine_field->_at($w,$h) eq '*'){
Branch: 1 branch
BranchTrueFalseTotal
if ($mine_field->_at($w, $h) eq '*') { } 0 1 1
548 0 return $mine_field->_at($w,$h);
549 }
550 else {
551 1 return 'f';
552 }
553 }
554 4 return $mine_field->_at($w,$h);
555 }
556  
557 =item AUTOLOAD
558  
559 Pass any unknown method calls to the contined object.
560  
561 =cut
562  
563 321 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
564 321 my($game) = shift;
565
566 # DESTROY messages should never be propagated.
567 321 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $Games::Mines::Play::AUTOLOAD =~ /::DESTROY$/ 58 263 321
568
569 263 my($attr) = $AUTOLOAD;
570 263 $attr =~ s/^.*:://;
571  
572 263 ▶ unless ( $game->{GAME}->can($attr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->{"GAME"}->can($attr)) 0 263 263
573 0 croak "Don't have a method of type $attr";
574 }
575
576 263 $game->{GAME}->$attr(@_);
577 }
578  
579 =end for developers
580  
581 =back
582  
583 =head1 SEE ALSO
584  
585 Games::Mines for more details of the base class.
586  
587 =head1 AUTHOR
588  
589 Martyn W. Peck <mwp@mwpnet.com>
590  
591 =head1 BUGS
592  
593 None known. But if you find any, let me know.
594  
595 =head1 COPYRIGHT
596  
597 Copyright 2003, Martyn Peck. All Rights Reserves.
598  
599 This program is free software. You may copy or redistribute
600 it under the same terms as Perl itself.
601  
602 =cut
603  
604 45 1;
↑ Summary Mines.pm »