i have completed part 1. the only thing is usertouch is not working

cause its only in the board class. idk if we should put it in the game
class for it to actually function on the interface. i am clueless.
This commit is contained in:
keshi 2025-05-07 21:18:56 +02:00
parent 5cc24e4348
commit 522f2e8331
2 changed files with 40 additions and 22 deletions

View File

@ -37,7 +37,6 @@ public class Main {
testBoard.setPiece(true, PieceType.Bishop, 4, 7); testBoard.setPiece(true, PieceType.Bishop, 4, 7);
testBoard.toString(); testBoard.toString();
// launches graphical interface : // launches graphical interface :
MyInterface mjf = new MyInterface(); MyInterface mjf = new MyInterface();
mjf.setVisible(true); mjf.setVisible(true);

View File

@ -6,6 +6,9 @@ public class Board {
private int width; private int width;
private int height; private int height;
private Piece[][] board;//creating an array for the coordinates of the board private Piece[][] board;//creating an array for the coordinates of the board
private Piece chosenPiece=null;
private int turnNumber=0;
private boolean isTurnWhite=true;
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width=colNum; this.width=colNum;
@ -14,29 +17,23 @@ public class Board {
} }
public int getWidth() { public int getWidth() {
//TODO
return width; return width;
} }
public int getHeight() { public int getHeight() {
//TODO
return height; return height;
} }
public int getTurnNumber() { public int getTurnNumber() {
//TODO return turnNumber;
return 0;
} }
public boolean isTurnWhite() { public boolean isTurnWhite() {
//TODO return isTurnWhite;
return false;
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
board[y][x]= new Piece (x, y, type, isWhite); board[y][x]= new Piece (x, y, type, isWhite);
} }
public void populateBoard() { public void populateBoard() {
@ -62,23 +59,28 @@ public class Board {
for(int x=0; x<8; x++) { for(int x=0; x<8; x++) {
board[x][y]= null; board[x][y]= null;
} }
} }
} }
public String toString() { public String toString() { //review the stringbuilder class that already exists in java aand see if we can replace it by smth else
StringBuilder builder= new StringBuilder();
for (int y=0; y< height; y++) { for (int y=0; y< height; y++) {
for (int x=0; x<width; x++) { for (int x=0; x<width; x++) {
Piece thePiece=board[x][y];
if(thePiece!=null) {
String color =thePiece.isWhite()? "w":"b";
builder.append(color).append(thePiece.getType().getSummary());
} else {
builder.append("--");
}
} }
builder.append("\n");
} }
return ""; return builder.toString();
} }
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();
for(int y=0; y<8; y++) { for(int y=0; y<8; y++) {
@ -92,13 +94,30 @@ public class Board {
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
//TODO Piece clickedPiece=board[x][y];
if(chosenPiece==null) {
//when no piece is selected
if(clickedPiece!=null) {
chosenPiece=clickedPiece;
}
}
else { //if a piece is already selected
if(isSelected(x,y)) {
chosenPiece=null; //unselect the chosen piece to revert to normal state
}
else { //move selected piece to new position
board[chosenPiece.getX()][chosenPiece.getY()]=null; //clear the old position
board[x][y]=new Piece(x, y, chosenPiece.getType(), chosenPiece.isWhite());//place at new position
chosenPiece=null; //unselect the piece moved
turnNumber++; //tracks the number of moves
isTurnWhite=!isTurnWhite;
}
}
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates
//TODO return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
return false;
} }
/* saving-loading feature :*/ /* saving-loading feature :*/