473,231 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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
1 23968
FatimaWalker
1 Bit
Sounds like you're going through a lot right now! Concurrency and threading can be tricky, so don't worry if you're finding it a little challenging. I'm sure the community can help you out with this one. One thing I'd suggest is to look at the documentation of the language you're using and look at any examples they have of threading and concurrency.
Mar 26 '23 #2

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

Similar topics

0
by: Charlie Cosse | last post by:
Asymptopia BlackJack is written in Python and uses PyGame for multimedia. Version 1.2 contains both Windows and Linux install scripts. Asymptopia BlackJack is a full-featured casino-style...
9
by: nan.li.g | last post by:
Hello, all, I have an interesting problem about stl map and pthread on Linux and g++. The source code is as follows. //mt_map_test.cpp #include <string> #include <map> #include <unistd.h>...
4
by: Eric Westrom | last post by:
Does anyone know of some source code for a basic Blackjack game or some tips on how to deal with the displaying of the cards? Thanks.
1
by: blinkrebel | last post by:
Hello I need some help implementing the game of blackjack using the xturtle package. the instructions can be found at http://katie.luther.edu/moodle/file.php/2387/BlackJack.pdf and .gif's...
1
by: EXotiX | last post by:
hey im new to vb6 and I am making a blackjack game but can not randomize the array correctly. Could you help please Option Explicit Const NumItems As Integer = 53 Public Sub RandomizeCards()...
3
by: devilinthebox | last post by:
I am not really familar with Java and I need help with creating this simple Blackjack program. Here is a layout of how the program should output: If the computer has more than 16 it wins,...
7
by: devilinthebox | last post by:
I'm fairly new to java and need help with adding letters (J, Q, K, A) into the program and adding values for each. Thanks. // February 8, 2008 // The "BlackJack" class. import java.awt.*;...
30
by: imran akhtar | last post by:
i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong...
21
by: imran akhtar | last post by:
hi, i am making black jack code, but i am stuck, i have made start, but for reason gettin errors, which i dont seem be able to fix, below is my code wht i started. import random deck = *4...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.