summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-06-01 23:09:46 -0500
committerJesse Luehrs <doy@tozt.net>2009-06-01 23:21:24 -0500
commitdab4c4cde50058ee7406ecfcd1f56b08a3b6f049 (patch)
treef8f3575e5d2cdeb5fb6cbc0b37c981d24db3ca88
parent6778932372f5a51d8000ca3ef011e36ab083bd2d (diff)
downloadbot-games-dab4c4cde50058ee7406ecfcd1f56b08a3b6f049.tar.gz
bot-games-dab4c4cde50058ee7406ecfcd1f56b08a3b6f049.zip
first stab at a chess implementation
-rw-r--r--lib/Bot/Games/Game/Chess.pm64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/Bot/Games/Game/Chess.pm b/lib/Bot/Games/Game/Chess.pm
new file mode 100644
index 0000000..06a014e
--- /dev/null
+++ b/lib/Bot/Games/Game/Chess.pm
@@ -0,0 +1,64 @@
+package Bot::Games::Game::Chess;
+use Bot::Games::OO::Game;
+extends 'Bot::Games::Game';
+
+use MooseX::AttributeHelpers;
+use Chess::Rep;
+use App::Nopaste;
+
+has '+help' => (
+ default => 'chess help',
+);
+
+has game => (
+ is => 'ro',
+ isa => 'Chess::Rep',
+ default => sub { Chess::Rep->new },
+);
+
+has turn_count => (
+ traits => ['Counter'],
+ is => 'ro',
+ isa => 'Int',
+ default => 1,
+ provides => {
+ inc => 'inc_turn',
+ }
+);
+
+augment turn => sub {
+ my $self = shift;
+ my ($player, $move) = @_;
+ $self->add_player($player) unless $self->has_player($player);
+ return unless $self->has_player($player);
+
+ my $status = eval { $self->game->go_move($move) };
+ return $@ if $@;
+ my $desc = $self->format_turn($status);
+ $self->game->inc_counter if $self->game->to_move;
+ $self->is_active(0) if $self->game->status->{mate}
+ || $self->game->status->{stalemate};
+
+ return "$desc: " . App::Nopaste::nopaste(text => $self->game->dump_pos,
+ desc => $desc,
+ nick => $player);
+};
+
+around allow_new_player => sub {
+ my $orig = shift;
+ my $self = shift;
+ return if $self->num_players >= 2;
+ return $self->$orig(@_);
+};
+
+sub format_turn {
+ my $self = shift;
+ my ($turn) = @_;
+ my $ret = $self->turn_count . ". ";
+ if ($self->game->to_move) {
+ $ret .= "... ";
+ }
+ $ret .= $turn->{san};
+}
+
+1;