everything until isSelect

This commit is contained in:
Lucie 2025-04-24 19:49:35 +02:00
parent 01557f979e
commit 0ea5fbaa49
3 changed files with 109 additions and 24 deletions

View File

@ -7,19 +7,23 @@ public class Board {
private int colNum; private int colNum;
private int lineNum; private int lineNum;
private Piece[][] board; private Piece[][] board;
private int x;
private int y;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.colNum = colNum; this.colNum = colNum;
this.lineNum = lineNum; this.lineNum = lineNum;
this.board = new Piece[lineNum][colNum]; this.board = new Piece[lineNum][colNum];
x = -1;
y = -1;
} }
public int getWidth() { public int getWidth() {
return colNum; return this.colNum;
} }
public int getHeight() { public int getHeight() {
return lineNum; return this.lineNum;
} }
public int getTurnNumber() { public int getTurnNumber() {
@ -33,36 +37,43 @@ public class Board {
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
Piece piece = new Piece(isWhite,type,x,y);
board[y][x] = piece;
} }
public void populateBoard() { public void populateBoard() {
boolean white = true;
for (int i = 0; i < lineNum; i++) { for (int i = 0; i < lineNum; i++) {
boolean white = true;
if (i == 0 || i == 1) { if (i == 0 || i == 1) {
white = false; white = false;
} }
for (int j = 0; j < colNum; j++) { for (int j = 0; j < colNum; j++) {
if (i == 1 || i == 6) { if (i == 1 || i == 6) {
setPiece(white, PieceType.Pawn, j, i); Piece pawn = new Piece(white, PieceType.Pawn, j, i);
board[i][j] = pawn;
} }
else if (i == 0 || i == 7) { else if (i == 0 || i == 7) {
if (j == 0 || j == 7) { if (j == 0 || j == 7) {
setPiece(white, PieceType.Rook, j, i); Piece rook = new Piece(white, PieceType.Rook, j, i);
board[i][j] = rook;
} }
else if (j == 1 || j == 6) { else if (j == 1 || j == 6) {
setPiece(white, PieceType.Knight, j, i); Piece knight = new Piece(white, PieceType.Knight, j, i);
board[i][j] = knight;
} }
else if (j == 2 || j == 5) { else if (j == 2 || j == 5) {
setPiece(white, PieceType.Bishop, j, i); Piece bishop = new Piece(white, PieceType.Bishop, j, i);
board[i][j] = bishop;
} }
else if (j == 3) { else if (j == 3) {
setPiece(white, PieceType.Queen, j, i); Piece queen = new Piece(white, PieceType.Queen, j, i);
board[i][j] = queen;
} }
else if (j == 4) { else if (j == 4) {
setPiece(white, PieceType.King, j, i); Piece king = new Piece(white, PieceType.King, j, i);
board[i][j] = king;
} }
} }
@ -71,20 +82,43 @@ public class Board {
} }
public void cleanBoard() { public void cleanBoard() {
//TODO for (int i = 0; i < lineNum; i++) {
for (int j = 0; j < colNum; j++) {
board[i][j] = null;
}
}
} }
public String toString() { public String toString() {
String str = "";
return ""; for (int i = 0; i < lineNum; i++) {
for (int j = 0; j < colNum; j++) {
if (board[i][j] == null) {
str += " ";
}
else if (board[i][j].isWhite() == true) {
str += "W";
str += board[i][j].getType().getSummary();
}
else if (board[i][j].isWhite() == false) {
str += "B";
str += board[i][j].getType().getSummary();
}
str += ",";
}
str += "\n";
}
return str;
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
for (int col=0; col<colNum; col++) { for (int col = 0; col < colNum; col ++) {
for (int row=0; row<lineNum; row++) { for (int row = 0; row < lineNum; row ++) {
Piece Piece=board[row][col]; Piece Piece = board[row][col];
if (Piece!=null) { if (Piece != null) {
pieces.add(Piece); pieces.add(Piece);
} }
} }
@ -94,12 +128,47 @@ public class Board {
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
//TODO boolean prevSelect = true;
if (this.x == -1 && this.y == -1) {
prevSelect = false;
}
if (prevSelect == true) {
if (this.x == x && this.y == y) {
this.x = -1;
this.y = -1;
}
else if (board[this.y][this.x] == null) {
this.x = -1;
this.y = -1;
}
else {
board[this.y][this.x].setX(x);
board[this.y][this.x].setY(y);
board[y][x] = board[this.y][this.x];
board[this.y][this.x] = null;
this.x = -1;
this.y = -1;
}
}
else {
this.x = x;
this.y = y;
}
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
//TODO if (this.x != -1 && this.y != -1) {
if (this.x == x && this.y == y) {
return true;
}
else if (board[this.y][this.x] == null) {
this.x = -1;
this.y = -1;
return false;
}
}
return false; return false;
} }

View File

@ -4,6 +4,15 @@ public class Piece {
private int x; private int x;
private int y; private int y;
private boolean isWhite;
private PieceType type;
public Piece(boolean isWhite, PieceType type, int x, int y) {
this.isWhite = isWhite;
this.type = type;
this.x = x;
this.y = y;
}
public int getX() { public int getX() {
return this.x; return this.x;
@ -13,13 +22,20 @@ public class Piece {
return this.y; return this.y;
} }
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public PieceType getType() { public PieceType getType() {
return this.type;
return null;
} }
public boolean isWhite() { public boolean isWhite() {
return false; return this.isWhite;
} }
} }

View File

@ -80,7 +80,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.white); this.setBackground(Color.black);
if(pieceSelectorMode) { if(pieceSelectorMode) {
g.drawImage( g.drawImage(
spriteSheet, spriteSheet,
@ -97,7 +97,7 @@ public class JPanelChessBoard extends JPanel {
float cellWidth = cellWidth(); float cellWidth = cellWidth();
float cellHeight = cellHeight(); float cellHeight = cellHeight();
g.setColor(Color.blue); g.setColor(Color.white);
for(int x=0; x<myGame.getWidth();x++) { for(int x=0; x<myGame.getWidth();x++) {
for (int y=0; y<myGame.getHeight(); y++) { for (int y=0; y<myGame.getHeight(); y++) {
boolean isSelect = myGame.isSelected(x,y); boolean isSelect = myGame.isSelected(x,y);