/
Hangman.pm
443 lines (320 loc) · 12.6 KB
/
Hangman.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package Bot::BasicBot::Pluggable::Module::Hangman;
use strict;
no warnings;
use base 'Bot::BasicBot::Pluggable::Module';
use Carp;
=head1 NAME
Bot::BasicBot::Pluggable::Module::Hangman - IRC Hangman game
=head1 VERSION
Version 0.01
=cut
our $VERSION = '0.01';
my $dictionary_file = '/usr/share/dict/words';
my $namespace = 'Reaper';
=head1 SYNOPSIS
A silly pointless IRC bot that provides a text version of Hangman for anyone
that's bored enough to play it :-)
use Bot::BasicBot::Pluggable::Module::Hangman;
my $hangman = Bot::BasicBot::Pluggable::Module::Hangman->new();
# IRC
!hangman # Start a new game, uses a word from standard dictionary.
!hangman-solo # Like above, but exclusively for you.
# Start a new PvP game using a specific word, if the optional nick is
# supplied then you challenge that person to a 1v1 game.
/msg <bot-name> hangman <word> [<nick_of_player>]
=head1 DESCRIPTION
Bored? Play Hangman! Challenge the bot to guess it's word or play against
others using you're own words.
=head2 Modes of Play
=over 4
=item General Play
The general game, invoked by saying !hangman in a channel where the bot resides,
is commenced with an underscored representation of the word to be guessed. The
word is selected from the dictionary file by default but a custom file can be
specified.
Players make their guesses by addressing the bot with a single letter. The bot
will then reveal if that letter was correct or not. If it was correct, all
occurrences of that letter will appear in place in the word. On an incorrect
guess however, a life is removed. A guess of the complete word can be made at
any time by addressing the bot with the complete word.
The object of the game is simple, the players must get the word correct before
they run out of lives. The players can win by guessing the correct letters to
complete the word or by directly guessing the word.
All players can participate in this mode of play.
=item Solo Play
The solo game follows the same format as the general game, with the exception
that the bot will only accept guesses from the player that started the game.
The solo game is invoked by saying !hangman-solo.
=item Challenge Mode
Challenge mode presents the opportunity for players to go head to head. A
player can specify their own word(s) to challenge others. This can be in both
the general play format or against a specific player.
To begin a challenge game, a player sends a private message to the bot
containing the chosen word(s), and optionally the nick of a player to challenge.
=head1 WISHLIST
Eventually I want to add the following:
=over 4
=item Rules access method
A !command to effect the game parameters, such as word length, number of lives
etc.
=back
=head1 METHODS
=head2 send
Overrides send from Bot::BasicBot::Pluggable
=cut
sub said {
my ($self, $message, $priority) = @_;
return unless $priority == 2;
my $player = $message->{who};
my $address = $message->{address};
my $body = $message->{body};
my $games = $self->bot->store->get($namespace, 'games') // {};
my $game_name
= (defined $games->{$player}) ? $message->{who} : $message->{channel};
my $game_data = $games->{$game_name};
# If we've been privately messaged, or it's a public message then we need
# to look for a command.
if ( !$address
|| $address eq 'msg')
{
# First off, look for a command to start the game.
# FIXME: the private message for challenge games doesn't work. Replies
# to the person, not the channel....
# !hangman command...
my $pling_optional = ($address) ? '?' : '';
if ($body =~ /^
!$pling_optional
(?<command>hangman(?:-solo)?)
(?:
\s+
(?:challenge \s+ )?
(?<channel> \#\S+ ):(?<nick> \S+ )
\s+
(?:with \s+ )?
(?<words> .+)
)?
/xi)
{
my %named_matches = %+;
return $self->_begin_game(
player => $player,
message => $message,
command => \%named_matches,
);
}
# !state command, outputs the current wordstate.
if ($body =~ /^!state/) {
return $self->_game_state($game_name);
}
# !guesses command, outputs the current failed attempts.
if ($body =~ /^!guesses/) {
my $letters = join ", ", @{ $game_data->{guesses} };
my $words = join ", ", @{ $game_data->{guessedwords} };
return "Guessed letters: $letters\nGuessed Words: $words";
}
# !games command, lists the games in progress.
if ($body =~ /^!games/) {
my $games = join ", ", keys %$games;
return "Current games: $games";
}
if ($body =~ /^!endgame \s (?<game> \#?\S+ )/xi) {
my $deleted = delete $games->{$+{game}}
if exists( $games->{$+{game}} );
$self->bot->store->set($namespace, 'games', $games);
return ($deleted) ? "Game ended: $+{game}" : "No game: $+{game}";
}
# Add the ability to take a single unaddressed letter to be a guess.
if (length $body == 1) {
return $self->_process_guess($message);
}
}
if ($address) {
# If we've been addressed then someone is making a guess.
return $self->_process_guess($message);
}
# Failing that, the message was nothing to do with us.
return undef;
}
# create a game hashref in the %games hash, keyed on the challenged player;
# storing the word, the current word state, guessed letters and the current
# number of lives. This hash is used by _process_guess() to keep track and
# advance the game.
sub _begin_game {
my ($self, %params) = @_;
my $bot = $self->bot;
my $player = $params{player};
my $command = $params{command};
my $message = $params{message};
# Construct the game.
my $game_data = {};
my $game_name =
( $command->{command} eq 'hangman-solo' ) ? $player
: ( $command->{nick} ) ? $command->{nick}
: $message->{channel};
my $command_channel = $command->{channel};
if (!$game_name) {
return "Internal Fail... no game name...";
}
my $games = $bot->store->get( $namespace, 'games' ) // {};
if ( exists $games->{$game_name} ) {
return "A game is already in progress!"
. $self->_game_state($game_name);
}
$game_data = {
guesses => [],
guessedwords => [],
lives => 10,
word => ($command->{words})
? lc $command->{words}
: lc _get_dict_word(),
};
($game_data->{wordstate} = $game_data->{word}) =~ s/\w/_ /g;
# Now add the game data to the games store.
$games->{$game_name} = $game_data;
$bot->store->set($namespace, 'games', $games);
# If this was a challenge game, it needs to return to the channel, else it
# can respond to the sender.
if ($command->{channel}) {
$self->bot->say(
who => $command->{nick},
channel => $command->{channel} || $message->{channel},
body => "You have been challenged by $player\n"
.$self->_game_state($game_name),
address => $command->{nick},
);
} else {
return $self->_game_state($game_name);
}
}
# Process a guess and progress the game.
sub _process_guess {
my ($self, $message) = @_;
my $player = $message->{who};
my $addressby = ($message->{address}) ? '' : "$player: ";
my $games = $self->bot->store->get($namespace, 'games');
my $game_name = ( exists( $games->{$player} ) )
? $player
: $message->{channel};
my $data = $games->{$game_name};
my $guess = lc $message->{body};
# Make sure there is actually a game!
if (!$data) {
return "${addressby}There is no game currently in progress!";
}
# If they guess a letter, look for it.
if (length $guess == 1) {
if ($guess ~~ @{ $data->{guesses} }) {
return "${addressby}That letter has already been guessed";
}
# Add the guess to the guesses
push @{ $data->{guesses} }, $guess;
# It's good! Update the state...
if ($data->{word} =~ m/$guess/) {
my @word = split //, $data->{word};
my @state = split / /, $data->{wordstate};
for (my $i = 0; $i < length $data->{word}; $i++) {
if ( $state[$i] eq "_"
&& $word[$i] eq $guess)
{
$state[$i] = $word[$i];
}
}
$data->{wordstate} = join " ", @state;
# Failing that, it's not good, decrement the lives.
} else {
$data->{lives}--;
}
# Else, assume it's a word.
} else {
my $word = $data->{word};
chomp $word;
if ($word ~~ @{ $data->{guessedwords} }) {
return "That word has been guessed!";
}
push @{ $data->{guessedwords} }, $guess;
if ($guess eq $word) {
($data->{wordstate} = $word) =~ s/(\w)/\1 /;
} else {
$data->{lives}--;
}
}
$self->bot->store->set($namespace, 'games', $games );
return $self->_game_state($game_name);
}
# Gets a word from the dict file, if the file is missing, sets the word
# to NODICT. This should be irritating enough to make the admin fix it.
sub _get_dict_word {
my ($self) = @_;
if ( ! -e $dictionary_file) {
return "NODICT";
}
open my $dict_fh, '<', $dictionary_file;
my @words = <$dict_fh>;
close $dict_fh;
my $word = $words[ sprintf("%2d", rand($#words)) ];
$word =~ s/'s$//;
chomp($word);
return $word;
}
# Prints the game state line "public: _ _ _ _ 10/10"
sub _game_state {
my ($self, $game_name) = @_;
# Get the game data.
my $games = $self->bot->store->get($namespace, 'games') // {};
my $data = $games->{$game_name};
if (!$data) {
return "There is currently no game in progess :-(";
}
# Check for teh win
if ($data->{wordstate} !~ m/_/) {
delete $games->{$game_name};
$self->bot->store->set($namespace, 'games', $games);
return "\\o/ Congratulations, You win! Word: $data->{word}";
}
# Check for teh lose.
if ($data->{lives} == 0) {
delete $games->{$game_name};
$self->bot->store->set($namespace, 'games', $games);
return ":-( You lose! Word: $data->{word}";
}
# Build a state line and return it to be said to the channel.
my $wordstate = $data->{wordstate};
chomp $wordstate;
return "$game_name: $wordstate $data->{lives}/10";
}
=head1 AUTHOR
James Ronan, C<< <james at ronanweb.co.uk> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-bot-basicbot-pluggable-module-hangman at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bot-BasicBot-Pluggable-Module-Hangman>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Bot::BasicBot::Pluggable::Module::Hangman
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bot-BasicBot-Pluggable-Module-Hangman>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Bot-BasicBot-Pluggable-Module-Hangman>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Bot-BasicBot-Pluggable-Module-Hangman>
=item * Search CPAN
L<http://search.cpan.org/dist/Bot-BasicBot-Pluggable-Module-Hangman/>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2011 James Ronan.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991 or at your option
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License is available in the source tree;
if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=cut
1; # End of Bot::BasicBot::Pluggable::Module::Hangman