diff --git a/Day 43, 45, 46 (chess application design)/Chess.class b/Day 43, 45, 46 (chess application design)/Chess.class index 00d69e0..a37f7e6 100644 Binary files a/Day 43, 45, 46 (chess application design)/Chess.class and b/Day 43, 45, 46 (chess application design)/Chess.class differ diff --git a/Day 43, 45, 46 (chess application design)/Chess.java b/Day 43, 45, 46 (chess application design)/Chess.java index a5f6622..c1bd178 100644 --- a/Day 43, 45, 46 (chess application design)/Chess.java +++ b/Day 43, 45, 46 (chess application design)/Chess.java @@ -91,9 +91,9 @@ public class Chess extends Application { //Set up mouse/click events board.setOnMousePressed(this::mouseClick); - undo.setOnMousePressed(this::undo); - reset.setOnMousePressed(this::reset); - aiMove.setOnMousePressed(this::moveAI); + undo.setOnAction(this::undo); + reset.setOnAction(this::reset); + aiMove.setOnAction(this::moveAI); //Refresh the board paintBoard(); @@ -133,7 +133,8 @@ public class Chess extends Application { } //Reset to the intial game position. - public void reset(MouseEvent e) { + public void reset(ActionEvent e) { + System.out.println(board().getAllPossibleMoves() ); //Set up the initial board //Clear out clickRo and clickCol //Clera out all the histories. @@ -141,7 +142,7 @@ public class Chess extends Application { } //AI makes a move - public void moveAI(MouseEvent e) { + public void moveAI(ActionEvent e) { //Ask the gameState for a list of all legal moves. //Get one of those in the list uniformly at random. // makeMove(...); @@ -162,7 +163,7 @@ public class Chess extends Application { } //Undo button for the most recent move - public void undo(MouseEvent e) { + public void undo(ActionEvent e) { //Pop those 3 history object things and update the GUI. } diff --git a/Day 43, 45, 46 (chess application design)/GameState.class b/Day 43, 45, 46 (chess application design)/GameState.class index a694af1..d1724e9 100644 Binary files a/Day 43, 45, 46 (chess application design)/GameState.class and b/Day 43, 45, 46 (chess application design)/GameState.class differ diff --git a/Day 43, 45, 46 (chess application design)/GameState.java b/Day 43, 45, 46 (chess application design)/GameState.java index 0dcdd84..10fa681 100644 --- a/Day 43, 45, 46 (chess application design)/GameState.java +++ b/Day 43, 45, 46 (chess application design)/GameState.java @@ -1,4 +1,6 @@ +import java.util.*; + // 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). @@ -14,7 +16,7 @@ public class GameState { boardSquares = new Square[] { new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), - new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, true), new Square(Square.KNIGHT, true), new Square(Square.BISHOP, true), new Square(Square.QUEEN, true), new Square(Square.KING, true), new Square(Square.BISHOP, true), new Square(Square.KNIGHT, true), new Square(Square.ROOK, true), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white + new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, true), new Square(Square.KNIGHT, true), new Square(Square.BISHOP, true), new Square(Square.KING, true), new Square(Square.QUEEN, true), new Square(Square.BISHOP, true), new Square(Square.KNIGHT, true), new Square(Square.ROOK, true), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.PAWN, true), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white pawns new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), @@ -23,7 +25,7 @@ public class GameState { new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.EMPTY), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.PAWN, false), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //white pawns - new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, false), new Square(Square.KNIGHT, false), new Square(Square.BISHOP, false), new Square(Square.QUEEN, false), new Square(Square.KING, false), new Square(Square.BISHOP, false), new Square(Square.KNIGHT, false), new Square(Square.ROOK, false), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //black + new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.ROOK, false), new Square(Square.KNIGHT, false), new Square(Square.BISHOP, false), new Square(Square.KING, false), new Square(Square.QUEEN, false), new Square(Square.BISHOP, false), new Square(Square.KNIGHT, false), new Square(Square.ROOK, false), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), //black new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), new Square(Square.OUT_OF_BOUNDS), }; @@ -41,8 +43,41 @@ public class GameState { return null; } - public Move[] getAllPossibleMoves() { - return null; + public ArrayList getAllPossibleMoves() { + ArrayList moves = new ArrayList(); + + //Loop through all squares + for(int index = 0; index < boardSquares.length; index++) { + int type = boardSquares[index].getType(); + if (type == Square.OUT_OF_BOUNDS) continue; + if (type == Square.EMPTY) continue; + + //For each square, find all possible places that piece can move to. + if (type == Square.KING) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, false)); + if (type == Square.QUEEN) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12,13,-13,11,-11}, true)); + if (type == Square.BISHOP) moves.addAll(findAllMovesFor(index, new int[] {13,-13,11,-11}, true)); + if (type == Square.ROOK) moves.addAll(findAllMovesFor(index, new int[] {1,-1,12,-12}, true)); + if (type == Square.KNIGHT) moves.addAll(findAllMovesFor(index, new int[] {10,14,23,25,-10,-14,-23,-25}, false)); + } + + return moves; + } + + private ArrayList findAllMovesFor(int index, int[] directions, boolean canMoveMultipleSquares) { + ArrayList moves = new ArrayList(); + //loop through the directions, see if they work - return the resulting arraylist. + for(int dir : directions) { + int destIndex = index + dir; + int type = boardSquares[destIndex].getType(); + if (boardSquares[index].isWhite() != isWhitesTurn) continue; //don't move other person's pieces! + + if (type == Square.OUT_OF_BOUNDS) continue; + if (type == Square.EMPTY || boardSquares[destIndex].isWhite() != isWhitesTurn) { + moves.add(new Move(index, destIndex)); + } + + } + return moves; } // returns something like "checkmate", "stalemate", etc. diff --git a/Day 43, 45, 46 (chess application design)/Move.class b/Day 43, 45, 46 (chess application design)/Move.class index 42b46f1..f4ea5be 100644 Binary files a/Day 43, 45, 46 (chess application design)/Move.class and b/Day 43, 45, 46 (chess application design)/Move.class differ diff --git a/Day 43, 45, 46 (chess application design)/Move.java b/Day 43, 45, 46 (chess application design)/Move.java index cd00174..9cd2fe9 100644 --- a/Day 43, 45, 46 (chess application design)/Move.java +++ b/Day 43, 45, 46 (chess application design)/Move.java @@ -1,14 +1,40 @@ public class Move { //source and dest coordinates + int startIndex; + int destIndex; + + + public Move(int startIndex, int destIndex) { + this.startIndex = startIndex; + this.destIndex = destIndex; + } public Move(int sourceRow, int sourceColumn, int destinationRow, int destinationColumn) { + //TODO + } + + public int getSourceRow() { + return startIndex / 12 - 2; + } + + public int getSourceCol() { + return startIndex % 12 - 2; + } + + public int getDestRow() { + return destIndex / 12 - 2; + + } + + public int getDestCol() { + return destIndex % 12 - 2; } //returns something like e2-e4 or Qc8++ public String toString() { - return ""; + return "("+getSourceRow()+","+getSourceCol() + ") -> " + "("+getDestRow()+","+getDestCol() + ")"; } } diff --git a/Day 43, 45, 46 (chess application design)/control structure notes.txt b/Day 43, 45, 46 (chess application design)/control structure notes.txt new file mode 100644 index 0000000..eb2e77e --- /dev/null +++ b/Day 43, 45, 46 (chess application design)/control structure notes.txt @@ -0,0 +1,18 @@ + +When should you use break or continue in a loop? + When it makes your code easier to read/maintain/understand as opposed to working it into the boolean condition for the loop. + The danger of using break or continue in a large loop is that it becomes 'hidden' and people might not realize that something inside the loop affects its control flow. + +What are the advantages/disadvantages of switch...case? + + The expression is only checked once in one place, and would only need to changed in one place + - You have to keep putting 'break;' at the end of each case if you don't want the case to "fall through". If you forget, it still compiles. + + You can exploit the fall-through behavior, as follows: + for(int days = 1; days <= 12; days++) { + switch (days) { + case 12: ...three french hens... + ... + case 3: ...three french hens... + case 2: ...two turtle doves... + case 1: ...partridge in a pear tree... + } + }