board init

This commit is contained in:
Romain MURPHY 2025-04-09 10:21:18 +02:00
parent 60e86f5471
commit 59431ad622
5 changed files with 108 additions and 25 deletions

View File

@ -1,9 +1,22 @@
import backend.Board;
import backend.Move;
import backend.Piece;
import backend.PieceType;
import windowInterface.MyInterface;
public class Main { public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
public static void main(String[] args) {
// testing :
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
System.out.println(testBoard.toString());
// launches graphical interface :
MyInterface mjf = new MyInterface();
mjf.setVisible(true);
} }
} }

View File

@ -3,19 +3,31 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Board { public class Board {
public int width;
public int height;
public ArrayList<ArrayList<Piece>> board = new ArrayList<>();
private int selectPosition = 0;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
//TODO this.width = colNum;
this.height = lineNum;
int rows = 8;
int cols = 8;
for (int i = 0; i < rows; i++) {
ArrayList<Piece> row = new ArrayList<>();
for (int j = 0; j < cols; j++) {
row.add(null); // Fill with null
}
this.board.add(row);
}
} }
public int getWidth() { public int getWidth() {
//TODO return this.width;
return 0;
} }
public int getHeight() { public int getHeight() {
//TODO return this.height;
return 0;
} }
public int getTurnNumber() { public int getTurnNumber() {
@ -28,32 +40,75 @@ public class Board {
return false; return false;
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(int x, int y, PieceType type, boolean isWhite) {
//TODO Piece piece = new Piece(x,y,type,isWhite);
board.get(y).set(x, piece);
} }
public void populateBoard() { public void populateBoard() {
//TODO for (int x = 0; x < this.width;x++) {
for (int y = 0; y < this.height; y++) {
if (y ==1 || y == 6) {
if (y == 1) {
this.setPiece(x,y,PieceType.Pawn,false);
}
else if (y == 6) {this.setPiece(x,y,PieceType.Pawn,true);}
}
if (y == 0) {
boolean col = false;
if (x == 0 || x == 7) {
this.setPiece(x,y,PieceType.Rook,col);
} else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}
else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}
else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}
else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}
} else if (y == 7) {
boolean col = true;
if (x == 0 || x == 7) {
this.setPiece(x,y,PieceType.Rook,col);
} else if (x == 1 || x == 6) {this.setPiece(x,y,PieceType.Knight,col);}
else if (x == 2 || x == 5) {this.setPiece(x,y,PieceType.Bishop,col);}
else if (x == 3) {this.setPiece(x,y,PieceType.Queen,col);}
else if (x == 4) {this.setPiece(x,y,PieceType.King,col);}
}
}
}
} }
public void cleanBoard() { public void cleanBoard() {
//TODO //TODO
} }
@Override
public String toString() { public String toString() {
//TODO return "Board [width=" + width + ", height=" + height + ", board=" + board + "]";
return "";
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
//TODO for (ArrayList<Piece> row : board) {
for (Piece piece : row) {
return pieces; if (piece != null) {
pieces.add(piece);
}
}
}
return pieces;
}
public Piece getPiece(int x, int y) {
return board.get(x).get(y);
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
//TODO Piece pieceToMove = new Piece();
while (this.selectPosition < 2) {
if (this.selectPosition == 0) {
pieceToMove = this.getPiece(y,x);
} else if (this.selectPosition == 1) {
this.setPiece(x, y, pieceToMove.getType(), pieceToMove.isWhite());
}
}
} }

View File

@ -66,8 +66,8 @@ public class Game extends Thread {
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(int x, int y, PieceType type, boolean isWhite) {
board.setPiece(isWhite, type, x, y); board.setPiece(x, y, type, isWhite);
} }
public String[] getFileRepresentation() { public String[] getFileRepresentation() {

View File

@ -1,21 +1,36 @@
package backend; package backend;
public class Piece { public class Piece {
public int x;
public int y;
public PieceType type;
public boolean isWhite;
public Piece() {
}
public Piece(int x, int y, PieceType type, boolean isWhite) {
this.x = x;
this.y = y;
this.type = type;
this.isWhite = isWhite;
}
public int getX() { public int getX() {
return 0; return this.x;
} }
public int getY() { public int getY() {
return 0; return this.y;
} }
public PieceType getType() { public PieceType getType() {
return null; return this.type;
} }
public boolean isWhite() { public boolean isWhite() {
return false; return this.isWhite;
}
@Override
public String toString() {
return "Piece [x=" + x + ", y=" + y + ", type=" + type + ", isWhite=" + isWhite + "]";
} }
} }

View File

@ -61,7 +61,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(selectedPieceIsWhite,selectedPieceType, x, y); myGame.setPiece(x, y, selectedPieceType, selectedPieceIsWhite);
pieceAdderMode = false; pieceAdderMode = false;
} else { } else {
myGame.clickCoords(x,y); myGame.clickCoords(x,y);