Fixed movements

main
Brett 2022-12-19 22:25:56 -05:00
parent e4d9c1334c
commit 80627f2012
9 changed files with 137 additions and 57 deletions

View File

@ -22,6 +22,6 @@ public class Bishop extends ChessPiece {
@Override @Override
public ArrayList<Move> getMoves() { public ArrayList<Move> getMoves() {
return new ArrayList<Move>(super.getDiagonalMoves(b.size())); return prune(super.getDiagonalMoves(b.size()));
} }
} }

View File

@ -56,8 +56,8 @@ public class Board {
// if we were unable to set the piece down we failed to move the piece // if we were unable to set the piece down we failed to move the piece
if (!set(m, selectedPiece)) if (!set(m, selectedPiece))
return false; return false;
selectedPiece.move(m);
// run special conditions. Only matters for pieces which have special conditions, since is defaulted to empty body. // run special conditions. Only matters for pieces which have special conditions, since is defaulted to empty body.
if (movedPiece != null)
selectedPiece.applySpecialMove(m); selectedPiece.applySpecialMove(m);
set(x, y, null); set(x, y, null);
return true; return true;
@ -66,6 +66,8 @@ public class Board {
} }
public ChessPiece get(Move m){ public ChessPiece get(Move m){
if (m == null)
return null;
return get(m.getX(), m.getY()); return get(m.getX(), m.getY());
} }
@ -78,10 +80,12 @@ public class Board {
} }
public boolean set(Move m, ChessPiece piece) { public boolean set(Move m, ChessPiece piece) {
if (m == null)
return false;
return set(m.getX(), m.getY(), piece); return set(m.getX(), m.getY(), piece);
} }
protected boolean set(int x, int y, ChessPiece piece){ public boolean set(int x, int y, ChessPiece piece){
if (x < 0 || x >= board.length) if (x < 0 || x >= board.length)
return false; return false;
if (y < 0 || y >= board.length) if (y < 0 || y >= board.length)
@ -90,6 +94,14 @@ public class Board {
return true; return true;
} }
protected Move checkIfMoveValid(Move m, boolean isWhite){
if (m == null)
return null;
if (get(m) != null && get(m).isWhite == isWhite)
return null;
return m;
}
public int size(){ public int size(){
return board.length; return board.length;
} }

View File

@ -8,7 +8,7 @@ public abstract class ChessPiece {
protected Board b; protected Board b;
protected int x, y; protected int x, y;
protected boolean isInDanger, isWhite; protected boolean isInDanger, isWhite;
protected boolean isFirstMove = true; protected int moveCount = 0;
public ChessPiece(Board b, boolean isWhite, int x, int y) { public ChessPiece(Board b, boolean isWhite, int x, int y) {
this.b = b; this.b = b;
@ -29,11 +29,16 @@ public abstract class ChessPiece {
} }
public boolean isFirstMove(){ public boolean isFirstMove(){
return isFirstMove; return moveCount < 1;
}
public boolean isSecondMove(){
return moveCount <= 1;
} }
public void setMoved(){ public void move(Move m){
isFirstMove = false; this.x = m.getX();
this.y = m.getY();
this.moveCount++;
} }
public abstract ArrayList<Move> getMoves(); public abstract ArrayList<Move> getMoves();
@ -42,26 +47,81 @@ public abstract class ChessPiece {
protected ArrayList<Move> getCardinalMoves(int length){ protected ArrayList<Move> getCardinalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
for (int i = 1; i <= length; i++){
// cardinals // cardinals
moves.add(new Move(x - i, y)); for (int i = 1; i <= length; i++){
moves.add(new Move(x + i, y)); if (add(moves, new Move(x - i, y)))
moves.add(new Move(x, y - i)); break;
moves.add(new Move(x, y + i)); }
for (int i = 1; i <= length; i++){
if (add(moves, new Move(x + i, y)))
break;
}
for (int i = 1; i <= length; i++){
if (add(moves, new Move(x, y - i)))
break;
}
for (int i = 1; i <= length; i++){
if (add(moves, new Move(x, y + i)))
break;
} }
return moves; return moves;
} }
protected ArrayList<Move> getDiagonalMoves(int length){ protected ArrayList<Move> getDiagonalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
// diagonals
for (int i = 1; i <= length; i++){ for (int i = 1; i <= length; i++){
// cardinals if (add(moves, new Move(x - i, y - i)))
moves.add(new Move(x - i, y - i)); break;
moves.add(new Move(x + i, y + i)); }
moves.add(new Move(x + i, y - i)); for (int i = 1; i <= length; i++){
moves.add(new Move(x - i, y + i)); if (add(moves, new Move(x - i, y + i)))
break;
}
for (int i = 1; i <= length; i++){
if (add(moves, new Move(x + i, y - i)))
break;
}
for (int i = 1; i <= length; i++){
if (add(moves, new Move(x + i, y + i)))
break;
} }
return moves; return moves;
} }
protected ArrayList<Move> prune(ArrayList<Move> moves){
var prunedMoves = new ArrayList<Move>();
// get rid of impossible moves
for (Move m : moves){
if (m == null)
continue;
if (m.getX() < 0 || m.getX() >= b.size())
continue;
if (m.getY() < 0 || m.getY() >= b.size())
continue;
prunedMoves.add(m);
}
return prunedMoves;
}
/**
* Checks to make sure that the proposed move is valid.
* @param moves array to add to
* @param move move to add to the array
* @return true if we need to stop here. (Break the loop)
*/
private boolean add(ArrayList<Move> moves, Move move){
var m = b.get(move);
if (m != null && m.isWhite != isWhite) {
moves.add(b.checkIfMoveValid(move, isWhite));
return true;
} else {
if (m != null)
return true;
moves.add(b.checkIfMoveValid(move, isWhite));
}
return false;
}
} }

View File

@ -25,22 +25,22 @@ public class King extends ChessPiece {
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
moves.addAll(super.getCardinalMoves(1)); moves.addAll(super.getCardinalMoves(1));
moves.addAll(super.getDiagonalMoves(1)); moves.addAll(super.getDiagonalMoves(1));
if (isFirstMove){ if (isFirstMove()){
ChessPiece rook = null; ChessPiece rook = null;
// castling // castling
if (isWhite){ if (isWhite){
if ((rook = b.get(0, 0)) != null && checkIfRookValid(rook)) if ((rook = b.get(0, 0)) != null && checkIfRookValid(rook))
moves.add(new Move(2, 0, Move.SpecialConditions.leftCastle)); moves.add(b.checkIfMoveValid(new Move(2, 0, Move.SpecialConditions.leftCastle), isWhite));
if ((rook = b.get(b.size() - 1, 0)) != null && checkIfRookValid(rook)) if ((rook = b.get(b.size() - 1, 0)) != null && checkIfRookValid(rook))
moves.add(new Move(b.size() - 2, 0, Move.SpecialConditions.rightCastle)); moves.add(b.checkIfMoveValid(new Move(b.size() - 2, 0, Move.SpecialConditions.rightCastle), isWhite));
} else { } else {
if ((rook = b.get(0, b.size()-1)) != null && checkIfRookValid(rook)) if ((rook = b.get(0, b.size()-1)) != null && checkIfRookValid(rook))
moves.add(new Move(2, b.size()-1, Move.SpecialConditions.leftCastle)); moves.add(b.checkIfMoveValid(new Move(2, b.size()-1, Move.SpecialConditions.leftCastle), isWhite));
if ((rook = b.get(b.size() - 1, b.size()-1)) != null && checkIfRookValid(rook)) if ((rook = b.get(b.size() - 1, b.size()-1)) != null && checkIfRookValid(rook))
moves.add(new Move(b.size() - 2, b.size()-1, Move.SpecialConditions.rightCastle)); moves.add(b.checkIfMoveValid(new Move(b.size() - 2, b.size()-1, Move.SpecialConditions.rightCastle), isWhite));
} }
} }
return moves; return prune(moves);
} }
@Override @Override
@ -54,9 +54,9 @@ public class King extends ChessPiece {
private void castleRight(){ private void castleRight(){
// casting has to move the rook on the right size of the king from white's perspective // casting has to move the rook on the right size of the king from white's perspective
if (this.isWhite) if (this.isWhite) {
b.set(b.size() - 3, 0, b.get(b.size() - 1, 0)); b.set(b.size() - 3, 0, b.get(b.size() - 1, 0));
else } else
b.set(b.size()-3, b.size()-1, b.get(b.size()-1, b.size()-1)); b.set(b.size()-3, b.size()-1, b.get(b.size()-1, b.size()-1));
} }

View File

@ -23,14 +23,14 @@ public class Knight extends ChessPiece {
@Override @Override
public ArrayList<Move> getMoves() { public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
moves.add(new Move(x + 2, y + 1)); moves.add(b.checkIfMoveValid(new Move(x + 2, y + 1), isWhite));
moves.add(new Move(x + 2, y - 1)); moves.add(b.checkIfMoveValid(new Move(x + 2, y - 1), isWhite));
moves.add(new Move(x - 2, y + 1)); moves.add(b.checkIfMoveValid(new Move(x - 2, y + 1), isWhite));
moves.add(new Move(x - 2, y - 1)); moves.add(b.checkIfMoveValid(new Move(x - 2, y - 1), isWhite));
moves.add(new Move(x - 1, y - 2)); moves.add(b.checkIfMoveValid(new Move(x - 1, y - 2), isWhite));
moves.add(new Move(x + 1, y - 2)); moves.add(b.checkIfMoveValid(new Move(x + 1, y - 2), isWhite));
moves.add(new Move(x - 1, y + 2)); moves.add(b.checkIfMoveValid(new Move(x - 1, y + 2), isWhite));
moves.add(new Move(x + 1, y + 2)); moves.add(b.checkIfMoveValid(new Move(x + 1, y + 2), isWhite));
return moves; return prune(moves);
} }
} }

View File

@ -24,38 +24,45 @@ public class Pawn extends ChessPiece {
public ArrayList<Move> getMoves() { public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
if (isWhite) { if (isWhite) {
moves.add(new Move(x, y + 1)); moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y + 1)), isWhite));
if (isFirstMove) if (isFirstMove())
moves.add(new Move(x, y + 2)); moves.add(b.checkIfMoveValid(new Move(x, y + 2), isWhite));
} else { } else {
moves.add(new Move(x, y - 1)); moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y - 1)), isWhite));
if (isFirstMove) if (isFirstMove())
moves.add(new Move(x, y - 2)); moves.add(b.checkIfMoveValid(new Move(x, y - 2), isWhite));
} }
ChessPiece neighbour = null; ChessPiece neighbour = null;
if (isWhite){ if (isWhite){
// En passant // En passant
if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour)) if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x-1, y + 1, Move.SpecialConditions.leftEnPassant)); moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x-1, y + 1, Move.SpecialConditions.leftEnPassant)), isWhite));
// En passant // En passant
if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour)) if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x+1, + 1, Move.SpecialConditions.rightEnPassant)); moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x+1, + 1, Move.SpecialConditions.rightEnPassant)), isWhite));
} else { } else {
// unfortunately have to flip the direction depending on player type // unfortunately have to flip the direction depending on player type
// En passant // En passant
if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour)) if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x-1, y - 1, Move.SpecialConditions.leftEnPassant)); moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x-1, y - 1, Move.SpecialConditions.leftEnPassant)), isWhite));
// En passant // En passant
if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour)) if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x+1, - 1, Move.SpecialConditions.rightEnPassant)); moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x+1, - 1, Move.SpecialConditions.rightEnPassant)), isWhite));
} }
return moves; return prune(moves);
}
private Move pawnForwardMoveValid(Move m){
// basically prevent a pawn from moving into the enemy head on.
if (b.get(m) != null)
return null;
return m;
} }
private boolean checkNeighbourEnPassant(ChessPiece neighbour){ private boolean checkNeighbourEnPassant(ChessPiece neighbour){
return neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove() && neighbour.isWhite != this.isWhite; return neighbour instanceof Pawn && neighbour.isSecondMove() && neighbour.isWhite != this.isWhite;
} }
@Override @Override
@ -69,15 +76,15 @@ public class Pawn extends ChessPiece {
private void enPassantLeft(){ private void enPassantLeft(){
if (isWhite) if (isWhite)
b.set(x-1, y, null); b.set(x, y-1, null);
else else
b.set(x+1, y, null); b.set(x, y+1, null);
} }
private void enPassantRight(){ private void enPassantRight(){
if (isWhite) if (isWhite)
b.set(x+1, y, null); System.out.println(b.set(x, y-1, null));
else else
b.set(x-1, y, null); b.set(x, y-1, null);
} }
} }

View File

@ -25,6 +25,6 @@ public class Queen extends ChessPiece {
ArrayList<Move> moves = new ArrayList<Move>(); ArrayList<Move> moves = new ArrayList<Move>();
moves.addAll(super.getCardinalMoves(b.size())); moves.addAll(super.getCardinalMoves(b.size()));
moves.addAll(super.getDiagonalMoves(b.size())); moves.addAll(super.getDiagonalMoves(b.size()));
return moves; return prune(moves);
} }
} }

View File

@ -20,6 +20,6 @@ public class Rook extends ChessPiece {
@Override @Override
public ArrayList<Move> getMoves() { public ArrayList<Move> getMoves() {
return new ArrayList<Move>(super.getCardinalMoves(b.size())); return prune(super.getCardinalMoves(b.size()));
} }
} }

View File

@ -51,7 +51,7 @@ public class Display extends JFrame implements MouseListener {
} }
if (selectionPointX != -1 && selectionPointY != -1) { if (selectionPointX != -1 && selectionPointY != -1) {
int localPointX = (selectionPointX - xOffset) / width; int localPointX = (selectionPointX - xOffset) / width;
int localPointY = (b.size() - 1) - ((selectionPointY - yOffset) / height); int localPointY = ((selectionPointY - yOffset) / height);
for (Move m : moves){ for (Move m : moves){
if (m.getX() == localPointX && m.getY() == localPointY){ if (m.getX() == localPointX && m.getY() == localPointY){
b.movePiece(piece.getPosition(), new Move(localPointX, localPointY)); b.movePiece(piece.getPosition(), new Move(localPointX, localPointY));
@ -68,9 +68,10 @@ public class Display extends JFrame implements MouseListener {
// handle user input (mouse clicking) // handle user input (mouse clicking)
if (selectionPointX != -1 && selectionPointY != -1){ if (selectionPointX != -1 && selectionPointY != -1){
int localPointX = (selectionPointX - xOffset) / width; int localPointX = (selectionPointX - xOffset) / width;
int localPointY = (b.size()-1) - ((selectionPointY - yOffset) / height); int localPointY = ((selectionPointY - yOffset) / height);
var piece = b.get(localPointX, localPointY); var piece = b.get(localPointX, localPointY);
if (piece != null) { if (piece != null) {
System.out.println(localPointX + " LL " + localPointY);
movingPointX = localPointX; movingPointX = localPointX;
movingPointY = localPointY; movingPointY = localPointY;
} }
@ -80,7 +81,7 @@ public class Display extends JFrame implements MouseListener {
} }
private void drawSelectionRect(Graphics g, Move m){ private void drawSelectionRect(Graphics g, Move m){
g.drawRect(xOffset + (m.getX() * width), yOffset + ((b.size()-1 - m.getY()) * height), width, height); g.drawRect(xOffset + (m.getX() * width), yOffset + (m.getY() * height), width, height);
} }
public boolean update(){ public boolean update(){