import java.util.*; public class Game { //fields public static int numPlayers = 5; //the number of players that started in the game. public static int playersActive = 5; //the number of players still in the game. private static int initialChips = 50; //this is the number of chips per player per game. public static DealerIMPL dealer = new DealerIMPL(); //the dealer to be used throughout the game. public static Object gameOver = new Object(); //lock object on if the game is over. public static int gameNumber = 1; public static Object readyForNewGame = new Object(); //monitor for when the players are ready for a new game. public static ArrayList players = new ArrayList(); //create the array list fo rhte players. //constructor public static void main (String args[]) throws InterruptedException { //create the inital deck. dealer.newHand(); //DEBUGING INFO System.out.println("New deck created."); //create the players threads for(int i=0; i < numPlayers; i++) { PlayerIMPL player = new PlayerIMPL(initialChips, i, players); players.add(player); } //start all of the threads for(int r=0; r < numPlayers; r++) { Thread playerThread = new Thread(players.get(r)); playerThread.start(); } //wait for the game to be over... when it is run this stuff. newGame(); } public static void newGame() { //ready for a new game, is everyone else?? synchronized(readyForNewGame) { //not every player is ready for a new game. while(PlayerIMPL.numberForNewGame < 5) { try { readyForNewGame.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } gameNumber++; dealer.newHand(); //reset the counter. PlayerIMPL.numberForNewGame = 0; } newGame(); } } import java.util.ArrayList; public class PlayerIMPL implements Player, Runnable { //fields private ArrayList players; //the array of players private int chips = 0; //the player's number of chips. private int playerNumber; //the player's number. private int numCards; //current number of cards given. private Card[] hand = new Card[Card.HAND_SIZE]; //the player's hand of cards. private int[] payment = new int[Game.numPlayers ]; //total of payments you owe. public static int numberForNewGame = 0; //number of players ready for a new game //monitors private Object readyToCompare = new Object(); //monitor for having all of the threads ready to compute. private Object myChipsLock = new Object(); //monitor for my chips private Object theirChipsLock = new Object(); //monitor for the other player's chips. private Object newGame = new Object(); //monitor for creating the new game. //constructor public PlayerIMPL(int chips, int playerNumber, ArrayList players) { this.players = players; this.chips = chips; this.playerNumber = playerNumber + 1; } //threaded method. public void run() { while(Game.numPlayers > 1) { int totalPayments = 0; //this will be used to get the total that the players paid out. //Get the cards for all of the players. for (int i=0; i < Card.HAND_SIZE; i++) { synchronized (readyToCompare) { hand[i] = Game.dealer.drawCard(); numCards++; readyToCompare.notifyAll(); } } //compare the hands for(int x=0; x < Game.numPlayers; x++) { if (x+1 != playerNumber) { //get the totals. PlayerIMPL opponent = players.get(x); payment[x] = opponent.compareHands(hand); //pay your opponent if you lost. (negative means the player lost) if (payment[x] < 0) { payment[x] *= -1; //make the amounts paid positive. //remove from your stack synchronized(myChipsLock) { chips -= payment[x]; } //add to their stack. opponent.receivePayment(payment[x]); //add to the total paid out totalPayments += payment[x]; } } } //determine if the player is the winner String output = Game.gameNumber + " " + playerNumber + " " + Card.toString(hand) + " " + chips; if(totalPayments == 0) { output += "*"; } //output the final game info. System.out.println(output); //notify to start a new Game. synchronized(Game.readyForNewGame) { PlayerIMPL.numberForNewGame++; //update that there is yet another one ready. Game.readyForNewGame.notifyAll(); } } } public int compareHands(Card[] opponentsCards) { //wait until everony has a card to cp synchronized (readyToCompare) { //wait until every player has their cards. while (numCards < Card.HAND_SIZE) { try { readyToCompare.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //return the amount of cash that is owed. return Card.compareHands(opponentsCards, hand); } } public boolean hasRetired() { Game.playersActive--; Game.gameOver.notify(); return false; } public void receivePayment(int payment) { synchronized(theirChipsLock) { //give the payment to the opponent. chips = chips + payment; } } }