Compare commits

...

6 Commits

Author SHA1 Message Date
Brett 22797be579 Fixed movement on a pieces and user input 2022-12-19 22:29:04 -05:00
Brett 5aba3bb6c8 Merge remote-tracking branch 'github/main' 2022-12-19 22:28:55 -05:00
Brett 80627f2012 Fixed movements 2022-12-19 22:25:56 -05:00
MikeBlu 715f566697
Added minMax algorithm test harness
Don't know where my original Main.java commit went, but here it is.
2022-12-18 02:11:32 -05:00
MikeBlu f6e40395d9
Added cloning, deepcopy, & Board getMoves 2022-12-18 02:08:35 -05:00
MikeBlu ecf88068ac
Added initial test harness for minMax algorithm 2022-12-18 02:03:41 -05:00
18 changed files with 743 additions and 55 deletions

View File

@ -0,0 +1,31 @@
package project.chess;
import java.awt.*;
import java.util.ArrayList;
import static project.ui.Display.loadImage;
public class Bishop extends ChessPiece {
private Image whiteBishop = loadImage("./resources/chess_piece_2_black_bishop.png");
private Image blackBishop = loadImage("./resources/chess_piece_2_white_bishop.png");
public Bishop(Board b, boolean isWhite, int x, int y) {
super(b,isWhite,x,y);
}
public Image getImage(){
if (isWhite)
return whiteBishop;
return blackBishop;
}
@Override
public ArrayList<Move> getMoves() {
return new ArrayList<Move>(super.getDiagonalMoves(b.size()));
}
public Bishop clone () {
return new Bishop(this.b,this.isWhite,this.x,this.y);
}
}

View File

@ -0,0 +1,141 @@
package project.chess;
import java.util.ArrayList;
public class Board {
private final ChessPiece[][] board = new ChessPiece[8][8];
/**
* create a basic chess board in default configuration
*/
public Board(){
for (int i = 0; i < size(); i++) {
board[i][1] = new Pawn(this, true, i, 1);
board[i][size() - 2] = new Pawn(this, false, i, size() - 2);
}
// white
board[0][0] = new Rook(this, true,0, 0);
board[size()-1][0] = new Rook(this, true, size() - 1, 0);
board[1][0] = new Knight(this, true, 1, 0);
board[size()-2][0] = new Knight(this, true, size() - 2, 0);
board[2][0] = new Bishop(this, true, 2, 0);
board[size()-3][0] = new Bishop(this, true, size() - 3, 0);
board[3][0] = new Queen(this, true, 3, 0);
board[size()-4][0] = new King(this, true, size() - 4, 0);
// black
board[0][size()-1] = new Rook(this, false,0, size()-1);
board[size()-1][size()-1] = new Rook(this, false, size() - 1, size()-1);
board[1][size()-1] = new Knight(this, false, 1, size()-1);
board[size()-2][size()-1] = new Knight(this, false, size() - 2, size()-1);
board[2][size()-1] = new Bishop(this, false, 2, size()-1);
board[size()-3][size()-1] = new Bishop(this, false, size() - 3, size()-1);
board[3][size()-1] = new Queen(this, false, 3, size()-1);
board[size()-4][size()-1] = new King(this, false, size() - 4, size()-1);
}
public boolean movePiece(Move movingPiece, Move newPos){
return movePiece(movingPiece.getX(), movingPiece.getY(), newPos.getX(), newPos.getY());
}
public boolean movePiece(int x, int y, int newX, int newY){
// System.out.println(x + " " + y + " || " + newX + " " + newY);
ChessPiece selectedPiece;
// make sure the place we are moving from has a piece
if ((selectedPiece = get(x, y)) == null)
return false;
ArrayList<Move> moves;
moves = selectedPiece.getMoves();
for (Move m : moves){
// reject the moves that don't correspond to where we want to move to.
if (m.getX() != newX || m.getY() != newY)
continue;
ChessPiece movedPiece = get(m);
// make sure they are of the same color. Since we know this move is the position we want to move to
// we can early exit because we are not allowed to move on top of our own pieces
if (movedPiece != null && selectedPiece.isWhite == movedPiece.isWhite)
return false;
// if we were unable to set the piece down we failed to move the piece
if (!set(m, selectedPiece))
return false;
// run special conditions. Only matters for pieces which have special conditions, since is defaulted to empty body.
if (movedPiece != null)
selectedPiece.applySpecialMove(m);
set(x, y, null);
return true;
}
return false;
}
public ArrayList<Board> getMoves(boolean isWhitesTurn) {
ArrayList<Board> moveStates = new ArrayList<>();
Board curr;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if ((board[i][j] = get(i, j)) == null || (isWhitesTurn && !board[i][j].isWhite()) ||
(!isWhitesTurn && board[i][j].isWhite()) ) continue;
for (Move pieceMove : board[i][j].getMoves()) {
curr = deepCopy();
curr.movePiece(board[i][j].x,board[i][j].y,pieceMove.getX(),pieceMove.getY());
moveStates.add(deepCopy());
}
}
}
return moveStates;
}
public int evaluate () { // !!!!!!!! Fix this, add actual heuristic evaluation
return (int)Math.random()*900;
}
public Board deepCopy () {
Board temp = new Board();
for (int i = 0; i < temp.board.length; i++) {
for (int j = 0; j < temp.board[0].length; j++) {
try {
if (this.board[i][j] == null) {
temp.board[i][j] = null;
continue;
}
temp.board[i][j] = this.board[i][j].clone();
} catch (Exception e) { e.printStackTrace();}
}
}
return temp;
}
public ChessPiece get(Move m){
return get(m.getX(), m.getY());
}
public ChessPiece get(int x, int y){
if (x < 0 || x >= board.length)
return null;
if (y < 0 || y >= board.length)
return null;
return board[x][y];
}
public boolean set(Move m, ChessPiece piece) {
return set(m.getX(), m.getY(), piece);
}
protected boolean set(int x, int y, ChessPiece piece){
if (x < 0 || x >= board.length)
return false;
if (y < 0 || y >= board.length)
return false;
board[x][y] = piece;
return true;
}
public int size(){
return board.length;
}
}

View File

@ -0,0 +1,68 @@
package project.chess;
import java.awt.*;
import java.util.ArrayList;
public abstract class ChessPiece {
protected Board b;
protected int x, y;
protected boolean isInDanger, isWhite;
protected boolean isFirstMove = true;
public ChessPiece(Board b, boolean isWhite, int x, int y) {
this.b = b;
this.x = x;
this.y = y;
this.isWhite = isWhite;
}
public void setInDanger(boolean isInDanger){
this.isInDanger = isInDanger;
}
public boolean isWhite(){
return isWhite;
}
public Move getPosition(){
return new Move(x, y);
}
public boolean isFirstMove(){
return isFirstMove;
}
public void setMoved(){
isFirstMove = false;
}
public abstract ArrayList<Move> getMoves();
public abstract Image getImage();
public void applySpecialMove(Move moveWithSpecial){}
protected ArrayList<Move> getCardinalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>();
for (int i = 1; i <= length; i++){
// cardinals
moves.add(new Move(x - i, y));
moves.add(new Move(x + i, y));
moves.add(new Move(x, y - i));
moves.add(new Move(x, y + i));
}
return moves;
}
protected ArrayList<Move> getDiagonalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>();
for (int i = 1; i <= length; i++){
// cardinals
moves.add(new Move(x - i, y - i));
moves.add(new Move(x + i, y + i));
moves.add(new Move(x + i, y - i));
moves.add(new Move(x - i, y + i));
}
return moves;
}
public abstract ChessPiece clone();
}

View File

@ -0,0 +1,79 @@
package project.chess;
import java.awt.*;
import java.util.ArrayList;
import static project.ui.Display.loadImage;
public class King extends ChessPiece {
private final Image whiteKing = loadImage("./resources/chess_piece_2_black_king.png");
private final Image blackKing = loadImage("./resources/chess_piece_2_white_king.png");
public King(Board b, boolean isWhite, int x, int y) {
super(b,isWhite,x,y);
}
public Image getImage(){
if (isWhite)
return whiteKing;
return blackKing;
}
@Override
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
moves.addAll(super.getCardinalMoves(1));
moves.addAll(super.getDiagonalMoves(1));
if (isFirstMove){
ChessPiece rook = null;
// castling
if (isWhite){
if ((rook = b.get(0, 0)) != null && checkIfRookValid(rook))
moves.add(new Move(2, 0, Move.SpecialConditions.leftCastle));
if ((rook = b.get(b.size() - 1, 0)) != null && checkIfRookValid(rook))
moves.add(new Move(b.size() - 2, 0, Move.SpecialConditions.rightCastle));
} else {
if ((rook = b.get(0, b.size()-1)) != null && checkIfRookValid(rook))
moves.add(new Move(2, b.size()-1, Move.SpecialConditions.leftCastle));
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));
}
}
return moves;
}
@Override
public void applySpecialMove(Move moveWithSpecial){
Move.SpecialConditions specialCondition;
specialCondition = moveWithSpecial.getSpecialCondition();
if(specialCondition == Move.SpecialConditions.leftCastle)
castleLeft();
else if (specialCondition == Move.SpecialConditions.rightCastle)
castleRight();
}
private void castleRight(){
// casting has to move the rook on the right size of the king from white's perspective
if (this.isWhite)
b.set(b.size()-3, 0, b.get(b.size()-1, 0));
else
b.set(b.size()-3, b.size()-1, b.get(b.size()-1, b.size()-1));
}
private boolean checkIfRookValid(ChessPiece piece){
return piece.isFirstMove() && piece instanceof Rook;
}
private void castleLeft(){
// casting has to move the rook on the left size of the king from white's perspective
if (this.isWhite)
b.set(3, 0, b.get(0, 0));
else
b.set(3, b.size()-1, b.get(0, b.size()-1));
}
public King clone () {
return new King(this.b,this.isWhite,this.x,this.y);
}
}

View File

@ -0,0 +1,40 @@
package project.chess;
import java.awt.*;
import java.util.ArrayList;
import static project.ui.Display.loadImage;
public class Knight extends ChessPiece {
private Image whiteKnight = loadImage("./resources/chess_piece_2_black_knight.png");
private Image blackKnight = loadImage("./resources/chess_piece_2_white_knight.png");
public Knight(Board b, boolean isWhite, int x, int y) {
super(b,isWhite,x,y);
}
public Image getImage(){
if (isWhite)
return whiteKnight;
return blackKnight;
}
@Override
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
moves.add(new Move(x + 2, y + 1));
moves.add(new Move(x + 2, y - 1));
moves.add(new Move(x - 2, y + 1));
moves.add(new Move(x - 2, y - 1));
moves.add(new Move(x - 1, y - 2));
moves.add(new Move(x + 1, y - 2));
moves.add(new Move(x - 1, y + 2));
moves.add(new Move(x + 1, y + 2));
return moves;
}
public Knight clone () {
return new Knight(this.b,this.isWhite,this.x,this.y);
}
}

View File

@ -0,0 +1,97 @@
package project;
import project.chess.Board;
import project.chess.Move;
import project.ui.Display;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
Board mainBoard = new Board();
int maxDepth = 2;
Board[] bestStates = new Board[maxDepth];
minMax(mainBoard,bestStates,maxDepth,true);
System.out.println("Ai Done Computing");
int counter = 0;
// mainBoard.getMoves();
// display stuff //
Display display = new Display(mainBoard);
long frames = 0;
long lastTime = System.currentTimeMillis();
long frameTime = System.currentTimeMillis();
while(display.update(mainBoard)){
display.repaint();
// limit usage of system resources by slowing the speed down to 60 fps.
while ((System.currentTimeMillis() - frameTime) < 64f){
Thread.yield();
}
frameTime = System.currentTimeMillis();
// print out the FPS of the display.
frames++;
if (System.currentTimeMillis() - lastTime > 1000){
System.out.println("FPS: " + frames);
frames = 0;
lastTime = System.currentTimeMillis();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (Exception ignored) {}
mainBoard = bestStates[counter%maxDepth];
counter++;
}
System.out.println("Hello world!");
}
/*
public int alphaBeta (Board position, int depth, int alpha, int beta, boolean maximizing) {
if (depth == 0 || position.kingInDanger) return Board.evaluate(position);
if (maximizing) {
int maxEval = Integer.MIN_VALUE;
position.
}
} */
public static int minMax (Board position, Board[] bestStates, int depth, boolean maximizing) {
if (depth == 0) {
bestStates[depth] = position;
return position.evaluate();
}
System.out.println(depth);
if (maximizing) {
int maxEval = Integer.MIN_VALUE;
for (Board state: position.getMoves(maximizing) ) {
int eval = minMax(state,bestStates,depth-1, false);
maxEval = Math.max(maxEval,eval);
}
bestStates[depth-1] = position;
return maxEval;
} else {
int minEval = Integer.MAX_VALUE;
for (Board state: position.getMoves(maximizing) ) {
int eval = minMax(state,bestStates,depth-1, true);
minEval = Math.min(minEval,eval);
}
bestStates[depth-1] = position;
return minEval;
}
}
}

View File

@ -0,0 +1,88 @@
package project.chess;
import project.ui.Display;
import java.awt.*;
import java.util.ArrayList;
public class Pawn extends ChessPiece {
private Image white = Display.loadImage("./resources/chess_piece_2_black_pawn.png");
private Image black = Display.loadImage("./resources/chess_piece_2_white_pawn.png");
public Pawn(Board b, boolean isWhite, int x, int y) {
super(b,isWhite,x,y);
}
public Image getImage(){
if (isWhite)
return white;
return black;
}
@Override
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
if (isWhite) {
moves.add(new Move(x, y + 1));
if (isFirstMove)
moves.add(new Move(x, y + 2));
} else {
moves.add(new Move(x, y - 1));
if (isFirstMove)
moves.add(new Move(x, y - 2));
}
ChessPiece neighbour = null;
if (isWhite){
// En passant
if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x-1, y + 1, Move.SpecialConditions.leftEnPassant));
// En passant
if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x+1, + 1, Move.SpecialConditions.rightEnPassant));
} else {
// unfortunately have to flip the direction depending on player type
// En passant
if ((neighbour = b.get(x-1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x-1, y - 1, Move.SpecialConditions.leftEnPassant));
// En passant
if ((neighbour = b.get(x+1, y)) != null && checkNeighbourEnPassant(neighbour))
moves.add(new Move(x+1, - 1, Move.SpecialConditions.rightEnPassant));
}
return moves;
}
private boolean checkNeighbourEnPassant(ChessPiece neighbour){
return neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove() && neighbour.isWhite != this.isWhite;
}
@Override
public void applySpecialMove(Move moveWithSpecial){
Move.SpecialConditions specialCondition;
specialCondition = moveWithSpecial.getSpecialCondition();
if(specialCondition == Move.SpecialConditions.leftEnPassant)
enPassantLeft();
else if (specialCondition == Move.SpecialConditions.rightEnPassant)
enPassantRight();
}
private void enPassantLeft(){
if (isWhite)
b.set(x-1, y, null);
else
b.set(x+1, y, null);
}
private void enPassantRight(){
if (isWhite)
b.set(x+1, y, null);
else
b.set(x-1, y, null);
}
public Pawn clone () {
return new Pawn(this.b,this.isWhite,this.x,this.y);
}
}

View File

@ -0,0 +1,34 @@
package project.chess;
import java.awt.*;
import java.util.ArrayList;
import static project.ui.Display.loadImage;
public class Queen extends ChessPiece {
private Image whiteQueen = loadImage("./resources/chess_piece_2_black_queen.png");
private Image blackQueen = loadImage("./resources/chess_piece_2_white_queen.png");
public Queen(Board b, boolean isWhite, int x, int y) {
super(b,isWhite,x,y);
}
public Image getImage(){
if (isWhite)
return whiteQueen;
return blackQueen;
}
@Override
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
moves.addAll(super.getCardinalMoves(b.size()));
moves.addAll(super.getDiagonalMoves(b.size()));
return moves;
}
public Queen clone () {
return new Queen(this.b,this.isWhite,this.x,this.y);
}
}

View File

@ -0,0 +1,29 @@
package project.chess;
import java.awt.*;
import java.util.ArrayList;
import static project.ui.Display.loadImage;
public class Rook extends ChessPiece {
private Image whiteRook = loadImage("./resources/chess_piece_2_black_rook.png");
private Image blackRook = loadImage("./resources/chess_piece_2_white_rook.png");
public Rook(Board b, boolean isWhite, int x, int y) {
super(b,isWhite,x,y);
}
public Image getImage(){
return isWhite ? whiteRook : blackRook;
}
@Override
public ArrayList<Move> getMoves() {
return new ArrayList<Move>(super.getCardinalMoves(b.size()));
}
public Rook clone () {
return new Rook(this.b,this.isWhite,this.x,this.y);
}
}

View File

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

View File

@ -56,9 +56,9 @@ public class Board {
// if we were unable to set the piece down we failed to move the piece
if (!set(m, selectedPiece))
return false;
selectedPiece.move(m);
// 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);
return true;
}
@ -66,6 +66,8 @@ public class Board {
}
public ChessPiece get(Move m){
if (m == null)
return null;
return get(m.getX(), m.getY());
}
@ -78,10 +80,12 @@ public class Board {
}
public boolean set(Move m, ChessPiece piece) {
if (m == null)
return false;
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)
return false;
if (y < 0 || y >= board.length)
@ -90,6 +94,14 @@ public class Board {
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(){
return board.length;
}

View File

@ -8,7 +8,7 @@ public abstract class ChessPiece {
protected Board b;
protected int x, y;
protected boolean isInDanger, isWhite;
protected boolean isFirstMove = true;
protected int moveCount = 0;
public ChessPiece(Board b, boolean isWhite, int x, int y) {
this.b = b;
@ -29,11 +29,16 @@ public abstract class ChessPiece {
}
public boolean isFirstMove(){
return isFirstMove;
return moveCount < 1;
}
public boolean isSecondMove(){
return moveCount <= 1;
}
public void setMoved(){
isFirstMove = false;
public void move(Move m){
this.x = m.getX();
this.y = m.getY();
this.moveCount++;
}
public abstract ArrayList<Move> getMoves();
@ -42,26 +47,81 @@ public abstract class ChessPiece {
protected ArrayList<Move> getCardinalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>();
// cardinals
for (int i = 1; i <= length; i++){
// cardinals
moves.add(new Move(x - i, y));
moves.add(new Move(x + i, y));
moves.add(new Move(x, y - i));
moves.add(new Move(x, y + i));
if (add(moves, new Move(x - i, y)))
break;
}
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;
}
protected ArrayList<Move> getDiagonalMoves(int length){
ArrayList<Move> moves = new ArrayList<Move>();
// diagonals
for (int i = 1; i <= length; i++){
// cardinals
moves.add(new Move(x - i, y - i));
moves.add(new Move(x + i, y + i));
moves.add(new Move(x + i, y - 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;
}
for (int i = 1; i <= length; i++){
if (add(moves, new Move(x + i, y + i)))
break;
}
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>();
moves.addAll(super.getCardinalMoves(1));
moves.addAll(super.getDiagonalMoves(1));
if (isFirstMove){
if (isFirstMove()){
ChessPiece rook = null;
// castling
if (isWhite){
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))
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 {
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))
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
@ -55,8 +55,9 @@ public class King extends ChessPiece {
private void castleRight(){
// casting has to move the rook on the right size of the king from white's perspective
if (this.isWhite)
b.set(b.size()-3, 0, b.get(b.size()-1, 0));
b.movePiece(b.size() - 1, 0, b.size() - 3, 0);
else
b.movePiece(b.size()-1, b.size()-1, b.size()-3, 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
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
moves.add(new Move(x + 2, y + 1));
moves.add(new Move(x + 2, y - 1));
moves.add(new Move(x - 2, y + 1));
moves.add(new Move(x - 2, y - 1));
moves.add(new Move(x - 1, y - 2));
moves.add(new Move(x + 1, y - 2));
moves.add(new Move(x - 1, y + 2));
moves.add(new Move(x + 1, y + 2));
return moves;
moves.add(b.checkIfMoveValid(new Move(x + 2, y + 1), isWhite));
moves.add(b.checkIfMoveValid(new Move(x + 2, y - 1), isWhite));
moves.add(b.checkIfMoveValid(new Move(x - 2, y + 1), isWhite));
moves.add(b.checkIfMoveValid(new Move(x - 2, y - 1), isWhite));
moves.add(b.checkIfMoveValid(new Move(x - 1, y - 2), isWhite));
moves.add(b.checkIfMoveValid(new Move(x + 1, y - 2), isWhite));
moves.add(b.checkIfMoveValid(new Move(x - 1, y + 2), isWhite));
moves.add(b.checkIfMoveValid(new Move(x + 1, y + 2), isWhite));
return prune(moves);
}
}

View File

@ -24,38 +24,45 @@ public class Pawn extends ChessPiece {
public ArrayList<Move> getMoves() {
ArrayList<Move> moves = new ArrayList<Move>();
if (isWhite) {
moves.add(new Move(x, y + 1));
if (isFirstMove)
moves.add(new Move(x, y + 2));
moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y + 1)), isWhite));
if (isFirstMove())
moves.add(b.checkIfMoveValid(new Move(x, y + 2), isWhite));
} else {
moves.add(new Move(x, y - 1));
if (isFirstMove)
moves.add(new Move(x, y - 2));
moves.add(b.checkIfMoveValid(pawnForwardMoveValid(new Move(x, y - 1)), isWhite));
if (isFirstMove())
moves.add(b.checkIfMoveValid(new Move(x, y - 2), isWhite));
}
ChessPiece neighbour = null;
if (isWhite){
// En passant
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
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 {
// unfortunately have to flip the direction depending on player type
// En passant
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
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){
return neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove() && neighbour.isWhite != this.isWhite;
return neighbour instanceof Pawn && neighbour.isSecondMove() && neighbour.isWhite != this.isWhite;
}
@Override
@ -69,15 +76,15 @@ public class Pawn extends ChessPiece {
private void enPassantLeft(){
if (isWhite)
b.set(x-1, y, null);
b.set(x, y-1, null);
else
b.set(x+1, y, null);
b.set(x, y+1, null);
}
private void enPassantRight(){
if (isWhite)
b.set(x+1, y, null);
System.out.println(b.set(x, y-1, null));
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>();
moves.addAll(super.getCardinalMoves(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
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) {
int localPointX = (selectionPointX - xOffset) / width;
int localPointY = (b.size() - 1) - ((selectionPointY - yOffset) / height);
int localPointY = ((selectionPointY - yOffset) / height);
for (Move m : moves){
if (m.getX() == localPointX && m.getY() == 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)
if (selectionPointX != -1 && selectionPointY != -1){
int localPointX = (selectionPointX - xOffset) / width;
int localPointY = (b.size()-1) - ((selectionPointY - yOffset) / height);
int localPointY = ((selectionPointY - yOffset) / height);
var piece = b.get(localPointX, localPointY);
if (piece != null) {
System.out.println(localPointX + " LL " + localPointY);
movingPointX = localPointX;
movingPointY = localPointY;
}
@ -80,7 +81,7 @@ public class Display extends JFrame implements MouseListener {
}
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(){