/* * returns false if no move has been made previously * * it undo's the previous move */ protected static boolean undoMove() { if (movesStack.size() <= 1) return false; Move currentPos = movesStack.pop(); Move prevPos = movesStack.peek(); if (!tourDraw || !tourRunning) return true; // Undo the current position if ((currentPos.row + currentPos.col) % 2 == 0) colorSquare(currentPos, Color.white); else colorSquare(currentPos, Color.black); if ((prevPos.row + prevPos.col) % 2 == 0) colorSquare(prevPos, Color.white); else colorSquare(prevPos, Color.black); drawKnight(prevPos); // delay(delayTime); return true; }
/** * This function simulates a move of the current knight. * * @param nextPos : Next position of the knight * @return: True if position is legal; false otherwise */ protected static boolean makeMove(Move nextPos) // -----need { if (movesStack.size() >= boardSize * boardSize || !isNextMoveValid(nextPos)) return false; Move currentPos = (!movesStack.empty()) ? movesStack.peek() : null; movesStack.push(nextPos); movesCounter++; updatePerformanceCounter(movesCounter); if (!tourDraw || !tourRunning) return true; if (currentPos != null) { // Draw the number of the last position colorSquare(currentPos, Color.yellow); drawNumber(currentPos, movesStack.size() - 1); } drawKnight(nextPos); // delay(1); return true; }
private static void drawKnight(Move position) { drawKnight(position.row, position.col); }
// Callbacks private void drawButtonActionActionPerformed(ActionEvent event) { drawChessPattern(); drawKnight(new Move(knightRow, knightColumn)); }