Permalink
Browse files

[Druid::Player::Computer] really dumb defense

But it's still better than 100% random moves.

Also fixed a .pick API carpet pull.
  • Loading branch information...
1 parent b2f6c89 commit 6cb1293b6653ca7db5795fd359e38ec453026d13 @masak committed Jul 30, 2009
Showing with 43 additions and 2 deletions.
  1. +43 −2 lib/Druid/Player/Computer.pm
@@ -9,11 +9,48 @@ A computer player. It currently only makes random sarsen moves, and is thus
ridiculously easily defeatable.
=end SUMMARY
+has $!last-move;
+
method choose-move() {
+ # First, see if we can put a piece close to the previous piece. With
+ # luck, this might even block the opponent.
+ if $!last-move {
+ given $.color {
+ when 1 {
+ my ($last-row, $last-column) = @($!last-move);
+ for ($last-column-2, $last-column-1,
+ $last-column+1, $last-column+2) -> $column {
+
+ next unless 0 <= $column < $.size;
+ next unless $.colors[$last-row][$column] == 0 | $!color;
+ my $move = chr(ord('a')+$column) ~ ($last-row+1);
+ say '';
+ say "The computer moves $move";
+ return $move;
+ }
+ }
+ when 2 {
+ my ($last-row, $last-column) = @($!last-move);
+ for ($last-row-2, $last-row-1,
+ $last-row+1, $last-row+2) -> $row {
+
+ next unless 0 <= $row < $.size;
+ next unless $.colors[$row][$last-column] == 0 | $!color;
+ my $move = chr(ord('a')+$last-column) ~ ($row+1);
+ say '';
+ say "The computer moves $move";
+ return $move;
+ }
+ }
+ }
+ }
+
+ # At this point, we content ourself with finding a random spot to
+ # settle down on.
my ($row, $column);
repeat {
- $row = (^$.size).pick[0];
- $column = (^$.size).pick[0];
+ $row = (^$.size).pick;
+ $column = (^$.size).pick;
} until $.colors[$row][$column] == 0 | $!color;
my $move = chr(ord('a')+$column) ~ ($row+1);
@@ -23,4 +60,8 @@ method choose-move() {
return $move;
}
+method add-piece($height, $row, $column, $color) {
+ $!last-move = [$row, $column];
+}
+
# vim: filetype=perl6

0 comments on commit 6cb1293

Please sign in to comment.