/*** * * Game * ****/ 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 ArrayList player = new ArrayList(); //create the array list fo rhte players. //constructor public static void main (String args[]) throws InterruptedException { //create the inital deck. dealer.newHand(); System.out.println("New deck created."); //create the players threads for(int i=0; i < numPlayers; i++) { player.add(new Thread(new PlayerIMPL(initialChips, i))); player.get(i).start(); System.out.println("Player " + i + " created."); } //wait for the game to be over... when it is run this stuff. //gameOver(); } /** * the game has finished. * @throws InterruptedException */ public static void gameOver() throws InterruptedException{ synchronized (gameOver) { //make the thread wait until the game has been finished. while (playersActive > 1) { gameOver.wait(); } //TODO write the code for when the game is finished. } } } import java.util.ArrayList; public class DealerIMPL implements Dealer { //fields ArrayList deck = new ArrayList (); //dealer //constructor public DealerIMPL(){} //draw a card. public synchronized Card drawCard() { //get the card from the top of the deck, and remove it from the deck. Card card = deck.get(0); deck.remove(0); //give the card to the player. return card; } public void newHand() { deck = Card.newDeck(); } } /*** * *PlayerIMPL * ****/ import java.util.ArrayList; public class PlayerIMPL implements Player, Runnable { //fields private int chips = 0; //the player's number of chips. private int playerNumber; //the player's number. public Card[] hand = new Card[Card.HAND_SIZE]; //the player's hand of cards. private static int playersWithCards = 0; //the number of cards that have all the card that they need. public static Object readyToCompare = new Object(); //monitor for having all of the threads ready to compute. //constructor public PlayerIMPL(int chips, int playerNumber) { this.chips = chips; this.playerNumber = playerNumber + 1; //System.out.println("player " + playerNumber + " created"); } //threaded method. public void run() { //Get the cards for all of the players. for (int i=0; i < Card.HAND_SIZE; i++) { synchronized(Game.dealer) { hand[i] = Game.dealer.drawCard(); } } //Compare the cards synchronized (readyToCompare) { playersWithCards++; readyToCompare.notifyAll(); //wait until every player has their cards. while (playersWithCards < Game.numPlayers) { try { readyToCompare.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //compare the cards String handString = ""; for (int i=0; i< Game.numPlayers; i++) handString += "[" + hand[i] + "] "; System.out.println("player " + playerNumber + ": " + handString); //compare hands compareHands(hand); //TODO write the code for when the game is finished. } } public int compareHands(Card[] cards) { Game.player.get(0).getId(); return 0; //make a new deck when finished with all cards. } public boolean hasRetired() { Game.playersActive--; Game.gameOver.notify(); return false; } public void receivePayment(int payment) { //add the payment to the chips currently owned. chips = chips + payment; } }