Merge branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project
This commit is contained in:
commit
eaba4bb370
|
|
@ -5,8 +5,6 @@ import backend.PieceType;
|
|||
import windowInterface.MyInterface;
|
||||
|
||||
|
||||
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -35,6 +35,11 @@ public class Board {
|
|||
return this.isWhiteTurn;
|
||||
}
|
||||
|
||||
public void resetTurn() {
|
||||
this.turnNumber = 0;
|
||||
this.isWhiteTurn = true;
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
Piece newPiece = new Piece(x, y, isWhite, type);
|
||||
pieces.add(newPiece);
|
||||
|
|
@ -180,18 +185,17 @@ public class Board {
|
|||
selectedY = -1;
|
||||
highlightedSquares.clear();
|
||||
|
||||
if (isKingInCheck(true)) {
|
||||
System.out.println("White is in check!");
|
||||
}
|
||||
if (isKingInCheck(false)) {
|
||||
System.out.println("Black is in check!");
|
||||
}
|
||||
// After move completed, check for check and checkmate
|
||||
for (int i = 0; i < 2; i++) {
|
||||
boolean isWhite = (i == 0);
|
||||
|
||||
if (isCheckmate(true)) {
|
||||
System.out.println("White is in checkmate!");
|
||||
if (isKingInCheck(isWhite)) {
|
||||
System.out.println((isWhite ? "White" : "Black") + " is in check!");
|
||||
|
||||
if (isCheckmate(isWhite)) {
|
||||
System.out.println((isWhite ? "White" : "Black") + " is in checkmate!");
|
||||
}
|
||||
}
|
||||
if (isCheckmate(false)) {
|
||||
System.out.println("Black is in checkmate!");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -341,7 +345,12 @@ public class Board {
|
|||
boolean kingInCheck = isKingInCheck(whiteKing);
|
||||
boolean hasEscape = false;
|
||||
|
||||
if (kingInCheck) {
|
||||
// 1. If the king is not in check, it's never checkmate
|
||||
if (!kingInCheck) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Try every possible move of every piece belonging to the checked side
|
||||
for (int i = 0; i < pieces.size(); i++) {
|
||||
Piece piece = pieces.get(i);
|
||||
|
||||
|
|
@ -353,10 +362,10 @@ public class Board {
|
|||
int newX = move[0];
|
||||
int newY = move[1];
|
||||
|
||||
// Simulate the board
|
||||
// 3. Simulate this move on a copied board
|
||||
Board simBoard = new Board(this);
|
||||
|
||||
// Find the same piece in the simulated board
|
||||
// 4. Find the corresponding piece on the cloned board
|
||||
Piece simPiece = null;
|
||||
for (int k = 0; k < simBoard.getPieces().size(); k++) {
|
||||
Piece p = simBoard.getPieces().get(k);
|
||||
|
|
@ -367,6 +376,7 @@ public class Board {
|
|||
}
|
||||
}
|
||||
|
||||
// 5. Apply the move and check if king is still in check
|
||||
if (simPiece != null) {
|
||||
Piece captured = simBoard.getPieceAt(newX, newY);
|
||||
if (captured != null) {
|
||||
|
|
@ -376,24 +386,59 @@ public class Board {
|
|||
simPiece.setX(newX);
|
||||
simPiece.setY(newY);
|
||||
|
||||
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
|
||||
|
||||
if (!stillInCheck) {
|
||||
if (!simBoard.isKingInCheck(whiteKing)) {
|
||||
hasEscape = true;
|
||||
}
|
||||
System.out.println("ESCAPE FOUND: " + piece.getType() + " from (" + piece.getX() + "," + piece.getY() + ") to (" + newX + "," + newY + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean checkmate = false;
|
||||
|
||||
if (kingInCheck && !hasEscape) {
|
||||
checkmate = true;
|
||||
// 6. If the king is in check and no move avoids it → checkmate
|
||||
return kingInCheck && !hasEscape;
|
||||
}
|
||||
|
||||
return checkmate;
|
||||
|
||||
/*
|
||||
private void enPassant(Board board, List<Move> moves) {
|
||||
int x = this.x;
|
||||
int y = this.y;
|
||||
|
||||
if (isWhite() == true && this.y == 3) {
|
||||
if(isEnPassant(board, new Piece(x - 1, y)))
|
||||
moves.add(new Move(this, new Piece(x - 1, y - 1),
|
||||
board.getPieceAt(new Piece(x - 1, y))));
|
||||
if(canCaptureEnPassant(board, new Piece(x + 1, y)))
|
||||
moves.add(new Move(this, new Piece(x + 1, y - 1),
|
||||
board.getPieceAt(new Piece(x + 1, y))));
|
||||
}
|
||||
if (isWhite() == false && this.y == 4) {
|
||||
if(isEnPassant(board, new Piece(x - 1, y)))
|
||||
moves.add(new Move(this, new Piece(x - 1, y + 1),
|
||||
board.getPieceAt(new Piece(x - 1, y))));
|
||||
if(canCaptureEnPassant(board, new Piece(x + 1, y)))
|
||||
moves.add(new Move(this, new Piece(x + 1, y + 1),
|
||||
board.getPieceAt(new Piece(x + 1, y))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
* Checks if the pawn can capture another pawn by en passant
|
||||
* @param pt location of the other pawn
|
||||
* @return true if can be captured
|
||||
|
||||
|
||||
private boolean isEnPassant(Board board, Point pt) {
|
||||
Piece temp = board.getPieceAt(pt);
|
||||
if(temp != null) {
|
||||
if (temp instanceof Pawn && temp.getColor() != this.color)
|
||||
if (((Pawn)temp).enPassantOk)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@ public class Game extends Thread {
|
|||
public void setDefaultSetup() {
|
||||
board.cleanBoard();
|
||||
board.populateBoard();
|
||||
board.resetTurn();
|
||||
}
|
||||
|
||||
public void setBoard(String[] array) {
|
||||
|
|
|
|||
|
|
@ -181,7 +181,7 @@ public class MoveConditions {
|
|||
if (target == null || target.isWhite() != isWhite) {
|
||||
Board simBoard = new Board(board);
|
||||
|
||||
// Find this same King in the simulated board
|
||||
// Safely locate the cloned king
|
||||
Piece simKing = null;
|
||||
for (int j = 0; j < simBoard.getPieces().size(); j++) {
|
||||
Piece p = simBoard.getPieces().get(j);
|
||||
|
|
@ -201,8 +201,7 @@ public class MoveConditions {
|
|||
simKing.setX(newX);
|
||||
simKing.setY(newY);
|
||||
|
||||
boolean stillInCheck = simBoard.isKingInCheck(isWhite);
|
||||
if (!stillInCheck) {
|
||||
if (!simBoard.isKingInCheck(isWhite)) {
|
||||
moves.add(new int[]{newX, newY});
|
||||
}
|
||||
}
|
||||
|
|
@ -214,4 +213,5 @@ public class MoveConditions {
|
|||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package backend;
|
||||
package backend;
|
||||
|
||||
public class Piece {
|
||||
private int x;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
/*package backend;
|
||||
|
||||
public class SpecialMoves {
|
||||
<<<<<<< HEAD
|
||||
|
||||
private Piece piece;
|
||||
private Board board;
|
||||
|
||||
|
||||
private Piece piece;
|
||||
private Board board;
|
||||
private int x = piece.getX();
|
||||
|
|
@ -21,12 +27,14 @@ public class SpecialMoves {
|
|||
int[][] offsets = {{1, 2}, {-1, 2}};
|
||||
}*/
|
||||
|
||||
|
||||
/* public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) {
|
||||
if (type == PieceType.Pawn || isWhite == true || y == 3) {
|
||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
|
||||
int[][] offsets = {{1, 0}, {-1, 0}};
|
||||
|
||||
}
|
||||
if (type == PieceType.Pawn || isWhite == false || y == 4) {
|
||||
if (type == PieceType.Pawn && isWhite == false && y == 4) {
|
||||
int[][] offsets = {{1, 0}, {-1, 0}};
|
||||
}
|
||||
|
||||
|
|
@ -36,11 +44,15 @@ public class SpecialMoves {
|
|||
|
||||
public boolean isEnpassant() {
|
||||
boolean isWhite = piece.isWhite();
|
||||
<<<<<<< HEAD
|
||||
if (boolean isWhite == true && PieceType getType() == PieceType.Pawn) { //no idea honestly
|
||||
=======
|
||||
if (isWhite == true || PieceType getType() == PieceType.Pawn) { //no idea honestly
|
||||
>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
|
||||
if ()
|
||||
|
||||
}
|
||||
if (boolean isWhite == false|| //PieceType type == Pawn) {
|
||||
if (boolean isWhite == false && //PieceType type == Pawn) {
|
||||
if ()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue