Problem with concurrency and a blackjack game.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Z E R O
    New Member
    • Feb 2008
    • 1

    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.

    Code:
    public class ComputerPlayer extends Player implements Runnable
    {
    	public ComputerPlayer(Dealer dealer)
    	{
    		first = null;
    		last = null;
    		this.dealer = dealer;
    		playerName = "Computer Player: Player - " + playerNumber;
    		thisPlayersNumber = playerNumber;
    		playerNumber++;
    	}
    	
    	public String getPlayerName()
    	{
    		return playerName;
    	}
    	
    	public synchronized void takeTurn()
    	{
    		while(thisPlayersNumber != dealer.getCurrentPlayerNumber())
    		{
    			try 
    			{
    				System.out.println(this.playerName + " is waiting.");
    				wait();
    				System.out.println(this.playerName + " entering running state.");
    			} 
    			catch (InterruptedException e) 
    			{
    			}
    		}
    		
    		boolean hit = true;
    		
    		for(short count = 0; count < 2; count++)
    		{
    			addCard(dealer.dealCard());
    		}
    		
    		System.out.println(getPlayerName());
    		System.out.println(showCards());
    		System.out.println(getHandValue());
    		System.out.println();
    		
    		if(getHandValue() >= 17)
    		{
    			hit = false;
    		}
    		
    		while(hit)
    		{			
    			addCard(dealer.dealCard());
    			
    			System.out.println(getPlayerName());
    			System.out.println(showCards());
    			System.out.println(getHandValue());
    			System.out.println();
    			
    			if(getHandValue() >= 17)
    			{
    				hit = false;
    				
    				if(getHandValue() == 21)
    				{
    					System.out.println("Winner: 21!");
    					System.out.println();
    				}
    				else if(getHandValue() > 21)
    				{
    					System.out.println("Busted!");
    					System.out.println();
    				}
    			}
    		}
    		
    		System.out.println(this.playerName + " is updating player number.");
    		dealer.updateCurrentPlayerNumber();
    		
    		notifyAll();
    	}
    
    	public void run() 
    	{
    		takeTurn();
    	}
    }
  • FatimaWalker
    New Member
    • Mar 2023
    • 1

    #2
    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.

    Comment

    • BraidenMaynard
      New Member
      • Jul 2023
      • 2

      #3
      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.

      Comment

      Working...