/** * End game method. Sets all spaces to not be clickable, resets the toolbar at the top of the * window, clears the stack, and resets the controller so its ready for a new game. */ private void gameOver() { gui.resetBackgrounds(); gui.resetToolbar(); moveStack = new Stack<Move>(); currentColor = WHITE; currentPiece = null; }
/** Method invoked at the beginning of the first game. Allows for players to select their names */ private void promptNames() { if (gui.getTeamName(WHITE).equals("") || gui.getTeamName(BLACK).equals("")) { String whiteName = ""; // ensure the name is not blank while (whiteName.equals("")) { whiteName = JOptionPane.showInputDialog("White team name:"); } String blackName = ""; while (blackName.equals("")) { blackName = JOptionPane.showInputDialog("Black team name:"); } gui.setTeamNames(whiteName, blackName); } gui.updateMessage(currentColor); }
/** * Checks the board to see if it is in a check, checkmate, or stalemate configuration. If it is, * it handles the situation properly. */ private void evaluateBoard() { if (master.isCheckmate(currentColor)) { gui.checkmateMessage(currentColor); gameOver(); } else if (master.isStalemate(currentColor)) { gui.stalemateMessage(); gameOver(); } else if (master.isCheck(currentColor) > 0) { // only a check, game not over gui.checkMessage(currentColor); } else { // display new turn gui.updateMessage(currentColor); } }
/** Toggles the current color, and sets the side to that color */ public void switchSides() { // alternate color currentColor = 1 - currentColor; gui.resetBackgrounds(); setCurrentSide(currentColor); evaluateBoard(); }
/** * 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); } }
/** * Helper method for actionPerformed. Iterates through the spaces and determines which space was * clicked on. * * @param event the click of a space * @return a (file, rank) pair corresponding to the space */ public Pair<Character, Integer> findSource(ActionEvent event) { for (int y = board.getYDimension(); y >= 1; y--) { for (char x = 'a'; x < ('a' + board.getXDimension()); x++) { if (event.getSource() == gui.getSpaceAt(x, y)) { return new Pair<Character, Integer>(x, y); } } } return null; }
/** * 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(); }
/** * Takes the action listeners that were created in initializeListeners and adds them to the board * using the GUI's add methods */ private void addListeners() { gui.addSpaceListeners(spaceListener); gui.addStandardGameListener(standardGameListener); gui.addaAlternateGameListener(alternateGameListener); gui.addUndoListener(undoListener); gui.addForfeitListener(forfeitListener); gui.addRestartListener(restartListener); }
/** * 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); } } } }
/** * Method which creates a new GUI, and calls helper methods to set up the listeners for all the * buttons on the board. * * @throws IOException */ public void init() throws IOException { gui = new GUI(board); setCurrentSide(WHITE); JFrame f = new JFrame("Fighting Illini Chess"); f.add(gui.getGUI()); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // ensures the frame is the minimum size it needs to be // in order display the components within it f.pack(); // ensures the minimum size is enforced. f.setMinimumSize(f.getSize()); f.setVisible(true); // set up and add button action listeners initializeListeners(); addListeners(); }
/** Entry method for resetting the backgrounds and setting the current side */ public void setBoard() { gui.resetBackgrounds(); setCurrentSide(currentColor); }