for board: modify the methods since we couldn't find a fi string.

This commit is contained in:
MSI 2025-05-22 22:46:07 +02:00
parent 3f88a4ea3c
commit 8948654df8
8 changed files with 394 additions and 399 deletions

View File

@ -1,17 +1,35 @@
package backend;
import java.util.ArrayList;
import java.util.Random;
public class AutoPlayer {
/**
* returns the best Move to try on provided board for active player
* @param board
* @return
*/
public Move computeBestMove(Board board) {
return null;
}
private final Random random = new Random();
public Move computeBestMove(Board board) {
ArrayList<Move> possibleMoves = new ArrayList<>();
for (Piece piece : board.getPieces()) {
if (piece.isWhite() != board.isTurnWhite()) continue;
int originX = piece.getX();
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()));
}
}

View File

@ -1,427 +1,381 @@
package backend;
import java.util.ArrayList;
import java.util.Stack;
public class Board {
private int cNum;
private int lNum;
private Piece[][] cells;
private Piece selectedCell;
private ArrayList<Move> moveHistory = new ArrayList<>();
private ArrayList<Piece> Pieces;
private Stack<Move> moveHistory = new Stack<>();
private int turnNumber = 0;
public Board(int colNum, int lineNum) {
cNum=colNum;
lNum=lineNum;
cells = new Piece[cNum][lNum]; // creation du tableau de pieces
selectedCell=null;
cNum = colNum;
lNum = lineNum;
Pieces = new ArrayList<>();
}
public int getWidth() {
return lNum;
}
public int getHeight() {
return cNum;
}
public int getTurnNumber() {
return turnNumber;
public int getHeight() {
return lNum;
}
public int getTurnNumber() {
return turnNumber;
}
public boolean isTurnWhite() {
return getTurnNumber() % 2 == 0;
return turnWhite;
}
public void setPiece(PieceType type, boolean isWhite, int x, int y) {
Piece newPiece = new Piece(type, isWhite, x, y);
cells[x][y] = newPiece;
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
Piece existing = null;
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() {
final int startWhite = 0;
final int startBlack = lNum-1;
//Black pieces populating
setPiece(PieceType.Rook,false,0,startWhite);
setPiece(PieceType.Knight,false, 1,startWhite);
setPiece(PieceType.Bishop,false,2,startWhite);
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);
Pieces.add(new Piece(0, 0, false, PieceType.Rook));
Pieces.add(new Piece(1, 0, false, PieceType.Knight));
Pieces.add(new Piece(2, 0, false, PieceType.Bishop));
Pieces.add(new Piece(3, 0, false, PieceType.Queen));
Pieces.add(new Piece(4, 0, false, PieceType.King));
Pieces.add(new Piece(5, 0, false, PieceType.Bishop));
Pieces.add(new Piece(6, 0, false, PieceType.Knight));
Pieces.add(new Piece(7, 0, false, PieceType.Rook));
for (int x = 0; x < cNum; x++) {
setPiece(PieceType.Pawn,true,x,startBlack-1);
for (int i = 0; i < 8; i++) {
Pieces.add(new Piece(i, 1, false, PieceType.Pawn));
}
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() {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
cells[x][y] = null;
}
}
selectedCell = null;
moveHistory.clear(); // clear undo history
turnNumber = 0;
Pieces.clear();
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int y = 0; y < lNum; y++) {
for (int x = 0; x < cNum; x++) {
Piece p = cells[x][y];
if (p != null) {
sb.append(p.isWhite() ? "W" : "B").append(p.getType().toString().charAt(0)).append(" ");
StringBuilder boardView = new StringBuilder();
for (int row = 0; row < lNum; row++) {
for (int col = 0; col < cNum; col++) {
Piece pieceAtPosition = getPieceAt(col, row);
if (pieceAtPosition != null) {
String colorPrefix = pieceAtPosition.isWhite() ? "W" : "B";
boardView.append(colorPrefix)
.append(pieceAtPosition.getType().getSummary())
.append(" ");
} 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() {
ArrayList<Piece> pieces = new ArrayList<>();
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (cells[x][y]!= null) {
pieces.add(cells[x][y]);
}
private Piece findPieceAt(int x, int y) {
for (Piece piece : Pieces) {
if (piece.getX() == x && piece.getY() == y) {
return piece;
}
}
return pieces;
return null;
}
public void userTouch(int col, int row) {
if (GameLost()) return;
public void userTouch(int x, int y) {
if (this.selectedCell != null) {
System.out.println(" cellule deja selectionne : " + this.selectedCell.getName());
Piece target = findPieceAt(col, row);
// CASE 1: Clicked same piece (deselect)
if (cells[x][y] != null &&
cells[x][y].getX() == selectedCell.getX() &&
cells[x][y].getY() == selectedCell.getY()) {
System.out.println("il a clique sur la meme cellule, je deselectionne");
this.selectedCell = null;
return;
if (!hasSelection) {
if (target != null && target.isWhite() == turnWhite) {
hasSelection = true;
selectedX = col;
selectedY = row;
}
// 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");
}
return;
}
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) {
return selectedCell != null && selectedCell.getX() == x && selectedCell.getY() == y;
return hasSelection && selectedX == x && selectedY == y;
}
//saving feature: "type + iswhite + x + y"
public String[] toFileRep() {
ArrayList<String> lines = new ArrayList<>();
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]);
return null;
}
//loading feature
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;
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
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;
Piece selected = null;
for (Piece p : Pieces) {
if (p.getX() == selectedX && p.getY() == selectedY) {
selected = p;
break;
}
}
if (selected == null) return false;
case Rook:
if (dx == 0 || dy == 0) {
int stepX = Integer.compare(dx, 0);
int stepY = Integer.compare(dy, 0);
int cx = currX + stepX;
int cy = currY + stepY;
while (cx != x || cy != y) {
if (cells[cx][cy] != null) return false;
cx += stepX;
cy += stepY;
}
return true;
int fromX = selected.getX(), fromY = selected.getY();
boolean isWhite = selected.isWhite();
PieceType type = selected.getType();
if (type == PieceType.Pawn) {
int dir = isWhite ? -1 : 1;
if (destX == fromX && destY == fromY + dir && getPieceAt(destX, destY) == null) return true;
if ((isWhite && fromY == 6 || !isWhite && fromY == 1) &&
destX == fromX && destY == fromY + 2 * dir &&
getPieceAt(fromX, fromY + dir) == null && getPieceAt(destX, destY) == null) return true;
if ((destX == fromX + 1 || destX == fromX - 1) && destY == fromY + dir) {
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 (Math.abs(dx) == Math.abs(dy)) {
int stepX = Integer.compare(dx, 0);
int stepY = Integer.compare(dy, 0);
int cx = currX + stepX;
int cy = currY + stepY;
while (cx != x || cy != y) {
if (cells[cx][cy] != null) return false;
cx += stepX;
cy += stepY;
}
return true;
if (type == PieceType.King) {
int[][] moves = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
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 Queen:
if (dx == 0 || dy == 0 || Math.abs(dx) == Math.abs(dy)) {
int stepX = Integer.compare(dx, 0);
int stepY = Integer.compare(dy, 0);
int cx = currX + stepX;
int cy = currY + stepY;
while (cx != x || cy != y) {
if (cells[cx][cy] != null) return false;
cx += stepX;
cy += stepY;
if (type == PieceType.Rook || type == PieceType.Queen) {
int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}};
for (int[] d : directions) {
int x = fromX + d[0], y = fromY + d[1];
while (x >= 0 && x < cNum && y >= 0 && y < lNum) {
Piece target = getPieceAt(x, y);
if (target == null) {
if (x == destX && y == destY) return true;
} else {
if (x == destX && y == destY && target.isWhite() != isWhite) return true;
break;
}
return true;
x += d[0]; y += d[1];
}
break;
}
}
case King:
if (Math.abs(dx) <= 1 && Math.abs(dy) <= 1) return true;
break;
case Knight:
if ((Math.abs(dx) == 2 && Math.abs(dy) == 1) || (Math.abs(dx) == 1 && Math.abs(dy) == 2)) return true;
break;
if (type == PieceType.Bishop || type == PieceType.Queen) {
int[][] directions = {{1,1},{-1,1},{1,-1},{-1,-1}};
for (int[] d : directions) {
int x = fromX + d[0], y = fromY + d[1];
while (x >= 0 && x < cNum && y >= 0 && y < lNum) {
Piece target = getPieceAt(x, y);
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;
}
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++) {
for (int y = 0; y < lNum; y++) {
Piece p = other.cells[x][y];
if (p != null) {
this.cells[x][y] = new Piece(p.getType(), p.isWhite(), x, y);
Piece getPieceAt(int x, int y) {
for (Piece p : Pieces) {
if (p.getX() == x && p.getY() == y) return p;
}
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) {
int fromX = move.getFromX();
int fromY = move.getFromY();
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 activateSelection(int col, int row) {
selectedX = col;
selectedY = row;
hasSelection = true;
}
public void undoLastMove() {
if (moveHistory.isEmpty()) {
System.out.println("No move to undo.");
return;
}
Move lastMove = moveHistory.remove(moveHistory.size() - 1);
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 isValidDestination(int x, int y) {
return isHighlighted(x, y);
}
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();
}
}

View File

@ -1,3 +1,4 @@
package backend;
import windowInterface.MyInterface;
@ -6,7 +7,6 @@ public class Game extends Thread {
private AutoPlayer aiPlayer;
private Board board;
private Piece newPiece;
private MyInterface mjf;
private int COL_NUM = 8;
@ -31,10 +31,6 @@ public class Game extends Thread {
public int 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() {
while(true) {
@ -69,7 +65,10 @@ public class Game extends Thread {
board.userTouch(x, y);
}
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board.setPiece(isWhite, type, x, y);
}
public String[] getFileRepresentation() {
@ -109,4 +108,4 @@ public class Game extends Thread {
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
}
}
}

View File

@ -1,42 +1,47 @@
package backend;
public class Move {
private Piece piece;
private int fromX, fromY;
private int toX, toY;
private Piece capturedPiece;
public Move ( Piece piece, int fromX, int fromY, int toX, int toY, Piece capturedPiece){
this.piece = piece;
this.fromX = fromX;
this.fromY = fromY;
private final int fromX;
private final int fromY;
private final int toX;
private final int toY;
private final Piece movedPiece;
private final Piece capturedPiece;
public Move(Piece movedPiece, int toX, int toY, Piece capturedPiece) {
this.fromX = movedPiece.getX();
this.fromY = movedPiece.getY();
this.toX = toX;
this.toY = toY;
this.movedPiece = movedPiece;
this.capturedPiece = capturedPiece;
}
public Piece getPiece() {
return piece;
public Move(Piece movedPiece, int toX, int toY) {
this(movedPiece, toX, toY, null);
}
public int getFromX() {
return fromX;
}
public int getFromY() {
return fromY;
}
public int getToX() {
return toX;
}
public int getToY() {
return toY;
}
public Piece getMovedPiece() {
return movedPiece;
}
public Piece getCapturedPiece() {
return capturedPiece;
}
public boolean isCapture() {
return capturedPiece != null;
}
public String toString() {
return piece + ":("+ fromX + "," + fromY + ")-("+ toX + "," + toY + ")" +
( capturedPiece != null? "capturing "+ capturedPiece: "");
}
}

View File

@ -1,19 +1,20 @@
package backend;
public class Piece {
private int x;
private int y;
private boolean isWhite;
private PieceType type;
private boolean color;
public Piece(PieceType type_P, boolean color_P, int xP, int yP) {
x = xP;
y = yP;
type = type_P;
color = color_P;
public Piece(int x, int y, boolean isWhite, PieceType type) {
this.x = x;
this.y = y;
this.isWhite = isWhite;
this.type = type;
}
public int getX() {
return x;
}
@ -27,15 +28,7 @@ public class Piece {
}
public boolean isWhite() {
return color;
}
public String getName() {
if(color) {
return type.getSummary().concat("W - ");
}
else {
return type.getSummary().concat("B - ");
}
return isWhite;
}
}

View File

@ -1,3 +1,4 @@
package backend;
public enum PieceType {
@ -25,4 +26,4 @@ public enum PieceType {
return PieceType.Queen;
}
}
}

View File

@ -1,3 +1,4 @@
package windowInterface;
import java.awt.Color;
@ -61,7 +62,7 @@ public class JPanelChessBoard extends JPanel {
int y = (me.getY()*myGame.getHeight())/getHeight();
if(pieceAdderMode) {
//TODO
myGame.setPiece(selectedPieceType, selectedPieceIsWhite, x, y);
myGame.setPiece(selectedPieceIsWhite,selectedPieceType, x, y);
pieceAdderMode = false;
} else {
myGame.clickCoords(x,y);
@ -80,7 +81,7 @@ public class JPanelChessBoard extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.black);
this.setBackground(Color.PINK);
if(pieceSelectorMode) {
g.drawImage(
spriteSheet,
@ -103,10 +104,10 @@ public class JPanelChessBoard extends JPanel {
boolean isSelect = myGame.isSelected(x,y);
boolean isHighlight = myGame.isHighlighted(x,y);
if(isSelect) {
g.setColor(Color.blue);
g.setColor(Color.RED);
}
if(isHighlight) {
g.setColor(Color.yellow);
g.setColor(Color.YELLOW);
}
if((x+y)%2==1 || isSelect || isHighlight) {
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++) {
int graphX = Math.round(x*cellWidth);
g.drawLine(graphX, 0, graphX, this.getHeight());
@ -192,5 +193,28 @@ public class JPanelChessBoard extends JPanel {
public boolean isPieceAdderMode() {
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
);
}
}

View File

@ -1,3 +1,4 @@
package windowInterface;
import java.awt.BorderLayout;
import java.awt.Dimension;
@ -266,4 +267,4 @@ public class MyInterface extends JFrame {
this.setStepBanner("Turn : X");
}
}
}