/** * Determines whether or not if the game has ended. * * <p>Game will end when either there are no empty spaces left or if neither player can make a * legitimate move in the empty spaces remaining. * * <p>This method will shift the game into a "critical stage" when the number of empty spaces left * on the board is less than the half of the initial number of empty spaces. * * <p>In the critical stage, every time a move has been made, the method will check to see if * either player still has legitimate moves remaining. If both players have legitimate moves * remaining, nothing is done. If only one player has legitimate moves remaining, it becomes the * "currentPlayer_" If no players have legitimate moves remaining, the game is ended. */ public void checkEnd() { if (emptySpacesLeft_ == 0) { int winner = 0; if (p1Score_ == p2Score_) { screen_.endGame(winner); } else { winner = p1Score_ > p2Score_ ? P1_ : P2_; screen_.endGame(winner); } } else // if( emptySpacesLeft_ <= (totalMovesPossible_)/2 )//Enters the "critical stage" { int otherPlayer = currentPlayer_ == 1 ? 2 : 1; if (!canStillPlay(currentPlayer_)) // If the current player has no more moves { if (canStillPlay(otherPlayer)) // And the other player does { switchPlayers(); screen_.forcedSwitchPlayers(); // Makes the interface change the currently active player } else // The other player has no moves either { int winner = 0; if (p1Score_ == p2Score_) { screen_.endGame(winner); } else { winner = p1Score_ > p2Score_ ? P1_ : P2_; screen_.endGame(winner); } } } } }
/** * Changes the "color" of a single piece to that of the current player. * * <p>Additionally, will call upon the screen to also make the appropriate changes. For example, * if a piece was changed to 1, the screen must also change the color of that particular * piece/space to the color representative of 1. * * <p>Will also update the score accordingly. * * @param x The x coordinate of the space * @param y The y coordinate of the space */ public void claimSpaceForCurrentPlayer(int x, int y) { changeScores(x, y); board_[x][y] = currentPlayer_; if (currentPlayer_ == P1_) { screen_.claimForPlayerOneVisually(x, y); } else { screen_.claimForPlayerTwoVisually(x, y); } }
/** * Updates the score. * * <p>In addition, this method will also call upon the interface to update the scores being * visually displayed. * * <p>This method will assume that the space described by the coordinates given is being claimed * by the current player. Thus it will only make decisions based on the status of the said space * prior to said claim. For example, if player one was claiming an empty space, this method will * add one to player one's score and do nothing else. However, if the space being claimed had * belonged to player two, this method will add one to player one's score and take one away from * player two's. * * <p>That being said, this method should not be called upon by itself, it is meant to only be * used as a part of the "claimSpace" method. * * @param x The x coordinate of the space. * @param y The y coordinate of the space. */ public void changeScores(int x, int y) { if (currentPlayer_ == P1_) // Player 1 is in play { if (board_[x][y] == EMPTY_SPACE_) { p1Score_++; } else { p1Score_++; p2Score_--; } } else // Player 2 is in play { if (board_[x][y] == EMPTY_SPACE_) { p2Score_++; } else { p2Score_++; p1Score_--; } } screen_.updateScores(p1Score_, p2Score_); }
/** * Switches the player currently in play. Switches to player 2 if player 1 is in play and vice * versa. * * <p>Also calls upon the interface to change the indicator displaying which player is the current * player. */ public void switchPlayers() { currentPlayer_ = currentPlayer_ == 1 ? 2 : 1; // Switches the current player screen_.switchPlayers(); }