473,396 Members | 1,676 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,396 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
2 27647
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
BraidenMaynard
2 2Bits
While I haven't dived into this specific scenario, I've been tinkering with Java concurrency for a while now, and it always feels like threading a needle – avoiding those deadlocks and race conditions can get tricky!

Thinking about this Blackjack game, the key might be separating the game logic from the player interaction. Maybe one thread handles the core mechanics (dealing cards, shuffling the deck), while another chills in the waiting area, ready to spring into action when a player makes a decision (hit, stay, etc.). This way, the game keeps moving smoothly without getting hung up on user input.

Of course, there's always the wild card of online casinos (not saying that's where this is headed, but places like www.crypto-casinos.com definitely deal with concurrency issues). In those high-stakes environments, you gotta make sure everything is watertight to avoid glitches and ensure fair play.
1 Week Ago #3

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.