diff options
| -rwxr-xr-x | challenge-281/mattneleigh/perl/ch-1.pl | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-281/mattneleigh/perl/ch-1.pl b/challenge-281/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..311236273b --- /dev/null +++ b/challenge-281/mattneleigh/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @chess_squares = ( + "d3", + "g5", + "e6" +); + +print("\n"); +foreach my $chess_square (@chess_squares){ + printf( + "Input: \$coordinates = \"%s\"\nOutput: %s\n\n", + $chess_square, + is_chess_square_light($chess_square) ? "true" : "false" + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether a given square on a chessboard is light or dark, given its +# coordinates in Algebraic Notation- file (a-h) and rank (1-8) +# Takes one argument: +# * The coordinates of the square in algebraic notation (e.g. "d3") +# Returns: +# * 0 if the square is dark; 1 if the square is light +################################################################################ +sub is_chess_square_light{ + my ($file, $rank) = split(//, shift()); + + return( + # Convert the file coordinate to a number + # (1-indexed) then determine whether each + # coordinate is even, and XOR the results + ((ord(lc($file)) - 96) % 2) ^ ($rank % 2) + ); + +} + + + |
