main
Michael Kowalczyk 11 months ago
parent dab0d64bfc
commit 3518368387

@ -91,9 +91,9 @@ public class Chess extends Application {
//Set up mouse/click events //Set up mouse/click events
board.setOnMousePressed(this::mouseClick); board.setOnMousePressed(this::mouseClick);
undo.setOnMousePressed(this::undo); undo.setOnAction(this::undo);
reset.setOnMousePressed(this::reset); reset.setOnAction(this::reset);
aiMove.setOnMousePressed(this::moveAI); aiMove.setOnAction(this::moveAI);
//Refresh the board //Refresh the board
paintBoard(); paintBoard();
@ -133,7 +133,8 @@ public class Chess extends Application {
} }
//Reset to the intial game position. //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 //Set up the initial board
//Clear out clickRo and clickCol //Clear out clickRo and clickCol
//Clera out all the histories. //Clera out all the histories.
@ -141,7 +142,7 @@ public class Chess extends Application {
} }
//AI makes a move //AI makes a move
public void moveAI(MouseEvent e) { public void moveAI(ActionEvent e) {
//Ask the gameState for a list of all legal moves. //Ask the gameState for a list of all legal moves.
//Get one of those in the list uniformly at random. //Get one of those in the list uniformly at random.
// makeMove(...); // makeMove(...);
@ -162,7 +163,7 @@ public class Chess extends Application {
} }
//Undo button for the most recent move //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. //Pop those 3 history object things and update the GUI.
} }

@ -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. // 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). // 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[] { 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.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.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), 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.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.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),
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; return null;
} }
public Move[] getAllPossibleMoves() { public ArrayList<Move> getAllPossibleMoves() {
return null; ArrayList<Move> moves = new ArrayList<Move>();
//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<Move> findAllMovesFor(int index, int[] directions, boolean canMoveMultipleSquares) {
ArrayList<Move> moves = new ArrayList<Move>();
//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. // returns something like "checkmate", "stalemate", etc.

@ -1,14 +1,40 @@
public class Move { public class Move {
//source and dest coordinates //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) { 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++ //returns something like e2-e4 or Qc8++
public String toString() { public String toString() {
return ""; return "("+getSourceRow()+","+getSourceCol() + ") -> " + "("+getDestRow()+","+getDestCol() + ")";
} }
} }

@ -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...
}
}
Loading…
Cancel
Save