| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # gin.pl - Play Gin | ||
| 4 | # | ||
| 5 | # Copyright 1999 Amir Karger (karger@post.harvard.edu) | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or modify it | ||
| 8 | # under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # TODO check for win every turn! | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use Games::Cards; | ||
| 14 | |||
| 15 | srand(); | ||
| 16 | |||
| 17 | ###################################################################### | ||
| 18 | # SETUP THE GAME | ||
| 19 | # Main variables | ||
| 20 | my $Gin; # the game object | ||
| 21 | my $Deck; # the deck we're using in the game | ||
| 22 | my $Discard; # the discard pile | ||
| 23 | my @Hands; # the players' hands | ||
| 24 | my $Cards_Per_Hand = 10; | ||
| 25 | my $Number_Of_Players = 2; | ||
| 26 | |||
| 27 | my $Error; # current error message | ||
| 28 | my $Usage =<<"ENDUSAGE"; | ||
| 29 | $0 - play gin | ||
| 30 | |||
| 31 | - On each turn, each player picks a card, then discards. | ||
| 32 | |||
| 33 | - A "meld" is 3 or 4 cards of the same rank, or a consecutive runs of | ||
| 34 | three or four cards of the same suit. You win when you have three melds | ||
| 35 | (two melds of 3 and one of 4). You have to "say gin" to win. | ||
| 36 | |||
| 37 | - Commands are made up of letters, card descriptions, or numbers (from 1 to | ||
| 38 | $Cards_Per_Hand). Commands/card names are case insensitive | ||
| 39 | |||
| 40 | Pick: | ||
| 41 | s Take the top card from the stock (deck) | ||
| 42 | d Take the top card from the discard pile | ||
| 43 | |||
| 44 | Discard: | ||
| 45 | 8D Discard the eight of diamonds | ||
| 46 | 8D g[in] Discard eight of diamonds and say "I have gin!" | ||
| 47 | (The program will complain if you don't actually have gin.) | ||
| 48 | |||
| 49 | At any time: | ||
| 50 | 8D 2 Put the 8 of diamonds at position 2 in your hand | ||
| 51 | so[rt] Sort cards in your hand by value (not suit) | ||
| 52 | q quits a game, either starting a new round or quitting entirely | ||
| 53 | h prints this help message | ||
| 54 | ENDUSAGE | ||
| 55 | |||
| 56 | # This otherwise uselessline will unconfuse vim's syntax highlighting! | ||
| 57 | |||
| 58 | # Create the game. | ||
| 59 | # Use the default deck (standard suits, cards, & card values) | ||
| 60 | $Gin = new Games::Cards::Game; | ||
| 61 | |||
| 62 | # Create and shuffle the deck | ||
| 63 | print "Creating new deck.\n"; | ||
| 64 | |||
| 65 | NEWGAME: # We go to here when starting a new game | ||
| 66 | $Deck = new Games::Cards::Deck ($Gin, "Deck"); | ||
| 67 | print "Shuffling the deck.\n"; | ||
| 68 | $Deck->shuffle; | ||
| 69 | |||
| 70 | # Deal out the Hands | ||
| 71 | @Hands = (); | ||
| 72 | foreach my $i (1 .. $Number_Of_Players) { | ||
| 73 | my $hand = new Games::Cards::Hand ($Gin, "Player $i") ; | ||
| 74 | $Deck->give_cards($hand, $Cards_Per_Hand); | ||
| 75 | $hand->sort_by_value; | ||
| 76 | push @Hands, $hand; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Put one face-up card in the discard pile | ||
| 80 | $Discard = new Games::Cards::Stack ($Gin, "Discard pile"); | ||
| 81 | $Deck->give_cards($Discard, 1); | ||
| 82 | $Discard->top_card->face_up; | ||
| 83 | |||
| 84 | # No undo in gin | ||
| 85 | # my $Undo = new Games::Cards::Undo(); | ||
| 86 | |||
| 87 | ###################################################################### | ||
| 88 | # Now play | ||
| 89 | |||
| 90 | my $turns = 1; # 1 gets subtracted later cuz Error is set | ||
| 91 | $Error = "Welcome! Type h for help, q to quit"; | ||
| 92 | TURN_LOOP: while (++$turns) { | ||
| 93 | PLAYER_LOOP: foreach my $hand (@Hands) { | ||
| 94 | # A turn includes picking a card and discarding a card | ||
| 95 | HALF_LOOP: foreach my $half ("pick", "discard") { | ||
| 96 | |||
| 97 | &print_game($hand); | ||
| 98 | |||
| 99 | # If we got an error on the last turn, print the game status *first*, then | ||
| 100 | # print the error right before the prompt (so that the reader will see it) | ||
| 101 | if ($Error) { | ||
| 102 | print "$Error\n\n"; | ||
| 103 | $Error = ""; | ||
| 104 | } | ||
| 105 | |||
| 106 | # Ask player what to do | ||
| 107 | print $hand->name, ", $half: "; | ||
| 108 | my $input = <STDIN>; chomp($input); | ||
| 109 | |||
| 110 | # Big case statement | ||
| 111 | for ($input) { | ||
| 112 | s/\s*//g; | ||
| 113 | |||
| 114 | # Pick from deck (stock) | ||
| 115 | if (/^s$/i) { | ||
| 116 | if ($half eq "pick") { | ||
| 117 | $Deck->give_cards($hand, 1); | ||
| 118 | } else { | ||
| 119 | $Error="Picking is only allowed in the first half of the turn"; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Pick from the discard pile | ||
| 123 | } elsif (/^d$/i) { | ||
| 124 | if ($half eq "pick") { | ||
| 125 | $Discard->give_cards($hand, 1); | ||
| 126 | } else { | ||
| 127 | $Error="Picking is only allowed in the first half of the turn"; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Discard | ||
| 131 | } elsif (/^(([jqka]|\d{1,2})[cdhs])(g(in)?)?$/i) { | ||
| 132 | if ($half eq "discard") { | ||
| 133 | my ($card, $gin) = ($1, $3); | ||
| 134 | $hand->give_a_card($Discard, $card) or | ||
| 135 | $Error = "ERROR! You don't have that card!"; | ||
| 136 | # did I win? | ||
| 137 | if ($gin && !$Error) { | ||
| 138 | &check_win($hand) or $Error = "You didn't win yet!\n"; | ||
| 139 | next HALF_LOOP; # don't redo turn even if we didn't win! | ||
| 140 | } | ||
| 141 | } else { | ||
| 142 | $Error="Discarding is only allowed in the 2nd half of the turn"; | ||
| 143 | } | ||
| 144 | |||
| 145 | # Arrange cards in my hand | ||
| 146 | } elsif (/^(([jqka]|\d{1,2})[cdhs])\s*(\d+)$/i) { | ||
| 147 | my ($card, $index) = ($1, $3 - 1); | ||
| 148 | if (defined $hand->index($card)) { | ||
| 149 | $hand->move_card($card, $index) or | ||
| 150 | $Error = "ERROR! Illegal hand rearrangement!"; | ||
| 151 | } else { | ||
| 152 | $Error = "ERROR! You don't have that card!" | ||
| 153 | } | ||
| 154 | redo HALF_LOOP; | ||
| 155 | |||
| 156 | # I got gin! | ||
| 157 | } elsif (/^so/i) { | ||
| 158 | $hand->sort_by_value; | ||
| 159 | redo HALF_LOOP; | ||
| 160 | |||
| 161 | # help | ||
| 162 | } elsif (/^h/i) { | ||
| 163 | print $Usage; | ||
| 164 | print "\nType RETURN to continue\n"; | ||
| 165 | <STDIN>; | ||
| 166 | redo HALF_LOOP; | ||
| 167 | |||
| 168 | # quit game | ||
| 169 | } elsif (/^q/i) { | ||
| 170 | print "Are you sure you want to quit? (y/n): "; | ||
| 171 | my $a = <STDIN>; | ||
| 172 | if ($a =~ /^y/i) { | ||
| 173 | print "Would you like to play another game? (y/n): "; | ||
| 174 | $a = <STDIN>; | ||
| 175 | if ($a =~ /^n/i) { | ||
| 176 | print "Bye!\n"; | ||
| 177 | last TURN_LOOP; | ||
| 178 | } else { | ||
| 179 | goto NEWGAME; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | # don't quit after all | ||
| 183 | redo HALF_LOOP; | ||
| 184 | |||
| 185 | } else { | ||
| 186 | $Error = "ERROR! unknown command. Try again (h for help)" | ||
| 187 | } # end case if | ||
| 188 | |||
| 189 | } # end pick case statement | ||
| 190 | |||
| 191 | redo if $Error; # redo this half of the turn | ||
| 192 | |||
| 193 | } # end loop over two halves of a turn | ||
| 194 | |||
| 195 | # Reset deck if it's empty | ||
| 196 | unless ($Deck->size) { | ||
| 197 | print "Deck is empty!\n"; | ||
| 198 | my $temp = new Games::Cards::Stack ""; | ||
| 199 | $Discard->give_cards($temp, 1); | ||
| 200 | $Discard->give_cards($Deck, "all"); | ||
| 201 | $temp->give_cards($Discard, 1); | ||
| 202 | } | ||
| 203 | |||
| 204 | } # end loop over players | ||
| 205 | } #end while (loop over turns) | ||
| 206 | |||
| 207 | exit; | ||
| 208 | |||
| 209 | ###################################################################### | ||
| 210 | |||
| 211 | sub print_game { | ||
| 212 | # print out the current status | ||
| 213 | my $hand = shift; | ||
| 214 | print "\n\n\n", "-" x 50,"\n"; | ||
| 215 | print "Stock has ", $Deck->size, " cards\n"; | ||
| 216 | print $Discard->print("short"),"\n"; | ||
| 217 | print $hand->print("short"); | ||
| 218 | print "\n\n"; | ||
| 219 | } # end sub print_game | ||
| 220 | |||
| 221 | sub check_win { | ||
| 222 | # arg0 is the hand of the player who (theoretically) won | ||
| 223 | my $hand = shift; | ||
| 224 | $hand->sort_by_value; # this makes searching for melds *much* easier | ||
| 225 | |||
| 226 | # Test for melds... | ||
| 227 | my $cards_ref = $hand->cards; | ||
| 228 | |||
| 229 | # Sub next_meld if there is there a meld of size arg0 in array of Cards | ||
| 230 | # arg1 at meld level arg2, it sets that meld (in @melds) and returns 1 | ||
| 231 | # Otherwise, it returns 0. | ||
| 232 | # Meld levels are (1..3) since gin require 3 melds to win. First we call | ||
| 233 | # with level 1 and find a possible meld. We mark those cards with a "1" | ||
| 234 | # (in @melds) so when we try to find the next meld (level 2) we don't use | ||
| 235 | # the cards already in meld #1 | ||
| 236 | # | ||
| 237 | # Static variables: | ||
| 238 | # @possible - array containing an (refs to) arrays of possible melds for | ||
| 239 | # each meld level | ||
| 240 | # @melds - array containing---for each card---the meld level of that card | ||
| 241 | # (or -1) I.e., this stores the current attempt at winning | ||
| 242 | # | ||
| 243 | # Note! sub next_meld goes *inside* sub check_win so that we can create | ||
| 244 | # static variables for meld(). (Use an anonymous sub ref instead of just | ||
| 245 | # 'sub next_meld {...}' to avoid a "Variable will not stay shared" warning. | ||
| 246 | # This also allows using the static vars in the outer subroutine.) | ||
| 247 | my @possible = ([], [], []); # STATIC VARIABLE FOR SUB NEXT_MELD | ||
| 248 | my @melds = (-1) x 10; # STATIC VARIABLE FOR SUB NEXT_MELD | ||
| 249 | |||
| 250 | my $next_meld = sub { | ||
| 251 | # Note that the cards are sorted by value, so e.g., 4 of a kind will be | ||
| 252 | # contiguous | ||
| 253 | my $size = shift; # size of meld we want | ||
| 254 | my $cards_ref = shift; # the cards | ||
| 255 | my $level = shift; # highest meld level we've done so far | ||
| 256 | # possible melds at this level---a meld is (ref to) an array | ||
| 257 | # containing card numbers of Cards that make up the meld. | ||
| 258 | my @poss = @{$possible[$level]}; | ||
| 259 | |||
| 260 | # Calculate @poss, if we haven't already | ||
| 261 | # First time you try to meld at a given level, (or the first time | ||
| 262 | # since you tried melding at a lower level) unmelded cards will | ||
| 263 | # have meld level < $level or -1. In that case we need to create the | ||
| 264 | # array containing possible melds at this level, which gets stored | ||
| 265 | # in $poss[$level]. | ||
| 266 | # Other times, the cards from the last attempted meld at this level | ||
| 267 | # will have meld level $level. | ||
| 268 | |||
| 269 | unless (grep {$_ == $level} @melds) { | ||
| 270 | # Create arrays of various aspects of the not yet melded cards | ||
| 271 | # (for convenience) | ||
| 272 | my @available; # (indices of) cards we can use | ||
| 273 | foreach (0 .. $#melds) { | ||
| 274 | push @available, $_ if $melds[$_]==-1; | ||
| 275 | } | ||
| 276 | # print ("Level $level. Avail: ", | ||
| 277 | # (map {$cards_ref->[$_]->print("short")} @available), "\n"); | ||
| 278 | my @values = map {$cards_ref->[$_]->value} @available; | ||
| 279 | my @names = map {$cards_ref->[$_]->name} @available; | ||
| 280 | my @suits = map {$cards_ref->[$_]->suit} @available; | ||
| 281 | |||
| 282 | # The meld must contain the first card in the array which isn't | ||
| 283 | # already in some other meld (so that all cards are included in | ||
| 284 | # *some* meld) | ||
| 285 | my ($first, $first_value, $first_name, $first_suit) = | ||
| 286 | (shift @available, shift @values, shift @names, shift @suits); | ||
| 287 | |||
| 288 | # 3/4 of a kind including $first | ||
| 289 | my $matches = grep {$_ == $first_value} @values; | ||
| 290 | if ($matches == $size-1) { # found exactly as many as we want | ||
| 291 | push @poss, [$first, @available[0..$size-2]] | ||
| 292 | } elsif ($matches == 3 && $size == 3) { # 4 of a kind, want 3 | ||
| 293 | push @poss, [$first, @available[0, 1]], | ||
| 294 | [$first, @available[0, 2]], | ||
| 295 | [$first, @available[1, 2]]; | ||
| 296 | } | ||
| 297 | |||
| 298 | # straight of length $size including $first | ||
| 299 | my @same_suit; | ||
| 300 | my $temp = 1; # next card's value (hopefully) | ||
| 301 | foreach (0 .. $#suits) { | ||
| 302 | push @same_suit, $available[$_] if | ||
| 303 | $suits[$_] eq $first_suit && | ||
| 304 | $values[$_] == $first_value + $temp++; | ||
| 305 | if (@same_suit == $size - 1) { # found a straight! | ||
| 306 | push @poss, [$first, @same_suit]; | ||
| 307 | last; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } # end creation of @poss array | ||
| 311 | #foreach (@poss) {print "Poss: @$_\n";} | ||
| 312 | |||
| 313 | # set previous meld back to zero | ||
| 314 | foreach (@melds) {$_ = -1 if $_ == $level} | ||
| 315 | |||
| 316 | # If we have any more possible melds, set the next one, return 1 | ||
| 317 | # else return 0 | ||
| 318 | my $ret = 0; | ||
| 319 | if (defined (my $try = pop @poss)) { | ||
| 320 | foreach (@$try) { | ||
| 321 | $melds[$_] = $level; | ||
| 322 | } | ||
| 323 | # Print the meld we're trying | ||
| 324 | #print ((map {$cards_ref->[$_]->print("short")} @$try), "\n"); | ||
| 325 | $ret = 1; | ||
| 326 | } | ||
| 327 | |||
| 328 | # Store melds. Particularly important if we have multiple melds at | ||
| 329 | # level 1 (e.g.) and need to try various melds at level 2 with each | ||
| 330 | # level 1 meld | ||
| 331 | $possible[$level] = \@poss; | ||
| 332 | return $ret; | ||
| 333 | }; # end sub(ref) next_meld | ||
| 334 | |||
| 335 | my $won; | ||
| 336 | # try 4, 3, 3 | ||
| 337 | TRY4: while (&$next_meld(4, $cards_ref, 0)) { | ||
| 338 | while (&$next_meld(3, $cards_ref, 1)) { | ||
| 339 | $won = &$next_meld(3, $cards_ref, 2); | ||
| 340 | last TRY4 if $won | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | unless ($won) { # didn't manage to win 4,3,3; try 3,4,3 | ||
| 345 | TRY3: while (&$next_meld(3, $cards_ref, 0)) { | ||
| 346 | while (&$next_meld(4, $cards_ref, 1)) { | ||
| 347 | $won = &$next_meld(3, $cards_ref, 2); | ||
| 348 | last TRY3 if $won; | ||
| 349 | } | ||
| 350 | |||
| 351 | # Didn't manage 3,4,3; try 3,3,4 | ||
| 352 | while (&$next_meld(3, $cards_ref, 1)) { | ||
| 353 | $won = &$next_meld(4, $cards_ref, 2); | ||
| 354 | last TRY3 if $won; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | return unless $won; | ||
| 360 | |||
| 361 | # You won! | ||
| 362 | print $hand->name, " won!\n"; | ||
| 363 | print "\n\nWould you like to play another game? (y/n): "; | ||
| 364 | my $a; | ||
| 365 | $a = <STDIN>; | ||
| 366 | goto NEWGAME if ($a =~ /^y/i); | ||
| 367 | exit; | ||
| 368 | |||
| 369 | } # end sub check_win | ||
| 370 |