parent
5dcff17a02
commit
bc00e065e7
@ -1,30 +0,0 @@
|
|||||||
|
|
||||||
//Main class and GUI
|
|
||||||
|
|
||||||
public class Chess {
|
|
||||||
private GameBoard board;
|
|
||||||
//Remembers the initial click square for the user's move
|
|
||||||
//Stack of GameBoard states for the move undo.
|
|
||||||
//Stack of tuples (moves) so they can be displayed.
|
|
||||||
|
|
||||||
public void start() {
|
|
||||||
//Set up the initial board
|
|
||||||
}
|
|
||||||
|
|
||||||
//Reset to the intial game position.
|
|
||||||
public void reset() {
|
|
||||||
}
|
|
||||||
|
|
||||||
//AI makes a move
|
|
||||||
public void moveAI() {
|
|
||||||
}
|
|
||||||
|
|
||||||
//For the human moving
|
|
||||||
public void mouseClick() {
|
|
||||||
}
|
|
||||||
|
|
||||||
//Undo button for the most recent move
|
|
||||||
public void undo() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
|
|
||||||
public class GameState {
|
|
||||||
//Tracks the game board as a 1D array of Square objects (effectively 12 x 12) with 2 layers of sentinel squares all around.
|
|
||||||
//Knows whose turn it is
|
|
||||||
|
|
||||||
public GameState() {
|
|
||||||
|
|
||||||
reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reset() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public void attemptMove(int sourceX, int sourceY, int destX, int destY) {
|
|
||||||
//See if it is legal.
|
|
||||||
//Change the board state accordingly.
|
|
||||||
}
|
|
||||||
|
|
||||||
public Moves[] getAllPossibleMoves() {
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns something like "checkmate", "stalemate", etc.
|
|
||||||
public String getFeedback() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,56 @@
|
|||||||
|
|
||||||
|
//Main class and GUI
|
||||||
|
|
||||||
|
public class Chess {
|
||||||
|
private GameBoard board;
|
||||||
|
|
||||||
|
//User's first click for a move.
|
||||||
|
private Integer clickRow; //null means no first click made currently.
|
||||||
|
private Integer clickColumn;
|
||||||
|
|
||||||
|
private ArrayList<GameState> gameStateHistory; //Stack of GameBoard states for the move undo.
|
||||||
|
private ArrayList<Move> moveHistory; //Stack of tuples (moves) so they can be displayed.
|
||||||
|
private ArrayList<String> moveNotationHistory; //Stack of move notations.
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
//Set up graphics on the screen, buttons
|
||||||
|
//Set up the initial board
|
||||||
|
//Set up empty move history, game history, notation history.
|
||||||
|
//clickRow and clickCol are null
|
||||||
|
//Set up mouse/click events
|
||||||
|
}
|
||||||
|
|
||||||
|
//Reset to the intial game position.
|
||||||
|
public void reset() {
|
||||||
|
//Set up the initial board
|
||||||
|
//Clear out clickRo and clickCol
|
||||||
|
//Clera out all the histories.
|
||||||
|
}
|
||||||
|
|
||||||
|
//AI makes a move
|
||||||
|
public void moveAI() {
|
||||||
|
//Ask the gameState for a list of all legal moves.
|
||||||
|
//Get one of those in the list uniformly at random.
|
||||||
|
makeMove(...);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void makeMove(...) {
|
||||||
|
//Clone the gameState object for the current state of the game, tell it to make the move, add it to the move history
|
||||||
|
//add the notation for it
|
||||||
|
//add the move to the history as well
|
||||||
|
}
|
||||||
|
|
||||||
|
//For the human moving
|
||||||
|
public void mouseClick() {
|
||||||
|
//If it is the first click (null check) then record it, updtae the GUI to show highlighting
|
||||||
|
//else attempt the move:
|
||||||
|
|
||||||
|
makeMove(...);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Undo button for the most recent move
|
||||||
|
public void undo() {
|
||||||
|
//Pop those 3 history object things and update the GUI.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
// We are making this an *immutable* class. The state that an object has when constructed is its state permanently.
|
||||||
|
// Instead of making changes to objects we create new ones (with attemptMove() and the constructor).
|
||||||
|
|
||||||
|
public class GameState {
|
||||||
|
Square[] boardSquares; //Tracks the game board as a 1D array of Square objects (effectively 12 x 12) with 2 layers of sentinel squares all around.
|
||||||
|
boolean isWhitesTurn; //Knows whose turn it is
|
||||||
|
|
||||||
|
public GameState() {
|
||||||
|
//Set turn to white
|
||||||
|
//Initialize squares with legal starting position.
|
||||||
|
}
|
||||||
|
|
||||||
|
public GameState attemptMove(Move m) {
|
||||||
|
//See if it is legal.
|
||||||
|
//Change the board state accordingly.
|
||||||
|
}
|
||||||
|
|
||||||
|
public Moves[] getAllPossibleMoves() {
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns something like "checkmate", "stalemate", etc.
|
||||||
|
public String getFeedback() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
//source and dest coordinates
|
//source and dest coordinates
|
||||||
//boolean check or not
|
|
||||||
//boolean capture or not
|
public Move(...source...dest...) {
|
||||||
//boolean checkmate or not
|
}
|
||||||
|
|
||||||
|
|
||||||
//returns something like e2-e4 or Qc8++
|
//returns something like e2-e4 or Qc8++
|
@ -0,0 +1,13 @@
|
|||||||
|
Design change to the Move class:
|
||||||
|
Two things that felt wrong:
|
||||||
|
1) Some Move objects will be half-constructed temporarily (move history) or permanently (enumeration of legal moves)
|
||||||
|
2) A Move object can't determine by itself which piece moved, whether it was a capture, check, checkmate, or stalemate.
|
||||||
|
What if we recompute the notation whenever needed, on the fly?
|
||||||
|
* Depending on the GUI implementation, making n moves may result in O(n^2) notation calculations
|
||||||
|
Observations:
|
||||||
|
* Whoever determines move notation needs to know the relevant board states and the move itself (the Chess class).
|
||||||
|
* Move notation can be stored as such; it isn't necessary to have 3 or 4 boolean variables for check, capture, etc.
|
||||||
|
* Move notation should be cached to avoid recalculations and potential O(n^2)-time behavior
|
||||||
|
Conclusion:
|
||||||
|
Put it in the Chess class, because we have access to the relevant board states and the move itself.
|
||||||
|
[Maybe the parallel arrays in the Chess class can be made into a single array of Turn objects...]
|
Loading…
Reference in new issue