isSelected and userTouch is done and modification on toString
This commit is contained in:
parent
093ce11e8a
commit
4908f3fc43
|
|
@ -7,6 +7,11 @@ public class Board {
|
|||
public int width;
|
||||
public int height;
|
||||
private Piece[][] board;
|
||||
private boolean hasSelectedPiece = false;
|
||||
private int selectedX = -1;
|
||||
private int selectedY = -1;
|
||||
private int turnNumber = 0;
|
||||
private boolean turnWhite = true;
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
|
|
@ -107,16 +112,70 @@ public class Board {
|
|||
}
|
||||
}
|
||||
return pieces;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
return false;
|
||||
return hasSelectedPiece && selectedX == x && selectedY == y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
if (x < 0 || x >= width || y < 0 || y >= height) return;
|
||||
|
||||
Piece clicked = board[x][y];
|
||||
|
||||
if (!hasSelectedPiece) {
|
||||
if (clicked != null && clicked.isWhite() == turnWhite) {
|
||||
// Select piece
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
hasSelectedPiece = true;
|
||||
}
|
||||
} else {
|
||||
if (selectedX == x && selectedY == y) {
|
||||
// Unselect
|
||||
hasSelectedPiece = false;
|
||||
} else {
|
||||
Piece selectedPiece = board[selectedX][selectedY];
|
||||
if (selectedPiece != null) {
|
||||
// Capture if needed
|
||||
if (board[x][y] != null) {
|
||||
if (board[x][y].isWhite() == selectedPiece.isWhite()) {
|
||||
// Can't capture own piece
|
||||
hasSelectedPiece = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Move piece
|
||||
board[x][y] = selectedPiece;
|
||||
board[selectedX][selectedY] = null;
|
||||
selectedPiece.setX(x);
|
||||
selectedPiece.setY(y);
|
||||
|
||||
// Switch turn
|
||||
turnWhite = !turnWhite;
|
||||
turnNumber++;
|
||||
|
||||
// Unselect
|
||||
hasSelectedPiece = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//public boolean isSelected(int x, int y) {
|
||||
//return false
|
||||
// }
|
||||
|
||||
/* saving-loading feature */
|
||||
public String[] toFileRep() {
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -23,6 +23,15 @@ public class Piece {
|
|||
return y;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
|
||||
public PieceType getType() {
|
||||
|
||||
return type;
|
||||
|
|
|
|||
Loading…
Reference in New Issue