Пример #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());
  }
Пример #2
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;
  }