Merge branch 'master' of

https://gitarero.ecam.fr/yohan.montagne/OOP_2B2_Project.git
This commit is contained in:
Tilman Crosetti 2025-05-07 16:27:53 +02:00
commit 28f3a08074
1 changed files with 171 additions and 98 deletions

View File

@ -6,22 +6,22 @@ import java.util.LinkedList;
public class Board {
private int Width;
private int Height;
private ArrayList <Piece> pieces;
private int turnNumber = 0;
private int Width;
private int Height;
private ArrayList <Piece> pieces;
private boolean turnIsWhite = true;
private Integer selectedX = null;
private int turnNumber = 0;
private Integer selectedY = null;
private boolean turnIsWhite = true;
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
private Integer selectedX = null;
private Integer selectedY = null;
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
@ -29,9 +29,9 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
this.Width=colNum;
this.Height=lineNum;
this.pieces= new ArrayList <> ();
}
public int getWidth() {
@ -43,21 +43,21 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
}
public int getTurnNumber() {
return turnNumber;
}
public boolean isTurnWhite() {
return turnIsWhite;
}
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
pieces.add(new Piece(isWhite, type, x, y));
}
public void populateBoard() {
@ -83,62 +83,62 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
for (int i=0;i<8;i++) {
setPiece(false,PieceType.Pawn,i,6);
}
}
public void cleanBoard() {
pieces.clear();
selectedX = null;
selectedY = null;
pieces.clear();
selectedX = null;
selectedY = null;
}
public ArrayList<Piece> getPieces() {
return pieces;
return pieces;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
Piece pieceAtPos = null;
for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) {
pieceAtPos = p;
break;
}
}
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
Piece pieceAtPos = null;
for (Piece p : pieces) {
if (p.getX() == x && p.getY() == y) {
pieceAtPos = p;
break;
}
}
if (pieceAtPos != null) {
char colorChar = pieceAtPos.isWhite() ? 'W' : 'B';
String typeChar = pieceAtPos.getType().getSummary();
sb.append(colorChar).append(typeChar);
} else {
sb.append(", ");
}
if (pieceAtPos != null) {
char colorChar = pieceAtPos.isWhite() ? 'W' : 'B';
String typeChar = pieceAtPos.getType().getSummary();
sb.append(colorChar).append(typeChar);
} else {
sb.append(", ");
}
sb.append(" ");
}
sb.append("\n");
}
sb.append(" ");
}
sb.append("\n");
}
sb.append("Turn: ").append(turnIsWhite ? "White" : "Black");
sb.append(" (Turn number: ").append(turnNumber).append(")\n");
sb.append("Turn: ").append(turnIsWhite ? "White" : "Black");
sb.append(" (Turn number: ").append(turnNumber).append(")\n");
return sb.toString();
return sb.toString();
}
public void userTouch(int x, int y) {
<<<<<<< HEAD
if (selectedX == null && selectedY == null) {
Piece pieceAtPos = getPieceAt(x, y);
if (pieceAtPos != null) {
@ -157,63 +157,137 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
Piece pieceToMove = getPieceAt(selectedX, selectedY);
if (pieceToMove != null) {
pieces.remove(pieceToMove);
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
turnNumber++;
turnIsWhite = !turnIsWhite;
Piece pieceAtDestination = getPieceAt(x, y);
if (pieceAtDestination == null ||
pieceAtDestination.isWhite() != pieceToMove.isWhite()) {
if (pieceAtDestination != null) {
pieces.remove(pieceAtDestination);
}
pieces.remove(pieceToMove);
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
turnNumber++;
turnIsWhite = !turnIsWhite;
}
}
selectedX = null;
selectedY = null;
}
}
=======
if (selectedX == null && selectedY == null) {
Piece pieceAtPos = getPieceAt(x, y);
if (pieceAtPos != null) {
selectedX = x;
selectedY = y;
}
} else {
if (selectedX == x && selectedY == y) {
selectedX = null;
selectedY = null;
} else {
Piece pieceToMove = getPieceAt(selectedX, selectedY);
if (pieceToMove != null) {
pieces.remove(pieceToMove);
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
turnNumber++;
turnIsWhite = !turnIsWhite;
}
selectedX = null;
selectedY = null;
}
}
>>>>>>> branch 'master' of https://gitarero.ecam.fr/yohan.montagne/OOP_2B1_Project.git
}
private Piece getPieceAt(int x, int y) {
for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y) {
return piece;
}
}
return null;
for (Piece piece : pieces) {
if (piece.getX() == x && piece.getY() == y) {
return piece;
}
}
return null;
}
public boolean isSelected(int x, int y) {
return selectedX != null && selectedY != null && selectedX == x && selectedY == y;
//TODO
return false;
}
/* saving-loading feature :*/
public String[] toFileRep() {
String[] lines = new String[Height + 1];
for (int y = 0; y < Height; y++) {
StringBuilder row = new StringBuilder();
for (int x = 0; x < Width; x++) {
Piece p = getPieceAt(x, y);
if (p != null) {
row.append (p.isWhite() ? "W" : "B").append(p.getType().getSummary());
}
row.append(x < Width - 1? "," : "");
}
lines[y] = row.toString();
}
lines[Height] = isTurnWhite() ? "W" : "B";
return lines;
String[] lines = new String[Height + 1];
for (int y = 0; y < Height; y++) {
StringBuilder row = new StringBuilder();
for (int x = 0; x < Width; x++) {
Piece p = getPieceAt(x, y);
if (p != null) {
row.append (p.isWhite() ? "W" : "B").append(p.getType().getSummary());
}
row.append(x < Width - 1? "," : "");
}
lines[y] = row.toString();
}
lines[Height] = isTurnWhite() ? "W" : "B";
return lines;
}
public Board(String[] array) {
//TODO
this.Height=array.length-1;
this.pieces= new ArrayList <> ();
for (int y=0;y<Height;y++) {
String line = array[y];
String[] decomposedLine = line.split(",");
this.Width=decomposedLine.length;
for (int x=0;x<Width;x++) {
String pieceStr = decomposedLine[x];
if(!pieceStr.equals("")) {
boolean isWhite = true;
if(pieceStr.charAt(0)=='B') {
isWhite = false;
}
PieceType myType = PieceType.fromSummary(pieceStr.charAt(1));
setPiece(isWhite, myType, x, y);
}
}
}
String turnIndicator = array[Height];
if(turnIndicator.equals("W")) {
this.turnIsWhite = true;
} else {
turnIsWhite = false;
}
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
@ -221,24 +295,24 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
return false;
}
private LinkedList<Move> moveHistory = new LinkedList<>();
public void undoLastMove() {
//TODO
}
public Board(Board board) {
//TODO
}
public void playMove(Move move) {
//TODO
}
public LinkedList<Move> getAllLegalMoves(boolean isWhite) {
LinkedList<Move> moves = new LinkedList<>();
@ -270,5 +344,4 @@ public LinkedList<Piece> getAllPieces() {
return pieces;
}
}