| Next | Program
Repair Shop ![]() |
174 |
That table seems complicated
What if the squares were named 0..8 instead?
That's a nice idea, but it doesn't really work:
@table = ([0,1,2], [3,4,5], [6,7,8],
[0,3,6], [1,4,7], [2,5,8],
[0,4,8], [2,4,7]);
for my $win (@table) {
my (@x, @y);
for (@$win) {
push @x, $_ % 3;
push @y, int($_ / 3);
}
if ($board->[$x[0]][$y[0]] eq $board->[$x[1]][$y[1]]
&& $board->[$x[0]][$y[0]] eq $board->[$x[2]][$y[2]]) {
return $board->[$x[0]][$y[0]];
}
}
If $board had been 1-d instead of 2-d, we would have had
for my $w (@table) {
if ($board->[$w->[0]] eq $board->[$w->[1]]
&& $board->[$w->[0]] eq $board->[$w->[2]]) {
return $board->[$w->[0]];
}
}
File this idea for later use
| Next | ![]() |
Copyright © 2002 M. J. Dominus |