Inspired by:

Lab: 04


(In continuation of Lab 03) To use Java threads and synchronization to implement players for a simple card game using a single deck.

The game has 4 players and and deck of cards. The players draw cards from the top and discard cards to the bottom of the piles in the following pattern:

Draw Discard
1 2
2 3
3 4
4 1

Each player takes his/her turn in the order P1, P2, P3, P4, P1, P2,.... till the time a player collects 4 cards of the same denomination and wins the game. The card deck has 32 cards: 4 of each denomination from 0 through 7. The card deck is shuffled before the start of the game. Initially each player is dealt 4 cards from the deck and the rest are used as the pile.


Each player must use the same game playing strategy i.e. similar winning condition. The simple strategy you should use has each player preferring certain card denominations. In particular, player 1 prefers 0's and 1's, player 2 prefers 2's and 3's, player 3 prefers 4's and 5's, and player 4 prefers 6's and 7's. This means that (after drawing a card from the proper deck) a player discards any card from a non-preferred denomination, if possible. If that is not possible, then the player discards a card from the denomination with fewer cards.

For example, if player 2 has (after drawing a card from the proper deck) one 2, one 4, one 5, and a pair of 3's, then the player would discard either the 4 or the 5.


In addition to a Main class that starts the game application, initializes the input deck, deals the initial hands, and creates the various threads needed to run the game, you will need to build classes for the players and for the card pile.

In addition, you will need to implement a routine that handles stopping players who did not win the game. The winning player can simply exit the run procedure that started its thread execution. However, this player must inform the other player threads that they need to stop. Each thread must print a message when it finishes (e.g., "Player 1 wins and exits", "Player 2 exits", etc.). Also, drawing and discarding is an atomic action, so a player must discard after a draw, even when the player wins.  This means a player always has 4 cards in the hand whenever printing the contents of a hand.

Each player must demonstrate card playing actions by printing the card drawn, the card discarded and the current hand for each action. The action must be labeled with the player number. Each player's actions should be written to the player's output file.

For example, intermediate moves should print something like the following:

player 1 draws a 4
player 1 discards 4
player 1 hand 5 5 5 6
The final move of the winner prints something like the following:
player 3 draws a 7
player 3 discards a 3
player 3 hand 7 7 7 7
player 3 wins
player 3 exits

As the final output for each player at the end of the game, the player should print three lines, appropriately customized for that player, to the player's output file.  The first states whether the player won or lost, the second lists the player's hand at the end of the game, and the third lists the contents of the pile the player draws from at the end of the game.  The lines look like this, with required keywords in bold:

WIN yes (or no)
HAND card1 card2 card3 card4
DRAWPILE contents of draw pile, separated by spaces (e.g., 2 5 3)

Instructions for submitting your work

  1. You are required to demonstrate your implementation during the lab.
  2. Create an executable java archive (jar) of your implementation and email it to

Last updated: 9th October, 2007

Last updated: 9th October, 2007