/* File: BasicManager.java - March 2018 */ package l02; import java.util.Arrays; /** * A basic manager the dice game. * @author Michael Albert */ public class BasicManager implements Manager { private static final int NO_ROLL = -1; private Player a; private Player b; private int[] aDice = new int[3]; private int[] bDice = new int[3]; private Player currentPlayer; private int oldRoll = NO_ROLL; private int currentRoll = NO_ROLL; public BasicManager(Player a, Player b) { this.a = a; this.b = b; } public int[] getMyDice(Player p) { if (p == a) { return aDice; } else { return bDice; } } public int[] getOppDice(Player p) { if (p == a) { return bDice; } else { return aDice; } } public void runGame() { // Generate the initial array of dice for each player for(int i = 0; i < aDice.length; i++) { aDice[i] = Die.roll(); bDice[i] = Die.roll(); } Arrays.sort(aDice); Arrays.sort(bDice); a.setManager(this); b.setManager(this); // Say hello System.out.println("Welcome to the game players " + a + " and " + b); System.out.println("The initial dice for " + a + " are " + Arrays.toString(aDice) ); System.out.println("The initial dice for " + b + " are " + Arrays.toString(bDice) ); // Loop currentPlayer = a; while (true) { // We will return from the middle of the loop at end of game // If old roll is available check if current player wants it boolean tookOldRoll = false; if (oldRoll != NO_ROLL) { System.out.println(currentPlayer + " is offered an old roll of " + oldRoll); int i = currentPlayer.processOldRoll(oldRoll); if (0 <= i && i < 3) { System.out.println("It is accepted and replaces die " + i); tookOldRoll = true; setDie(i, currentPlayer, oldRoll); oldRoll = NO_ROLL; } else { System.out.println("It is refused."); } } if (!tookOldRoll) { int newRoll = Die.roll(); System.out.println(currentPlayer + " is offered a new roll of " + newRoll); int i = currentPlayer.processNewRoll(newRoll); if (0 <= i && i < 3) { System.out.println("It is accepted and replaces die " + i); oldRoll = NO_ROLL; setDie(i, currentPlayer, newRoll); } else { System.out.println("It is refused."); oldRoll = newRoll; } } System.out.println(currentPlayer + "'s dice are: " + Arrays.toString(getDice(currentPlayer))); // End the game if over if (currentPlayer == a && isWinning(aDice) || currentPlayer == b && isWinning(bDice) ) { System.out.println("Congratulations " + currentPlayer + " you win!"); return; } // Switch players and repeat switchPlayers(); } // End of loop } public void setDie(int index, Player p, int value) { if (p == a) { aDice[index] = value; Arrays.sort(aDice); } else { bDice[index] = value; Arrays.sort(bDice); } } // Determines if a (sorted) set of dice is winning. If first and last are // equal then all three are equal and it wins, otherwise second should be // one bigger than first and third one bigger than second to win. private boolean isWinning(int[] d) { Arrays.sort(d); return (d[0] == d[2]) || (d[1] - d[0] == 1 && d[2] - d[1] == 1); } private void switchPlayers() { if (currentPlayer == a) { currentPlayer = b; } else { currentPlayer = a; } } private int[] getDice(Player p) { if (p == a) { return aDice; } else { return bDice; } } }