Merge branch 'master' of

https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
Louise BERTELOOT 2025-05-13 14:13:31 +02:00
commit 1a828a991b
6 changed files with 153 additions and 47 deletions

View File

@ -1,3 +1,4 @@
eclipse.preferences.version=1
encoding//src/Main.java=UTF-8
encoding//src/backend/Board.java=UTF-8
encoding/<project>=windows-1252

View File

@ -5,6 +5,13 @@ import backend.PieceType;
import windowInterface.MyInterface;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
public class Main {
@ -25,6 +32,14 @@ public class Main {
MyInterface mjf = new MyInterface();
mjf.setVisible(true);
//print the table for the memory
}
}
}

View File

@ -202,44 +202,42 @@ public class Board {
/* saving-loading feature :*/
public String[] toFileRep() {
ArrayList<String> lines = new ArrayList<>();
ArrayList<String> lines = new ArrayList<>();
lines.add(String.valueOf(turnNumber)); //nombre xe tours
lines.add(String.valueOf(isWhiteTurn)); //couleur du joueur
// Line 0 : number tour
lines.add(String.valueOf(turnNumber));
for (Piece piece : pieces) { //ajoute la pièce à la mémoire
String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite();
lines.add(line);
}
System.out.println("The modifications are: " + lines);
return lines.toArray(new String[0]);
}
// Line 1 : color of the player
lines.add(String.valueOf(isWhiteTurn));
// Line 2+: each piece
for (Piece piece : pieces) {
String line = piece.getType() + "," + piece.getX() + "," + piece.getY() + "," + piece.isWhite();
lines.add(line);
}
return lines.toArray(new String[0]);
}
public Board(String[] array) {
this.colNum = 8;
this.lineNum = 8;
this.colNum = 8;
this.lineNum = 8;
this.turnNumber = Integer.parseInt(array[0]);
this.isWhiteTurn = Boolean.parseBoolean(array[1]);
this.pieces = new ArrayList<>();
for (int i = 2; i < array.length; i++) {
String[] parts = array[i].split(",");
PieceType type = PieceType.valueOf(parts[0]);
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
boolean isWhite = Boolean.parseBoolean(parts[3]);
PieceType type = PieceType.valueOf(parts[0]);
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
boolean isWhite = Boolean.parseBoolean(parts[3]);
pieces.add(new Piece(x, y, isWhite, type));}
System.out.println("The modifications are: " + pieces);
pieces.add(new Piece(x, y, isWhite, type));
}
}
/* The following methods require more work ! */
@ -317,9 +315,10 @@ public class Board {
if (king != null) {
int kingX = king.getX();
int kingY = king.getY();
for (int i = 0; i < pieces.size(); i++) {
Piece p = pieces.get(i);
if (p.isWhite() != whiteKing) {
if (p.isWhite() != whiteKing && p.getType() != PieceType.King) {
ArrayList<int[]> moves = getValidMoves(p);
for (int j = 0; j < moves.size(); j++) {
@ -344,29 +343,41 @@ public class Board {
Piece piece = pieces.get(i);
if (piece.isWhite() == whiteKing) {
ArrayList<int[]> moves = getValidMoves(piece);
ArrayList<int[]> rawMoves = getValidMoves(piece);
for (int j = 0; j < moves.size(); j++) {
int[] move = moves.get(j);
for (int j = 0; j < rawMoves.size(); j++) {
int[] move = rawMoves.get(j);
int newX = move[0];
int newY = move[1];
// Simulate the board
Board simBoard = new Board(this);
Piece simPiece = simBoard.getPieceAt(piece.getX(), piece.getY());
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
// Find the same piece in the simulated board
Piece simPiece = null;
for (int k = 0; k < simBoard.getPieces().size(); k++) {
Piece p = simBoard.getPieces().get(k);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
}
simPiece.setX(newX);
simPiece.setY(newY);
if (simPiece != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
simPiece.setX(newX);
simPiece.setY(newY);
if (!stillInCheck) {
hasEscape = true;
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
if (!stillInCheck) {
hasEscape = true;
}
}
}
}
@ -381,5 +392,42 @@ public class Board {
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;
}
}

View File

@ -167,22 +167,51 @@ public class MoveConditions {
int[][] offsets = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
for (int[] offset : offsets) {
int newX = x + offset[0];
int newY = y + offset[1];
for (int i = 0; i < offsets.length; i++) {
int newX = x + offsets[i][0];
int newY = y + offsets[i][1];
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) {
moves.add(new int[]{newX, newY});
Board simBoard = new Board(board);
// Find this same King in the simulated board
Piece simKing = null;
for (int j = 0; j < simBoard.getPieces().size(); j++) {
Piece p = simBoard.getPieces().get(j);
if (p.getX() == x && p.getY() == y &&
p.getType() == PieceType.King &&
p.isWhite() == isWhite) {
simKing = p;
}
}
if (simKing != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
simKing.setX(newX);
simKing.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(isWhite);
if (!stillInCheck) {
moves.add(new int[]{newX, newY});
}
}
}
}
}
return moves;
}
}

View File

@ -1,4 +1,4 @@
package backend;
package backend;
public class Piece {
private int x;
@ -38,4 +38,8 @@ public class Piece {
this.y = y;
}
public String toString() {
return (isWhite ? "White " : "Black ") + type + " at (" + x + "," + y + ")";
}
}

View File

@ -1,4 +1,4 @@
package backend;
/*package backend;
public class SpecialMoves {
<<<<<<< HEAD
@ -27,8 +27,13 @@ public class SpecialMoves {
int[][] offsets = {{1, 2}, {-1, 2}};
}*/
<<<<<<< HEAD
public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) {
if (type == PieceType.Pawn && isWhite == true && y == 3) {
=======
/* 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}};
}
@ -42,7 +47,11 @@ 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 ()
}
@ -51,4 +60,4 @@ public class SpecialMoves {
}
}
}
}*/