AAAAAAAAAAAAAAAAAAAAAAAAA

This commit is contained in:
Jacob Signorovitch
2025-05-18 15:03:31 -04:00
parent 562d88d937
commit b41762e1bd
5 changed files with 43 additions and 8 deletions

View File

@@ -6,17 +6,26 @@ abstract class Actor {
Boolean inCheck; // Whether the Actor is currently in check. Boolean inCheck; // Whether the Actor is currently in check.
Boolean canCastle; // Whether the Actor can castle. Boolean canCastle; // Whether the Actor can castle.
Boolean won; // Whether the Actor has won. Boolean won; // Whether the Actor has won.
Board board; // Reference to the board the actor is playing on.
Actor(Util.Col col, Boolean inCheck, Boolean canCastle, Boolean won) { Actor(
Util.Col col, Boolean inCheck, Boolean canCastle, Boolean won,
Board board
) {
this.col = col; this.col = col;
this.inCheck = inCheck; this.inCheck = inCheck;
this.canCastle = canCastle; this.canCastle = canCastle;
this.won = won; this.won = won;
this.board = board;
} }
// Convenience constructor for start of game. // Convenience constructor for start of game.
Actor(Util.Col col) { Actor(Util.Col col, Board board) {
this.col = col; this.col = col;
this.inCheck = this.canCastle = this.won = false; this.inCheck = this.canCastle = this.won = false;
this.board = board;
} }
// Process a user click on the board if the active actor.
void click(Coord coord) {}
} }

View File

@@ -1,4 +1,5 @@
package chess; package chess;
import java.awt.Color; import java.awt.Color;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -37,6 +38,14 @@ class Board {
void set(Coord coord, Piece piece) { this.board.put(coord, piece); } void set(Coord coord, Piece piece) { this.board.put(coord, piece); }
Piece get(Coord coord) { return this.board.get(coord); } Piece get(Coord coord) { return this.board.get(coord); }
void drop(Coord coord) { this.board.remove(coord); }
// Move a piece.
void move(Coord targ, Coord dest) {
Piece piece = this.get(targ);
this.drop(targ);
this.set(dest, piece);
}
} }
// Measured from top left. // Measured from top left.

View File

@@ -2,5 +2,5 @@ package chess;
// A chess bot. // A chess bot.
class Bot extends Actor { class Bot extends Actor {
Bot(Util.Col col) { super(col); } Bot(Util.Col col, Board board) { super(col, board); }
} }

View File

@@ -1,24 +1,29 @@
package chess; package chess;
import chess.Util.Col;
import javalib.impworld.*; import javalib.impworld.*;
import javalib.worldimages.*;
class Game extends World { class Game extends World {
Actor white; Actor white;
Actor black; Actor black;
Board board; Board board;
WorldScene scene; WorldScene scene;
Actor activeActor;
Game(Actor white, Actor black, Board board) { Game(Actor white, Actor black, Board board) {
this.white = white; this.white = white;
this.black = black; this.black = black;
this.board = board; this.board = board;
this.activeActor = this.white;
} }
Game() { Game() {
this( this.board = new Board();
new Player(Util.Col.WHITE), new Player(Util.Col.BLACK), new Board()
); this.white = new Player(Util.Col.WHITE, this.board);
this.black = new Player(Util.Col.BLACK, this.board);
this.activeActor = this.white;
this.setup(); this.setup();
} }
@@ -32,6 +37,7 @@ class Game extends World {
void run() { void run() {
this.bigBang(Util.boardW * Util.scale, Util.boardW * Util.scale, 0.01); this.bigBang(Util.boardW * Util.scale, Util.boardW * Util.scale, 0.01);
} }
void draw() { this.scene = this.board.draw(); } void draw() { this.scene = this.board.draw(); }
void setup() { void setup() {
@@ -101,4 +107,13 @@ class Game extends World {
new Coord(7, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE) new Coord(7, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
); );
} }
// Convert Posn (pixel coord) to Coord (board coord).
Coord where(Posn posn) {
return new Coord(posn.x / Util.scale, posn.y / Util.scale);
}
public void onMouseClicked(Posn posn) {
this.activeActor.click(this.where(posn));
}
} }

View File

@@ -2,5 +2,7 @@ package chess;
// A human-controlled Actor. // A human-controlled Actor.
class Player extends Actor { class Player extends Actor {
Player(Util.Col col) { super(col); } Player(Util.Col col, Board board) { super(col, board); }
void click(Coord coord) { this.board.move(coord, new Coord(4, 4)); }
} }