This commit is contained in:
Jacob Signorovitch
2025-05-19 08:56:54 -04:00
parent c57a0a8056
commit f982a48875
3 changed files with 46 additions and 24 deletions

View File

@@ -14,10 +14,12 @@ import javalib.worldimages.WorldImage;
class Board {
Map<Coord, Piece> board;
Map<Coord, Info> info;
Game game;
Board() {
Board(Game game) {
this.board = new HashMap<Coord, Piece>();
this.info = new HashMap<Coord, Info>();
this.game = game;
}
WorldScene draw() {
@@ -93,6 +95,7 @@ class Board {
Piece piece = this.get(targ);
this.drop(targ);
this.set(dest, piece);
this.game.changeActive();
}
// Get the valid moves for a piece.

View File

@@ -18,7 +18,7 @@ class Game extends World {
}
Game() {
this.board = new Board();
this.board = new Board(this);
this.white = new Player(Util.Col.WHITE, this.board);
this.black = new Player(Util.Col.BLACK, this.board);
this.activeActor = this.white;
@@ -116,4 +116,9 @@ class Game extends World {
}
public void onKeyEvent(String key) { this.activeActor.key(key); }
void changeActive() {
if (this.activeActor.equals(this.white)) this.activeActor = this.black;
else this.activeActor = this.white;
}
}

View File

@@ -9,36 +9,50 @@ class Player extends Actor {
Player(Util.Col col, Board board) { super(col, board); }
void click(Coord coord) {
if (this.board.get(coord) == null) {
if (this.selected == null) return;
Piece piece = this.board.get(coord);
if (this.selected == null) {
if (piece == null) {
return;
} else {
if (piece.col.equals(this.col)) {
System.out.println("clicked piece with nothing selected");
this.selected = coord;
this.board.displaySelected(coord);
this.board.displayMoves(coord);
return;
} else {
System.out.println(
"clicked other actor with nothing selected"
);
return;
}
}
} else {
ArrayList<Coord> moves = this.board.getMoves(this.selected);
if (moves.contains(coord)) {
System.out.println(
"clicked on moveable space with something selected"
);
this.board.move(this.selected, coord);
this.board.unselect(this.selected);
this.board.undisplayMoves(moves);
this.selected = null;
return;
} else {
System.out.println(
"clicked on bad space with something selected"
);
this.board.undisplayMoves(moves);
this.board.unselect(coord);
this.selected = null;
return;
}
this.board.unselect(this.selected);
this.board.undisplayMoves(moves);
this.selected = null;
return;
};
// Click while something selected.
if (this.selected != null) {
ArrayList<Coord> moves = this.board.getMoves(this.selected);
// Click on same piece -- unselect.
if (this.selected.equals(coord)) {}
this.board.unselect(this.selected);
this.board.undisplayMoves(moves);
this.selected = null;
return;
} else {
this.selected = coord;
this.board.displaySelected(this.selected);
this.board.displayMoves(this.selected);
return;
}
}
void key(String key) {
if (this.selected == null) return;
this.board.undisplayMoves(this.board.getMoves(this.selected));
this.board.unselect(this.selected);
this.selected = null;