Want to contribute? Fork me on Codeberg.org!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

165 lines
5.6 KiB

import java.awt.Color;
import java.awt.Graphics;
2 years ago
public class Board {
// Width and height of each board tile
2 years ago
static final int TILE_SIZE = 64;
// Width and height of board in tiles
2 years ago
static final int BOARD_SIZE = 8;
// Width and height of window in pixels
2 years ago
static final int DIMENSION = TILE_SIZE * BOARD_SIZE;
// Colors from Lost Century 24
// https://lospec.com/palette-list/lost-century-24
// new Color() takes in an integer representing the color
// Colors are represented in hexadecimal, so we can write the hex literal by prefixing the color code with 0x
2 years ago
static final Color BLACK = new Color(0x6c595c);
static final Color WHITE = new Color(0xab9b8e);
final DrawingPanel panel;
final Graphics graphics;
// The board is a two-dimensional array of nullable pieces
2 years ago
Piece[][] board;
// The current board coordinate that's being dragged
2 years ago
BoardCoordinate dragging = null;
public Board() {
// Initialize DrawingPanel
2 years ago
panel = new DrawingPanel(DIMENSION, DIMENSION);
graphics = panel.getGraphics();
// Connect up event handlers
2 years ago
panel.onMouseDown(this::handleMouseDown);
panel.onMouseDrag((x, y) -> {
// We want to re-render with new mouse position when dragging pieces
if (dragging != null) draw(x, y);
});
2 years ago
panel.onMouseUp(this::handleMouseUp);
// Initialize board
2 years ago
board = new Piece[BOARD_SIZE][BOARD_SIZE];
// Initialize pieces
for (int y = 0; y < 2; y++)
2 years ago
for (int x = 0; x < BOARD_SIZE; x++) {
set(x, y, new Piece(true));
set(x, y + 6, new Piece(false));
}
}
public Piece get(int x, int y) {
return board[y][x];
}
public Piece get(BoardCoordinate coordinate) {
return get(coordinate.x, coordinate.y);
}
public void set(int x, int y, Piece piece) {
board[y][x] = piece;
}
public void set(BoardCoordinate coordinate, Piece piece) {
set(coordinate.x, coordinate.y, piece);
}
public void move(int fromX, int fromY, int toX, int toY) {
set(toX, toY, get(fromX, fromY));
set(fromX, fromY, null);
}
public void move(BoardCoordinate from, BoardCoordinate to) {
move(from.x, from.y, to.x, to.y);
}
// Mouse down event handler
// This sets the currently dragging piece
2 years ago
void handleMouseDown(int x, int y) {
// Get board coordinate of mouse click
2 years ago
BoardCoordinate coordinate = new ScreenCoordinate(x, y).toBoard();
// If there's no piece there, return
2 years ago
if (get(coordinate) == null) return;
// Set currently dragging piece to that coordinate
2 years ago
dragging = coordinate;
// Redraw with dragging
2 years ago
draw(x, y);
}
void handleMouseUp(int x, int y) {
// Get board coordinate of mouse release
2 years ago
BoardCoordinate newCoordinate = new ScreenCoordinate(x, y).toBoard();
// Only do something if new coordinate is different from the originating coordinate
2 years ago
if (!dragging.equals(newCoordinate)) {
// Get piece on target coordinate
2 years ago
Piece capturedPiece = get(newCoordinate);
// Only move piece to square if there's nothing there, or they are of differing colors
2 years ago
if (capturedPiece == null || capturedPiece.black != get(dragging).black) {
move(dragging, newCoordinate);
}
}
// Clear dragging
2 years ago
dragging = null;
// Redraw without dragging
2 years ago
draw();
}
public void draw() {
draw(null);
2 years ago
}
public void draw(int mouseX, int mouseY) {
draw(new ScreenCoordinate(mouseX, mouseY));
}
public void draw(ScreenCoordinate mousePosition) {
2 years ago
// Draw board
graphics.setColor(WHITE);
graphics.fillRect(0, 0, DIMENSION, DIMENSION);
graphics.setColor(BLACK);
for (int y = 0; y < BOARD_SIZE; y++)
for (int x = y % 2; x < BOARD_SIZE; x += 2)
graphics.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
// Draw pieces
forEachPiece((boardCoordinate, piece) -> {
// If piece is the one being dragged, render it at the mouse position
// Otherwise, render it at the center of the board tile
piece.draw(graphics, boardCoordinate.equals(dragging) ? mousePosition : boardCoordinate.toScreen());
2 years ago
});
}
// Functional interfaces for forEachPiece
// Provide ability to run code for each piece on board without having to duplicate nested for loop boilerplate
2 years ago
@FunctionalInterface
interface PieceActionXY {
void forEachTile(int x, int y, Piece piece);
}
@FunctionalInterface
interface PieceActionCoordinate {
void forEachTile(BoardCoordinate coordinate, Piece piece);
}
// Run code on each tile on board
// Usage:
// forEachPiece((x, y, piece) -> {
// // do something with piece
// });
2 years ago
public void forEachPiece(PieceActionXY tileAction) {
for (int y = 0; y < BOARD_SIZE; y++)
for (int x = 0; x < BOARD_SIZE; x++) {
Piece piece = get(x, y);
2 years ago
if (piece == null) continue;
tileAction.forEachTile(x, y, piece);
}
}
// Run code on each tile on board
// Usage:
// forEachPiece((coordinate, piece) -> {
// // do something with piece
// });
2 years ago
public void forEachPiece(PieceActionCoordinate tileAction) {
forEachPiece((x, y, piece) -> tileAction.forEachTile(new BoardCoordinate(x, y), get(x, y)));
2 years ago
}
}