/** It enables only the pits from the player whose turn it is */ void enableActiveSide() { boolean enableNorth; boolean enableSouth; if (usersSide == null) { // sideTheUserIs is not yet initialized enableNorth = false; enableSouth = false; } else if (state.getWhoseTurn().isNorth() && usersSide.isNorth()) { enableNorth = true; enableSouth = false; } else if (state.getWhoseTurn().isNorth() && usersSide.isSouth()) { enableNorth = false; enableSouth = false; } else if (state.getWhoseTurn().isSouth() && usersSide.isSouth()) { enableNorth = false; enableSouth = true; } else { enableNorth = false; enableSouth = false; } for (int i = 0; i < state.getNorthPits().length - 1; i++) { graphics.setPitEnabled(PlayerColor.N, i, enableNorth); graphics.setPitEnabled(PlayerColor.S, i, enableSouth); } }
/** When there are zero seeds in a pit it can't be chosen either so disable them */ void disableZeroSeedPits() { int[] activePits = new int[7]; if (state.getWhoseTurn().isNorth()) activePits = state.getNorthPits(); else activePits = state.getSouthPits(); // state.getNorthPits().length-1 because the last array field is the // treasure chest for (int i = 0; i < state.getNorthPits().length - 1; i++) { if (activePits[i] == 0) graphics.setPitEnabled(state.getWhoseTurn(), i, false); } }
/** * Updates all elements that are necessary after the state changed 1. Update all the seedAmounts * in the pits after a move was made 2. It enables only the pits from the player whose turn it is * 3. When there are zero seeds in a pit it can't be chosen either so disable them 4. Set a * message in the case of game over */ void updateBoard() { if (usersSide == null) graphics.setTurnLabelText(messages.startNewGame()); else { graphics.setTurnLabelText( state.getWhoseTurn().equals(usersSide) ? messages.itsYourTurn() : messages.opponentsTurn()); } updatePits(); enableActiveSide(); disableZeroSeedPits(); message(); }
/** * After the user clicked on a pit the seeds should be distributed in an animated fashion * * @param chosenPitIndex the index the user chose to distribute the seeds from * @param oldState the state before the user chose his pit */ void makeAnimatedMove(int chosenPitIndex, State oldState) { // disable board until the animation is over disableBoard(); state.makeMove(chosenPitIndex); PlayerColor whoseTurn = oldState.getWhoseTurn(); PlayerColor sideToPlaceSeedOn = whoseTurn; int seedAmount = oldState.getPitsOfWhoseTurn()[chosenPitIndex]; boolean lastAnimation = false; int indexToPlaceSeedIn = chosenPitIndex; int maxIndex = 6; for (int i = 1; i <= seedAmount; i++) { indexToPlaceSeedIn++; maxIndex = whoseTurn.equals(sideToPlaceSeedOn) ? 6 : 5; if ((indexToPlaceSeedIn) > maxIndex) { sideToPlaceSeedOn = sideToPlaceSeedOn.getOpposite(); indexToPlaceSeedIn = 0; } if (i == seedAmount) lastAnimation = true; graphics.animateFromPitToPit( whoseTurn, chosenPitIndex, sideToPlaceSeedOn, indexToPlaceSeedIn, 400 * (i - 1), lastAnimation); } if (this.state.getLastMoveWasOppositeCapture()) { // graphics.oppositeCaptureSound(); // TODO: give this it's own animation // //int[] opposingPits = whoseTurn.isNorth() ? // this.state.getSouthPits() : state.getNorthPits(); // int seedAmountInOpposingPit = this.state.getOppositeSeeds(); // // graphics.animateFromPitToPit(whoseTurn, indexToPlaceSeedIn, // whoseTurn, 6, seedAmount * 400 + 1400); // for(int i = 0; i < seedAmountInOpposingPit; i++) // graphics.animateFromPitToPit(whoseTurn.getOpposite(), // State.getMirrorIndex(indexToPlaceSeedIn, 5), whoseTurn, 6, // seedAmount * 400 + 1000 + 400 * i); } }