summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-06-02 01:17:11 -0500
committerJesse Luehrs <doy@tozt.net>2009-06-02 01:17:11 -0500
commit83c5f5f920656948d0528320c28150c618593ff6 (patch)
treebe9d4e4340ce0f7100e1d398614bb605a780aa12
parent23d8dfac4d65c100ea79c49076657cf2933d1936 (diff)
downloadbot-games-83c5f5f920656948d0528320c28150c618593ff6.tar.gz
bot-games-83c5f5f920656948d0528320c28150c618593ff6.zip
factor out the current player logic into a role
-rw-r--r--lib/Bot/Games/Game/Ghost.pm28
-rw-r--r--lib/Bot/Games/Game/Role/CurrentPlayer.pm35
2 files changed, 36 insertions, 27 deletions
diff --git a/lib/Bot/Games/Game/Ghost.pm b/lib/Bot/Games/Game/Ghost.pm
index 43798f7..8a3a9c6 100644
--- a/lib/Bot/Games/Game/Ghost.pm
+++ b/lib/Bot/Games/Game/Ghost.pm
@@ -2,18 +2,12 @@ package Bot::Games::Game::Ghost;
use Bot::Games::OO::Game;
use Games::Word::Wordlist;
extends 'Bot::Games::Game';
+with 'Bot::Games::Game::Role::CurrentPlayer';
has '+help' => (
default => "ghost help",
);
-has current_player => (
- is => 'rw',
- isa => 'Str',
- predicate => 'has_current_player',
- command => 1,
-);
-
has state => (
is => 'rw',
isa => 'Str',
@@ -111,18 +105,6 @@ command challenge => sub {
}
};
-command previous_player => sub {
- my $self = shift;
- return unless $self->has_current_player;
- return $self->players->[$self->current_player_index - 1];
-};
-
-command next_player => sub {
- my $self = shift;
- return unless $self->has_current_player;
- return $self->players->[($self->current_player_index + 1) % $self->num_players];
-};
-
command valid_move => sub {
my $self = shift;
my ($move) = @_;
@@ -136,14 +118,6 @@ command valid_word_from_state => sub {
return uc($word_prefix) eq $self->state;
}, formatter => 'Bool';
-sub current_player_index {
- my $self = shift;
- for (0..($self->num_players - 1)) {
- return $_ if $self->current_player eq $self->players->[$_];
- }
- return 0;
-}
-
sub maybe_add_player {
my $self = shift;
my ($player) = @_;
diff --git a/lib/Bot/Games/Game/Role/CurrentPlayer.pm b/lib/Bot/Games/Game/Role/CurrentPlayer.pm
new file mode 100644
index 0000000..9fd8023
--- /dev/null
+++ b/lib/Bot/Games/Game/Role/CurrentPlayer.pm
@@ -0,0 +1,35 @@
+package Bot::Games::Game::Role::CurrentPlayer;
+use Bot::Games::OO::Game::Role;
+
+requires 'players', 'num_players';
+
+has current_player => (
+ is => 'rw',
+ isa => 'Str',
+ predicate => 'has_current_player',
+ command => 1,
+);
+
+command previous_player => sub {
+ my $self = shift;
+ return unless $self->has_current_player;
+ return $self->players->[$self->current_player_index - 1];
+};
+
+command next_player => sub {
+ my $self = shift;
+ return unless $self->has_current_player;
+ return $self->players->[($self->current_player_index + 1) % $self->num_players];
+};
+
+sub current_player_index {
+ my $self = shift;
+ for (0..($self->num_players - 1)) {
+ return $_ if $self->current_player eq $self->players->[$_];
+ }
+ return 0;
+}
+
+no Bot::Games::OO::Game::Role;
+
+1;