MOVE DAMNIT MOVE AAAAAAAAAAAAAAAA
This commit is contained in:
@@ -28,4 +28,7 @@ abstract class Actor {
|
||||
|
||||
// Process a user click on the board if the active actor.
|
||||
void click(Coord coord) {}
|
||||
|
||||
// Process a user key.
|
||||
void key(String key){};
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import javalib.worldimages.EmptyImage;
|
||||
import javalib.worldimages.OutlineMode;
|
||||
import javalib.worldimages.OverlayImage;
|
||||
import javalib.worldimages.RectangleImage;
|
||||
import javalib.worldimages.WorldImage;
|
||||
|
||||
class Board {
|
||||
Map<Coord, Piece> board;
|
||||
@@ -25,21 +26,25 @@ class Board {
|
||||
for (int i = 0; i < Util.boardW; i++)
|
||||
for (int j = 0; j < Util.boardW; j++) {
|
||||
Piece piece = this.board.get(new Coord(i, j));
|
||||
Info info = this.info.get(new Coord(i, j));
|
||||
WorldImage infoImage = new EmptyImage();
|
||||
|
||||
if (info == Info.CANMOVE)
|
||||
infoImage = new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
new Color(0, 200, 150, 150)
|
||||
);
|
||||
else if (info == Info.SELECTED)
|
||||
infoImage = new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
new Color(0, 150, 200, 150)
|
||||
);
|
||||
|
||||
scene.placeImageXY(
|
||||
new OverlayImage(
|
||||
(this.info.get(new Coord(i, j)) != null)
|
||||
? new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
new Color(0, 200, 200, 150)
|
||||
)
|
||||
: new EmptyImage(),
|
||||
infoImage,
|
||||
new OverlayImage(
|
||||
(piece == null) ? new EmptyImage() : piece.draw(),
|
||||
new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
(i + j) % 2 == 0 ? Color.DARK_GRAY
|
||||
: Color.LIGHT_GRAY
|
||||
)
|
||||
this.drawPiece(piece), this.drawChecker(i, j)
|
||||
)
|
||||
),
|
||||
i * Util.scale + Util.halfScale,
|
||||
@@ -50,6 +55,17 @@ class Board {
|
||||
return scene;
|
||||
}
|
||||
|
||||
WorldImage drawChecker(int i, int j) {
|
||||
return new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
(i + j) % 2 == 0 ? Color.DARK_GRAY : Color.LIGHT_GRAY
|
||||
);
|
||||
}
|
||||
|
||||
WorldImage drawPiece(Piece piece) {
|
||||
return (piece == null) ? new EmptyImage() : piece.draw();
|
||||
}
|
||||
|
||||
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); }
|
||||
@@ -60,6 +76,12 @@ class Board {
|
||||
for (Coord move : moves) this.info.put(move, Info.CANMOVE);
|
||||
}
|
||||
|
||||
// Show which piece is selected.
|
||||
void displaySelected(Coord coord) { this.info.put(coord, Info.SELECTED); }
|
||||
|
||||
// Unselect a piece.
|
||||
void unselect(Coord coord) { this.info.remove(coord); }
|
||||
|
||||
// Move a piece.
|
||||
void move(Coord targ, Coord dest) {
|
||||
Piece piece = this.get(targ);
|
||||
@@ -71,16 +93,12 @@ class Board {
|
||||
ArrayList<Coord> getMoves(Coord coord) {
|
||||
Piece piece = this.get(coord);
|
||||
|
||||
// If not the active actor, no moves possible and so return empty list.
|
||||
// if (!piece.col.equals(this.activeActor.col))
|
||||
// return new ArrayList<Coord>();
|
||||
|
||||
return piece.moves(coord);
|
||||
}
|
||||
}
|
||||
|
||||
// Information layered over the board.
|
||||
enum Info { CANMOVE, WARN }
|
||||
enum Info { CANMOVE, WARN, SELECTED }
|
||||
|
||||
// Measured from top left.
|
||||
class Coord {
|
||||
|
@@ -114,4 +114,6 @@ class Game extends World {
|
||||
public void onMouseClicked(Posn posn) {
|
||||
this.activeActor.click(this.where(posn));
|
||||
}
|
||||
|
||||
public void onKey(String key) { this.activeActor.key(key); }
|
||||
}
|
||||
|
@@ -52,8 +52,11 @@ class Rook extends Piece {
|
||||
|
||||
ArrayList<Coord> moves(Coord coord) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
for (int i = -7; i < 8; i++) moves.add(new Coord(coord.x, coord.y + i));
|
||||
for (int i = -7; i < 8; i++) moves.add(new Coord(coord.x + i, coord.y));
|
||||
for (int i = -7; i < 8; i++) {
|
||||
if (i == 0) continue; // Can't move to self.
|
||||
moves.add(new Coord(coord.x, coord.y + i));
|
||||
moves.add(new Coord(coord.x + i, coord.y));
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
@@ -91,6 +94,16 @@ class Bishop extends Piece {
|
||||
"Bishop.png"
|
||||
);
|
||||
}
|
||||
|
||||
ArrayList<Coord> moves(Coord coord) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
for (int i = -7; i < 8; i++) {
|
||||
if (i == 0) continue; // Can't move to own position.
|
||||
moves.add(new Coord(coord.x + i, coord.y + i));
|
||||
moves.add(new Coord(coord.x + i, coord.y - i));
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
||||
class Queen extends Piece {
|
||||
@@ -101,6 +114,18 @@ class Queen extends Piece {
|
||||
"Queen.png"
|
||||
);
|
||||
}
|
||||
|
||||
ArrayList<Coord> moves(Coord coord) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
for (int i = -7; i < 8; i++) {
|
||||
if (i == 0) continue; // Can't move to own position.
|
||||
moves.add(new Coord(coord.x + i, coord.y + i));
|
||||
moves.add(new Coord(coord.x + i, coord.y - i));
|
||||
moves.add(new Coord(coord.x, coord.y + i));
|
||||
moves.add(new Coord(coord.x + i, coord.y));
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
||||
class King extends Piece {
|
||||
@@ -111,4 +136,17 @@ class King extends Piece {
|
||||
"King.png"
|
||||
);
|
||||
}
|
||||
|
||||
ArrayList<Coord> moves(Coord coord) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
moves.add(new Coord(coord.x, coord.y + 1));
|
||||
moves.add(new Coord(coord.x, coord.y - 1));
|
||||
moves.add(new Coord(coord.x + 1, coord.y));
|
||||
moves.add(new Coord(coord.x - 1, coord.y));
|
||||
moves.add(new Coord(coord.x + 1, coord.y + 1));
|
||||
moves.add(new Coord(coord.x + 1, coord.y - 1));
|
||||
moves.add(new Coord(coord.x - 1, coord.y + 1));
|
||||
moves.add(new Coord(coord.x - 1, coord.y - 1));
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
@@ -2,7 +2,14 @@ package chess;
|
||||
|
||||
// A human-controlled Actor.
|
||||
class Player extends Actor {
|
||||
Coord selected; // The currently selected piece.
|
||||
|
||||
Player(Util.Col col, Board board) { super(col, board); }
|
||||
|
||||
void click(Coord coord) { this.board.displayMoves(coord); }
|
||||
void click(Coord coord) {
|
||||
this.selected = coord;
|
||||
this.board.displaySelected(this.selected);
|
||||
this.board.displayMoves(this.selected);
|
||||
}
|
||||
void key(String key) { this.board.unselect(this.selected); }
|
||||
}
|
||||
|
Reference in New Issue
Block a user