469,326 Members | 1,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Problem with concurrency and a blackjack game.

I'm in a really fast pace training program and we are covering a lot of material in a relatively short period of time. I've really not had much trouble up until this problem. I had to do one other problem using concurrency and it worked out well but this problem is stumping me a little. The problem is a non-network black jack game with concurrency.

Here are the specs given:

The game will consist of 3 types of parts: a human user player, a computer player, and a dealer. Each part should reside on its own thread, with the dealer controlling the “deck of cards” resource. The player threads (computer or human) wait to be dealt cards, and the controller waits on player decision to deal the next card. Each player is assigned a player number, and the dealer deals to the players in the order of their player number.

The problem I'm having is trying to get the players to take their turn in order by player number. I'm going to post some code from my computer player class below. I started with an abstract class (player) and extended from it with a computer player and human player class to control the two different types of players. I came up with an idea letting the dealer be the shared resource and have the dealer control which player number he is currently dealing to. Then I could use that to synchronize the takeTurn method and if the current player's player number didn't match the current number the dealer was dealing to then that player's thread would be put into a waiting state. Then once the current player had finished taking their turn the dealer's player number he was dealing to would be updated and the notifyAll method called to put the other threads back into the running state. I stubbed out the program a little to try and watch it in the console before I started with the GUI part and I can see that the threads enter a waiting state and that the current player updates the dealer's player number he is dealing to but I can't seem to get the other threads to enter the running state.

Any help/suggestions would be appreciated. I may not be going about this in a good way and this idea for the takeTurn method I came up with may not work period but I don't understand why it doesn't. Here is what the console output looks lile:

Computer Player: Player - 4 is waiting.
Computer Player: Player - 3 is waiting.
Computer Player: Player - 2 is waiting.
Computer Player: Player - 1
Jack of Diamonds
Three of Hearts

13

Computer Player: Player - 1
Jack of Diamonds
Three of Hearts
Two of Hearts

15

Computer Player: Player - 1
Jack of Diamonds
Three of Hearts
Two of Hearts
Five of Hearts

20

Computer Player: Player - 1 is updating player number.

Expand|Select|Wrap|Line Numbers
  1. public class ComputerPlayer extends Player implements Runnable
  2. {
  3.     public ComputerPlayer(Dealer dealer)
  4.     {
  5.         first = null;
  6.         last = null;
  7.         this.dealer = dealer;
  8.         playerName = "Computer Player: Player - " + playerNumber;
  9.         thisPlayersNumber = playerNumber;
  10.         playerNumber++;
  11.     }
  12.  
  13.     public String getPlayerName()
  14.     {
  15.         return playerName;
  16.     }
  17.  
  18.     public synchronized void takeTurn()
  19.     {
  20.         while(thisPlayersNumber != dealer.getCurrentPlayerNumber())
  21.         {
  22.             try 
  23.             {
  24.                 System.out.println(this.playerName + " is waiting.");
  25.                 wait();
  26.                 System.out.println(this.playerName + " entering running state.");
  27.             } 
  28.             catch (InterruptedException e) 
  29.             {
  30.             }
  31.         }
  32.  
  33.         boolean hit = true;
  34.  
  35.         for(short count = 0; count < 2; count++)
  36.         {
  37.             addCard(dealer.dealCard());
  38.         }
  39.  
  40.         System.out.println(getPlayerName());
  41.         System.out.println(showCards());
  42.         System.out.println(getHandValue());
  43.         System.out.println();
  44.  
  45.         if(getHandValue() >= 17)
  46.         {
  47.             hit = false;
  48.         }
  49.  
  50.         while(hit)
  51.         {            
  52.             addCard(dealer.dealCard());
  53.  
  54.             System.out.println(getPlayerName());
  55.             System.out.println(showCards());
  56.             System.out.println(getHandValue());
  57.             System.out.println();
  58.  
  59.             if(getHandValue() >= 17)
  60.             {
  61.                 hit = false;
  62.  
  63.                 if(getHandValue() == 21)
  64.                 {
  65.                     System.out.println("Winner: 21!");
  66.                     System.out.println();
  67.                 }
  68.                 else if(getHandValue() > 21)
  69.                 {
  70.                     System.out.println("Busted!");
  71.                     System.out.println();
  72.                 }
  73.             }
  74.         }
  75.  
  76.         System.out.println(this.playerName + " is updating player number.");
  77.         dealer.updateCurrentPlayerNumber();
  78.  
  79.         notifyAll();
  80.     }
  81.  
  82.     public void run() 
  83.     {
  84.         takeTurn();
  85.     }
  86. }
Feb 21 '08 #1
0 1002

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Charlie Cosse | last post: by
4 posts views Thread by Eric Westrom | last post: by
30 posts views Thread by imran akhtar | last post: by
21 posts views Thread by imran akhtar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.