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:
parent
5cc24e4348
commit
522f2e8331
|
|
@ -37,7 +37,6 @@ public class Main {
|
|||
testBoard.setPiece(true, PieceType.Bishop, 4, 7);
|
||||
testBoard.toString();
|
||||
|
||||
|
||||
// launches graphical interface :
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@ public class Board {
|
|||
private int width;
|
||||
private int height;
|
||||
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) {
|
||||
this.width=colNum;
|
||||
|
|
@ -14,29 +17,23 @@ public class Board {
|
|||
}
|
||||
|
||||
public int getWidth() {
|
||||
//TODO
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
//TODO
|
||||
return height;
|
||||
}
|
||||
|
||||
public int getTurnNumber() {
|
||||
//TODO
|
||||
return 0;
|
||||
return turnNumber;
|
||||
}
|
||||
|
||||
public boolean isTurnWhite() {
|
||||
//TODO
|
||||
return false;
|
||||
return isTurnWhite;
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
board[y][x]= new Piece (x, y, type, isWhite);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void populateBoard() {
|
||||
|
|
@ -62,23 +59,28 @@ public class Board {
|
|||
for(int x=0; x<8; x++) {
|
||||
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 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() {
|
||||
ArrayList<Piece> pieces = new ArrayList<>();
|
||||
for(int y=0; y<8; y++) {
|
||||
|
|
@ -92,13 +94,30 @@ public class Board {
|
|||
}
|
||||
|
||||
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) {
|
||||
//TODO
|
||||
return false;
|
||||
public boolean isSelected(int x, int y) { //checks if any piece is selected and if its position matches that of the x,y coordinates
|
||||
return chosenPiece != null && chosenPiece.getX()==x && chosenPiece.getY()==y;
|
||||
}
|
||||
|
||||
/* saving-loading feature :*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue