Exemplo n.º 1
0
  private void takeTurnForPlayer(Player player) {
    ArrayList<Integer> validMoves = new ArrayList<>(mBoard.getWidth());
    // Iterate through each column
    for (int i = 0; i < mBoard.getWidth(); i++) {
      // Check if column has empty spaces available
      if (mBoard.get(0, i).getValue() == BoardSpace.Value.Empty) {
        // If so, add it to valid moves
        validMoves.add(i);
      }
    }

    // Choose a random available column
    Random random = new Random();
    int moveToTake = validMoves.get(random.nextInt(validMoves.size()));
    mBoard.addPiece(moveToTake, player.getSymbol());
  }
Exemplo n.º 2
0
  public void start() {
    Logger logger = new ConsoleLogger();
    logger.print("Welcome to Connect 4\n");
    logger.print("\n");

    // Set game state to STARTED
    mGameState = GameState.STARTED;
    mCurrentPlayer = 0;
    printBoard();

    // Continuously take turns while the game is incomplete
    while (mGameState != GameState.COMPLETE) {
      // If no more moves are available, set game state to complete and break out of loop
      if (mBoard.isFull()) {
        mGameState = GameState.COMPLETE;
        continue;
      }

      // Otherwise, take turn for current player
      Player currentPlayer = mPlayers.get(mCurrentPlayer);
      takeTurnForPlayer(currentPlayer);

      // Print the current state of the board
      printBoard();

      // If someone won, set the game state to complete
      mWinner = getWinner();
      if (mWinner != null) {
        mGameState = GameState.COMPLETE;
        continue;
      }

      // Otherwise, set the next player in line
      mCurrentPlayer++;
      if (mCurrentPlayer >= mNumberOfPlayers) mCurrentPlayer = 0;

      // Amateur technique for pausing application, so we can more easily see the game progress
      try {
        Thread.sleep(TURN_DELAY);
      } catch (InterruptedException e) {
        mLogger.error(e.getMessage());
        e.printStackTrace();
      }
    }

    showResult();
  }
Exemplo n.º 3
0
 private void printBoard() {
   mLogger.print(mBoard.toString());
   mLogger.print("\n");
 }
Exemplo n.º 4
0
  /**
   * @return The value of spaces with at least four matching in a row, horizontally, vertically, or
   *     diagonally
   */
  private BoardSpace.Value getWinner() {
    // Check every column
    for (int column = 0; column < mBoard.getWidth(); column++) {
      BoardSpace.Value previousValue = BoardSpace.Value.Empty;
      BoardSpace.Value currentValue;
      int consecutivePiecesOfSameValue = 1;
      for (int row = 0; row < mBoard.getHeight(); row++) {
        currentValue = mBoard.get(row, column).getValue();
        if (currentValue == BoardSpace.Value.Empty || currentValue != previousValue) {
          consecutivePiecesOfSameValue = 1;
        } else { // We have a streak
          consecutivePiecesOfSameValue++;
          // Check if there are at least four in a row
          if (consecutivePiecesOfSameValue >= 4) {
            // Found a winner
            return currentValue;
          }
        }
        previousValue = currentValue;
      }
    }

    // Check every row
    for (int row = 0; row < mBoard.getHeight(); row++) {
      BoardSpace.Value previousValue = BoardSpace.Value.Empty;
      BoardSpace.Value currentValue;
      int consecutivePiecesOfSameValue = 1;
      for (int column = 0; column < mBoard.getWidth(); column++) {
        currentValue = mBoard.get(row, column).getValue();
        if (currentValue == BoardSpace.Value.Empty || currentValue != previousValue) {
          consecutivePiecesOfSameValue = 1;
        } else { // We have a streak
          consecutivePiecesOfSameValue++;
          // Check if there are at least four in a row
          if (consecutivePiecesOfSameValue >= 4) {
            // Found a winner
            return currentValue;
          }
        }
        previousValue = currentValue;
      }
    }

    // Check diagonals from top-left to bottom-right
    // Check for each diagonal from the left column
    for (int row = 0; row < mBoard.getHeight(); row++) {
      BoardSpace.Value previousValue = BoardSpace.Value.Empty;
      BoardSpace.Value currentValue;
      int consecutivePiecesOfSameValue = 1;
      int checkSpaceRow = row;
      int checkSpaceColumn = 0;
      while (checkSpaceRow < mBoard.getHeight() && checkSpaceColumn < mBoard.getWidth()) {
        currentValue = mBoard.get(checkSpaceRow, checkSpaceColumn).getValue();
        if (currentValue == BoardSpace.Value.Empty || currentValue != previousValue) {
          consecutivePiecesOfSameValue = 1;
        } else { // We have a streak
          consecutivePiecesOfSameValue++;
          // Check if there are at least four in a row
          if (consecutivePiecesOfSameValue >= 4) {
            // Found a winner
            return currentValue;
          }
        }
        previousValue = currentValue;
        checkSpaceRow++;
        checkSpaceColumn++;
      }
    }
    // Check for each diagonal from the top row
    for (int column = 0; column < mBoard.getHeight(); column++) {
      BoardSpace.Value previousValue = BoardSpace.Value.Empty;
      BoardSpace.Value currentValue;
      int consecutivePiecesOfSameValue = 1;
      int checkSpaceRow = 0;
      int checkSpaceColumn = column;
      while (checkSpaceRow < mBoard.getHeight() && checkSpaceColumn < mBoard.getWidth()) {
        currentValue = mBoard.get(checkSpaceRow, checkSpaceColumn).getValue();
        if (currentValue == BoardSpace.Value.Empty || currentValue != previousValue) {
          consecutivePiecesOfSameValue = 1;
        } else { // We have a streak
          consecutivePiecesOfSameValue++;
          // Check if there are at least four in a row
          if (consecutivePiecesOfSameValue >= 4) {
            // Found a winner
            return currentValue;
          }
        }
        previousValue = currentValue;
        checkSpaceRow++;
        checkSpaceColumn++;
      }
    }

    // Check diagonals from top-right to bottom-left
    // Check for each diagonal from the right column
    for (int row = 0; row < mBoard.getHeight(); row++) {
      BoardSpace.Value previousValue = BoardSpace.Value.Empty;
      BoardSpace.Value currentValue;
      int consecutivePiecesOfSameValue = 1;
      int checkSpaceRow = row;
      int checkSpaceColumn = mBoard.getWidth() - 1;
      while (checkSpaceRow < mBoard.getHeight() && checkSpaceColumn >= 0) {
        currentValue = mBoard.get(checkSpaceRow, checkSpaceColumn).getValue();
        if (currentValue == BoardSpace.Value.Empty || currentValue != previousValue) {
          consecutivePiecesOfSameValue = 1;
        } else { // We have a streak
          consecutivePiecesOfSameValue++;
          // Check if there are at least four in a row
          if (consecutivePiecesOfSameValue >= 4) {
            // Found a winner
            return currentValue;
          }
        }
        previousValue = currentValue;
        checkSpaceRow++;
        checkSpaceColumn--;
      }
    }
    // Check for each diagonal from the top row
    for (int column = 0; column < mBoard.getWidth(); column++) {
      BoardSpace.Value previousValue = BoardSpace.Value.Empty;
      BoardSpace.Value currentValue;
      int consecutivePiecesOfSameValue = 1;
      int checkSpaceRow = 0;
      int checkSpaceColumn = column;
      while (checkSpaceRow < mBoard.getHeight() && checkSpaceColumn >= 0) {
        currentValue = mBoard.get(checkSpaceRow, checkSpaceColumn).getValue();
        if (currentValue == BoardSpace.Value.Empty || currentValue != previousValue) {
          consecutivePiecesOfSameValue = 1;
        } else { // We have a streak
          consecutivePiecesOfSameValue++;
          // Check if there are at least four in a row
          if (consecutivePiecesOfSameValue >= 4) {
            // Found a winner
            return currentValue;
          }
        }
        previousValue = currentValue;
        checkSpaceRow++;
        checkSpaceColumn--;
      }
    }

    // If we didn't find a winner, return null
    return null;
  }