daily pastebin goal
79%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Game {
  2.     private Map<Player, Token> playerTokenMap;
  3.     private Player winner;
  4.     private final int lastSquareLocation = 101;
  5.     private Space[] board;
  6.     private final Player computer = new Player("COMPUTER");
  7.     private LinkedList<Player> turnOrder;
  8.     private int turnNumber;
  9.  
  10.     public Game(SpecialEnds specialEnds) {
  11.         playerTokenMap = new HashMap<Player, Token>();
  12.         turnOrder = new LinkedList<Player>();
  13.         board = new Space[lastSquareLocation];
  14.         for(int squareLocation = 1; squareLocation < lastSquareLocation; squareLocation++){
  15.             Space space = new Space();
  16.             if(specialEnds.leadsToAnotherSquareLocation(squareLocation))
  17.                 space.setDestination(specialEnds.getDestination(squareLocation));
  18.             board[squareLocation] = space;
  19.         }
  20.         this.winner = Player.NO_ONE;
  21.     }
  22.  
  23.     public void addPlayer(Player player) {
  24.         turnOrder.clear();
  25.         playerTokenMap.put(player, new Token());
  26.     }
  27.  
  28.     public int getPositionOfTokenOf(Player tokenOwner) {
  29.         return playerTokenMap.get(tokenOwner).getLocation();
  30.     }
  31.  
  32.     public void turnPlayerMove(int spaces) {
  33.         Player turnPlayer = getTurnPlayer();
  34.         Token token = playerTokenMap.get(turnPlayer);
  35.         if(tokenWillGoPastTheLastSquare(token, spaces))
  36.             return;
  37.         token.move(spaces);
  38.         int tokenLocation = token.getLocation();
  39.         if(hasSpecialEnd(tokenLocation))
  40.             token.move(this.board[tokenLocation].getDestination() - tokenLocation);
  41.         if(hasWonTheGame(token))
  42.             this.winner = turnPlayer;
  43.         turnNumber++;
  44.     }
  45.  
  46.     private boolean hasSpecialEnd(int squareLocationOfToken) {
  47.         return board[squareLocationOfToken].isYieldingToAnotherDestination();
  48.     }
  49.  
  50.     private boolean hasWonTheGame(Token token) {
  51.         return token.getLocation() == lastSquareLocation - 1;
  52.     }
  53.  
  54.     private boolean tokenWillGoPastTheLastSquare(Token token, int numberOfSpacesToMove) {
  55.         return token.getLocation() + numberOfSpacesToMove >  lastSquareLocation - 1;
  56.     }
  57.  
  58.     public Player getWinner() {
  59.         return winner;
  60.     }
  61.  
  62.     public Player getTurnPlayer() {
  63.         if(turnOrder.isEmpty())
  64.             return Player.NO_ONE;
  65.         return turnOrder.get(turnNumber % playerTokenMap.size());
  66.     }
  67.  
  68.     public void determinePlayOrder() {
  69.         SortedMap<Integer, Player> playOrderPlayerMap = new TreeMap<Integer, Player>();
  70.         for(Player player : playerTokenMap.keySet()){
  71.             int dieRollResult = player.rollDice();
  72.             if(!playOrderPlayerMap.containsKey(dieRollResult))
  73.                 playOrderPlayerMap.put(dieRollResult, player);
  74.             else{
  75.                 while(playOrderPlayerMap.containsKey(dieRollResult)){
  76.                     dieRollResult = player.rollDice();
  77.                 }
  78.                 playOrderPlayerMap.put(dieRollResult, player);
  79.             }
  80.         }
  81.         turnOrder.addAll(playOrderPlayerMap.values());
  82.         Collections.reverse(turnOrder);
  83.     }
  84.  
  85.     public void addComputerToGame() {
  86.         addPlayer(computer);
  87.     }
  88. }
  89.    
  90. public class Space {
  91.     private int destination;
  92.  
  93.     public Space() {
  94.         this.destination = -1;
  95.     }
  96.  
  97.     public void setDestination(int destination) {
  98.         this.destination = destination;
  99.     }
  100.  
  101.     public boolean isYieldingToAnotherDestination() {
  102.         return destination != -1;
  103.     }
  104.  
  105.     public int getDestination() {
  106.         return destination;
  107.     }
  108. }
  109.    
  110. public class SpecialEnds {
  111.     private Map<Integer, Integer> mapOfSpecialEnds;
  112.  
  113.     public SpecialEnds() {
  114.         mapOfSpecialEnds = new HashMap<Integer, Integer>();
  115.     }
  116.  
  117.     public void add(int source, int destination){
  118.         mapOfSpecialEnds.put(source, destination);
  119.     }
  120.  
  121.     public boolean leadsToAnotherSquareLocation(int squareLocation) {
  122.         return mapOfSpecialEnds.containsKey(squareLocation);
  123.     }
  124.  
  125.     public int getDestination(int squareLocation) {
  126.         return mapOfSpecialEnds.get(squareLocation);
  127.     }
  128. }
  129.    
  130. public class GameTest {
  131.     private Game game;
  132.     private Player player1;
  133.  
  134.     @Before
  135.     public void setUp() throws Exception {
  136.         SpecialEnds specialEnds = new SpecialEnds();
  137.         specialEnds.add(12, 2);
  138.         specialEnds.add(3, 13);
  139.         game = new Game(specialEnds);
  140.         player1 = new Player("Rome");
  141.         game.addPlayer(player1);
  142.         game.determinePlayOrder();
  143.     }
  144.  
  145.     @Test
  146.     public void tokenShouldStartOnSquareOne() throws Exception {
  147.         int expectedSquareLocation = 1;
  148.         assertPlayerIsIn(player1, expectedSquareLocation);
  149.     }
  150.  
  151.     @Test
  152.     public void tokenShouldBeInSquare4AfterMoving3SpacesFromSquare1() throws Exception {
  153.         int expectedSquareLocation = 4;
  154.         int numberOfSpacesToMove = 3;
  155.         turnPlayerMove(numberOfSpacesToMove);
  156.         assertPlayerIsIn(player1, expectedSquareLocation);
  157.     }
  158.  
  159.     @Test
  160.     public void tokenShouldBeInSquare8AfterMoving3SpacesThen4SpacesFromSquare1() throws Exception {
  161.         int expectedSquareLocation = 8;
  162.         int firstNumberOfSpacesToMove = 3;
  163.         int secondNumberOfSpacesToMove = 4;
  164.         turnPlayerMove(firstNumberOfSpacesToMove);
  165.         turnPlayerMove(secondNumberOfSpacesToMove);
  166.         assertPlayerIsIn(player1, expectedSquareLocation);
  167.     }
  168.  
  169.     private void turnPlayerMove(int spaces) {
  170.         game.turnPlayerMove(spaces);
  171.     }
  172.  
  173.     @Test
  174.     public void numberOfMovesShouldBeDecidedByDieRolls() throws Exception {
  175.         int dieResult = 1;
  176.         int expectedSquareLocation = 5;
  177.         while (dieResult != 4) {
  178.             dieResult = player1.rollDice();
  179.         }
  180.         turnPlayerMove(dieResult);
  181.         assertPlayerIsIn(player1, expectedSquareLocation);
  182.     }
  183.  
  184.     @Test
  185.     public void playerShouldWinTheGameWhenTheirTokenReachesThe100thSquare() throws Exception {
  186.         turnPlayerMove(96);
  187.         int numberOfSpacesToMove = 3;
  188.         turnPlayerMove(numberOfSpacesToMove);
  189.         assertEquals(player1, game.getWinner());
  190.     }
  191.  
  192.     @Test
  193.     public void playerShouldNotYetWinTheGameWhenTheirTokenMovementWillExceedThe100thSquare() throws Exception {
  194.         turnPlayerMove(96);
  195.         int numberOfSpacesToMove = 4;
  196.         turnPlayerMove(numberOfSpacesToMove);
  197.         assertNotEquals(player1, game.getWinner());
  198.     }
  199.  
  200.     @Test
  201.     public void tokenThatLandsOnASnakeHeadShouldGoDownOnTheTail() throws Exception {
  202.         int expectedSquareLocation = 2;
  203.         turnPlayerMove(11);
  204.         assertPlayerIsIn(player1, expectedSquareLocation);
  205.     }
  206.  
  207.     @Test
  208.     public void tokenThatLandsOnASnakeTailShouldStayThere() throws Exception {
  209.         int expectedSquareLocation = 2;
  210.         turnPlayerMove(1);
  211.         assertPlayerIsIn(player1, expectedSquareLocation);
  212.     }
  213.  
  214.     @Test
  215.     public void tokenThatLandsOnLowerEndOfTheLadderShouldGoUpTheLadder() throws Exception {
  216.         int expectedSquareLocation = 13;
  217.         turnPlayerMove(2);
  218.         assertPlayerIsIn(player1, expectedSquareLocation);
  219.     }
  220.  
  221.     @Test
  222.     public void tokenThatLandsOnUpperEndOfTheLadderShouldStayThere() throws Exception {
  223.         int expectedSquareLocation = 13;
  224.         turnPlayerMove(12);
  225.         assertPlayerIsIn(player1, expectedSquareLocation);
  226.     }
  227.  
  228.     @Test
  229.     public void player1ShouldBeFirstIfPlayer1RollsHigherThanPlayer2() throws Exception {
  230.         Player player2 = new Player("Ein");
  231.         game.addPlayer(player2);
  232.         while (isNotFirstToRoll(player1))
  233.             game.determinePlayOrder();
  234.         assertTurnPlayerIs(player1);
  235.     }
  236.  
  237.     @Test
  238.     public void player2ShouldBeFirstIfPlayer2RollsHigherThanPlayer1() throws Exception {
  239.         Player player2 = new Player("Ein");
  240.         game.addPlayer(player2);
  241.         while (isNotFirstToRoll(player2))
  242.             game.determinePlayOrder();
  243.         assertTurnPlayerIs(player2);
  244.     }
  245.  
  246.     private void assertTurnPlayerIs(Player player2) {
  247.         assertEquals(player2, game.getTurnPlayer());
  248.     }
  249.  
  250.     @Test
  251.     public void player2ShouldGoNextAfterPlayer1() throws Exception {
  252.         Player player2 = new Player("Ein");
  253.         game.addPlayer(player2);
  254.         while (isNotFirstToRoll(player1))
  255.             game.determinePlayOrder();
  256.         turnPlayerMove(2);
  257.         assertTurnPlayerIs(player2);
  258.     }
  259.  
  260.     @Test
  261.     public void player1ShouldGoNextAfterPlayer2GoingAfterPlayer1() throws Exception {
  262.         Player player2 = new Player("Ein");
  263.         game.addPlayer(player2);
  264.         while (isNotFirstToRoll(player1))
  265.             game.determinePlayOrder();
  266.         turnPlayerMove(2);
  267.         turnPlayerMove(2);
  268.         assertTurnPlayerIs(player1);
  269.     }
  270.  
  271.     private boolean isNotFirstToRoll(Player player) {
  272.         return !player.equals(game.getTurnPlayer());
  273.     }
  274.  
  275.     @Test
  276.     public void computerShouldGoFirstIfComputerRollsHigherThanPlayer1() throws Exception {
  277.         Player expectedTurnPlayer = new Player("COMPUTER");
  278.         game.addComputerToGame();
  279.         while (isNotFirstToRoll(expectedTurnPlayer))
  280.             game.determinePlayOrder();
  281.         assertTurnPlayerIs(expectedTurnPlayer);
  282.     }
  283.  
  284.     @Test
  285.     public void computerShouldBeAbleToRollTheDie() throws Exception {
  286.         int expectedSquareLocation = 2;
  287.         Player computer = new Player("COMPUTER");
  288.         game.addComputerToGame();
  289.         while (isNotFirstToRoll(computer))
  290.             game.determinePlayOrder();
  291.         turnPlayerMove(1);
  292.         assertPlayerIsIn(computer, expectedSquareLocation);
  293.     }
  294.  
  295.     private void assertPlayerIsIn(Player computer, int expectedSquareLocation) {
  296.         assertTrue(game.getPositionOfTokenOf(computer) == expectedSquareLocation);
  297.     }
  298. }
RAW Paste Data
Top