bloc les possibilités de mouvement et couleurs alterné + selection des

pions
This commit is contained in:
Romain MURPHY 2025-04-12 12:35:30 +02:00
parent 8788aaae85
commit 5a8de97bc2
2 changed files with 39 additions and 95 deletions

View File

@ -16,19 +16,19 @@ public class Board {
this.width = colNum;
this.height = lineNum;
int rows = 8;
int cols = 8;
for (int i = 0; i < rows; i++) {
ArrayList<Piece> row = new ArrayList<>();
ArrayList<Boolean> rowB = new ArrayList<>();
for (int j = 0; j < cols; j++) {
row.add(null); // Fill with null
rowB.add(false);
}
this.board.add(row);
this.possibleMoves.add(rowB);
}
this.turnNumber = 0;
this.turnColor = true;
int cols = 8;
for (int i = 0; i < rows; i++) {
ArrayList<Piece> row = new ArrayList<>();
ArrayList<Boolean> rowB = new ArrayList<>();
for (int j = 0; j < cols; j++) {
row.add(null); // Fill with null
rowB.add(false);
}
this.board.add(row);
this.possibleMoves.add(rowB);
}
this.turnNumber = 0;
this.turnColor = true;
}
@ -82,7 +82,7 @@ public class Board {
}
}
}
public void cleanBoard() {
int rows = 8;
int cols = 8;
@ -92,28 +92,28 @@ public class Board {
}
}
}
@Override
public String toString() {
return "Board [width=" + width + ", height=" + height + ", board=" + board + "]";
}
public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>();
for (ArrayList<Piece> row : board) {
for (Piece piece : row) {
if (piece != null) {
pieces.add(piece);
}
}
}
return pieces;
for (ArrayList<Piece> row : board) {
for (Piece piece : row) {
if (piece != null) {
pieces.add(piece);
}
}
}
return pieces;
}
public Piece getPiece(int x, int y) {
return board.get(y).get(x);
}
public void movePiece(int x, int y) {
if (select) {
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
@ -123,20 +123,22 @@ public class Board {
}
public void userTouch(int x, int y) {
if (this.select == false && board.get(y).get(x) != null) {
if ((this.select == false && board.get(y).get(x) != null && this.board.get(y).get(x).isWhite() == turnColor) || (select == true && board.get(y).get(x) != null && board.get(y).get(x).isWhite() == board.get(ym).get(xm).isWhite())) {
this.xm = x;
this.ym = y;
select = true;
possibleMoves = board.get(y).get(x).getPossibleMoves(board);
}
else if (select == true && this.xm != x || this.ym != y){
this.movePiece(x, y);
select = false;
this.turnNumber += 1;
// System.out.println(this.toString()); // Debug
this.turnColor = !this.turnColor;
if (isHighlighted(x,y)) {
this.movePiece(x, y);
select = false;
this.turnNumber += 1;
// System.out.println(this.toString()); // Debug
this.turnColor = !this.turnColor;
}
} else {
// System.out.println("a"); // debug
// System.out.println("a"); // Debug
select = false;
}
}
@ -147,7 +149,7 @@ public class Board {
else {S = false;}
return S;
}
/* saving-loading feature :*/
public String[] toFileRep() {
@ -159,7 +161,7 @@ public class Board {
//TODO
}
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
@ -173,14 +175,14 @@ public class Board {
public void undoLastMove() {
//TODO
}
public Board(Board board) {
//TODO
}
public void playMove(Move move) {
//TODO

View File

@ -1,58 +0,0 @@
package backend;
import java.util.ArrayList;
public class PossibleMovements {
ArrayList<ArrayList<Piece>> board;
Piece pieceToMove;
PieceType type;
int x;
int y;
boolean turnColor;
public PossibleMovements(ArrayList<ArrayList<Piece>> board, int x, int y,boolean turnColor) {
this.board = board;
this.pieceToMove = board.get(y).get(x);
this.type = pieceToMove.getType();
this.x = x;
this.y = y;
this.turnColor = turnColor;
}
public ArrayList<ArrayList<Boolean>> PM(){
ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
int rows = 8;
int cols = 8;
for (int i = 0; i < rows; i++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int j = 0; j < cols; j++) {
row.add(false); // Fill with false
}
possibleMoves.add(row);
}
// Pawn movements for white
if (turnColor) {
if (type == PieceType.Pawn) {
if (y!= 0) {
if (x == 0) {
if (board.get(y-1).get(x+1) != null) {
possibleMoves.get(y-1).set(x+1, true);
}
}
else if (x==7) {
if (board.get(y-1).get(x-1) != null) {
possibleMoves.get(y-1).set(x-1, true);
}
}
else {
if (board.get(y-1).get(x-1) != null) {
possibleMoves.get(y-1).set(x-1, true);
}
if (board.get(y-1).get(x+1) != null) {
possibleMoves.get(y-1).set(x+1, true);
}
possibleMoves.get(y-1).set(x, true);
}
}
}
}
}
}