diff --git a/Day 43 (design chess application)/Chess.java b/Day 43 (design chess application)/Chess.java deleted file mode 100644 index cdec6e2..0000000 --- a/Day 43 (design chess application)/Chess.java +++ /dev/null @@ -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() { - } - -} \ No newline at end of file diff --git a/Day 43 (design chess application)/GameState.java b/Day 43 (design chess application)/GameState.java deleted file mode 100644 index c314bd0..0000000 --- a/Day 43 (design chess application)/GameState.java +++ /dev/null @@ -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() { - } - -} diff --git a/Day 43, 45, 46 (chess application design)/Chess.java b/Day 43, 45, 46 (chess application design)/Chess.java new file mode 100644 index 0000000..fc282d7 --- /dev/null +++ b/Day 43, 45, 46 (chess application design)/Chess.java @@ -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 gameStateHistory; //Stack of GameBoard states for the move undo. + private ArrayList moveHistory; //Stack of tuples (moves) so they can be displayed. + private ArrayList 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. + } + +} \ No newline at end of file diff --git a/Day 43, 45, 46 (chess application design)/GameState.java b/Day 43, 45, 46 (chess application design)/GameState.java new file mode 100644 index 0000000..913398e --- /dev/null +++ b/Day 43, 45, 46 (chess application design)/GameState.java @@ -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() { + } + +} diff --git a/Day 43 (design chess application)/Move.java b/Day 43, 45, 46 (chess application design)/Move.java similarity index 62% rename from Day 43 (design chess application)/Move.java rename to Day 43, 45, 46 (chess application design)/Move.java index db18041..59c84fe 100644 --- a/Day 43 (design chess application)/Move.java +++ b/Day 43, 45, 46 (chess application design)/Move.java @@ -1,9 +1,9 @@ public class Move { //source and dest coordinates - //boolean check or not - //boolean capture or not - //boolean checkmate or not + + public Move(...source...dest...) { + } //returns something like e2-e4 or Qc8++ diff --git a/Day 43 (design chess application)/Square.java b/Day 43, 45, 46 (chess application design)/Square.java similarity index 100% rename from Day 43 (design chess application)/Square.java rename to Day 43, 45, 46 (chess application design)/Square.java diff --git a/Day 43 (design chess application)/chess program design.txt b/Day 43, 45, 46 (chess application design)/chess program design.txt similarity index 100% rename from Day 43 (design chess application)/chess program design.txt rename to Day 43, 45, 46 (chess application design)/chess program design.txt diff --git a/Day 43, 45, 46 (chess application design)/refactoring Move.txt b/Day 43, 45, 46 (chess application design)/refactoring Move.txt new file mode 100644 index 0000000..5069113 --- /dev/null +++ b/Day 43, 45, 46 (chess application design)/refactoring Move.txt @@ -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...] diff --git a/Day 43 (design chess application)/requirements and spec.txt b/Day 43, 45, 46 (chess application design)/requirements and spec.txt similarity index 100% rename from Day 43 (design chess application)/requirements and spec.txt rename to Day 43, 45, 46 (chess application design)/requirements and spec.txt