aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScimon <simon.proctor@gmail.com>2025-08-18 10:43:35 +0100
committerScimon <simon.proctor@gmail.com>2025-08-18 10:43:35 +0100
commitd4ec19f9f6a8563fe106879deb42adcf3fbbcb51 (patch)
tree315dc7f48b05f6e659694f708a649160652e8557
parenta3d438220113eae12749f3e9d49df17535efc96c (diff)
downloadperlweeklychallenge-club-d4ec19f9f6a8563fe106879deb42adcf3fbbcb51.tar.gz
perlweeklychallenge-club-d4ec19f9f6a8563fe106879deb42adcf3fbbcb51.tar.bz2
perlweeklychallenge-club-d4ec19f9f6a8563fe106879deb42adcf3fbbcb51.zip
Part 2, works might come back to it
-rwxr-xr-xchallenge-335/simon-proctor/raku/ch-2.raku61
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-335/simon-proctor/raku/ch-2.raku b/challenge-335/simon-proctor/raku/ch-2.raku
new file mode 100755
index 0000000000..e4c6f2dbf4
--- /dev/null
+++ b/challenge-335/simon-proctor/raku/ch-2.raku
@@ -0,0 +1,61 @@
+#!/usr/bin/env raku
+
+subset Move of Int where 0 <= * <= 2;
+
+class OXO {
+ has @.board = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']];
+ has Str $!current = 'A';
+
+ multi method move(@move where @move.elems==2 && all(@move) ~~ Move) {
+ self.move(|@move);
+ }
+ multi method move(Move $x,Move $y where @!board[$x][$y] ~~ ' ') {
+ @!board[$x][$y] = $!current;
+ $!current = $!current ~~ 'A' ?? 'B' !! 'A';
+ return self;
+ }
+ multi method move($x,$y) { fail "{$x} {$y} is already filled"}
+ method moves(@moves) {
+ self.move($_) for @moves;
+ }
+
+
+ method Str { @!board.Str }
+ method gist { @!board.map(*.join("|")).join("\n-+-+-\n") }
+
+ method winner() {
+ my $victory = any(
+ all(@!board[0][0],@!board[0][1],@!board[0][2]),
+ all(@!board[1][0],@!board[1][1],@!board[1][2]),
+ all(@!board[2][0],@!board[2][1],@!board[2][2]),
+ all(@!board[0][0],@!board[1][0],@!board[2][0]),
+ all(@!board[0][1],@!board[1][1],@!board[2][1]),
+ all(@!board[0][2],@!board[1][2],@!board[2][2]),
+ all(@!board[0][0],@!board[1][1],@!board[2][2]),
+ all(@!board[2][0],@!board[1][1],@!board[0][2])
+ );
+ return 'A' if $victory ~~ 'A';
+ return 'B' if $victory ~~ 'B';
+ return 'Draw' if none(@!board.map(*.flat).flat) ~~ ' ';
+ return 'Pending';
+ }
+}
+
+
+sub MAIN(:t(:$test)) is hidden-from-USAGE {
+ use Test;
+ my @tests = (
+ [[[0,0],[2,0],[1,1],[2,1],[2,2]],'A'],
+ [([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]),'B'],
+ [([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]),'Draw'],
+ [([0,0],[1,1]),'Pending']
+ );
+ for @tests -> $test {
+ my $moves = $test[0];
+ my $expected = $test[1];
+ my $board = OXO.new;
+ $board.moves($moves);
+ is $board.winner, $expected, "\n{$board.gist} victory for {$expected}";
+ }
+ done-testing
+}