Exemplo n.º 1
0
 /**
  * Makes it so all buttons are not clickable except for the spaces with pieces of a particular
  * color
  *
  * @param color Pieces of this color can be clicked on
  */
 public void setCurrentSide(int color) {
   for (char file = 'a'; file < ('a' + board.getXDimension()); file++) {
     for (int rank = 1; rank <= board.getYDimension(); rank++) {
       Piece piece = board.at(file, rank);
       JButton space = gui.getSpaceAt(file, rank);
       // check if the space is empty, or if it matches the color parameter
       if (piece != null && piece.getColor() == color) {
         space.setEnabled(true);
       } else {
         space.setEnabled(false);
       }
     }
   }
 }
Exemplo n.º 2
0
  /**
   * Method called to handle the situation where a user selects one of his/her pieces to move.
   *
   * @param piece Piece selected on the board
   */
  public void handlePickPiece(Piece piece) {
    // set currentPiece and locate its position
    currentPiece = piece;
    char file = piece.getFile();
    int rank = piece.getRank();

    // low-light its space
    light(gui.getSpaceAt(file, rank), false);
    ArrayList<Pair<Character, Integer>> moveList = master.moveList(piece);

    // highlight and enable legal moves
    for (javafx.util.Pair<Character, Integer> move : moveList) {
      int currRank = move.getValue();
      char currFile = move.getKey();
      light(gui.getSpaceAt(currFile, currRank), true);
    }
  }
Exemplo n.º 3
0
  /**
   * Method called to handle the situation where a user is moving the currentPiece to a new location
   *
   * @param position New space on the board for a piece to move
   */
  public void handleMove(Pair<Character, Integer> position) {
    // place the current move onto the stack
    Piece captured = board.at(position);
    Pair<Character, Integer> oldPosition = currentPiece.getPosition();
    moveStack.push(new Move(currentPiece, captured, oldPosition, position));

    // make the move
    master.move(currentPiece, position);

    // refresh board after piece is moved
    try {
      gui.refreshBoard();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    if (!moveStack.empty()) {
      // can undo now that there are moves in the stack
      gui.setUndoEnabled(true);
    }
    // reset backgrounds and change sides
    switchSides();
  }