diff --git a/chess/src/chess/Actor.java b/chess/src/chess/Actor.java index f907b33..5044276 100644 --- a/chess/src/chess/Actor.java +++ b/chess/src/chess/Actor.java @@ -6,17 +6,26 @@ abstract class Actor { Boolean inCheck; // Whether the Actor is currently in check. Boolean canCastle; // Whether the Actor can castle. 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.inCheck = inCheck; this.canCastle = canCastle; this.won = won; + this.board = board; } // Convenience constructor for start of game. - Actor(Util.Col col) { + Actor(Util.Col col, Board board) { this.col = col; 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) {} } diff --git a/chess/src/chess/Board.java b/chess/src/chess/Board.java index 7602610..4a1fea0 100644 --- a/chess/src/chess/Board.java +++ b/chess/src/chess/Board.java @@ -1,4 +1,5 @@ package chess; + import java.awt.Color; import java.util.HashMap; import java.util.Map; @@ -37,6 +38,14 @@ class Board { void set(Coord coord, Piece piece) { this.board.put(coord, piece); } 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. diff --git a/chess/src/chess/Bot.java b/chess/src/chess/Bot.java index 426f192..0a20553 100644 --- a/chess/src/chess/Bot.java +++ b/chess/src/chess/Bot.java @@ -2,5 +2,5 @@ package chess; // A chess bot. class Bot extends Actor { - Bot(Util.Col col) { super(col); } + Bot(Util.Col col, Board board) { super(col, board); } } diff --git a/chess/src/chess/Game.java b/chess/src/chess/Game.java index 9629888..ad64268 100644 --- a/chess/src/chess/Game.java +++ b/chess/src/chess/Game.java @@ -1,24 +1,29 @@ package chess; -import chess.Util.Col; import javalib.impworld.*; +import javalib.worldimages.*; class Game extends World { Actor white; Actor black; Board board; WorldScene scene; + Actor activeActor; Game(Actor white, Actor black, Board board) { this.white = white; this.black = black; this.board = board; + this.activeActor = this.white; } Game() { - this( - new Player(Util.Col.WHITE), new Player(Util.Col.BLACK), new Board() - ); + this.board = 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(); } @@ -32,6 +37,7 @@ class Game extends World { void run() { this.bigBang(Util.boardW * Util.scale, Util.boardW * Util.scale, 0.01); } + void draw() { this.scene = this.board.draw(); } void setup() { @@ -101,4 +107,13 @@ class Game extends World { 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)); + } } diff --git a/chess/src/chess/Player.java b/chess/src/chess/Player.java index 86ba04d..9b6a958 100644 --- a/chess/src/chess/Player.java +++ b/chess/src/chess/Player.java @@ -2,5 +2,7 @@ package chess; // A human-controlled 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)); } }