line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Sudoku::OO::Board; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
54941
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
264
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
73
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
1637
|
use Games::Sudoku::OO::Set::Row; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
60
|
|
7
|
2
|
|
|
2
|
|
1401
|
use Games::Sudoku::OO::Set::Column; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
54
|
|
8
|
2
|
|
|
2
|
|
1307
|
use Games::Sudoku::OO::Set::Square; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
88
|
|
9
|
2
|
|
|
2
|
|
1720
|
use Games::Sudoku::OO::Cell; |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
60
|
|
10
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2150
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
11
|
|
|
11
|
0
|
7603
|
my $proto = shift; |
16
|
11
|
|
|
|
|
47
|
my %args = (text_grid=>undef, @_); |
17
|
11
|
|
|
|
|
26
|
my $self = {}; |
18
|
11
|
|
|
|
|
24
|
bless ($self); |
19
|
|
|
|
|
|
|
|
20
|
11
|
50
|
|
|
|
44
|
if ($args{text_grid}){ |
21
|
0
|
|
|
|
|
0
|
$self->importGrid($args{text_grid}); |
22
|
|
|
|
|
|
|
} |
23
|
11
|
|
|
|
|
36
|
return $self; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub importGrid{ |
29
|
10
|
|
|
10
|
0
|
55
|
my $self = shift; |
30
|
10
|
|
|
|
|
19
|
my $text_grid = shift; |
31
|
|
|
|
|
|
|
|
32
|
10
|
50
|
|
|
|
583
|
open my $fh, $text_grid or confess "can't open $text_grid: $!"; |
33
|
10
|
|
|
|
|
346
|
my @grid_lines = <$fh>; |
34
|
10
|
|
|
|
|
255
|
close $fh; |
35
|
|
|
|
|
|
|
|
36
|
10
|
|
|
|
|
48
|
my $size = $self->{size} = @grid_lines; |
37
|
10
|
50
|
66
|
|
|
70
|
croak "I only support grids of size 9 or 16\n" |
38
|
|
|
|
|
|
|
unless (($size == 9) or ($size == 16)); |
39
|
|
|
|
|
|
|
|
40
|
10
|
|
|
|
|
17
|
my %possibles; |
41
|
|
|
|
|
|
|
my $first; |
42
|
10
|
100
|
|
|
|
31
|
if ($size <= 10){ |
43
|
9
|
|
|
|
|
20
|
$first =1; |
44
|
|
|
|
|
|
|
}else { |
45
|
1
|
|
|
|
|
4
|
$first =0; |
46
|
|
|
|
|
|
|
} |
47
|
10
|
|
|
|
|
41
|
for (my $i = 0; $i < $size; $i++){ |
48
|
97
|
|
|
|
|
452
|
$possibles{$i+$first} = 1; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
10
|
|
|
|
|
37
|
for (my $i = 0; $i < $size; $i++){ |
53
|
97
|
|
|
|
|
123
|
push @{$self->{ROWS}}, Games::Sudoku::OO::Set::Row->new(possibles=>\%possibles); |
|
97
|
|
|
|
|
687
|
|
54
|
97
|
|
|
|
|
151
|
push @{$self->{COLUMNS}}, Games::Sudoku::OO::Set::Column->new(possibiles=>\%possibles); |
|
97
|
|
|
|
|
498
|
|
55
|
97
|
|
|
|
|
147
|
push @{$self->{SQUARES}}, Games::Sudoku::OO::Set::Square->new(possibles=>\%possibles); |
|
97
|
|
|
|
|
503
|
|
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
10
|
|
|
|
|
41
|
foreach (my $row =0; $row < $size; $row++){ |
59
|
97
|
|
|
|
|
243
|
foreach (my $column =0; $column < $size; $column++){ |
60
|
985
|
|
|
|
|
3381
|
my $cell = Games::Sudoku::OO::Cell->new(possibles=>\%possibles); |
61
|
985
|
|
|
|
|
3225
|
my $square = int ($column/sqrt($size)) + int ($row/sqrt($size))*int(sqrt($size)); |
62
|
985
|
|
|
|
|
3512
|
($self->{ROWS}[$row])->addCell($cell); |
63
|
985
|
|
|
|
|
3486
|
($self->{COLUMNS}[$column])->addCell($cell); |
64
|
985
|
|
|
|
|
3481
|
($self->{SQUARES}[$square])->addCell($cell); |
65
|
985
|
|
|
|
|
2361
|
my $value = substr($grid_lines[$row], $column, 1); |
66
|
985
|
100
|
|
|
|
4787
|
if ($value =~ /[0-9A-Fa-f]/){ |
67
|
476
|
|
|
|
|
1593
|
$cell->setValue(hex $value); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
10
|
|
|
|
|
185
|
1; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub toStr { |
75
|
7
|
|
|
7
|
0
|
15
|
my $self = shift; |
76
|
7
|
|
|
|
|
20
|
my $string = ""; |
77
|
7
|
|
|
|
|
10
|
foreach my $row(@{$self->{ROWS}}){ |
|
7
|
|
|
|
|
27
|
|
78
|
70
|
|
|
|
|
216
|
$string .= $row->toStr() . "\n"; |
79
|
|
|
|
|
|
|
} |
80
|
7
|
|
|
|
|
58
|
return $string; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub solve { |
84
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
85
|
0
|
|
|
|
|
0
|
my ($type, $i) = @_; |
86
|
0
|
|
|
|
|
0
|
my $set_name = uc $type . 'S'; #convert row to ROWS, column to COLUMNS |
87
|
0
|
|
|
|
|
0
|
my $set = $self->{$set_name}[$i]; |
88
|
0
|
|
|
|
|
0
|
$set->solve(); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub solveRow { |
92
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
93
|
0
|
|
|
|
|
0
|
$self->solve('row', @_); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub solveColumn { |
97
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
98
|
0
|
|
|
|
|
0
|
$self->solve('column', @_); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub solveSquare { |
102
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
103
|
0
|
|
|
|
|
0
|
$self->solve('square', @_); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub solveAll { |
107
|
10
|
|
|
10
|
0
|
19
|
my $self = shift; |
108
|
|
|
|
|
|
|
|
109
|
10
|
|
|
|
|
16
|
my $unsolved = 1; |
110
|
|
|
|
|
|
|
|
111
|
10
|
|
|
|
|
62
|
for(my $i=0; $i < $self->{size}**2; $i++){ |
112
|
55
|
|
|
|
|
98
|
$unsolved = 0; |
113
|
55
|
|
|
|
|
85
|
foreach my $set (@{$self->{ROWS}}, |
|
55
|
|
|
|
|
150
|
|
|
55
|
|
|
|
|
138
|
|
114
|
55
|
|
|
|
|
133
|
@{$self->{COLUMNS}}, |
115
|
|
|
|
|
|
|
@{$self->{SQUARES}}) |
116
|
|
|
|
|
|
|
{ |
117
|
1695
|
|
|
|
|
5430
|
my $cells_remaining = $set->solve(); |
118
|
1695
|
|
100
|
|
|
6005
|
$unsolved ||= $cells_remaining; |
119
|
|
|
|
|
|
|
} |
120
|
55
|
100
|
|
|
|
988
|
last unless $unsolved; |
121
|
|
|
|
|
|
|
} |
122
|
10
|
|
|
|
|
115
|
return not $unsolved; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
__END__ |