| 1 | package Game::Durak::Result; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Exporter 'import'; | ||
| 7 | |||
| 8 | our $VERSION = '0.04'; | ||
| 9 | our @EXPORT_OK = qw(out_seats places_for result_for resign_for); | ||
| 10 | |||
| 11 | sub out_seats { | ||
| 12 | my ($counts, $talon_left) = @_; | ||
| 13 | return () if $talon_left; | ||
| 14 | return grep { !$counts->{$_} } sort { $a <=> $b } keys %$counts; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub places_for { | ||
| 18 | my ($fool, $seats) = @_; | ||
| 19 | return { map { $_ => (defined $fool && $_ == $fool) ? 2 : 1 } @$seats }; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub result_for { | ||
| 23 | my ($counts, $talon_left) = @_; | ||
| 24 | |||
| 25 | my @seats = sort { $a <=> $b } keys %$counts; | ||
| 26 | my @out = out_seats($counts, $talon_left); | ||
| 27 | return undef unless @out; | ||
| 28 | |||
| 29 | my @holding = grep { $counts->{$_} } @seats; | ||
| 30 | |||
| 31 | return { | ||
| 32 | outcome => 'draw', | ||
| 33 | fool => undef, | ||
| 34 | places => places_for(undef, \@seats), | ||
| 35 | } unless @holding; | ||
| 36 | |||
| 37 | return { | ||
| 38 | outcome => 'fool', | ||
| 39 | fool => $holding[0], | ||
| 40 | places => places_for($holding[0], \@seats), | ||
| 41 | }; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub resign_for { | ||
| 45 | my ($seat, $seats) = @_; | ||
| 46 | return { | ||
| 47 | outcome => 'resign', | ||
| 48 | fool => $seat, | ||
| 49 | places => places_for($seat, $seats), | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |