| 1 | package Game::WordChainGame; | ||
| 2 | 3 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use WordNet::QueryData; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | |||
| 5 | our $VERSION = '1.0'; | ||
| 6 | |||
| 7 | has 'players' => ( | ||
| 8 | is => 'ro', | ||
| 9 | isa => 'ArrayRef[Str]', | ||
| 10 | required => 2 | ||
| 11 | ); | ||
| 12 | |||
| 13 | has 'wn' => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'WordNet::QueryData', | ||
| 16 | default => sub { WordNet::QueryData->new } | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'used_words' => ( | ||
| 20 | is => 'rw', | ||
| 21 | isa => 'HashRef', | ||
| 22 | default => sub { {} } | ||
| 23 | ); | ||
| 24 | |||
| 25 | has 'last_active_player' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'Str', | ||
| 28 | default => sub { $_[0]->players ? $_[0]->players->[0] : '' } # Default to an empty string if players are not defined | ||
| 29 | ); | ||
| 30 | |||
| 31 | has 'winner' => ( | ||
| 32 | is => 'rw', | ||
| 33 | isa => 'Str', | ||
| 34 | default => '' | ||
| 35 | ); | ||
| 36 | |||
| 37 | sub validate_word { | ||
| 38 | my ($self, $word) = @_; | ||
| 39 | return $self->wn->validForms($word); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub play_turn { | ||
| 43 | my ($self, $current_player) = @_; | ||
| 44 | |||
| 45 | my $prev_player = $self->last_active_player; | ||
| 46 | my $prev_word = $self->used_words->{$prev_player}; | ||
| 47 | |||
| 48 | if (defined $prev_word) { | ||
| 49 | my $last_letter = substr($prev_word, -1); | ||
| 50 | print "$current_player, enter a word starting with '$last_letter': "; | ||
| 51 | } else { | ||
| 52 | print "$current_player, enter a word: "; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $current_word = lc(<STDIN>); | ||
| 56 | chomp $current_word; | ||
| 57 | |||
| 58 | if ($current_word eq "quit") { | ||
| 59 | print "Thanks for playing!\n"; | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | |||
| 63 | if (!$self->validate_word($current_word)) { | ||
| 64 | print "The word '$current_word' is not valid. $current_player loses.\n"; | ||
| 65 | return 0; # Player eliminated | ||
| 66 | } | ||
| 67 | |||
| 68 | if ($self->used_words->{$current_word}) { | ||
| 69 | print "Word already used. $current_player loses.\n"; | ||
| 70 | return 0; # Player eliminated | ||
| 71 | } | ||
| 72 | |||
| 73 | if (defined $prev_word && substr($current_word, 0, 1) ne substr($prev_word, -1)) { | ||
| 74 | print "Invalid word. $current_player loses.\n"; | ||
| 75 | return 0; # Player eliminated | ||
| 76 | } | ||
| 77 | |||
| 78 | $self->used_words->{$current_word} = 1; # Mark the word as used | ||
| 79 | $self->used_words->{$current_player} = $current_word; | ||
| 80 | $self->last_active_player($current_player) if $current_player ne $prev_player; | ||
| 81 | |||
| 82 | return 1; # Player continues | ||
| 83 | } | ||
| 84 | |||
| 85 | sub play { | ||
| 86 | my ($self) = @_; | ||
| 87 | my $num_players = @{$self->players}; | ||
| 88 | print "Welcome to Word Chain Game with $num_players players!\n"; | ||
| 89 | |||
| 90 | $self->_recursive_play(@{$self->players}); | ||
| 91 | return $self->winner if ($self->winner); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _recursive_play { | ||
| 95 | my ($self, $current_player, @remaining_players) = @_; | ||
| 96 | |||
| 97 | return if !@remaining_players; | ||
| 98 | |||
| 99 | my $player_continues = $self->play_turn($current_player); | ||
| 100 | if ($player_continues) { | ||
| 101 | my $next_player = shift @remaining_players; | ||
| 102 | push @remaining_players, $current_player; | ||
| 103 | $self->_recursive_play($next_player, @remaining_players); | ||
| 104 | } else { | ||
| 105 | my $next_player = shift @remaining_players; | ||
| 106 | $self->_recursive_play($next_player, @remaining_players); | ||
| 107 | } | ||
| 108 | |||
| 109 | # Check if there's only one player left and set them as the winner | ||
| 110 | if (@remaining_players == 0 && !$self->winner) { | ||
| 111 | $self->winner($current_player); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ | ||