| 1 | package Game::Gin::Deal; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Object::Proto::Sugar -types; | ||
| 7 | |||
| 8 | use Game::Gin::Deck (); | ||
| 9 | use Game::Gin::Hand (); | ||
| 10 | use Game::Gin::Error (); | ||
| 11 | use Game::Gin::Deadwood qw(KNOCK_AT); | ||
| 12 | use Game::Gin::Scoring qw(settle); | ||
| 13 | |||
| 14 | our $VERSION = '0.04'; | ||
| 15 | |||
| 16 | has seed => (is => 'ro', isa => Str); | ||
| 17 | has number => (is => 'ro', isa => Int); | ||
| 18 | has dealer => (is => 'ro', isa => Str); | ||
| 19 | has hands => (is => 'rw', isa => HashRef); | ||
| 20 | has stock => (is => 'rw', isa => ArrayRef); | ||
| 21 | has discard => (is => 'rw', isa => ArrayRef); | ||
| 22 | has turn => (is => 'rw', isa => Str); | ||
| 23 | has phase => (is => 'rw', isa => Str); | ||
| 24 | has taken => (is => 'rw', isa => Int); | ||
| 25 | has result => (is => 'rw', isa => HashRef); | ||
| 26 | |||
| 27 | has history => (is => 'rw', isa => ArrayRef); | ||
| 28 | |||
| 29 | sub other { return $_[0] eq 'p1' ? 'p2' : 'p1' } | ||
| 30 | |||
| 31 | sub build { | ||
| 32 | my ($class, %o) = @_; | ||
| 33 | my $seed = $o{seed}; | ||
| 34 | return Game::Gin::Error->new_code('no_seed') | ||
| 35 | unless defined $seed && length $seed == 32; | ||
| 36 | |||
| 37 | my $number = $o{number} || 1; | ||
| 38 | my $dealer = $o{dealer} || 'p1'; | ||
| 39 | my $pone = other($dealer); | ||
| 40 | |||
| 41 | my $deal = Game::Gin::Deck::deal_for($seed, $number); | ||
| 42 | |||
| 43 | my $self = $class->new( | ||
| 44 | seed => $seed, | ||
| 45 | number => $number, | ||
| 46 | dealer => $dealer, | ||
| 47 | hands => { | ||
| 48 | $pone => Game::Gin::Hand->new(cards => [ @{ $deal->{non_dealer} } ]), | ||
| 49 | $dealer => Game::Gin::Hand->new(cards => [ @{ $deal->{dealer} } ]), | ||
| 50 | }, | ||
| 51 | stock => [ @{ $deal->{stock} } ], | ||
| 52 | discard => [ $deal->{upcard} ], | ||
| 53 | turn => $pone, | ||
| 54 | phase => 'upcard', | ||
| 55 | taken => 0, | ||
| 56 | history => [], | ||
| 57 | result => undef, | ||
| 58 | ); | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub over { return $_[0]->result ? 1 : 0 } | ||
| 63 | sub upcard { my $d = $_[0]->discard; return @$d ? $d->[-1] : undef } | ||
| 64 | sub hand_of { return $_[0]->hands->{ $_[1] } } | ||
| 65 | sub stock_left { return scalar @{ $_[0]->stock } } | ||
| 66 | |||
| 67 | sub legal { | ||
| 68 | my ($self, $seat) = @_; | ||
| 69 | return [] if $self->over; | ||
| 70 | return [] unless defined $seat && ($self->turn // '') eq $seat; | ||
| 71 | |||
| 72 | my $phase = $self->phase; | ||
| 73 | return [ { kind => 'take' }, { kind => 'pass' } ] if $phase eq 'upcard'; | ||
| 74 | |||
| 75 | return [ { kind => 'draw' } ] if $phase eq 'forced_draw'; | ||
| 76 | |||
| 77 | if ($phase eq 'draw') { | ||
| 78 | my @out = ({ kind => 'draw' }); | ||
| 79 | push @out, { kind => 'take' } if defined $self->upcard; | ||
| 80 | return \@out; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $hand = $self->hand_of($seat); | ||
| 84 | my @out; | ||
| 85 | push @out, { kind => 'big_gin' } if $hand->deadwood == 0 && $hand->full; | ||
| 86 | for my $card (@{ $hand->cards }) { | ||
| 87 | next if defined $self->taken && $self->taken && $card == $self->taken; | ||
| 88 | push @out, { kind => 'discard', card => $card }; | ||
| 89 | my @rest = grep { $_ != $card } @{ $hand->cards }; | ||
| 90 | my $left = Game::Gin::Deadwood::deadwood(\@rest); | ||
| 91 | push @out, { kind => 'discard', card => $card, knock => 1 } if $left <= KNOCK_AT; | ||
| 92 | } | ||
| 93 | return \@out; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub apply { | ||
| 97 | my ($self, $seat, $move) = @_; | ||
| 98 | return Game::Gin::Error->new_code('hand_over') if $self->over; | ||
| 99 | return Game::Gin::Error->new_code('not_your_turn') | ||
| 100 | unless defined $seat && ($self->turn // '') eq $seat; | ||
| 101 | |||
| 102 | my $kind = ref $move eq 'HASH' ? ($move->{kind} // '') : ''; | ||
| 103 | my $phase = $self->phase; | ||
| 104 | |||
| 105 | my @out; | ||
| 106 | if ($phase eq 'upcard') { @out = $self->_first_turn($seat, $kind) } | ||
| 107 | elsif ($phase eq 'forced_draw') { | ||
| 108 | return Game::Gin::Error->new_code('not_legal') unless $kind eq 'draw'; | ||
| 109 | @out = $self->_draw($seat); | ||
| 110 | } | ||
| 111 | elsif ($phase eq 'draw') { | ||
| 112 | if ($kind eq 'draw') { @out = $self->_draw($seat) } | ||
| 113 | elsif ($kind eq 'take') { @out = $self->_take($seat) } | ||
| 114 | else { return Game::Gin::Error->new_code('not_legal') } | ||
| 115 | } | ||
| 116 | elsif ($kind eq 'big_gin') { @out = $self->_big_gin($seat) } | ||
| 117 | elsif ($kind eq 'discard') { @out = $self->_discard($seat, $move) } | ||
| 118 | else { return Game::Gin::Error->new_code('not_legal') } | ||
| 119 | |||
| 120 | return $out[0] if @out == 1 && ref $out[0] eq 'Game::Gin::Error'; | ||
| 121 | $self->history([ @{ $self->history || [] }, @out ]); | ||
| 122 | return @out; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _first_turn { | ||
| 126 | my ($self, $seat, $kind) = @_; | ||
| 127 | return $self->_take($seat) if $kind eq 'take'; | ||
| 128 | return Game::Gin::Error->new_code('not_legal') unless $kind eq 'pass'; | ||
| 129 | |||
| 130 | my @out = ({ kind => 'pass', seat => $seat }); | ||
| 131 | if ($seat eq $self->dealer) { | ||
| 132 | $self->turn(other($self->dealer)); | ||
| 133 | $self->phase('forced_draw'); | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | $self->turn($self->dealer); | ||
| 137 | } | ||
| 138 | return @out; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _take { | ||
| 142 | my ($self, $seat) = @_; | ||
| 143 | my $card = $self->upcard; | ||
| 144 | return Game::Gin::Error->new_code('not_legal') unless defined $card; | ||
| 145 | my @pile = @{ $self->discard }; | ||
| 146 | pop @pile; | ||
| 147 | $self->discard(\@pile); | ||
| 148 | $self->hand_of($seat)->add($card); | ||
| 149 | $self->taken($card); | ||
| 150 | $self->phase('discard'); | ||
| 151 | return ({ kind => 'take', seat => $seat, card => $card }); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _draw { | ||
| 155 | my ($self, $seat) = @_; | ||
| 156 | my @stock = @{ $self->stock }; | ||
| 157 | return Game::Gin::Error->new_code('not_legal') unless @stock; | ||
| 158 | my $card = shift @stock; | ||
| 159 | $self->stock(\@stock); | ||
| 160 | $self->hand_of($seat)->add($card); | ||
| 161 | $self->taken(0); | ||
| 162 | $self->phase('discard'); | ||
| 163 | return ({ kind => 'draw', seat => $seat }); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub _discard { | ||
| 167 | my ($self, $seat, $move) = @_; | ||
| 168 | my $card = $move->{card}; | ||
| 169 | my $hand = $self->hand_of($seat); | ||
| 170 | return Game::Gin::Error->new_code('not_held') unless $hand->has_card($card); | ||
| 171 | return Game::Gin::Error->new_code('just_taken') | ||
| 172 | if $self->taken && $card == $self->taken; | ||
| 173 | |||
| 174 | $hand->remove($card); | ||
| 175 | $self->discard([ @{ $self->discard }, $card ]); | ||
| 176 | $self->taken(0); | ||
| 177 | |||
| 178 | my $left = $hand->deadwood; | ||
| 179 | if ($move->{knock}) { | ||
| 180 | return Game::Gin::Error->new_code('cannot_knock') if $left > KNOCK_AT; | ||
| 181 | my @out = ({ kind => 'discard', seat => $seat, card => $card, knock => 1 }); | ||
| 182 | push @out, $self->_finish($seat, gin => ($left == 0 ? 1 : 0)); | ||
| 183 | return @out; | ||
| 184 | } | ||
| 185 | |||
| 186 | my @out = ({ kind => 'discard', seat => $seat, card => $card }); | ||
| 187 | |||
| 188 | if ($self->stock_left <= 2) { | ||
| 189 | $self->result({ kind => 'cancelled', winner => undef, points => 0 }); | ||
| 190 | $self->turn(undef); | ||
| 191 | $self->phase('over'); | ||
| 192 | push @out, { kind => 'cancelled' }; | ||
| 193 | return @out; | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->turn(other($seat)); | ||
| 197 | $self->phase('draw'); | ||
| 198 | return @out; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _big_gin { | ||
| 202 | my ($self, $seat) = @_; | ||
| 203 | my $hand = $self->hand_of($seat); | ||
| 204 | return Game::Gin::Error->new_code('not_big_gin') | ||
| 205 | unless $hand->full && $hand->deadwood == 0; | ||
| 206 | return ({ kind => 'big_gin', seat => $seat }, $self->_finish($seat, big_gin => 1)); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub _finish { | ||
| 210 | my ($self, $knocker, %how) = @_; | ||
| 211 | my $defender = other($knocker); | ||
| 212 | my $hand = $self->hand_of($knocker); | ||
| 213 | my $best = $hand->best; | ||
| 214 | |||
| 215 | my $r = settle( | ||
| 216 | knocker => $knocker, | ||
| 217 | defender => $defender, | ||
| 218 | knocker_deadwood => $best->{deadwood}, | ||
| 219 | knocker_melds => $best->{melds}, | ||
| 220 | defender_cards => $self->hand_of($defender)->cards, | ||
| 221 | gin => ($how{gin} || $how{big_gin} ? 1 : 0), | ||
| 222 | big_gin => ($how{big_gin} ? 1 : 0), | ||
| 223 | ); | ||
| 224 | $self->result($r); | ||
| 225 | $self->turn(undef); | ||
| 226 | $self->phase('over'); | ||
| 227 | return ({ %$r, how => $r->{kind}, kind => 'hand_end' }); | ||
| 228 | } | ||
| 229 | |||
| 230 | 1; | ||
| 231 | |||
| 232 | __END__ |