Part 2
This commit is contained in:
parent
d3843705e4
commit
4d4fee3bac
|
|
@ -20,6 +20,7 @@ public class Board {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
|
|
@ -152,39 +153,52 @@ public class Board {
|
|||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
if (x < 0 || x >= width || y < 0 || y >= height) {
|
||||
if (x < 0 || x >= width || y < 0 || y >= height) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedX == -1 && selectedY == -1) {
|
||||
Piece touchedPiece = getPieceAt(x, y);
|
||||
if (touchedPiece != null && touchedPiece.isWhite() == isTurnWhite) {
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
highlightedPositions.clear();
|
||||
highlightedPositions.addAll(getValidMoves(touchedPiece));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (selectedX == x && selectedY == y) {
|
||||
selectedX = -1;
|
||||
selectedY = -1;
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
else {
|
||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||
if (selectedPiece != null) {
|
||||
setPiece(selectedPiece.isWhite(), selectedPiece.getType(), x, y);
|
||||
|
||||
for (int i = 0; i < pieces.size(); i++) {
|
||||
Piece piece = pieces.get(i);
|
||||
if (piece.getX() == selectedX && piece.getY() == selectedY) {
|
||||
pieces.remove(i);
|
||||
boolean moveIsValid = false;
|
||||
for (int[] pos : highlightedPositions) {
|
||||
if (pos[0] == x && pos[1] == y) {
|
||||
moveIsValid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
turnNumber++;
|
||||
isTurnWhite = !isTurnWhite;
|
||||
if (moveIsValid) {
|
||||
setPiece(selectedPiece.isWhite(), selectedPiece.getType(), x, y);
|
||||
|
||||
for (int i = 0; i < pieces.size(); i++) {
|
||||
Piece piece = pieces.get(i);
|
||||
if (piece.getX() == selectedX && piece.getY() == selectedY) {
|
||||
pieces.remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
turnNumber++;
|
||||
isTurnWhite = !isTurnWhite;
|
||||
}
|
||||
selectedX = -1;
|
||||
selectedY = -1;
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -211,6 +225,11 @@ public class Board {
|
|||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
for (int[] pos : highlightedPositions) {
|
||||
if (pos[0] == x && pos[1] == y) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//TODO
|
||||
return false;
|
||||
}
|
||||
|
|
@ -230,4 +249,129 @@ public class Board {
|
|||
|
||||
}
|
||||
|
||||
private ArrayList<int[]> getValidMoves(Piece piece) {
|
||||
ArrayList<int[]> validMoves = new ArrayList<>();
|
||||
if (piece == null) return validMoves;
|
||||
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
PieceType type = piece.getType();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
switch (type) {
|
||||
case Pawn:
|
||||
int direction = isWhite ? -1 : 1;
|
||||
|
||||
if (isPositionEmpty(x, y + direction)) {
|
||||
validMoves.add(new int[]{x, y + direction});
|
||||
|
||||
int startRow = isWhite ? 6 : 1;
|
||||
if (y == startRow && isPositionEmpty(x, y + 2 * direction)) {
|
||||
validMoves.add(new int[]{x, y + 2 * direction});
|
||||
}
|
||||
}
|
||||
|
||||
if (x > 0 && canCapturePieceAt(x - 1, y + direction, isWhite)) {
|
||||
validMoves.add(new int[]{x - 1, y + direction});
|
||||
}
|
||||
if (x < width - 1 && canCapturePieceAt(x + 1, y + direction, isWhite)) {
|
||||
validMoves.add(new int[]{x + 1, y + direction});
|
||||
}
|
||||
break;
|
||||
|
||||
case Rook:
|
||||
addMovesInDirection(validMoves, x, y, 1, 0, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, -1, 0, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, 0, -1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, 0, 1, isWhite);
|
||||
break;
|
||||
|
||||
case Knight:
|
||||
int[][] knightMoves = {
|
||||
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
|
||||
{1, -2}, {1, 2}, {2, -1}, {2, 1}
|
||||
};
|
||||
|
||||
for (int[] move : knightMoves) {
|
||||
int newX = x + move[0];
|
||||
int newY = y + move[1];
|
||||
|
||||
if (isValidPosition(newX, newY) &&
|
||||
(isPositionEmpty(newX, newY) || canCapturePieceAt(newX, newY, isWhite))) {
|
||||
validMoves.add(new int[]{newX, newY});
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Bishop:
|
||||
addMovesInDirection(validMoves, x, y, 1, -1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, -1, -1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, 1, 1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, -1, 1, isWhite);
|
||||
break;
|
||||
|
||||
case Queen:
|
||||
addMovesInDirection(validMoves, x, y, 1, 0, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, -1, 0, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, 0, -1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, 0, 1, isWhite);
|
||||
|
||||
addMovesInDirection(validMoves, x, y, 1, -1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, -1, -1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, 1, 1, isWhite);
|
||||
addMovesInDirection(validMoves, x, y, -1, 1, isWhite);
|
||||
break;
|
||||
|
||||
case King:
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
if (isValidPosition(newX, newY) &&
|
||||
(isPositionEmpty(newX, newY) || canCapturePieceAt(newX, newY, isWhite))) {
|
||||
validMoves.add(new int[]{newX, newY});
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return validMoves;
|
||||
}
|
||||
|
||||
private void addMovesInDirection(ArrayList<int[]> moves, int x, int y, int dx, int dy, boolean isWhite) {
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
while (isValidPosition(newX, newY)) {
|
||||
if (isPositionEmpty(newX, newY)) {
|
||||
moves.add(new int[]{newX, newY});
|
||||
newX += dx;
|
||||
newY += dy;
|
||||
} else if (canCapturePieceAt(newX, newY, isWhite)) {
|
||||
moves.add(new int[]{newX, newY});
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPositionEmpty(int x, int y) {
|
||||
return getPieceAt(x, y) == null;
|
||||
}
|
||||
|
||||
private boolean isValidPosition(int x, int y) {
|
||||
return x >= 0 && x < width && y >= 0 && y < height;
|
||||
}
|
||||
|
||||
private boolean canCapturePieceAt(int x, int y, boolean isWhite) {
|
||||
Piece pieceToCapture = getPieceAt(x, y);
|
||||
return pieceToCapture != null && pieceToCapture.isWhite() != isWhite;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue