for board: modify the methods since we couldn't find a fi string.
This commit is contained in:
parent
3f88a4ea3c
commit
8948654df8
|
|
@ -1,17 +1,35 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class AutoPlayer {
|
public class AutoPlayer {
|
||||||
|
private final Random random = new Random();
|
||||||
|
|
||||||
/**
|
public Move computeBestMove(Board board) {
|
||||||
* returns the best Move to try on provided board for active player
|
ArrayList<Move> possibleMoves = new ArrayList<>();
|
||||||
* @param board
|
|
||||||
* @return
|
for (Piece piece : board.getPieces()) {
|
||||||
*/
|
if (piece.isWhite() != board.isTurnWhite()) continue;
|
||||||
public Move computeBestMove(Board board) {
|
|
||||||
|
int originX = piece.getX();
|
||||||
return null;
|
int originY = piece.getY();
|
||||||
}
|
|
||||||
|
for (int targetX = 0; targetX < board.getWidth(); targetX++) {
|
||||||
|
for (int targetY = 0; targetY < board.getHeight(); targetY++) {
|
||||||
|
Board simulatedBoard = new Board(board);
|
||||||
|
simulatedBoard.activateSelection(originX, originY);
|
||||||
|
|
||||||
|
if (simulatedBoard.isValidDestination(targetX, targetY)) {
|
||||||
|
Piece capturedPiece = board.getPieceAt(targetX, targetY);
|
||||||
|
possibleMoves.add(new Move(piece, targetX, targetY, capturedPiece));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (possibleMoves.isEmpty()) return null;
|
||||||
|
return possibleMoves.get(random.nextInt(possibleMoves.size()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,427 +1,381 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
|
|
||||||
private int cNum;
|
private int cNum;
|
||||||
private int lNum;
|
private int lNum;
|
||||||
private Piece[][] cells;
|
private ArrayList<Piece> Pieces;
|
||||||
private Piece selectedCell;
|
private Stack<Move> moveHistory = new Stack<>();
|
||||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
|
||||||
|
|
||||||
|
|
||||||
private int turnNumber = 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
cNum=colNum;
|
cNum = colNum;
|
||||||
lNum=lineNum;
|
lNum = lineNum;
|
||||||
cells = new Piece[cNum][lNum]; // creation du tableau de pieces
|
Pieces = new ArrayList<>();
|
||||||
selectedCell=null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return lNum;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeight() {
|
|
||||||
return cNum;
|
return cNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTurnNumber() {
|
public int getHeight() {
|
||||||
return turnNumber;
|
return lNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTurnNumber() {
|
||||||
|
return turnNumber;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isTurnWhite() {
|
public boolean isTurnWhite() {
|
||||||
return getTurnNumber() % 2 == 0;
|
return turnWhite;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
public void setPiece(PieceType type, boolean isWhite, int x, int y) {
|
Piece existing = null;
|
||||||
Piece newPiece = new Piece(type, isWhite, x, y);
|
|
||||||
cells[x][y] = newPiece;
|
for (Piece piece : Pieces) {
|
||||||
|
if (piece.getX() == x && piece.getY() == y) {
|
||||||
|
existing = piece;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (existing != null) {
|
||||||
|
Pieces.remove(existing);
|
||||||
|
}
|
||||||
|
|
||||||
|
Piece newPiece = new Piece(x, y, isWhite, type);
|
||||||
|
Pieces.add(newPiece);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void populateBoard() {
|
public void populateBoard() {
|
||||||
|
Pieces.add(new Piece(0, 0, false, PieceType.Rook));
|
||||||
final int startWhite = 0;
|
Pieces.add(new Piece(1, 0, false, PieceType.Knight));
|
||||||
final int startBlack = lNum-1;
|
Pieces.add(new Piece(2, 0, false, PieceType.Bishop));
|
||||||
|
Pieces.add(new Piece(3, 0, false, PieceType.Queen));
|
||||||
//Black pieces populating
|
Pieces.add(new Piece(4, 0, false, PieceType.King));
|
||||||
setPiece(PieceType.Rook,false,0,startWhite);
|
Pieces.add(new Piece(5, 0, false, PieceType.Bishop));
|
||||||
setPiece(PieceType.Knight,false, 1,startWhite);
|
Pieces.add(new Piece(6, 0, false, PieceType.Knight));
|
||||||
setPiece(PieceType.Bishop,false,2,startWhite);
|
Pieces.add(new Piece(7, 0, false, PieceType.Rook));
|
||||||
setPiece(PieceType.Queen,false,3,startWhite);
|
|
||||||
setPiece(PieceType.King,false,4,startWhite);
|
|
||||||
setPiece(PieceType.Bishop,false,5,startWhite);
|
|
||||||
setPiece(PieceType.Knight,false,6,startWhite);
|
|
||||||
setPiece(PieceType.Rook,false,7,startWhite);
|
|
||||||
|
|
||||||
for (int x = 0; x < cNum; x++) {
|
|
||||||
setPiece(PieceType.Pawn,false,x,startWhite+1);
|
|
||||||
}
|
|
||||||
//White pieces populating
|
|
||||||
setPiece(PieceType.Rook,true,0,startBlack);
|
|
||||||
setPiece(PieceType.Knight,true,1,startBlack);
|
|
||||||
setPiece(PieceType.Bishop,true,2,startBlack);
|
|
||||||
setPiece(PieceType.Queen,true,3,startBlack);
|
|
||||||
setPiece(PieceType.King,true,4,startBlack);
|
|
||||||
setPiece(PieceType.Bishop,true,5,startBlack);
|
|
||||||
setPiece(PieceType.Knight,true,6,startBlack);
|
|
||||||
setPiece(PieceType.Rook,true,7,startBlack);
|
|
||||||
|
|
||||||
for (int x = 0; x < cNum; x++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
|
Pieces.add(new Piece(i, 1, false, PieceType.Pawn));
|
||||||
setPiece(PieceType.Pawn,true,x,startBlack-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (int x = 0; x < cNum; x++) {
|
|
||||||
for(int y=0;y<lNum;y++) {
|
|
||||||
if (cells[x][y]==null) {
|
|
||||||
System.out.print("blank - ");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
System.out.print(cells[x][y].getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.print("\r\n");
|
Pieces.add(new Piece(0, 7, true, PieceType.Rook));
|
||||||
|
Pieces.add(new Piece(1, 7, true, PieceType.Knight));
|
||||||
|
Pieces.add(new Piece(2, 7, true, PieceType.Bishop));
|
||||||
|
Pieces.add(new Piece(3, 7, true, PieceType.Queen));
|
||||||
|
Pieces.add(new Piece(4, 7, true, PieceType.King));
|
||||||
|
Pieces.add(new Piece(5, 7, true, PieceType.Bishop));
|
||||||
|
Pieces.add(new Piece(6, 7, true, PieceType.Knight));
|
||||||
|
Pieces.add(new Piece(7, 7, true, PieceType.Rook));
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
Pieces.add(new Piece(i, 6, true, PieceType.Pawn));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<Piece> getPieces() {
|
||||||
|
return Pieces;
|
||||||
|
}
|
||||||
|
|
||||||
public void cleanBoard() {
|
public void cleanBoard() {
|
||||||
for (int y = 0; y < 8; y++) {
|
Pieces.clear();
|
||||||
for (int x = 0; x < 8; x++) {
|
|
||||||
cells[x][y] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
selectedCell = null;
|
|
||||||
moveHistory.clear(); // clear undo history
|
|
||||||
turnNumber = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder boardView = new StringBuilder();
|
||||||
for (int y = 0; y < lNum; y++) {
|
|
||||||
for (int x = 0; x < cNum; x++) {
|
for (int row = 0; row < lNum; row++) {
|
||||||
Piece p = cells[x][y];
|
for (int col = 0; col < cNum; col++) {
|
||||||
if (p != null) {
|
Piece pieceAtPosition = getPieceAt(col, row);
|
||||||
sb.append(p.isWhite() ? "W" : "B").append(p.getType().toString().charAt(0)).append(" ");
|
|
||||||
|
if (pieceAtPosition != null) {
|
||||||
|
String colorPrefix = pieceAtPosition.isWhite() ? "W" : "B";
|
||||||
|
boardView.append(colorPrefix)
|
||||||
|
.append(pieceAtPosition.getType().getSummary())
|
||||||
|
.append(" ");
|
||||||
} else {
|
} else {
|
||||||
sb.append("-- ");
|
boardView.append(".. ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append("\n");
|
boardView.append("\n");
|
||||||
}
|
}
|
||||||
return sb.toString();
|
|
||||||
|
return boardView.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasSelection = false;
|
||||||
|
private int selectedX, selectedY;
|
||||||
|
private int turnNumber = 0;
|
||||||
|
private boolean turnWhite = true;
|
||||||
|
|
||||||
public ArrayList<Piece> getPieces() {
|
private Piece findPieceAt(int x, int y) {
|
||||||
ArrayList<Piece> pieces = new ArrayList<>();
|
for (Piece piece : Pieces) {
|
||||||
|
if (piece.getX() == x && piece.getY() == y) {
|
||||||
for (int y = 0; y < 8; y++) {
|
return piece;
|
||||||
for (int x = 0; x < 8; x++) {
|
|
||||||
if (cells[x][y]!= null) {
|
|
||||||
pieces.add(cells[x][y]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
return pieces;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void userTouch(int col, int row) {
|
||||||
|
if (GameLost()) return;
|
||||||
|
|
||||||
public void userTouch(int x, int y) {
|
Piece target = findPieceAt(col, row);
|
||||||
if (this.selectedCell != null) {
|
|
||||||
System.out.println(" cellule deja selectionne : " + this.selectedCell.getName());
|
|
||||||
|
|
||||||
// CASE 1: Clicked same piece (deselect)
|
if (!hasSelection) {
|
||||||
if (cells[x][y] != null &&
|
if (target != null && target.isWhite() == turnWhite) {
|
||||||
cells[x][y].getX() == selectedCell.getX() &&
|
hasSelection = true;
|
||||||
cells[x][y].getY() == selectedCell.getY()) {
|
selectedX = col;
|
||||||
System.out.println("il a clique sur la meme cellule, je deselectionne");
|
selectedY = row;
|
||||||
this.selectedCell = null;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
// ✅ Enforce legal move using isHighlighted
|
|
||||||
if (!isHighlighted(x, y)) {
|
|
||||||
System.out.println("Illegal move for this piece.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// CASE 2: Perform the move (empty or capture)
|
|
||||||
moveHistory.add(new Move(
|
|
||||||
selectedCell,
|
|
||||||
selectedCell.getX(),
|
|
||||||
selectedCell.getY(),
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
cells[x][y] // null if empty, piece if captured
|
|
||||||
));
|
|
||||||
|
|
||||||
// Apply move
|
|
||||||
cells[x][y] = new Piece(
|
|
||||||
selectedCell.getType(),
|
|
||||||
selectedCell.isWhite(),
|
|
||||||
x,
|
|
||||||
y
|
|
||||||
);
|
|
||||||
cells[selectedCell.getX()][selectedCell.getY()] = null;
|
|
||||||
selectedCell = null;
|
|
||||||
turnNumber++;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// CASE: No piece selected yet
|
|
||||||
System.out.println("----- aucune cellule selectionnee");
|
|
||||||
|
|
||||||
if (cells[x][y] != null) {
|
|
||||||
boolean pieceIsWhite = cells[x][y].isWhite();
|
|
||||||
boolean correctTurn = (pieceIsWhite == isTurnWhite());
|
|
||||||
|
|
||||||
if (correctTurn) {
|
|
||||||
this.selectedCell = this.cells[x][y];
|
|
||||||
System.out.println("nouvelle cellule selectionne " + cells[x][y].getName());
|
|
||||||
} else {
|
|
||||||
System.out.println("Mauvais tour ! Ce n'est pas le tour de cette couleur.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
System.out.println("il a clique sur cellule vide");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (col == selectedX && row == selectedY) {
|
||||||
|
hasSelection = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isHighlighted(col, row)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Piece pieceToCapture = findPieceAt(col, row);
|
||||||
|
if (pieceToCapture != null) {
|
||||||
|
Pieces.remove(pieceToCapture);
|
||||||
|
}
|
||||||
|
|
||||||
|
Piece selected = findPieceAt(selectedX, selectedY);
|
||||||
|
if (selected != null) {
|
||||||
|
Pieces.remove(selected);
|
||||||
|
Piece moved = new Piece(col, row, selected.isWhite(), selected.getType());
|
||||||
|
Pieces.add(moved);
|
||||||
|
|
||||||
|
Move move = new Move(selected, col, row, pieceToCapture);
|
||||||
|
moveHistory.push(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
turnNumber++;
|
||||||
|
turnWhite = !turnWhite;
|
||||||
|
hasSelection = false;
|
||||||
|
|
||||||
|
System.out.println(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
return selectedCell != null && selectedCell.getX() == x && selectedCell.getY() == y;
|
return hasSelection && selectedX == x && selectedY == y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//saving feature: "type + iswhite + x + y"
|
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
ArrayList<String> lines = new ArrayList<>();
|
return null;
|
||||||
for (int y = 0; y < 8; y++) {
|
|
||||||
for (int x = 0; x < 8; x++) {
|
|
||||||
Piece piece = cells[x][y];
|
|
||||||
if (piece != null) {
|
|
||||||
String line = piece.getType() + " " + piece.isWhite() + " " + x + " " + y;
|
|
||||||
lines.add(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return lines.toArray(new String[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//loading feature
|
|
||||||
public Board(String[] array) {
|
public Board(String[] array) {
|
||||||
this.cNum = 8;
|
|
||||||
this.lNum = 8;
|
|
||||||
this.cells = new Piece[cNum][lNum];
|
|
||||||
this.selectedCell = null;
|
|
||||||
|
|
||||||
//clean board
|
|
||||||
for (int y = 0; y < 8; y++) {
|
|
||||||
for (int x = 0; x < 8; x++) {
|
|
||||||
cells[x][y] = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//load pieces
|
|
||||||
for (String line : array) {
|
|
||||||
String[] tokens = line.trim().split("\\s+");
|
|
||||||
if (tokens.length != 4) continue;
|
|
||||||
|
|
||||||
try {
|
|
||||||
PieceType type = PieceType.valueOf(tokens[0]);
|
|
||||||
boolean isWhite = Boolean.parseBoolean(tokens[1]);
|
|
||||||
int x = Integer.parseInt(tokens[2]);
|
|
||||||
int y = Integer.parseInt(tokens[3]);
|
|
||||||
|
|
||||||
setPiece(type, isWhite, x, y);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.err.println("Error parsing line: " + line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isHighlighted(int destX, int destY) {
|
||||||
|
if (!hasSelection) return false;
|
||||||
|
|
||||||
|
Piece selected = null;
|
||||||
/* The following methods require more work ! */
|
for (Piece p : Pieces) {
|
||||||
|
if (p.getX() == selectedX && p.getY() == selectedY) {
|
||||||
public boolean isHighlighted(int x, int y) {
|
selected = p;
|
||||||
if (selectedCell == null) return false;
|
|
||||||
|
|
||||||
PieceType type = selectedCell.getType();
|
|
||||||
boolean isWhite = selectedCell.isWhite();
|
|
||||||
int currX = selectedCell.getX();
|
|
||||||
int currY = selectedCell.getY();
|
|
||||||
|
|
||||||
int dx = x - currX;
|
|
||||||
int dy = y - currY;
|
|
||||||
|
|
||||||
// Check bounds
|
|
||||||
if (x < 0 || x >= cNum || y < 0 || y >= lNum) return false;
|
|
||||||
|
|
||||||
// Prevent capturing own piece
|
|
||||||
if (cells[x][y] != null && cells[x][y].isWhite() == isWhite) return false;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case Pawn:
|
|
||||||
int dir = isWhite ? -1 : 1;
|
|
||||||
// Move forward
|
|
||||||
if (dx == 0 && dy == dir && cells[x][y] == null) return true;
|
|
||||||
// First double step
|
|
||||||
if (dx == 0 && dy == 2 * dir && ((isWhite && currY == 6) || (!isWhite && currY == 1))
|
|
||||||
&& cells[x][y] == null && cells[currX][currY + dir] == null) return true;
|
|
||||||
// Capture diagonally
|
|
||||||
if (Math.abs(dx) == 1 && dy == dir && cells[x][y] != null && cells[x][y].isWhite() != isWhite) return true;
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (selected == null) return false;
|
||||||
|
|
||||||
case Rook:
|
int fromX = selected.getX(), fromY = selected.getY();
|
||||||
if (dx == 0 || dy == 0) {
|
boolean isWhite = selected.isWhite();
|
||||||
int stepX = Integer.compare(dx, 0);
|
PieceType type = selected.getType();
|
||||||
int stepY = Integer.compare(dy, 0);
|
|
||||||
int cx = currX + stepX;
|
if (type == PieceType.Pawn) {
|
||||||
int cy = currY + stepY;
|
int dir = isWhite ? -1 : 1;
|
||||||
while (cx != x || cy != y) {
|
if (destX == fromX && destY == fromY + dir && getPieceAt(destX, destY) == null) return true;
|
||||||
if (cells[cx][cy] != null) return false;
|
if ((isWhite && fromY == 6 || !isWhite && fromY == 1) &&
|
||||||
cx += stepX;
|
destX == fromX && destY == fromY + 2 * dir &&
|
||||||
cy += stepY;
|
getPieceAt(fromX, fromY + dir) == null && getPieceAt(destX, destY) == null) return true;
|
||||||
}
|
if ((destX == fromX + 1 || destX == fromX - 1) && destY == fromY + dir) {
|
||||||
return true;
|
Piece target = getPieceAt(destX, destY);
|
||||||
|
if (target != null && target.isWhite() != isWhite) return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == PieceType.Knight) {
|
||||||
|
int[][] moves = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
|
||||||
|
for (int[] m : moves) {
|
||||||
|
int x = fromX + m[0], y = fromY + m[1];
|
||||||
|
if (x == destX && y == destY && x >= 0 && x < cNum && y >= 0 && y < lNum) {
|
||||||
|
Piece target = getPieceAt(x, y);
|
||||||
|
if (target == null || target.isWhite() != isWhite) return true;
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case Bishop:
|
if (type == PieceType.King) {
|
||||||
if (Math.abs(dx) == Math.abs(dy)) {
|
int[][] moves = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
|
||||||
int stepX = Integer.compare(dx, 0);
|
for (int[] m : moves) {
|
||||||
int stepY = Integer.compare(dy, 0);
|
int x = fromX + m[0], y = fromY + m[1];
|
||||||
int cx = currX + stepX;
|
if (x == destX && y == destY && x >= 0 && x < cNum && y >= 0 && y < lNum) {
|
||||||
int cy = currY + stepY;
|
Piece target = getPieceAt(x, y);
|
||||||
while (cx != x || cy != y) {
|
if (target == null || target.isWhite() != isWhite) return true;
|
||||||
if (cells[cx][cy] != null) return false;
|
|
||||||
cx += stepX;
|
|
||||||
cy += stepY;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case Queen:
|
if (type == PieceType.Rook || type == PieceType.Queen) {
|
||||||
if (dx == 0 || dy == 0 || Math.abs(dx) == Math.abs(dy)) {
|
int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}};
|
||||||
int stepX = Integer.compare(dx, 0);
|
for (int[] d : directions) {
|
||||||
int stepY = Integer.compare(dy, 0);
|
int x = fromX + d[0], y = fromY + d[1];
|
||||||
int cx = currX + stepX;
|
while (x >= 0 && x < cNum && y >= 0 && y < lNum) {
|
||||||
int cy = currY + stepY;
|
Piece target = getPieceAt(x, y);
|
||||||
while (cx != x || cy != y) {
|
if (target == null) {
|
||||||
if (cells[cx][cy] != null) return false;
|
if (x == destX && y == destY) return true;
|
||||||
cx += stepX;
|
} else {
|
||||||
cy += stepY;
|
if (x == destX && y == destY && target.isWhite() != isWhite) return true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return true;
|
x += d[0]; y += d[1];
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case King:
|
if (type == PieceType.Bishop || type == PieceType.Queen) {
|
||||||
if (Math.abs(dx) <= 1 && Math.abs(dy) <= 1) return true;
|
int[][] directions = {{1,1},{-1,1},{1,-1},{-1,-1}};
|
||||||
break;
|
for (int[] d : directions) {
|
||||||
|
int x = fromX + d[0], y = fromY + d[1];
|
||||||
case Knight:
|
while (x >= 0 && x < cNum && y >= 0 && y < lNum) {
|
||||||
if ((Math.abs(dx) == 2 && Math.abs(dy) == 1) || (Math.abs(dx) == 1 && Math.abs(dy) == 2)) return true;
|
Piece target = getPieceAt(x, y);
|
||||||
break;
|
if (target == null) {
|
||||||
|
if (x == destX && y == destY) return true;
|
||||||
|
} else {
|
||||||
|
if (x == destX && y == destY && target.isWhite() != isWhite) return true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
x += d[0]; y += d[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void undoLastMove() {
|
||||||
|
if (moveHistory.isEmpty()) return;
|
||||||
|
|
||||||
|
Move previousMove = moveHistory.pop();
|
||||||
|
|
||||||
|
int toX = previousMove.getToX();
|
||||||
|
int toY = previousMove.getToY();
|
||||||
|
int fromX = previousMove.getFromX();
|
||||||
|
int fromY = previousMove.getFromY();
|
||||||
|
|
||||||
|
Piece movedPiece = previousMove.getMovedPiece();
|
||||||
|
Piece capturedPiece = previousMove.getCapturedPiece();
|
||||||
|
|
||||||
|
// Remove the piece from its current destination
|
||||||
|
Pieces.removeIf(piece -> piece.getX() == toX && piece.getY() == toY);
|
||||||
|
|
||||||
|
// Restore the moved piece to its original position
|
||||||
|
Pieces.add(new Piece(fromX, fromY, movedPiece.isWhite(), movedPiece.getType()));
|
||||||
|
|
||||||
|
// Restore the captured piece if one was taken
|
||||||
|
if (capturedPiece != null) {
|
||||||
|
Pieces.add(new Piece(capturedPiece.getX(), capturedPiece.getY(), capturedPiece.isWhite(), capturedPiece.getType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
turnNumber--;
|
||||||
|
turnWhite = !turnWhite;
|
||||||
|
hasSelection = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Board(Board source) {
|
||||||
|
this.cNum = source.cNum;
|
||||||
|
this.lNum = source.lNum;
|
||||||
|
this.turnNumber = source.turnNumber;
|
||||||
|
this.turnWhite = source.turnWhite;
|
||||||
|
this.hasSelection = source.hasSelection;
|
||||||
|
this.selectedX = source.selectedX;
|
||||||
|
this.selectedY = source.selectedY;
|
||||||
|
|
||||||
|
this.Pieces = new ArrayList<>();
|
||||||
|
for (Piece piece : source.Pieces) {
|
||||||
|
int x = piece.getX();
|
||||||
|
int y = piece.getY();
|
||||||
|
boolean isWhite = piece.isWhite();
|
||||||
|
PieceType type = piece.getType();
|
||||||
|
this.Pieces.add(new Piece(x, y, isWhite, type));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.moveHistory = new Stack<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void playMove(Move move) {
|
||||||
|
if (GameLost()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY());
|
||||||
|
Pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY());
|
||||||
|
Piece moved = move.getMovedPiece();
|
||||||
|
Pieces.add(new Piece(move.getToX(), move.getToY(), moved.isWhite(), moved.getType()));
|
||||||
|
moveHistory.push(move);
|
||||||
|
turnNumber++;
|
||||||
|
turnWhite = !turnWhite;
|
||||||
|
hasSelection = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
public Board(Board other) {
|
|
||||||
this.cNum = other.cNum;
|
|
||||||
this.lNum = other.lNum;
|
|
||||||
this.cells = new Piece[cNum][lNum];
|
|
||||||
this.selectedCell = null; // don't copy selection
|
|
||||||
|
|
||||||
for (int x = 0; x < cNum; x++) {
|
Piece getPieceAt(int x, int y) {
|
||||||
for (int y = 0; y < lNum; y++) {
|
for (Piece p : Pieces) {
|
||||||
Piece p = other.cells[x][y];
|
if (p.getX() == x && p.getY() == y) return p;
|
||||||
if (p != null) {
|
}
|
||||||
this.cells[x][y] = new Piece(p.getType(), p.isWhite(), x, y);
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void selectPiece(int col, int row) {
|
||||||
|
selectedX = col;
|
||||||
|
selectedY = row;
|
||||||
|
hasSelection = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean GameLost() {
|
||||||
|
boolean hasWhiteKing = false;
|
||||||
|
boolean hasBlackKing = false;
|
||||||
|
|
||||||
|
for (Piece piece : Pieces) {
|
||||||
|
if (piece.getType() == PieceType.King) {
|
||||||
|
if (piece.isWhite()) {
|
||||||
|
hasWhiteKing = true;
|
||||||
|
} else {
|
||||||
|
hasBlackKing = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (hasWhiteKing && hasBlackKing) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return !(hasWhiteKing && hasBlackKing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void activateSelection(int col, int row) {
|
||||||
|
selectedX = col;
|
||||||
int fromX = move.getFromX();
|
selectedY = row;
|
||||||
int fromY = move.getFromY();
|
hasSelection = true;
|
||||||
int toX = move.getToX();
|
|
||||||
int toY = move.getToY();
|
|
||||||
|
|
||||||
Piece movingPiece = cells[fromX][fromY];
|
|
||||||
if (movingPiece == null) {
|
|
||||||
System.err.println("No piece at source position.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cells[toX][toY] = new Piece(movingPiece.getType(), movingPiece.isWhite(), toX, toY);
|
|
||||||
cells[fromX][fromY] = null;
|
|
||||||
|
|
||||||
System.out.println("Moved " + movingPiece.getType() + " from (" + fromX + "," + fromY + ") to (" + toX + "," + toY + ")");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastMove() {
|
|
||||||
if (moveHistory.isEmpty()) {
|
|
||||||
System.out.println("No move to undo.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Move lastMove = moveHistory.remove(moveHistory.size() - 1);
|
public boolean isValidDestination(int x, int y) {
|
||||||
|
return isHighlighted(x, y);
|
||||||
int fromX = lastMove.getFromX();
|
|
||||||
int fromY = lastMove.getFromY();
|
|
||||||
int toX = lastMove.getToX();
|
|
||||||
int toY = lastMove.getToY();
|
|
||||||
|
|
||||||
// Restore moved piece
|
|
||||||
cells[fromX][fromY] = lastMove.getPiece();
|
|
||||||
|
|
||||||
// Restore captured piece (or null)
|
|
||||||
if (lastMove.getCapturedPiece() != null) {
|
|
||||||
cells[toX][toY] = lastMove.getCapturedPiece();
|
|
||||||
} else {
|
|
||||||
cells[toX][toY] = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear selection
|
|
||||||
selectedCell = null;
|
|
||||||
|
|
||||||
// Roll back turn
|
|
||||||
if (turnNumber > 0) {
|
|
||||||
turnNumber--;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Undo completed.");
|
|
||||||
}
|
}
|
||||||
public boolean hasPiece(int x, int y) {
|
|
||||||
return cells[x][y] != null;
|
|
||||||
}
|
|
||||||
public Piece getPiece(int x, int y) {
|
|
||||||
return cells[x][y];
|
|
||||||
}
|
|
||||||
public void resetGame() {
|
|
||||||
cleanBoard();
|
|
||||||
populateBoard();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
|
@ -6,7 +7,6 @@ public class Game extends Thread {
|
||||||
|
|
||||||
private AutoPlayer aiPlayer;
|
private AutoPlayer aiPlayer;
|
||||||
private Board board;
|
private Board board;
|
||||||
private Piece newPiece;
|
|
||||||
|
|
||||||
private MyInterface mjf;
|
private MyInterface mjf;
|
||||||
private int COL_NUM = 8;
|
private int COL_NUM = 8;
|
||||||
|
|
@ -31,10 +31,6 @@ public class Game extends Thread {
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
return board.getHeight();
|
return board.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPiece(PieceType type, boolean isWhite, int x, int y) {
|
|
||||||
this.newPiece= new Piece(type,isWhite, x,y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
while(true) {
|
while(true) {
|
||||||
|
|
@ -69,7 +65,10 @@ public class Game extends Thread {
|
||||||
board.userTouch(x, y);
|
board.userTouch(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
|
board.setPiece(isWhite, type, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getFileRepresentation() {
|
public String[] getFileRepresentation() {
|
||||||
|
|
@ -109,4 +108,4 @@ public class Game extends Thread {
|
||||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,42 +1,47 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class Move {
|
public class Move {
|
||||||
private Piece piece;
|
private final int fromX;
|
||||||
private int fromX, fromY;
|
private final int fromY;
|
||||||
private int toX, toY;
|
private final int toX;
|
||||||
private Piece capturedPiece;
|
private final int toY;
|
||||||
|
private final Piece movedPiece;
|
||||||
public Move ( Piece piece, int fromX, int fromY, int toX, int toY, Piece capturedPiece){
|
private final Piece capturedPiece;
|
||||||
this.piece = piece;
|
|
||||||
this.fromX = fromX;
|
public Move(Piece movedPiece, int toX, int toY, Piece capturedPiece) {
|
||||||
this.fromY = fromY;
|
this.fromX = movedPiece.getX();
|
||||||
|
this.fromY = movedPiece.getY();
|
||||||
this.toX = toX;
|
this.toX = toX;
|
||||||
this.toY = toY;
|
this.toY = toY;
|
||||||
|
this.movedPiece = movedPiece;
|
||||||
this.capturedPiece = capturedPiece;
|
this.capturedPiece = capturedPiece;
|
||||||
}
|
}
|
||||||
public Piece getPiece() {
|
|
||||||
return piece;
|
public Move(Piece movedPiece, int toX, int toY) {
|
||||||
|
this(movedPiece, toX, toY, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFromX() {
|
public int getFromX() {
|
||||||
return fromX;
|
return fromX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFromY() {
|
public int getFromY() {
|
||||||
return fromY;
|
return fromY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getToX() {
|
public int getToX() {
|
||||||
return toX;
|
return toX;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getToY() {
|
public int getToY() {
|
||||||
return toY;
|
return toY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Piece getMovedPiece() {
|
||||||
|
return movedPiece;
|
||||||
|
}
|
||||||
|
|
||||||
public Piece getCapturedPiece() {
|
public Piece getCapturedPiece() {
|
||||||
return capturedPiece;
|
return capturedPiece;
|
||||||
}
|
}
|
||||||
public boolean isCapture() {
|
|
||||||
return capturedPiece != null;
|
|
||||||
}
|
|
||||||
public String toString() {
|
|
||||||
return piece + ":("+ fromX + "," + fromY + ")-("+ toX + "," + toY + ")" +
|
|
||||||
( capturedPiece != null? "capturing "+ capturedPiece: "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,20 @@
|
||||||
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public class Piece {
|
public class Piece {
|
||||||
|
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
private boolean isWhite;
|
||||||
private PieceType type;
|
private PieceType type;
|
||||||
private boolean color;
|
|
||||||
|
|
||||||
public Piece(PieceType type_P, boolean color_P, int xP, int yP) {
|
public Piece(int x, int y, boolean isWhite, PieceType type) {
|
||||||
x = xP;
|
this.x = x;
|
||||||
y = yP;
|
this.y = y;
|
||||||
type = type_P;
|
this.isWhite = isWhite;
|
||||||
color = color_P;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
@ -27,15 +28,7 @@ public class Piece {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isWhite() {
|
public boolean isWhite() {
|
||||||
return color;
|
return isWhite;
|
||||||
}
|
|
||||||
public String getName() {
|
|
||||||
if(color) {
|
|
||||||
return type.getSummary().concat("W - ");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return type.getSummary().concat("B - ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
public enum PieceType {
|
public enum PieceType {
|
||||||
|
|
@ -25,4 +26,4 @@ public enum PieceType {
|
||||||
return PieceType.Queen;
|
return PieceType.Queen;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
|
@ -61,7 +62,7 @@ public class JPanelChessBoard extends JPanel {
|
||||||
int y = (me.getY()*myGame.getHeight())/getHeight();
|
int y = (me.getY()*myGame.getHeight())/getHeight();
|
||||||
if(pieceAdderMode) {
|
if(pieceAdderMode) {
|
||||||
//TODO
|
//TODO
|
||||||
myGame.setPiece(selectedPieceType, selectedPieceIsWhite, x, y);
|
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
|
||||||
pieceAdderMode = false;
|
pieceAdderMode = false;
|
||||||
} else {
|
} else {
|
||||||
myGame.clickCoords(x,y);
|
myGame.clickCoords(x,y);
|
||||||
|
|
@ -80,7 +81,7 @@ public class JPanelChessBoard extends JPanel {
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g) {
|
protected void paintComponent(Graphics g) {
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
this.setBackground(Color.black);
|
this.setBackground(Color.PINK);
|
||||||
if(pieceSelectorMode) {
|
if(pieceSelectorMode) {
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
spriteSheet,
|
spriteSheet,
|
||||||
|
|
@ -103,10 +104,10 @@ public class JPanelChessBoard extends JPanel {
|
||||||
boolean isSelect = myGame.isSelected(x,y);
|
boolean isSelect = myGame.isSelected(x,y);
|
||||||
boolean isHighlight = myGame.isHighlighted(x,y);
|
boolean isHighlight = myGame.isHighlighted(x,y);
|
||||||
if(isSelect) {
|
if(isSelect) {
|
||||||
g.setColor(Color.blue);
|
g.setColor(Color.RED);
|
||||||
}
|
}
|
||||||
if(isHighlight) {
|
if(isHighlight) {
|
||||||
g.setColor(Color.yellow);
|
g.setColor(Color.YELLOW);
|
||||||
}
|
}
|
||||||
if((x+y)%2==1 || isSelect || isHighlight) {
|
if((x+y)%2==1 || isSelect || isHighlight) {
|
||||||
g.fillRect(
|
g.fillRect(
|
||||||
|
|
@ -123,7 +124,7 @@ public class JPanelChessBoard extends JPanel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.setColor(Color.gray);
|
g.setColor(Color.blue);
|
||||||
for(int x=0; x<myGame.getWidth();x++) {
|
for(int x=0; x<myGame.getWidth();x++) {
|
||||||
int graphX = Math.round(x*cellWidth);
|
int graphX = Math.round(x*cellWidth);
|
||||||
g.drawLine(graphX, 0, graphX, this.getHeight());
|
g.drawLine(graphX, 0, graphX, this.getHeight());
|
||||||
|
|
@ -192,5 +193,28 @@ public class JPanelChessBoard extends JPanel {
|
||||||
public boolean isPieceAdderMode() {
|
public boolean isPieceAdderMode() {
|
||||||
return pieceAdderMode;
|
return pieceAdderMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void drawWinMessage(Graphics g, boolean isWhite) {
|
||||||
|
PieceType type = PieceType.Queen;
|
||||||
|
Color color = isWhite ? Color.white : Color.black;
|
||||||
|
|
||||||
}
|
// Draw 'W'
|
||||||
|
drawSinglePiece(g, 0, 2, type, isWhite);
|
||||||
|
drawSinglePiece(g, 0, 3, type, isWhite);
|
||||||
|
drawSinglePiece(g, 1, 4, type, isWhite);
|
||||||
|
drawSinglePiece(g, 2, 3, type, isWhite);
|
||||||
|
drawSinglePiece(g, 2, 2, type, isWhite);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawSinglePiece(Graphics g, int boardX, int boardY, PieceType type, boolean isWhite) {
|
||||||
|
g.drawImage(
|
||||||
|
getChessPieceImageFromType(type, isWhite),
|
||||||
|
MARGIN + xCoordFromGame(boardX),
|
||||||
|
MARGIN + yCoordFromGame(boardY),
|
||||||
|
null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
package windowInterface;
|
package windowInterface;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
|
|
@ -266,4 +267,4 @@ public class MyInterface extends JFrame {
|
||||||
this.setStepBanner("Turn : X");
|
this.setStepBanner("Turn : X");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue