/** * Checks if the last marble landed on the current player's playerCup * * @param buttonID */ protected boolean giveAnotherTurn(int buttonID) { if (player1.getTurn() && buttonID == 7 && areThereValidMoves(player1)) { return true; } if (player2.getTurn() && buttonID == 15 && areThereValidMoves(player2)) { return true; } return false; }
/** Initialise values of the variables needed for first Turn */ private void initialiseVariablesFirstTurn() { player1.setTurn(false); player2.setTurn(false); // The player who will have the first play firstID = -1; secondID = -1; firstHasPlayed = false; secondHasPlayed = false; isFirstTurn = true; }
protected void stealOppositeCup(int cupNumber) { PocketCup nextPocketCup = (PocketCup) board.getCups()[cupNumber]; PocketCup oppositeCup; if (player1.getTurn() && cupNumber < 7) { oppositeCup = (PocketCup) board.getCups()[cupNumber + ((7 - cupNumber) * 2)]; int oppositeCupNumbers = oppositeCup.emptyCup(); // take last marble from the cup alongside the opposite cup's one. nextPocketCup.addMarbles(-1); board.getPlayerCup1().addMarbles(oppositeCupNumbers + 1); } else if (player2.getTurn() && cupNumber > 7 && cupNumber < 15) { oppositeCup = (PocketCup) board.getCups()[(14 - cupNumber)]; int oppositeCupNumbers = oppositeCup.emptyCup(); // take last marble from the cup alongside the opposite cup's one. nextPocketCup.addMarbles(-1); board.getPlayerCup2().addMarbles(oppositeCupNumbers + 1); } }
/** * PressCup method only for the first turn of the game. after both players make their moves, the * one that clicked first is first to go * * @param id the id of the pressed cup */ public void firstTurnPlay(int id) { if (id < 7) { if (!secondHasPlayed) { player2.setTurn(true); } firstID = id; firstHasPlayed = true; } else { if (!firstHasPlayed) { player1.setTurn(true); } secondID = id; secondHasPlayed = true; } if (firstHasPlayed && secondHasPlayed) { isFirstTurn = false; applyFirstTurnChanges(firstID, secondID); } }
/** * Returns the names of both of the players and their score as an array. The order of the elements * is: The winner's name, his score, other player's name and finally the other player's name. * * @return array with the 2 players and their respective scores */ public String[] getFinalResults() { String winner; int winnerScore; String loser; int loserScore; if (checkWinner().equals(player1)) { winner = player1.getName(); winnerScore = board.getPlayerCup1Marbles(); loser = player2.getName(); loserScore = board.getPlayerCup2Marbles(); } else { winner = player2.getName(); winnerScore = board.getPlayerCup2Marbles(); loser = player1.getName(); loserScore = board.getPlayerCup1Marbles(); } String[] results = {winner, Integer.toString(winnerScore), loser, Integer.toString(loserScore)}; return results; }
/** This forces the switch of turns.It is called when one player doesn't have valid moves */ protected void switchTurn() { if (player1.getTurn() && areThereValidMoves(player2)) { player2.setTurn(true); player1.setTurn(false); } else if (player2.getTurn() && areThereValidMoves(player1)) { player1.setTurn(true); player2.setTurn(false); } }
/** * @param idCurrentCup * @param marblesFromEmptiedCup */ protected void putMarblesInNextCups(int idCurrentCup, int marblesFromEmptiedCup) { int cupNumber = idCurrentCup + 1; for (int i = 0; i < marblesFromEmptiedCup; i++) { if (cupNumber == 7 && player2.getTurn()) { cupNumber++; } else if (cupNumber == 15 && player1.getTurn()) { cupNumber = 0; } Cup nextPocketCup = board.getCups()[cupNumber]; // check at the last iteration if cup is empty if ((i == marblesFromEmptiedCup - 1) && nextPocketCup.isEmpty() && cupNumber != 7 && cupNumber != 15) { stealOppositeCup(cupNumber); } nextPocketCup.addMarbles(1); cupNumber++; // stay in the range of 16 cups. if (cupNumber > 15) { cupNumber = 0; } } }
/** * This method loops through the current player's half and determine if you can make a move If * there is no valid move, it switches the player to make a move */ protected boolean areThereValidMoves(Player player) { int cupIndex = 0; if (player.equals(player2)) { cupIndex += 8; } for (int i = cupIndex; i < 7 + cupIndex; i++) { // break if there is a valid move if (board.getCups()[i].getMarbles() > 0) { return true; } } return false; }