blib/lib/Game/Brandubh.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« bin/brandubh ↑ Summary blib/lib/Game/Brandubh/Error.pm »
1 package Game::Brandubh;
2  
3 11 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Game::Brandubh::Engine qw(
11 ATTACKERS DEFENDERS EMPTY ATTACKER DEFENDER KING
12 KING_HOME KING_TAKEN
13 WHY_OK WHY_NO_PIECE WHY_NOT_YOURS WHY_NO_MOVE WHY_NOT_A_LINE
14 WHY_THRONE WHY_CORNER WHY_BLOCKED
15 );
16 use Game::Brandubh::Rules qw(PLAY_OK BY_CORNER outcome_name);
17 use Game::Brandubh::Notation qw(
18 SETUP square_name square_parse move_parse move_split move_display
19 position_ok game_string game_parse
20 );
21 use Game::Brandubh::Variant;
22 use Game::Brandubh::Error;
23 use Game::Brandubh::Result;
24 use Digest::SHA ();
25  
26 our $VERSION = '0.03';
27  
28 my $E = 'Game::Brandubh::Engine';
29 my $X = 'Game::Brandubh::Error';
30  
31 my (@SIDE, @PIECE, %REFUSAL);
32 BEGIN {
33 @SIDE = ('attackers', 'defenders');
34 @PIECE = ('', 'attacker', 'defender', 'king');
35 %REFUSAL = (
36 WHY_NO_PIECE() => 'no_piece',
37 WHY_NOT_YOURS() => 'not_your_piece',
38 WHY_NO_MOVE() => 'no_move',
39 WHY_NOT_A_LINE() => 'not_a_line',
40 WHY_THRONE() => 'throne_closed',
41 WHY_CORNER() => 'corner_closed',
42 WHY_BLOCKED() => 'path_blocked',
43 );
44 }
45  
46 has attackers => (is => 'ro', default => 'p1');
47  
48 has _seed => (is => 'ro', init_arg => 'seed', private => 1);
49  
50 has _variant_given => (is => 'ro', init_arg => 'variant', private => 1);
51  
52 has _start_given => (is => 'ro', init_arg => 'position', private => 1);
53  
54 has _variant => (is => 'rw', private => 1);
55  
56 has _start => (is => 'rw', private => 1);
57  
58 has _game => (is => 'rw', private => 1);
59  
60 has _log => (is => 'ro', default => [], private => 1);
61  
62 has _shown => (is => 'ro', default => [], private => 1);
63  
64 has _ended => (is => 'rw', private => 1);
65  
66 has _offer => (is => 'rw', private => 1);
67  
68 sub BUILD {
69 my ($self) = @_;
70  
71 my $attackers = $self->attackers;
72 Carp::croak("Game::Brandubh: attackers must be 'p1' or 'p2', not '" . ($attackers // 'undef') . "'")
73 unless defined $attackers && ($attackers eq 'p1' || $attackers eq 'p2');
74  
75 my $seed = $self->_seed;
76 Carp::croak('Game::Brandubh: a seed, when one is given, is exactly thirty-two bytes')
77 if defined $seed && (ref $seed || length($seed) != 32);
78  
79 my $given = $self->_variant_given;
80 my $variant;
81 if (!defined $given) {
82 $variant = Game::Brandubh::Variant->named;
83 }
84 elsif (ref $given eq 'Game::Brandubh::Variant') {
85 $variant = $given;
86 }
87 elsif (ref $given eq 'HASH') {
88 $variant = Game::Brandubh::Variant->custom(%$given);
89 }
90 elsif (!ref $given) {
91 $variant = Game::Brandubh::Variant->from_string($given);
92 Carp::croak("Game::Brandubh: '$given' is not a rule set") unless $variant;
93 }
94 else {
95 Carp::croak('Game::Brandubh: a variant is a name, a hash of fields or a Game::Brandubh::Variant');
96 }
97 $self->_variant($variant);
98  
99 my $start = $self->_start_given;
100 $start = SETUP unless defined $start;
101 Carp::croak("Game::Brandubh: '$start' is not a position") unless position_ok($start);
102 $self->_start($start);
103  
104 $self->_game(Game::Brandubh::Rules->new(position => $start, variant => $variant->as_hash));
105 return;
106 }
107  
108 sub variant { $_[0]->_variant }
109 sub start { $_[0]->_start }
110  
111 sub status {
112 my ($self) = @_;
113 return 'finished' if $self->_ended || $self->_game->is_over;
114 return 'active';
115 }
116  
117 sub side_of {
118 my ($self, $seat) = @_;
119 return undef unless defined $seat && ($seat eq 'p1' || $seat eq 'p2');
120 return $seat eq $self->attackers ? 'attackers' : 'defenders';
121 }
122  
123 sub seat_of {
124 my ($self, $side) = @_;
125 return undef unless defined $side;
126 return $self->attackers if $side eq 'attackers';
127 return ($self->attackers eq 'p1' ? 'p2' : 'p1') if $side eq 'defenders';
128 return undef;
129 }
130  
131 sub side_to_move {
132 my ($self) = @_;
133 return undef unless $self->status eq 'active';
134 return $SIDE[ $self->_game->side ];
135 }
136  
137 sub turn {
138 my ($self) = @_;
139 my $side = $self->side_to_move;
140 return defined $side ? $self->seat_of($side) : undef;
141 }
142  
143 sub _names_of {
144 my ($mv) = @_;
145 my $from = $E->move_from($mv);
146 my $to = $E->move_to($mv);
147 return (square_name($E->file_of($from), $E->rank_of($from)),
148 square_name($E->file_of($to), $E->rank_of($to)));
149 }
150  
151 sub _square_of {
152 my ($name) = @_;
153 my ($file, $rank) = square_parse($name) or return undef;
154 return $E->square_of($file, $rank);
155 }
156  
157 sub _about {
158 my ($self, $mv) = @_;
159 my $game = $self->_game;
160 my ($flags, @squares) = $game->preview($mv);
161 my $piece = $game->at($E->move_from($mv));
162 my @captures = sort map { square_name($E->file_of($_), $E->rank_of($_)) } @squares;
163  
164 my $wins = ($flags & (KING_HOME | KING_TAKEN)) ? 1 : 0;
165 if (!$wins && $piece != ATTACKER && @squares) {
166 my $attackers_taken = grep { $game->at($_) == ATTACKER } @squares;
167 $wins = 1 if $attackers_taken == $game->count(ATTACKER);
168 }
169 return {
170 piece => $PIECE[$piece],
171 king => $piece == KING ? 1 : 0,
172 captures => \@captures,
173 king_home => ($flags & KING_HOME) ? 1 : 0,
174 king_taken => ($flags & KING_TAKEN) ? 1 : 0,
175 wins => $wins,
176 };
177 }
178  
179 sub legal {
180 my ($self) = @_;
181 return [] unless $self->status eq 'active';
182 my @legal;
183 for my $mv ($self->_game->moves) {
184 my ($from, $to) = _names_of($mv);
185 my $about = $self->_about($mv);
186 push @legal, {
187 move => $from . $to,
188 from => $from,
189 to => $to,
190 piece => $about->{piece},
191 captures => $about->{captures},
192 wins => $about->{wins},
193 };
194 }
195 return \@legal;
196 }
197  
198 sub _seat_refusal {
199 my ($self, $seat) = @_;
200 return $X->throw('not_a_seat') unless defined $seat && !ref $seat && ($seat eq 'p1' || $seat eq 'p2');
201 return 0;
202 }
203  
204 sub play {
205 my ($self, $move, $seat) = @_;
206 my %with = (defined $move && !ref $move ? (move => $move) : ());
207  
208 return $X->throw('game_over', %with) unless $self->status eq 'active';
209 if (defined $seat) {
210 my $refused = $self->_seat_refusal($seat);
211 return $refused if $refused;
212 return $X->throw('not_your_turn', %with) unless $seat eq $self->turn;
213 }
214  
215 my $wire = move_parse($move);
216 return $X->throw('bad_move', %with) unless defined $wire;
217 my ($from_name, $to_name) = move_split($wire);
218 my ($from, $to) = (_square_of($from_name), _square_of($to_name));
219  
220 my $game = $self->_game;
221 my $why = $game->why_not($from, $to);
222 return $X->throw($REFUSAL{$why} // 'bad_move', %with) unless $why == WHY_OK;
223  
224 my $mv = $E->move($from, $to);
225 my $about = $self->_about($mv);
226 return $X->throw('bad_move', %with) unless $game->play($mv) == PLAY_OK;
227  
228 push @{ $self->_log }, $wire;
229 push @{ $self->_shown }, move_display($wire, $about);
230 $self->_offer(undef);
231 return 0;
232 }
233  
234 sub play_or_die {
235 my ($self, $move, $seat) = @_;
236 my $refused = $self->play($move, $seat);
237 Carp::croak('Game::Brandubh: ' . (defined $move ? "'$move'" : 'that') . ' was refused: ' . $refused->message)
238 if $refused;
239 return $self;
240 }
241  
242 sub undo {
243 my ($self) = @_;
244 if ($self->_ended) {
245 $self->_ended(undef);
246 return 1;
247 }
248 return 0 unless $self->_game->undo;
249 pop @{ $self->_log };
250 pop @{ $self->_shown };
251 $self->_offer(undef);
252 return 1;
253 }
254  
255 sub resign {
256 my ($self, $seat) = @_;
257 return $X->throw('game_over') unless $self->status eq 'active';
258 my $refused = $self->_seat_refusal($seat);
259 return $refused if $refused;
260  
261 my $winner = $self->side_of($seat) eq 'attackers' ? 'defenders' : 'attackers';
262 $self->_ended(Game::Brandubh::Result->new(
263 how => 'resign',
264 winner => $winner,
265 seat => $self->seat_of($winner),
266 ply => $self->_game->ply,
267 position => $self->_game->position,
268 ));
269 $self->_offer(undef);
270 return 0;
271 }
272  
273 sub offer_draw {
274 my ($self, $seat) = @_;
275 return $X->throw('game_over') unless $self->status eq 'active';
276 my $refused = $self->_seat_refusal($seat);
277 return $refused if $refused;
278 return $X->throw('offer_standing') if defined $self->_offer;
279 $self->_offer($seat);
280 return 0;
281 }
282  
283 sub _answer_refusal {
284 my ($self, $seat) = @_;
285 return $X->throw('game_over') unless $self->status eq 'active';
286 my $refused = $self->_seat_refusal($seat);
287 return $refused if $refused;
288 return $X->throw('no_offer') unless defined $self->_offer;
289 return $X->throw('own_offer') if $self->_offer eq $seat;
290 return 0;
291 }
292  
293 sub accept_draw {
294 my ($self, $seat) = @_;
295 my $refused = $self->_answer_refusal($seat);
296 return $refused if $refused;
297 $self->_ended(Game::Brandubh::Result->new(
298 how => 'agreed',
299 ply => $self->_game->ply,
300 position => $self->_game->position,
301 ));
302 $self->_offer(undef);
303 return 0;
304 }
305  
306 sub decline_draw {
307 my ($self, $seat) = @_;
308 my $refused = $self->_answer_refusal($seat);
309 return $refused if $refused;
310 $self->_offer(undef);
311 return 0;
312 }
313  
314 sub draw_offered_by { $_[0]->_offer }
315  
316 sub result {
317 my ($self) = @_;
318 return $self->_ended if $self->_ended;
319 my $game = $self->_game;
320 return undef unless $game->is_over;
321  
322 my $outcome = $game->outcome;
323 my $how = outcome_name($outcome);
324 $how = 'edge' if $outcome == BY_CORNER && $self->_variant->escape eq 'edge';
325  
326 my $side = $game->winner;
327 my $winner = defined $side ? $SIDE[$side] : undef;
328 return Game::Brandubh::Result->new(
329 how => $how,
330 (defined $winner ? (winner => $winner, seat => $self->seat_of($winner)) : ()),
331 ply => $game->ply,
332 position => $game->position,
333 );
334 }
335  
336 sub winner {
337 my ($self) = @_;
338 my $result = $self->result;
339 return $result ? $result->seat : undef;
340 }
341  
342 sub position { $_[0]->_game->position }
343 sub signature { $_[0]->_game->key_hex }
344 sub repeats { $_[0]->_game->repeats }
345 sub ply { $_[0]->_game->ply }
346 sub log { [ @{ $_[0]->_log } ] }
347 sub shown { [ @{ $_[0]->_shown } ] }
348  
349 sub seed {
350 my ($self) = @_;
351 return undef unless $self->status eq 'finished';
352 return $self->_seed;
353 }
354  
355 sub at {
356 my ($self, $name) = @_;
357 my $square = _square_of($name);
358 return undef unless defined $square;
359 return $PIECE[ $self->_game->at($square) ];
360 }
361  
362 sub pieces {
363 my ($self) = @_;
364 my $game = $self->_game;
365 my %pieces;
366 for my $square ($E->all_squares) {
367 my $piece = $game->at($square);
368 next if $piece == EMPTY;
369 $pieces{ square_name($E->file_of($square), $E->rank_of($square)) } = $PIECE[$piece];
370 }
371 return \%pieces;
372 }
373  
374 sub search {
375 my ($self, %with) = @_;
376 return undef unless $self->status eq 'active';
377  
378 my $salt = defined $with{salt} ? $with{salt} : '';
379 Carp::croak('Game::Brandubh: a search salt is a string') if ref $salt;
380 my $seed = unpack 'N', Digest::SHA::sha256(join '|', $salt, $self->turn, $self->_game->ply, $self->_game->key_hex);
381  
382 my $found = $self->_game->search(
383 budget => $with{budget},
384 seed => $seed || 1,
385 (defined $with{depth} ? (depth => $with{depth}) : ()),
386 (defined $with{weights} ? (weights => $with{weights}) : ()),
387 );
388 return undef unless $found;
389 my ($from, $to) = _names_of($found->{move});
390 return {
391 move => $from . $to,
392 score => $found->{score},
393 depth => $found->{depth},
394 nodes => $found->{nodes},
395 stopped => $found->{stopped},
396 };
397 }
398  
399 sub bot { require Game::Brandubh::Bot; return 'Game::Brandubh::Bot' }
400  
401 sub _apply_result {
402 my ($self, $result) = @_;
403 return 0 unless defined $result;
404 return $X->throw('bad_move') unless ref $result eq 'HASH';
405 my $how = $result->{how} // '';
406 if ($how eq 'resign') {
407 return $self->resign($self->seat_of($result->{by}));
408 }
409 if ($how eq 'agreed') {
410 return $X->throw('game_over') unless $self->status eq 'active';
411 my $first = $self->turn;
412 my $refused = $self->offer_draw($first);
413 return $refused if $refused;
414 return $self->accept_draw($first eq 'p1' ? 'p2' : 'p1');
415 }
416 return $X->throw('bad_move');
417 }
418  
419 sub _walk {
420 my ($self, $moves, $result, $offer) = @_;
421 for my $i (0 .. $#$moves) {
422 my $refused = $self->play($moves->[$i]);
423 return wantarray ? ($self, $refused, $i) : undef if $refused;
424 }
425 my $refused = defined $offer ? $self->offer_draw($offer) : 0;
426 $refused ||= $self->_apply_result($result);
427 return wantarray ? ($self, $refused, scalar @$moves) : undef if $refused;
428 return wantarray ? ($self, 0, scalar @$moves) : $self;
429 }
430  
431 sub replay {
432 my ($proto, @rest) = @_;
433  
434 if (ref $proto) {
435 my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ];
436 my $fresh = (ref $proto)->new(
437 attackers => $proto->attackers,
438 variant => $proto->_variant,
439 position => $proto->_start,
440 (defined $proto->_seed ? (seed => $proto->_seed) : ()),
441 );
442 return $fresh->_walk($moves);
443 }
444  
445 Carp::croak('Game::Brandubh: replay takes name => value pairs') if @rest % 2;
446 my %given = @rest;
447 my $moves = delete $given{moves};
448 my $result = delete $given{result};
449 my $offer = delete $given{offer};
450 Carp::croak('Game::Brandubh: moves is an array reference') if defined $moves && ref $moves ne 'ARRAY';
451 my $fresh = $proto->new(%given);
452 return $fresh->_walk($moves || [], $result, $offer);
453 }
454  
455 sub as_text {
456 my ($self) = @_;
457 my $ended = $self->_ended;
458 my $result;
459 if ($ended) {
460 $result = $ended->how eq 'resign' ? { how => 'resign', by => $ended->loser }
461 : { how => 'agreed' };
462 }
463 return game_string({
464 variant => $self->_variant->as_string,
465 attackers => $self->attackers,
466 start => $self->_start,
467 moves => $self->_log,
468 result => $result,
469 offer => $self->_offer,
470 });
471 }
472  
473 sub from_text {
474 my ($class, $text, @rest) = @_;
475 Carp::croak('Game::Brandubh: from_text takes the text and then name => value pairs') if @rest % 2;
476 my $record = game_parse($text);
477 return wantarray ? (undef, $X->throw('bad_move'), 0) : undef unless $record;
478 my $variant = Game::Brandubh::Variant->from_string($record->{variant});
479 return wantarray ? (undef, $X->throw('bad_move'), 0) : undef unless $variant;
480 return $class->replay(
481 (defined $record->{attackers} ? (attackers => $record->{attackers}) : ()),
482 @rest,
483 variant => $variant,
484 position => $record->{start},
485 moves => $record->{moves},
486 result => $record->{result},
487 offer => $record->{offer},
488 );
489 }
490  
491 1;
492  
493 __END__
« bin/brandubh ↑ Summary blib/lib/Game/Brandubh/Error.pm »