Inspired by: http://www.cs.umd.edu/~als/cmsc330-f99/projects/assign7.html

Lab: 03


Objective

To use Java threads to implement players for a simple card game.

The game has 4 players and 4 piles of cards, one for each player. The players draw cards from the top and discard cards to the bottom of the piles in the following pattern:

Pile
Draw Discard
1 2
2 3
3 4
4 1

The first player to collect 4 cards of the same denomination 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 his respective deck and the rest are used as the pile.

Strategy

Each player must use the same game playing strategy . 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.

Implementation

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 piles.

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 (e.g., by using interrupt() ). 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 a separate 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 umar.kalim@niit.edu.pk

Last updated: 2nd October, 2007

>

Last updated: 2nd October, 2007