Beispiel #1
0
  /**
   * Checks whether the position is located on the playing area or the outer dots.
   *
   * @param p the position of which should be determined whether it is on the board
   */
  public boolean isPositionOnPlayAreaOrOuterDots(Position p) {
    int col = p.getColName() - 'a' + 1;
    int row = p.getRowNumber();

    // See google doc for explanation of the formula
    return !(row <= 0 || col >= 10 || row + col >= 15 || col - row <= -5 || col <= 0);
  }
Beispiel #2
0
  /**
   * Checks whether the position is located on the inner board (the playing area). Returns false for
   * positions on the outer positions, as well as positions that are not on the board.
   *
   * <p>By Leroy
   *
   * @param p position of which is to be determined whether the position is located on the inner
   *     board
   */
  private boolean isOnInnerBoard(Position p) {
    int col = p.getColName() - 'a' + 1;
    int row = p.getRowNumber();

    // See google doc for explanation of the formula
    return !(row <= 1 || col >= 9 || row + col >= 14 || col - row <= -4 || col <= 1);
  }
Beispiel #3
0
  /** By Dingding */
  private Set<Line.Segment> getRemovableLineSegments(
      Map<Position, Piece> pieceMap, PieceColor pieceColor) {
    Set<Line.Segment> removableLines = new HashSet<>();
    Set<Line> linesOnTheBoard =
        Line.getLinesOnTheBoard(
            this); // Get all the possible lines on the board. Positions don't need to be occupied.

    for (Line line : linesOnTheBoard) {
      Position currentPosition = line.getStartPosition();
      Position startOfSegment = null;
      Position endOfSegment = null;
      Direction direction = line.getDirection();
      int consecutivePieces =
          0; // We start at a dot position, so we can assume that we don't start in a set of
             // consecutive pieces
      boolean isInLineSegment = false;

      // Break the for-loop if an endOfSegment has been found (because the largest lines only have 7
      // positions on the board, there
      // can't be more than one set of four pieces of the same color (requiring at least 9
      // positions) on the board.
      for (;
          endOfSegment == null && isPositionOnPlayAreaOrOuterDots(currentPosition);
          currentPosition = currentPosition.next(direction)) {
        PieceColor currentPieceColor =
            pieceMap.containsKey(currentPosition)
                ? pieceMap.get(currentPosition).getPieceColor()
                : null;

        // Update the consecutivePieces
        if (currentPieceColor == pieceColor) consecutivePieces++;
        if (consecutivePieces == 4) isInLineSegment = true;
        if (currentPieceColor != pieceColor) consecutivePieces = 0;

        if (isInLineSegment) {
          if (isDotPosition(currentPosition) || currentPieceColor == null) {
            endOfSegment = currentPosition.previous(direction);
          }
        }

        // Update the startOfSegment if necessary
        if (startOfSegment == null) {
          if (currentPieceColor != null) {
            startOfSegment = currentPosition;
          }
        }
        if (currentPieceColor == null && endOfSegment == null) {
          startOfSegment = null;
        }

        // Add a line segment to the list if we have found one
        if (endOfSegment != null) {
          removableLines.add(new Line.Segment(this, startOfSegment, endOfSegment, direction));
        }
      }
    }

    return removableLines;
  }