473,517 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with concurrency and a blackjack game.

1 New Member
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 32514
FatimaWalker
1 New Member
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 New Member
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.
4 Weeks Ago #3

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

Similar topics

0
2135
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 blackjack game written in Python using PyGame for graphics. You play with up to 6 robots against the dealer. You can specify which player is you. It is...
9
7976
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> #include <sys/types.h>
4
2205
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
2078
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 for the cards can be found at http://katie.luther.edu/moodle/file.php/2387/cards.zip
1
1966
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() Dim i As Integer Dim j As Integer Dim tmp As Integer
3
6176
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, or something. I didn't really get what the teacher was saying. If you know how blackjack is played, you might understand. Please don't ask any...
7
6002
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.*; import hsa.Console; import java.util.Random; // Only the Random class import java.util.*; // All classes in the java.util package public class...
30
9071
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 with it. that will be great. thereis an attched file, to see the code more cleaer. from random import choice as randomcards def total(hand): ...
21
4174
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 player= computer= #r = random.randint computer.append(random.randint(deck))
0
7295
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7197
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5120
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4786
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3277
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
833
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.