line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Games::Othello; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20580
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
222
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Games::Othello - Perl extension for modelling a game of Othello. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.01 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Games::Othello; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $game = Games::Othello->new(); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
while( !game->over ) { |
25
|
|
|
|
|
|
|
printf "It is presently %s's move", |
26
|
|
|
|
|
|
|
($game->whos_move eq 'b') ? 'black', 'white'; |
27
|
|
|
|
|
|
|
my @possible_moves = values $game->possible_moves(); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
if ( ! @possible_moves ) { |
30
|
|
|
|
|
|
|
print "You have no moves available, you must pass. |
31
|
|
|
|
|
|
|
$game->pass_to_opponent; |
32
|
|
|
|
|
|
|
} else { |
33
|
|
|
|
|
|
|
foreach ( my $move ) @possible_moves ) { |
34
|
|
|
|
|
|
|
printf |
35
|
|
|
|
|
|
|
"You will take %d of your opponents chips if you place your chip on %d,%d", |
36
|
|
|
|
|
|
|
scalar @{ $move->{chips} }, $move->{x}, $move->{y}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
my ($locx, $locy) = get_move(); |
39
|
|
|
|
|
|
|
my $flipped = $game->place_chip( $locx, $locy ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $layout = $game->chip_layout(); |
43
|
|
|
|
|
|
|
foreach my $row ( @$layout ) { |
44
|
|
|
|
|
|
|
foreach my $pos ( @$row ) { |
45
|
|
|
|
|
|
|
printf '%3s', |
46
|
|
|
|
|
|
|
($pos eq 'b') ? 'B' # Black occupied square. |
47
|
|
|
|
|
|
|
: ($pos eq 'w') ? 'W' # White occupied square. |
48
|
|
|
|
|
|
|
: ' ' # Un-occupied square. |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
print "\n\n"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
my ($black_score, $white_score) = $game->score; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 STATUS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
This module is B. Do not expect it to do anything at this point. |
58
|
|
|
|
|
|
|
Do not expect the API to remain as documented. If this module interests you |
59
|
|
|
|
|
|
|
and you have feedback on its design please DO forward that to me. Contact |
60
|
|
|
|
|
|
|
information is contained at the bottom of this documentation. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 DESCRIPTION |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
This module is used to model the common board game, Othello. Othello has been |
65
|
|
|
|
|
|
|
around for a very long time, and has been re-produced in several different |
66
|
|
|
|
|
|
|
formats. The goal of the game is to eventually fill the board with all of your |
67
|
|
|
|
|
|
|
chips. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The board itself is an 8x8 grid represented as a matrix of x,y coordinates. |
70
|
|
|
|
|
|
|
The location of which way each axis is numbered is irrelevant. But for the |
71
|
|
|
|
|
|
|
examples in this documentation, the the 0,0 cordinate is located on the |
72
|
|
|
|
|
|
|
top-left of the board, and 7,7 is located on the bottom-right of the board. |
73
|
|
|
|
|
|
|
Initially there are two white chips and two black chips each organized |
74
|
|
|
|
|
|
|
diagonally in the center of the board: |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
0 1 2 3 4 5 6 7 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
0 |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1 |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
2 |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
3 W B |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
4 B W |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
5 |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
6 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
7 |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Black always has the first move. Each player must place their color chip on |
95
|
|
|
|
|
|
|
the board in a square that is horizontally, vertically, or diagonally adjacent |
96
|
|
|
|
|
|
|
to at least one opponent's chip on the board. Beyond the opponents chip must |
97
|
|
|
|
|
|
|
be a straight row of contiguous chips terminated another one of the player's |
98
|
|
|
|
|
|
|
own chips. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
In each instance where a row of opponent chips is terminated by the player's |
101
|
|
|
|
|
|
|
recently placed chip and another one of that player's chips, the opponents |
102
|
|
|
|
|
|
|
chips are said to be "flipped". When this happens, the opponents chips change |
103
|
|
|
|
|
|
|
color and become owned by the player that just played a chip. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
For example: At the beginning of the game, Black always moves first and always |
106
|
|
|
|
|
|
|
has the following moves available: (3,2), (2,3), (5,4), (4,5). Suppose that |
107
|
|
|
|
|
|
|
black were to place its first chip on (2,3), resulting in the following layout: |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
2 3 4 |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
3 B W B |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
4 B W |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Since the new chip at (3,2) now causes the white chip at (3,3) to be in between |
116
|
|
|
|
|
|
|
two black chips, that chip would be flipped: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
2 3 4 |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
3 B B B |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
4 B W |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
White must now play a chip at (2,2) for a diagonal, (4,2) for a vertical, or |
125
|
|
|
|
|
|
|
(2,4) for a horizontal row. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
If a player is un-able to place a chip on the board in a way that causes at |
128
|
|
|
|
|
|
|
least one flip, they must pass their turn. The game is over under the following |
129
|
|
|
|
|
|
|
conditions: |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=over |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=item 1. All squares are occupied. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item 2. Neither player has any moves available. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item 3. One player has had all chips eliminated. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=back |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
At the end of the game, the winner is the player with the most chips on the |
142
|
|
|
|
|
|
|
board. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 METHODS |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 new |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This object constructor returns a new Games::Othello object. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub new { |
153
|
0
|
|
|
0
|
1
|
|
die 'un-implemented.'; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 game_over |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
This method returns a boolean to indicate if the game is over. See the above |
159
|
|
|
|
|
|
|
game description for more information. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub game_over { |
164
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 whos_move |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This method will return the character 'b', or 'w' to indicate if it is |
170
|
|
|
|
|
|
|
presently black or white's turn. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub whos_move { |
175
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 possible_moves |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
This method returns a hash of hashrefs. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
The outer hash keys are strings representing the comma-separated x and y |
183
|
|
|
|
|
|
|
coordinates of a particular chip placement that is available in the present |
184
|
|
|
|
|
|
|
turn (i.e. "3,4"). The values are hashrefs containing information about that |
185
|
|
|
|
|
|
|
chip placement for the present turn. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
An empty outer hash indicates there are no chip placements possible. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Each inner hashref contains the following key/values: |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=over |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item * x |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
This is the x-axis value of the placement. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * y |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
This is the y-axis value of the placement. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item * chips |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This is a list of opponent's chips that will be flipped over if the move is |
204
|
|
|
|
|
|
|
executed. Each chip is represented as an array-ref containing the [x,y] pair |
205
|
|
|
|
|
|
|
of coordinates for the chip to be flipped. Because the rules of the game |
206
|
|
|
|
|
|
|
state that you must always flip at least one opponent's chip or pass your turn, |
207
|
|
|
|
|
|
|
it is assumed that this list will always contain at least one element. |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=cut |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub possible_moves { |
212
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 pass_to_opponent |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
This method passes game play to the opponent. This may only be when there are |
218
|
|
|
|
|
|
|
no possible moves available for the current player. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
If the turn is passed to the opponent while there are moves available, then a |
221
|
|
|
|
|
|
|
Games::Othello::InvalidMove exception will be thrown. (see EXCEPTIONS, below) |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub pass_to_opponent { |
226
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 place_chip |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
This method accepts two parameters: x and y coordinates where the player has |
232
|
|
|
|
|
|
|
elected to place their chip. |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
When called, this method will update the state of the board to reflect that the |
235
|
|
|
|
|
|
|
chipped has been place. Any opponent chips that need flipped will be flipped |
236
|
|
|
|
|
|
|
and the game will be updated so that it is the other player's turn. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
A list of chips that were flipped will be returned by this method in the form |
239
|
|
|
|
|
|
|
of [x,y] pairs in array-refs. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
If place_chip is called with an invalid set of coordinates, then a |
242
|
|
|
|
|
|
|
Games::Othello::InvalidMove exception will be thrown. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=cut |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub place_chip { |
247
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head2 chip_layout |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
This method returns information about the position of all chips on the board. |
253
|
|
|
|
|
|
|
It returns an array of arrays. Each outer array represents one row on the |
254
|
|
|
|
|
|
|
board (starting from 0), and each inner array represents the horizontal grid |
255
|
|
|
|
|
|
|
squares in that row (starting from 0). |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
Each square is represented with the single character 'b' for a black occupied |
258
|
|
|
|
|
|
|
square, 'w' for a white occupied square, or ' ' (single space) for an |
259
|
|
|
|
|
|
|
un-occupied square. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub chip_layout { |
264
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head2 score |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
This method returns the black score and white score in a list. The score for each player is simply a count of how many chips they have on the board. |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=cut |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sub score { |
274
|
0
|
|
|
0
|
1
|
|
die 'un-implemented'; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head1 EXECPTIONS |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Games::Othello uses the L model for errors. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head1 AUTHOR |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Daniel J. Wright, C<< >> |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head1 BUGS |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
288
|
|
|
|
|
|
|
C, or through the web interface at |
289
|
|
|
|
|
|
|
L. |
290
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
291
|
|
|
|
|
|
|
your bug as I make changes. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head1 SUPPORT |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
perldoc Games::Othello |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
You can also look for information at: |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=over 4 |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
L |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=item * CPAN Ratings |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
L |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
L |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=item * Search CPAN |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
L |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=back |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
Thanks to L for giving me some thoughts on how to organize |
324
|
|
|
|
|
|
|
this. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
Copyright 2006 Daniel J. Wright, all rights reserved. |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
331
|
|
|
|
|
|
|
under the same terms as Perl itself. |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
=cut |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
1; # End of Games::Othello |