示例#1
0
  private void drawLeft(
      Square square, int startX, int startY, int endX, int endY, List<Square> path) {
    final int x = square.getX();
    final int y = square.getY();

    if (y - 1 < 0) {
      if ((square.x == startX) && (square.y == startY)) {
        System.out.print(CLOSED_LEFT_START);
        return;
      }

      if ((square.x == endX) && (square.y == endY)) {
        System.out.print(CLOSED_LEFT_GOAL);
        return;
      }

      if (path.contains(square)) {
        System.out.print(CLOSED_LEFT_PATH);
        return;
      }

      System.out.print(CLOSED_LEFT);
      return;
    }

    for (Square neighbor : square.getAdjacencies()) {
      if (neighbor.getX() == x && neighbor.getY() == y - 1) {
        if ((square.x == endX) && (square.y == endY)) {
          System.out.print(OPEN_LEFT_GOAL);
          return;
        }
        if ((square.x == startX) && (square.y == startY)) {
          System.out.print(OPEN_LEFT_START);
          return;
        }
        if (path.contains(square)) {
          System.out.print(OPEN_LEFT_PATH);
          return;
        }

        System.out.print(OPEN_LEFT);
        return;
      }
    }

    if ((square.x == endX) && (square.y == endY)) {
      System.out.print(CLOSED_LEFT_GOAL);
      return;
    }
    if ((square.x == startX) && (square.y == startY)) {
      System.out.print(CLOSED_LEFT_START);
      return;
    }
    if (path.contains(square)) {
      System.out.print(CLOSED_LEFT_PATH);
      return;
    }

    System.out.print(CLOSED_LEFT);
  }
示例#2
0
  /**
   * Returns a collection of potentially legal moves determined by whether the move has the move
   * vector for the given piece.
   *
   * @param endSquare The end square of the potential moves.
   * @return A HashSet of the resulting moves.
   */
  public HashSet<Move> possibleMovesWhichEndAt(Square endSquare) {
    final HashSet<Move> moves = new HashSet<>();
    int x2 = endSquare.getX();
    int y2 = endSquare.getY();

    for (int x1 = 0; x1 < Chess.BOARD_SIZE; x1++) {
      for (int y1 = 0; y1 < Chess.BOARD_SIZE; y1++) {

        if (!this.isMovablePieceAtSquare(new Square(x1, y1))) {
          continue;
        }

        MoveVector moveVector = new MoveVector(Math.abs(x2 - x1), Math.abs(y2 - y1));

        if (Chess.vectorMask(moveVector)) {
          Square square1 = new Square(x1, y1);

          moves.add(new Move(square1, endSquare));
          if (board[x1][y1].getType().equals(PieceType.PAWN)) {
            moves.add(new Move(square1, endSquare, PieceType.QUEEN));
            moves.add(new Move(square1, endSquare, PieceType.KNIGHT));
            moves.add(new Move(square1, endSquare, PieceType.ROOK));
            moves.add(new Move(square1, endSquare, PieceType.BISHOP));
          }
        }
      }
    }

    return moves;
  }
示例#3
0
  private void drawTop(Square square) {
    final int x = square.getX();
    final int y = square.getY();

    if (x == 0) {
      System.out.print(CLOSED_TOP);
      return;
    }

    for (Square neighbor : square.getAdjacencies()) {
      if (neighbor.getX() == x - 1 && neighbor.getY() == y) {
        System.out.print(OPEN_TOP);
        return;
      }
    }

    System.out.print(CLOSED_TOP);
  }
示例#4
0
  private void initBoard() {
    for (int x = 0; x <= NUM_X + 1; x++) {
      setSquareOnBoard(x, 0, Square.Status.OB);
      setSquareOnBoard(x, NUM_Y + 1, Square.Status.OB);
    }
    for (int y = 0; y <= NUM_Y + 1; y++) {
      setSquareOnBoard(0, y, Square.Status.OB);
      setSquareOnBoard(NUM_X + 1, y, Square.Status.OB);
    }
    for (int y = 1; y <= NUM_Y; y++) {
      for (int x = 1; x <= NUM_X; x++) {
        setSquareOnBoard(x, y, Square.Status.BLANK);
      }
    }

    for (Square squareInitial : INITIAL_PLACEMENTS) {
      int x = squareInitial.getX();
      int y = squareInitial.getY();
      board[x][y] = squareInitial;
    }
  }
示例#5
0
文件: IA.java 项目: jmarginier/chess
  public static StrokeIA getIAStroke(Square[][] plateau, EColor color, int profondeur)
      throws SquareException {
    if (profondeur < 0) return null;
    profondeur -= 1;

    Square[][] tmp = Chessboard.boardClone(plateau);
    int quality_max = Integer.MIN_VALUE;
    StrokeIA stroke = null;
    ArrayList<Square> p_cases = new ArrayList<Square>();
    // Identification de la liste des cases/iece du joueur
    for (int i = 0; i < tmp.length; i++) {
      for (int j = 0; j < tmp[0].length; j++) {
        if (tmp[i][j].getChessmen() != null && tmp[i][j].getChessmen().getColor() == color) {
          p_cases.add(tmp[i][j]);
        }
      }
    }

    // Recherche du meilleur coup pour le joueur
    for (Square src : p_cases) {
      ArrayList<Square> dst_pos = src.getChessmen().movePossibilities(tmp, src);
      for (Square dst : dst_pos) {
        int quality = 0;
        Square[][] tmp_dst = Chessboard.boardClone(tmp);
        // Simultation du Deplacement dans un tableau temporaire
        Chessmen take =
            Chessboard.move(
                tmp_dst, tmp_dst[src.getX()][src.getY()], tmp_dst[dst.getX()][dst.getY()]);

        if (Chessboard.isEchec(tmp, (color == EColor.BLACK) ? EColor.WHITE : EColor.BLACK)) {
          quality += 10;

          if (Chessboard.isEchecMat(tmp, (color == EColor.BLACK) ? EColor.WHITE : EColor.BLACK)) {
            quality += 100;
          }
        } else {
          /* On quantifie le poid de la piece prise */
          if (take != null) {
            if (take instanceof Pawn) {
              quality += 1;
            } else if (take instanceof Rook) {
              quality += 2;
            } else if (take instanceof Knight) {
              quality += 3;
            } else if (take instanceof Bishop) {
              quality += 4;
            } else if (take instanceof Queen) {
              quality += 5;
            } else if (take instanceof King) {
              quality += 6;
            }
          }
        }

        StrokeIA stroketmp =
            getIAStroke(tmp, (color == EColor.BLACK) ? EColor.WHITE : EColor.BLACK, profondeur);
        if (stroketmp != null) {
          quality += stroketmp.quality;
        }

        if (quality > quality_max) {
          quality_max = quality;
          stroke = new StrokeIA(quality, src.getX(), src.getY(), dst.getX(), dst.getY());
        }
      }
    }

    return stroke;
  }
  public boolean isPassedPawn(Square square) {
    int xs = square.getX();
    int ys = square.getY();
    if (color == Color.BLACK) {
      if (ys == 0) return false;
      if (xs == 0) {
        for (int i = 0; i < 2; i++) {
          for (int q = (ys - 1); q > 0; q--) {
            if (board.getSquare(i, q).occupiedBy() != Color.NONE) {
              return false;
            }
          }
        }
      } else if (xs == 7) {
        for (int i = 7; i >= 6; i--) {
          for (int q = (ys - 1); q > 0; q--) {
            if (board.getSquare(i, q).occupiedBy() != Color.NONE) {
              return false;
            }
          }
        }
      } else {
        for (int i = xs - 1; i <= xs + 1; i++) {
          for (int q = (ys - 1); q > 0; q--) {
            if (board.getSquare(i, q).occupiedBy() != Color.NONE) {
              return false;
            }
          }
        }
      }
    } else if (color == Color.WHITE) {
      if (ys == 7) return false;
      if (xs == 0) {
        for (int i = 0; i < 2; i++) {
          for (int q = (ys - 1); q <= 7; q++) {
            if (board.getSquare(i, q).occupiedBy() != Color.NONE) {
              return false;
            }
          }
        }
      } else if (xs == 7) {
        for (int i = 7; i >= 6; i--) {
          for (int q = (ys - 1); q <= 7; q++) {
            if (board.getSquare(i, q).occupiedBy() != Color.NONE) {
              return false;
            }
          }
        }
      } else {
        for (int i = xs - 1; i <= xs + 1; i++) {
          for (int q = (ys - 1); q <= 7; q++) {
            if (board.getSquare(i, q).occupiedBy() != Color.NONE) {
              return false;
            }
          }
        }
      }
    } else {
      return false;
    }

    return true;
  }
示例#7
0
  /**
   * Insert all enclosed regions in a list.
   *
   * @param board
   * @return ArrayList of ArrayLists, each one containing square objects representing the position
   *     of the enclosed pieces of the same region
   */
  public static ArrayList<ArrayList<Square>> createArrayOfEnclosedRegions(Board board) {

    ArrayList<ArrayList<Square>> list_of_enclosed_regions = new ArrayList<ArrayList<Square>>();
    ArrayList<Square> unchecked_enclosed_pieces = new ArrayList<Square>();

    // Insert all enclosed squares in an ArrayList.
    for (int i = 0; i < board.dimension; i++) {
      for (int j = 0; j < board.dimension; j++) {
        Piece piece = board.squares[i][j].getPieceOnTop();
        if (piece.isEnclosed()) {
          unchecked_enclosed_pieces.add(board.squares[i][j]);
        }
      }
    }

    while (!unchecked_enclosed_pieces.isEmpty()) {
      ArrayList<Square> pieces_in_enclosed_region = new ArrayList<Square>();
      ArrayList<Square> queue = new ArrayList<Square>();

      Square location = unchecked_enclosed_pieces.get(0);
      queue.add(location);

      while (!queue.isEmpty()) {
        Square current_location = queue.get(0);
        queue.remove(0);
        pieces_in_enclosed_region.add(current_location);
        unchecked_enclosed_pieces.remove(current_location);
        addToQueueIfEnclosed(
            board,
            current_location.getX(),
            current_location.getY() + 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX(),
            current_location.getY() - 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() - 1,
            current_location.getY(),
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() + 1,
            current_location.getY(),
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() - 1,
            current_location.getY() - 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() - 1,
            current_location.getY() + 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() + 1,
            current_location.getY() + 1,
            pieces_in_enclosed_region,
            queue);
        addToQueueIfEnclosed(
            board,
            current_location.getX() + 1,
            current_location.getY() - 1,
            pieces_in_enclosed_region,
            queue);
      }

      list_of_enclosed_regions.add(pieces_in_enclosed_region);
    }
    return list_of_enclosed_regions;
  }