From 114a22263299efccafbb7e62081171f5d497f7fe Mon Sep 17 00:00:00 2001 From: doy Date: Wed, 14 Jan 2009 22:11:50 -0500 Subject: convert the plugins over to using the command syntax --- lib/Bot/Games/Game/24.pm | 21 ++++++++++--------- lib/Bot/Games/Game/Ghost.pm | 4 ++-- lib/Bot/Games/Game/Ghostlike.pm | 45 ++++++++++++++++++++++------------------ lib/Bot/Games/Game/Spook.pm | 4 ++-- lib/Bot/Games/Game/Superghost.pm | 4 ++-- lib/Bot/Games/Game/Xghost.pm | 4 ++-- 6 files changed, 44 insertions(+), 38 deletions(-) (limited to 'lib/Bot/Games/Game') diff --git a/lib/Bot/Games/Game/24.pm b/lib/Bot/Games/Game/24.pm index 8580d7b..039b551 100644 --- a/lib/Bot/Games/Game/24.pm +++ b/lib/Bot/Games/Game/24.pm @@ -10,16 +10,17 @@ has '+help' => ( has state => ( is => 'rw', isa => 'Str', + command => 1, ); -has _solution => ( +has solution => ( is => 'rw', isa => 'Str', ); -sub _init { +sub init { my $self = shift; - return $self->_generate_24; + return $self->generate_24; } sub turn { @@ -30,7 +31,7 @@ sub turn { my $solution = join ',', sort split(' ', $self->state); return "invalid numbers" unless $numbers eq $solution; - my $eval = $self->_evaluate($expr); + my $eval = $self->evaluate($expr); if ($eval == 24) { $self->is_over("$player wins!"); return; @@ -40,15 +41,15 @@ sub turn { } } -sub give_up { +command give_up => { my $self = shift; - $self->is_over($self->_solution); + $self->is_over($self->solution); return; -} +}; my @ops = ('+', '-', '*', '/'); -sub _generate_24 { +sub generate_24 { my $self = shift; my @nums = (24); for (1..3) { @@ -81,12 +82,12 @@ sub _generate_24 { } pop @nums; shift @nums; - $self->_solution(join '', @nums); + $self->solution(join '', @nums); $self->state(join ' ', (grep { /\d/ } @nums)); return $self->state; } -sub _evaluate { +sub evaluate { my $self = shift; my ($expr) = @_; return 0 unless $expr =~ /^[-\d\+\*\/\(\)]+$/; diff --git a/lib/Bot/Games/Game/Ghost.pm b/lib/Bot/Games/Game/Ghost.pm index 52ad2d0..1d65683 100644 --- a/lib/Bot/Games/Game/Ghost.pm +++ b/lib/Bot/Games/Game/Ghost.pm @@ -7,11 +7,11 @@ has '+help' => ( default => "ghost help", ); -sub valid_move { +command valid_move => { my $self = shift; my ($move) = @_; return uc(substr($move, 0, -1)) eq $self->state; -} +}; __PACKAGE__->meta->make_immutable; no Bot::Games::OO; diff --git a/lib/Bot/Games/Game/Ghostlike.pm b/lib/Bot/Games/Game/Ghostlike.pm index 9ebbe89..e6bad0e 100644 --- a/lib/Bot/Games/Game/Ghostlike.pm +++ b/lib/Bot/Games/Game/Ghostlike.pm @@ -7,33 +7,38 @@ extends 'Bot::Games::Game'; has current_player => ( is => 'rw', isa => 'Str', - predicate => '_has_current_player', + predicate => 'has_current_player', + command => 1, ); has players_fixed => ( is => 'rw', isa => 'Bool', default => 0, + command => 1, ); has state => ( is => 'rw', isa => 'Str', default => '', + command => 1, ); has challenger => ( is => 'rw', isa => 'Str', predicate => 'has_challenger', + command => 1, + commands => ['has_challenger'], ); -has _wordlist => ( +has wordlist => ( is => 'ro', isa => 'Games::Word::Wordlist', default => sub { Games::Word::Wordlist->new('/usr/share/dict/words') }, handles => { - _valid_word => 'is_word', + valid_word => 'is_word', }, ); @@ -50,10 +55,10 @@ sub turn { my $self = shift; my ($player, $state) = @_; - if ($self->_current_player_index == 0 + if ($self->current_player_index == 0 && !$self->players_fixed && !grep { $player eq $_ } $self->players) { - $self->_add_player($player); + $self->add_player($player); $self->current_player($player); } @@ -68,7 +73,7 @@ sub turn { return $self->state($state); } -sub challenge { +command challenge => { my $self = shift; my ($word, $args) = @_; my $player = $args->{player}; @@ -80,7 +85,7 @@ sub challenge { if (!$self->valid_word_from_state($word)) { return "$word is not valid for state " . $self->state . "!"; } - elsif ($self->_valid_word($word)) { + elsif ($self->valid_word($word)) { $self->is_over("$word is a word! $challenger wins!"); return; } @@ -94,29 +99,29 @@ sub challenge { $self->current_player($prev); return "$player is challenging $prev!"; } -} +}; -sub previous_player { +command previous_player => { my $self = shift; - return unless $self->_has_current_player; - return $self->players->[$self->_current_player_index - 1]; -} + return unless $self->has_current_player; + return $self->players->[$self->current_player_index - 1]; +}; -sub next_player { +command next_player => { my $self = shift; - return unless $self->_has_current_player; - return $self->players->[($self->_current_player_index + 1) % $self->num_players]; -} + return unless $self->has_current_player; + return $self->players->[($self->current_player_index + 1) % $self->num_players]; +}; -sub valid_move { 1 } +command valid_move => { 1 }; -sub valid_word_from_state { +command valid_word_from_state => { my $self = shift; my ($word) = @_; return uc($word) eq $self->state; -} +}; -sub _current_player_index { +sub current_player_index { my $self = shift; for (0..($self->num_players - 1)) { return $_ if $self->current_player eq $self->players->[$_]; diff --git a/lib/Bot/Games/Game/Spook.pm b/lib/Bot/Games/Game/Spook.pm index 74a44e6..2ec4e2e 100644 --- a/lib/Bot/Games/Game/Spook.pm +++ b/lib/Bot/Games/Game/Spook.pm @@ -8,14 +8,14 @@ has '+help' => ( default => "spook help", ); -sub valid_move { +command valid_move => { my $self = shift; my ($move) = @_; return is_subpermutation($self->state, uc($move)) && length($self->state) + 1 == length($move); } -sub valid_word_from_state { +command valid_word_from_state => { my $self = shift; my ($word) = @_; $word = uc join '', sort split(//, $word); diff --git a/lib/Bot/Games/Game/Superghost.pm b/lib/Bot/Games/Game/Superghost.pm index ae4438a..f6a4d7b 100644 --- a/lib/Bot/Games/Game/Superghost.pm +++ b/lib/Bot/Games/Game/Superghost.pm @@ -7,12 +7,12 @@ has '+help' => ( default => "superghost help", ); -sub valid_move { +command valid_move => { my $self = shift; my ($move) = @_; return uc(substr($move, 0, -1)) eq $self->state || uc(substr($move, 1)) eq $self->state; -} +}; __PACKAGE__->meta->make_immutable; no Bot::Games::OO; diff --git a/lib/Bot/Games/Game/Xghost.pm b/lib/Bot/Games/Game/Xghost.pm index 8aeabb2..8f9092d 100644 --- a/lib/Bot/Games/Game/Xghost.pm +++ b/lib/Bot/Games/Game/Xghost.pm @@ -8,12 +8,12 @@ has '+help' => ( default => "xghost help", ); -sub valid_move { +command valid_move => { my $self = shift; my ($move) = @_; return is_substring($self->state, uc($move)) && length($self->state) + 1 == length($move); -} +}; __PACKAGE__->meta->make_immutable; no Bot::Games::OO; -- cgit v1.2.3-54-g00ecf