parent
91c04cbcf2
commit
309231e304
@ -1,12 +1,23 @@
|
||||
// Board coordinate class.
|
||||
// Provides tile coordinates for squares on the chess board.
|
||||
public class BoardCoordinate extends Coordinate {
|
||||
public BoardCoordinate(int x, int y) {
|
||||
super(x, y);
|
||||
super(x, y); // Execute superclass constructor
|
||||
}
|
||||
|
||||
// TODO: reimplement as casting
|
||||
// Convert to screen coordinate by getting center pixel of tile
|
||||
public ScreenCoordinate toScreen() {
|
||||
return new ScreenCoordinate(
|
||||
x * Board.TILE_SIZE + Board.TILE_SIZE / 2,
|
||||
y * Board.TILE_SIZE + Board.TILE_SIZE / 2
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (!(obj instanceof BoardCoordinate other)) return false;
|
||||
return other.x == x && other.y == y;
|
||||
if (obj == this) return true; // Equals if same object
|
||||
if (!(obj instanceof BoardCoordinate other)) return false; // If other is not BoardCoordinate, not equals
|
||||
return other.x == x && other.y == y; // If is BoardCoordinate and x- and y-values match, equals
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,28 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class Piece {
|
||||
// Width and height of placeholder rectangle graphic
|
||||
public static final int DIMENSION = 32;
|
||||
public boolean black;
|
||||
|
||||
// If no parameter, default to white
|
||||
public Piece() { }
|
||||
|
||||
public Piece(boolean black) {
|
||||
this.black = black;
|
||||
}
|
||||
|
||||
// The Piece class doesn't store position,
|
||||
// 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) {
|
||||
graphics.setColor(black ? Color.BLACK : Color.WHITE);
|
||||
// Drawing is performed from the top-left corner.
|
||||
// We need the drawn rectangle to be offset by half of the width and height
|
||||
// so it is centered on the provided position.
|
||||
graphics.fillRect(x - DIMENSION / 2, y - DIMENSION / 2, DIMENSION, DIMENSION);
|
||||
}
|
||||
|
||||
public void draw(Graphics graphics, ScreenCoordinate coordinate) {
|
||||
draw(graphics, coordinate.x, coordinate.y);
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,21 @@
|
||||
// Screen coordinate class.
|
||||
// Holds pixel coordinates relative to window.
|
||||
public class ScreenCoordinate extends Coordinate {
|
||||
public ScreenCoordinate(int x, int y) {
|
||||
super(x, y);
|
||||
super(x, y); // Execute superclass constructor
|
||||
}
|
||||
|
||||
// TODO: reimplement as casting
|
||||
public BoardCoordinate toBoard() {
|
||||
// We can convert to board coordinates by dividing by the tile size of each square on the board
|
||||
return new BoardCoordinate(x / Board.TILE_SIZE, y / Board.TILE_SIZE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) return true;
|
||||
if (obj instanceof BoardCoordinate) return obj == toBoard();
|
||||
if (!(obj instanceof ScreenCoordinate other)) return false;
|
||||
return other.x == x && other.y == y;
|
||||
if (obj == this) return true; // Equals if same object
|
||||
if (obj instanceof BoardCoordinate) return obj == toBoard(); // If other is BoardCoordinate, we can see if they match
|
||||
if (!(obj instanceof ScreenCoordinate other)) return false; // If not BoardCoordinate or ScreenCoordinate, not equals
|
||||
return other.x == x && other.y == y; // If is ScreenCoordinate and x- and y-values match, equals
|
||||
}
|
||||
}
|
Reference in new issue