-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.pm
102 lines (72 loc) · 1.85 KB
/
Block.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Block;
use strict;
use warnings;
use Cell;
use Data::Dumper;
=head1 new
Our constructor method.
Creates a new block object populated with 9 cell objects numbered 1 through 9.
my $block = Block->new;
=cut
sub new {
my $class = shift;
my $self;
for my $num (1 .. 9) {
my $cell = Cell->new();
$self->{$num} = $cell;
}
return bless $self, $class;
}
=head1 get_block_values
Method that returns all the values for a given block, specified by cell number.
my $values = $board->get_block_values;
my $value_for_cell_3 = $values->{3};
=cut
sub get_block_values {
my $self = shift;
my $values;
for my $num (1 .. 9) {
my $value = $self->{$num}->get_value();
if (defined $value) {
$values->{$num} = $value;
} else {
$values->{$num} = ' ';
}
}
return $values;
}
=head1 get_cell
Simple getter to get the cell object stored inside the given block;
my $cell = $block->get_cell(3);
=cut
sub get_cell {
my ($self, $cell_num) = @_;
return $self->{$cell_num};
}
=head1 remove_options_by_block
Method that goes through our block, cell by cell, and if a cell has a value,
removes that as an option for the other cells.
$block->remove_options_by_block;
=cut
sub remove_options_by_block {
my $self = shift;
my $removals = 0;
for my $cell_number (1 .. 9) {
my $cell = $self->get_cell($cell_number);
my $value = $cell->get_value;
if (defined $value) {
for my $other_cell_number (1 .. 9) {
my $other_cell = $self->get_cell($other_cell_number);
next if ($cell_number == $other_cell_number);
next if (defined $other_cell->get_value);
my $options = $other_cell->get_options;
if (defined $options->{$value}) {
$other_cell->remove_option($value);
$removals++;
}
}
}
}
return $removals;
}
1;