Write a Java Program to solve the following synchronization

Write a Java Program to solve the following synchronization problem.

Problem description: In the ‘Ace-King-Queen’ card game three players seat at a table. One player plays only with ‘Aces’, another plays only with ‘Kings’, and the third one plays with ‘Queen’ cards only. Each of the players can get a replacement of his own card as long as s/he wants to play (that is, continuous supply of the cards). A dealer agent deals randomly (using a dealing machine) any two of these three cards. The player with the third card places his card on the table and completes the ‘Ace-King-Queen’ group and wins the current deal.

Execution: User is not expected to supply any input. The user will simply compile and run your program. A sample output could be like the following (we assume the game continues infinitely). There should be a short delay between each round (deal) of the game.
Game continues... Dealer places \'Ace\' and \'Queen\' on the table. Player with \'King\' places his card on the table. Player with \'King\' wins the current deal.

Game continues... Dealer places \'Ace\' and \'King\' on the table. Player with \'Queen\' places his card on the table. Player with \'Queen\' wins the current deal.

Game continues... Dealer places \'King\' and \'Queen\' on the table. Player with \'Ace\' places his card on the table. Player with \'Ace\' wins the current deal.

Game continues... Dealer places \'Ace\' and \'Queen\' card on the table. Player with \'King\' wins the current deal. Game continues... …

[Hint: you may view each player as a separate thread. The dealer agent can be a separate thread as well. When the thread runs it is indicated by corresponding message. Each thread is selected based on the two cards placed on the table by the dealer agent. The agent randomly selects two cards out of Ace, King, and Queen to place on the table. You may use Thread.yield() and Thread.sleep() methods as needed. ]

Solution

Answer :-

import java.util.*;

import java.io.*;

public class Card
{
public static String Deck [] =
{
\"Ace\",\"King\",\"Queen\"
};
public static int randomInt(int min, int max)
{
Random random = new Random();
int randomNum = random.nextInt((max-min)+1)+min;
return randomNum;
}

public static void main(String[] args)
{
Card game = new Card();
int dealerFirstCard;
int dealerSecondCard;
int p1;
int p2;
int p3;
do
{
dealerFirstCard = Card.randomInt(0,2);
dealerSecondCard =Card.randomInt(0,2);
}
while(dealerFirstCard==dealerSecondCard);

do
{
p1 = Card.randomInt(0,2);
p2 = Card.randomInt(0,2);
p3 = Card.randomInt(0,2);
}
while(p1==p2 || p2==p3 || p1==p3);
System.out.println();
System.out.println(\"Dealer places \"+ Deck[dealerFirstCard] + \" and \"+ Deck[dealerSecondCard]);
  
System.out.println(\"p1 has\"+ Deck[p1] + \"\ \"+ \"p2 has\"+ Deck[p2] + \"\ \"+\"p3 has\"+ Deck[p3]+\"\ \");

}
}

Write a Java Program to solve the following synchronization problem. Problem description: In the ‘Ace-King-Queen’ card game three players seat at a table. One p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site