Durak.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Durak/Error.pm ↑ Summary Durak/Terminal.pm »
1 package Game::Durak;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 use Game::Durak::Card ();
9 use Game::Durak::Deck qw(HAND_SIZE);
10 use Game::Durak::Bout ();
11 use Game::Durak::Error ();
12 use Game::Durak::Rules qw(cap_for legal_attacks legal_beats forced closes);
13 use Game::Durak::Result qw(out_seats result_for resign_for);
14  
15 our $VERSION = '0.04';
16  
17 has seed => (is => 'ro', isa => Str);
18 has number => (is => 'ro', isa => Int);
19 has trump => (is => 'ro', isa => Str);
20 has trump_card => (is => 'rw', isa => Int);
21  
22 has hands => (is => 'rw', isa => HashRef);
23 has talon => (is => 'rw', isa => ArrayRef);
24 has discard => (is => 'rw', isa => Int, default => 0);
25 has bout => (is => 'rw');
26 has six_owner => (is => 'rw');
27 has over => (is => 'rw', isa => Int, default => 0);
28 has result => (is => 'rw');
29 has history => (is => 'rw', isa => ArrayRef, default => sub { [] });
30  
31 sub build {
32 my ($class, %o) = @_;
33  
34 my $seed = $o{seed};
35 return Game::Durak::Error->new_code('no_seed')
36 unless defined $seed && length $seed == 32;
37  
38 my $number = defined $o{number} ? $o{number} : 1;
39 return Game::Durak::Error->new_code('bad_deal')
40 unless $number =~ /\A[1-9][0-9]*\z/;
41  
42 my $seats = defined $o{seats} ? $o{seats} : Game::Durak::Deck::SEATS;
43 return Game::Durak::Error->new_code('bad_seats')
44 unless $seats =~ /\A[0-9]+\z/ && $seats == Game::Durak::Deck::SEATS;
45  
46 my $deal = Game::Durak::Deck::deal_for($seed, $number, $seats);
47  
48 my $six = Game::Durak::Card::six_of($deal->{trump});
49 my ($owner) = grep {
50 scalar grep { $_ == $six } @{ $deal->{hands}{$_} }
51 } 1 .. $seats;
52  
53 my $self = $class->new(
54 seed => $seed,
55 number => $number,
56 trump => $deal->{trump},
57 trump_card => $deal->{trump_card},
58 hands => { map { $_ => [ @{ $deal->{hands}{$_} } ] } 1 .. $seats },
59 talon => [ @{ $deal->{talon} } ],
60 discard => 0,
61 bout => undef,
62 six_owner => $owner,
63 over => 0,
64 result => undef,
65 history => [],
66 );
67  
68 $self->_open_bout($deal->{first});
69 return $self;
70 }
71  
72 sub _other { return 3 - $_[0] }
73  
74 sub hand_of { return $_[0]->hands->{ $_[1] } }
75  
76 sub count_of { return scalar @{ $_[0]->hands->{ $_[1] } } }
77  
78 sub talon_left { return scalar @{ $_[0]->talon } }
79  
80 sub seats { return ( 1, 2 ) }
81  
82 sub six_of_trumps { return Game::Durak::Card::six_of($_[0]->trump) }
83  
84 sub turn_up_left {
85 my ($self) = @_;
86 my $up = $self->trump_card;
87 return scalar(grep { $_ == $up } @{ $self->talon }) ? 1 : 0;
88 }
89  
90 sub can_swap {
91 my ($self, $seat) = @_;
92 return 0 if $self->over;
93 my $owner = $self->six_owner;
94 return 0 unless defined $owner && defined $seat && $owner == $seat;
95 my $six = $self->six_of_trumps;
96 return 0 unless grep { $_ == $six } @{ $self->hand_of($seat) };
97 return $self->turn_up_left;
98 }
99  
100 sub phase {
101 my ($self) = @_;
102 return 'over' if $self->over;
103 my $bout = $self->bout;
104 return 'over' unless $bout;
105 return 'pile_on' if $bout->taken;
106 return 'defend' if defined $bout->unbeaten;
107 return 'attack';
108 }
109  
110 sub turn {
111 my ($self) = @_;
112 my $phase = $self->phase;
113 return undef if $phase eq 'over';
114 my $bout = $self->bout;
115 return $phase eq 'defend' ? $bout->defender : $bout->attacker;
116 }
117  
118 sub legal {
119 my ($self, $seat) = @_;
120 return [] if $self->over;
121 return [] unless defined $seat && $seat =~ /\A[12]\z/;
122 my $turn = $self->turn;
123 return [] unless defined $turn && $turn == $seat;
124  
125 my $bout = $self->bout;
126 my $hand = $self->hand_of($seat);
127 my @out;
128  
129 my $swap = $self->can_swap($seat);
130  
131 if ($self->phase eq 'defend') {
132 my $beats = legal_beats($hand, $bout, $self->trump);
133 push @out, map { { kind => 'beat', card => $_ } } @$beats;
134 push @out, { kind => 'take' } if @$beats || $swap;
135 push @out, { kind => 'swap' } if $swap;
136 return \@out;
137 }
138  
139 my $attacks = legal_attacks($hand, $bout);
140 push @out, map { { kind => 'attack', card => $_ } } @$attacks;
141 push @out, { kind => 'done' } if $bout->size && (@$attacks || $swap);
142 push @out, { kind => 'swap' } if $swap;
143 return \@out;
144 }
145  
146 sub view {
147 my ($self, $seat) = @_;
148 die "Game::Durak: a view is for a seat\n"
149 unless defined $seat && $seat =~ /\A[12]\z/;
150  
151 my $bout = $self->bout;
152  
153 return {
154 seat => $seat,
155 ply => scalar @{ $self->history },
156 phase => $self->phase,
157 turn => $self->turn,
158 trump => $self->trump,
159 trump_card => $self->turn_up_left ? $self->trump_card : undef,
160 talon => $self->talon_left,
161 discard => $self->discard,
162 hand => [ @{ $self->hand_of($seat) } ],
163 counts => { map { $_ => $self->count_of($_) } $self->seats },
164 swap => $self->can_swap($seat),
165 legal => $self->legal($seat),
166 over => $self->over,
167 result => $self->result,
168 bout => $bout ? {
169 attacker => $bout->attacker,
170 defender => $bout->defender,
171 cap => $bout->cap,
172 size => $bout->size,
173 taken => $bout->taken,
174 unbeaten => $bout->unbeaten,
175 pairs => [ map {
176 { attack => $bout->attacks->[$_], beat => $bout->beats->[$_] }
177 } 0 .. $bout->size - 1 ],
178 } : undef,
179 };
180 }
181  
182 my %HANDLER = (
183 attack => '_attack',
184 beat => '_beat',
185 take => '_take',
186 done => '_done',
187 swap => '_swap',
188 );
189  
190 my %STAGE = (
191 attack => { attack => 1, pile_on => 1 },
192 beat => { defend => 1 },
193 take => { defend => 1 },
194 done => { attack => 1, pile_on => 1 },
195 swap => { attack => 1, pile_on => 1, defend => 1 },
196 resign => { attack => 1, pile_on => 1, defend => 1 },
197 );
198  
199 sub apply {
200 my ($self, $seat, $move) = @_;
201  
202 return Game::Durak::Error->new_code('game_over') if $self->over;
203 return Game::Durak::Error->new_code('not_your_turn')
204 unless defined $seat && $seat =~ /\A[12]\z/;
205  
206 my $kind = ref $move eq 'HASH' && defined $move->{kind} ? $move->{kind} : '';
207 return Game::Durak::Error->new_code('not_legal') unless $STAGE{$kind};
208  
209 my @out;
210 if ($kind eq 'resign') {
211 @out = $self->_resign($seat);
212 }
213 else {
214 my $turn = $self->turn;
215 return Game::Durak::Error->new_code('not_your_turn')
216 unless defined $turn && $turn == $seat;
217 return Game::Durak::Error->new_code('wrong_phase')
218 unless $STAGE{$kind}{ $self->phase };
219  
220 my $handler = $HANDLER{$kind};
221 @out = $self->$handler($seat, $move);
222 return $out[0] if @out == 1 && ref $out[0] eq 'Game::Durak::Error';
223  
224 push @out, $self->_advance;
225 }
226  
227 $self->history([ @{ $self->history }, @out ]);
228 return @out;
229 }
230  
231 sub is_move { return $STAGE{ defined $_[1] ? $_[1] : '' } ? 1 : 0 }
232  
233 sub moves_of {
234 my ($class, $events) = @_;
235 return [ grep { $class->is_move($_->{kind}) && defined $_->{seat} } @$events ];
236 }
237  
238 sub replay {
239 my ($class, %o) = @_;
240  
241 my $moves = delete $o{moves};
242 $moves = [] unless defined $moves;
243  
244 my $game = $class->build(%o);
245 return $game if ref $game eq 'Game::Durak::Error';
246  
247 my @events;
248 for my $move (@$moves) {
249 my @out = $game->apply($move->{seat}, $move);
250 return $out[0] if ref $out[0] eq 'Game::Durak::Error';
251 push @events, @out;
252 }
253  
254 return { game => $game, events => \@events };
255 }
256  
257 sub _held {
258 my ($self, $seat, $move) = @_;
259 my $card = ref $move eq 'HASH' ? $move->{card} : undef;
260 return Game::Durak::Error->new_code('card_not_held')
261 unless defined $card && $card =~ /\A[1-9][0-9]*\z/
262 && grep { $_ == $card } @{ $self->hand_of($seat) };
263 return $card;
264 }
265  
266 sub _attack {
267 my ($self, $seat, $move) = @_;
268 my $card = $self->_held($seat, $move);
269 return $card if ref $card;
270  
271 my $bout = $self->bout;
272 return Game::Durak::Error->new_code('bout_full') if $bout->room < 1;
273  
274 if ($bout->size) {
275 return Game::Durak::Error->new_code('rank_not_in_bout')
276 unless $bout->ranks->{ Game::Durak::Card::rank_of($card) };
277 }
278  
279 $self->_remove($seat, $card);
280 $bout->add_attack($card);
281 return ({ kind => 'attack', seat => $seat, card => $card });
282 }
283  
284 sub _beat {
285 my ($self, $seat, $move) = @_;
286 my $card = $self->_held($seat, $move);
287 return $card if ref $card;
288  
289 my $bout = $self->bout;
290 my $att = $bout->unbeaten;
291 die "Game::Durak: the defending stage with nothing to beat\n"
292 unless defined $att;
293  
294 return Game::Durak::Error->new_code('beats_nothing')
295 unless Game::Durak::Card::beats($card, $att, $self->trump);
296  
297 $self->_remove($seat, $card);
298 $bout->add_beat($card);
299 return ({ kind => 'beat', seat => $seat, card => $card });
300 }
301  
302 sub _take {
303 my ($self, $seat) = @_;
304 $self->bout->taken(1);
305 return ({ kind => 'take', seat => $seat });
306 }
307  
308 sub _done {
309 my ($self, $seat) = @_;
310 my $bout = $self->bout;
311 return Game::Durak::Error->new_code('must_attack') unless $bout->size;
312 return ({ kind => 'done', seat => $seat },
313 $self->_close_bout($bout->taken ? 'taken' : 'done'));
314 }
315  
316 sub _swap {
317 my ($self, $seat) = @_;
318  
319 my $six = $self->six_of_trumps;
320 my $owner = $self->six_owner;
321  
322 return Game::Durak::Error->new_code('not_the_six')
323 unless defined $owner && $owner == $seat
324 && grep { $_ == $six } @{ $self->hand_of($seat) };
325  
326 return Game::Durak::Error->new_code('talon_shut') unless $self->turn_up_left;
327  
328 my $up = $self->trump_card;
329 my $talon = [ @{ $self->talon } ];
330 die "Game::Durak: the turn-up is not the last card of the talon\n"
331 unless @$talon && $talon->[-1] == $up;
332  
333 $talon->[-1] = $six;
334 $self->talon($talon);
335  
336 $self->_remove($seat, $six);
337 $self->_give($seat, $up);
338 $self->trump_card($six);
339 $self->six_owner(undef);
340  
341 return ({ kind => 'swap', seat => $seat });
342 }
343  
344 sub _resign {
345 my ($self, $seat) = @_;
346 my $result = resign_for($seat, [ $self->seats ]);
347 $self->_finish($result);
348 return ({ kind => 'resign', seat => $seat },
349 { kind => 'game_end', %$result });
350 }
351  
352 sub _advance {
353 my ($self) = @_;
354 my @out;
355  
356 while (1) {
357 last if $self->over;
358 my $bout = $self->bout;
359 last unless $bout;
360  
361 if ($bout->taken) {
362 last unless $bout->room < 1 || $self->_forced($bout->attacker);
363 push @out, $self->_close_bout('taken');
364 next;
365 }
366  
367 my $how = closes($bout, $self->count_of($bout->defender));
368 if ($how) {
369 push @out, $self->_close_bout($how);
370 next;
371 }
372  
373 my $turn = $self->turn;
374 last unless $self->_forced($turn);
375  
376 if ($self->phase eq 'defend') {
377 $bout->taken(1);
378 next;
379 }
380  
381 last unless $bout->size;
382 push @out, $self->_close_bout('exhausted');
383 }
384  
385 return @out;
386 }
387  
388 sub _forced {
389 my ($self, $seat) = @_;
390 return forced($self->hand_of($seat), $self->bout, $self->trump,
391 $self->phase, $self->can_swap($seat));
392 }
393  
394 sub _close_bout {
395 my ($self, $how) = @_;
396  
397 my $bout = $self->bout;
398 my @cards = $bout->cards;
399 my $taken = $bout->taken ? 1 : 0;
400 my $attacker = $bout->attacker;
401 my $defender = $bout->defender;
402 my $next = $taken ? $attacker : $defender;
403  
404 if ($taken) {
405 my $hands = { %{ $self->hands } };
406 $hands->{$defender} =
407 [ sort { $a <=> $b } @{ $hands->{$defender} }, @cards ];
408 $self->hands($hands);
409  
410 my $six = $self->six_of_trumps;
411 my $owner = $self->six_owner;
412 $self->six_owner(undef)
413 if defined $owner && $owner != $defender
414 && grep { $_ == $six } @cards;
415 }
416 else {
417 $self->discard($self->discard + scalar @cards);
418 }
419  
420 my $event = {
421 kind => 'bout_end',
422 taken => $taken,
423 how => $how,
424 cards => scalar @cards,
425 discard => $self->discard,
426 next_attacker => $next,
427 };
428  
429 $self->bout(undef);
430 return ($event, $self->_refill($attacker, $defender),
431 $self->_after_bout($next));
432 }
433  
434 sub _refill {
435 my ($self, $first, $second) = @_;
436 my %drawn = (1 => 0, 2 => 0);
437 $drawn{$_} = $self->_draw_to($_) for $first, $second;
438 return {
439 kind => 'refill',
440 drawn => \%drawn,
441 talon => $self->talon_left,
442 };
443 }
444  
445 sub _draw_to {
446 my ($self, $seat) = @_;
447  
448 my $talon = [ @{ $self->talon } ];
449 my $six = $self->six_of_trumps;
450 my $drawn = 0;
451  
452 while (@$talon && $self->count_of($seat) < HAND_SIZE) {
453 my $card = shift @$talon;
454 $self->_give($seat, $card);
455 $self->six_owner($seat) if $card == $six;
456 $drawn++;
457 }
458  
459 $self->talon($talon);
460 return $drawn;
461 }
462  
463 sub _after_bout {
464 my ($self, $next) = @_;
465  
466 my $counts = { map { $_ => $self->count_of($_) } $self->seats };
467 my $result = result_for($counts, $self->talon_left);
468  
469 unless ($result) {
470 $self->_open_bout($next);
471 return ();
472 }
473  
474 my @out = map { { kind => 'out', seat => $_ } }
475 out_seats($counts, $self->talon_left);
476  
477 $self->_finish($result);
478 return (@out, { kind => 'game_end', %$result });
479 }
480  
481 sub _finish {
482 my ($self, $result) = @_;
483 die "Game::Durak: the deal has a result already\n" if $self->result;
484 $self->result($result);
485 $self->over(1);
486 return;
487 }
488  
489 sub _open_bout {
490 my ($self, $attacker) = @_;
491 my $defender = _other($attacker);
492 $self->bout(Game::Durak::Bout->build(
493 attacker => $attacker,
494 defender => $defender,
495 cap => cap_for($self->count_of($defender)),
496 ));
497 return;
498 }
499  
500 sub _give {
501 my ($self, $seat, @cards) = @_;
502 my $hands = { %{ $self->hands } };
503 $hands->{$seat} = [ sort { $a <=> $b } @{ $hands->{$seat} }, @cards ];
504 $self->hands($hands);
505 return;
506 }
507  
508 sub _remove {
509 my ($self, $seat, $card) = @_;
510 my $hands = { %{ $self->hands } };
511 my $gone = 0;
512 my @left;
513 for my $id (@{ $hands->{$seat} }) {
514 if (!$gone && $id == $card) { $gone = 1; next }
515 push @left, $id;
516 }
517 $hands->{$seat} = \@left;
518 $self->hands($hands);
519 return $gone;
520 }
521  
522 1;
523  
524 __END__
« Durak/Error.pm ↑ Summary Durak/Terminal.pm »