aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-11 10:47:54 +0100
committerGitHub <noreply@github.com>2024-08-11 10:47:54 +0100
commit13067c4b61ecbeb8a5075c33fcf6922678ec29a3 (patch)
tree6641c6406d8cd62a1ae03c9b90bb3e7ea700a5cd
parent70fff8294eb39896d29ce5e4543af547e5de724c (diff)
parent26ebb13dac6bd28888a545a3514c185f5e0320c9 (diff)
downloadperlweeklychallenge-club-13067c4b61ecbeb8a5075c33fcf6922678ec29a3.tar.gz
perlweeklychallenge-club-13067c4b61ecbeb8a5075c33fcf6922678ec29a3.tar.bz2
perlweeklychallenge-club-13067c4b61ecbeb8a5075c33fcf6922678ec29a3.zip
Merge pull request #10578 from mattneleigh/pwc281
new file: challenge-281/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-281/mattneleigh/perl/ch-1.pl54
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)
+ );
+
+}
+
+
+