This commit is contained in:
Jacob Signorovitch
2025-05-19 08:14:00 -04:00
parent f94890a6ef
commit 634f932871
3 changed files with 45 additions and 8 deletions

View File

@@ -1,7 +1,6 @@
package chess; package chess;
import java.awt.Color; import java.awt.Color;
import java.awt.Rectangle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -72,10 +71,18 @@ class Board {
// Show the moves a piece can take. // Show the moves a piece can take.
void displayMoves(Coord coord) { void displayMoves(Coord coord) {
ArrayList<Coord> moves = this.get(coord).moves(coord); Piece piece = this.get(coord);
if (piece == null) return;
ArrayList<Coord> moves = piece.moves(coord);
for (Coord move : moves) this.info.put(move, Info.CANMOVE); for (Coord move : moves) this.info.put(move, Info.CANMOVE);
} }
// Clear moves from screen.
void undisplayMoves(Coord coord) {
ArrayList<Coord> moves = this.get(coord).moves(coord);
for (Coord move : moves) this.info.remove(move);
}
// Show which piece is selected. // Show which piece is selected.
void displaySelected(Coord coord) { this.info.put(coord, Info.SELECTED); } void displaySelected(Coord coord) { this.info.put(coord, Info.SELECTED); }

View File

@@ -115,5 +115,5 @@ class Game extends World {
this.activeActor.click(this.where(posn)); this.activeActor.click(this.where(posn));
} }
public void onKey(String key) { this.activeActor.key(key); } public void onKeyEvent(String key) { this.activeActor.key(key); }
} }

View File

@@ -1,15 +1,45 @@
package chess; package chess;
// A human-controlled Actor. import java.util.ArrayList;
// A human-controlled Actor."
class Player extends Actor { class Player extends Actor {
Coord selected; // The currently selected piece. Coord selected; // The currently selected piece.
Player(Util.Col col, Board board) { super(col, board); } Player(Util.Col col, Board board) { super(col, board); }
void click(Coord coord) { void click(Coord coord) {
this.selected = coord; if (this.board.get(coord) == null) {
this.board.displaySelected(this.selected); this.board.unselect(this.selected);
this.board.displayMoves(this.selected); this.board.undisplayMoves(this.selected);
this.selected = null;
return;
};
// Click while something selected.
if (this.selected != null) {
// Click on same piece -- unselect.
if (this.selected.equals(coord)) {
} else if (this.board.getMoves(this.selected).contains(coord)) {
System.out.println("HHHHHHHHHHERE");
this.board.move(this.selected, coord);
}
this.board.unselect(this.selected);
this.board.undisplayMoves(this.selected);
this.selected = null;
return;
} else {
this.selected = coord;
this.board.displaySelected(this.selected);
this.board.displayMoves(this.selected);
return;
}
}
void key(String key) {
this.board.undisplayMoves(this.selected);
this.board.unselect(this.selected);
this.selected = null;
} }
void key(String key) { this.board.unselect(this.selected); }
} }