RabbitFarm
2024-08-10
Checking Out the Knight’s Moves
The examples used here are from the weekly challenge problem statement and demonstrate the working solution.
File Index
Part 1: Check Color
You are given coordinates, a string that represents the coordinates of a square of the chessboard. Write a script to return true if the square is light, and false if the square is dark.
The complete solution is contained in one file that has a simple structure.
For this problem we do not need to include very much. We’re just specifying to use the current version of Perl, for all the latest features in the language. This fragment is also used in Part 2.
The color of a given square is determined by calculating its color number. If the color number is positive the square is dark. If the color number is negative then it is light.
We determine the color number in the following code fragment. Here we compute $n as -1 raised tot he power of the letter’s index. In this way we get alternating -1/1 starting with ’a’. We do the same with the second part of the co-rdinate to get an alternating -1/1 for the chessboard row. These are multiplied together to get the color number.
Now all we need are a few lines of code for running some tests.
-
MAIN:{
say check_color q/d3/;
say check_color q/g5/;
say check_color q/e6/;
say check_color q/b1/;
say check_color q/b8/;
say check_color q/h1/;
say check_color q/h8/;
}
◇
-
Fragment referenced in 1.
Sample Run
$ perl perl/ch-1.pl true false true true false true false
Part 2: Knight’s Move
A Knight in chess can move from its current position to any square two rows or columns plus one column or row away. Write a script which takes a starting position and an ending position and calculates the least number of moves required.
The bulk of the code is just setting up the main data structure, a graph. For each square of the chessboard we add an edge to all the squares that are reachable by a knight.
-
sub build_graph{
my $graph = Graph->new();
do {
my $c = $_;
do {
my $r = $_;
my($s, $t);
##
# up
##
$s = $r + 2;
$t = chr(ord(qq/$c/) - 1);
$t = chr(ord(qq/$c/) + 1);
##
# down
##
$s = $r - 2;
$t = chr(ord(qq/$c/) - 1);
$t = chr(ord(qq/$c/) + 1);
##
# left
##
$s = $r - 1;
$t = chr(ord(qq/$c/) - 2);
$s = $r + 1;
##
# right
##
$s = $r - 1;
$t = chr(ord(qq/$c/) + 2);
$s = $r + 1;
} for 1 .. 8;
} for q/a/ .. q/h/;
return $graph;
}
◇
For convenience I use a little bit of nuweb hackery instead of a new subroutine to seperate out this code which is repeated in the final generated code file.
After we go through the work of setting up the graph the result can be easily gotten via use of Djikstra’s shortest path algorithm.
Finally, here’s a few tests to confirm everything is working right.
Sample Run
$ perl ch-2.pl g2 ---> a8 4: g2 -> e3 -> c4 -> b6 -> a8 g2 ---> h2 3: g2 -> e1 -> f3 -> h2
References
The Weekly Challenge 281
Generated Code
Graph.pm
Knight’s Tours
posted at: 16:52 by: Adam Russell | path: /perl | permanent link to this entry