Make piece move restrictions, minor bugfixes

main
Elnu 2 years ago
parent df2268cd55
commit 1f578b0770

@ -1,5 +1,6 @@
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.util.ArrayList;
public class Board { public class Board {
// Width and height of each board tile // Width and height of each board tile
@ -46,11 +47,21 @@ public class Board {
} }
} }
public boolean outOfBounds(int x, int y) {
return x < 0 || y < 0 || x >= BOARD_SIZE || y >= BOARD_SIZE;
}
public boolean outOfBounds(BoardCoordinate coordinate) {
return outOfBounds(coordinate.x, coordinate.y);
}
public Piece get(int x, int y) { public Piece get(int x, int y) {
if (outOfBounds(x, y)) return null;
return board[y][x]; return board[y][x];
} }
public Piece get(BoardCoordinate coordinate) { public Piece get(BoardCoordinate coordinate) {
if (coordinate == null) return null;
return get(coordinate.x, coordinate.y); return get(coordinate.x, coordinate.y);
} }
@ -88,12 +99,15 @@ public class Board {
// Get board coordinate of mouse release // Get board coordinate of mouse release
BoardCoordinate newCoordinate = new ScreenCoordinate(x, y).toBoard(); BoardCoordinate newCoordinate = new ScreenCoordinate(x, y).toBoard();
// Only do something if new coordinate is different from the originating coordinate // Only do something if new coordinate is different from the originating coordinate
if (!dragging.equals(newCoordinate)) { if (dragging != null && !newCoordinate.equals(dragging)) {
// Get piece on target coordinate // dragging is BoardCoordinate of piece being dragged
Piece capturedPiece = get(newCoordinate); Piece piece = get(dragging);
// Only move piece to square if there's nothing there, or they are of differing colors ArrayList<BoardCoordinate> legalMoves = piece.getLegalMoves(dragging, this);
if (capturedPiece == null || capturedPiece.black != get(dragging).black) { for (BoardCoordinate legalMove : legalMoves) {
if (newCoordinate.equals(legalMove)) {
move(dragging, newCoordinate); move(dragging, newCoordinate);
break;
}
} }
} }
// Clear dragging // Clear dragging

@ -1,4 +1,5 @@
import java.awt.*; import java.awt.*;
import java.util.ArrayList;
public class Piece { public class Piece {
// Width and height of placeholder rectangle graphic // Width and height of placeholder rectangle graphic
@ -12,6 +13,37 @@ public class Piece {
this.black = black; this.black = black;
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y)); // to left
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y)); // to right
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); // to up
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); // to down
return possibleMoves;
}
public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> legalMoves = getPossibleMoves(position);
for (int i = 0; i < legalMoves.size(); i++) {
BoardCoordinate possibleMove = legalMoves.get(i);
Piece targetPiece = board.get(possibleMove);
if (
// other piece of same color
(targetPiece != null && targetPiece.black == black) ||
// outside of bounds of board
possibleMove.x < 0 ||
possibleMove.y < 0 ||
possibleMove.x >= Board.BOARD_SIZE ||
possibleMove.y >= Board.BOARD_SIZE) {
legalMoves.remove(i);
i--;
}
// TODO: puts us into check
}
return legalMoves;
}
// The Piece class doesn't store position, // The Piece class doesn't store position,
// so when drawing we need to be provided this along with a graphics context when drawing // so when drawing we need to be provided this along with a graphics context when drawing
public void draw(Graphics graphics, int x, int y) { public void draw(Graphics graphics, int x, int y) {