Some things.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package chess;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Rectangle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import javalib.impworld.WorldScene;
|
||||
@@ -11,8 +13,12 @@ import javalib.worldimages.RectangleImage;
|
||||
|
||||
class Board {
|
||||
Map<Coord, Piece> board;
|
||||
Map<Coord, Info> info;
|
||||
|
||||
Board() { this.board = new HashMap<Coord, Piece>(); }
|
||||
Board() {
|
||||
this.board = new HashMap<Coord, Piece>();
|
||||
this.info = new HashMap<Coord, Info>();
|
||||
}
|
||||
|
||||
WorldScene draw() {
|
||||
WorldScene scene = new WorldScene(Util.size, Util.size);
|
||||
@@ -21,11 +27,19 @@ class Board {
|
||||
Piece piece = this.board.get(new Coord(i, j));
|
||||
scene.placeImageXY(
|
||||
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.info.get(new Coord(i, j)) != null)
|
||||
? new RectangleImage(
|
||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||
new Color(0, 200, 200, 150)
|
||||
)
|
||||
: new EmptyImage(),
|
||||
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
|
||||
)
|
||||
)
|
||||
),
|
||||
i * Util.scale + Util.halfScale,
|
||||
@@ -40,14 +54,34 @@ class Board {
|
||||
Piece get(Coord coord) { return this.board.get(coord); }
|
||||
void drop(Coord coord) { this.board.remove(coord); }
|
||||
|
||||
// Show the moves a piece can take.
|
||||
void displayMoves(Coord coord) {
|
||||
ArrayList<Coord> moves = this.get(coord).moves(coord);
|
||||
for (Coord move : moves) this.info.put(move, Info.CANMOVE);
|
||||
}
|
||||
|
||||
// Move a piece.
|
||||
void move(Coord targ, Coord dest) {
|
||||
Piece piece = this.get(targ);
|
||||
this.drop(targ);
|
||||
this.set(dest, piece);
|
||||
}
|
||||
|
||||
// Get the valid moves for a piece.
|
||||
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 }
|
||||
|
||||
// Measured from top left.
|
||||
class Coord {
|
||||
int x, y;
|
||||
|
@@ -19,10 +19,8 @@ class Game extends World {
|
||||
|
||||
Game() {
|
||||
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();
|
||||
|
@@ -1,9 +1,13 @@
|
||||
package chess;
|
||||
|
||||
import chess.Util.Col;
|
||||
import tester.Tester;
|
||||
|
||||
class Examples {
|
||||
void testItAll(Tester t) { new Game().run(); }
|
||||
void testItAll(Tester t) {
|
||||
Game game = new Game();
|
||||
game.run();
|
||||
}
|
||||
|
||||
void testCoord(Tester t) {
|
||||
Coord coord = new Coord(1, 1);
|
||||
@@ -14,11 +18,4 @@ class Examples {
|
||||
t.checkExpect(coord.hashCode() == cod.hashCode(), true);
|
||||
t.checkExpect(coord.hashCode() == cor.hashCode(), false);
|
||||
}
|
||||
|
||||
void testBoard(Tester t) {
|
||||
Piece pawn = new Pawn(Util.Col.BLACK);
|
||||
Board board = new Board();
|
||||
board.set(new Coord(0, 0), pawn);
|
||||
t.checkExpect(board.get(new Coord(0, 0)), pawn);
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package chess;
|
||||
|
||||
import chess.Util.Col;
|
||||
import java.util.ArrayList;
|
||||
import javalib.worldimages.*;
|
||||
|
||||
// The piece types.
|
||||
@@ -16,6 +18,9 @@ abstract class Piece {
|
||||
}
|
||||
|
||||
WorldImage draw() { return new EmptyImage(); };
|
||||
|
||||
// Return list of possible move locations, centered around the given coord.
|
||||
ArrayList<Coord> moves(Coord coord) { return new ArrayList<Coord>(); }
|
||||
}
|
||||
|
||||
class Pawn extends Piece {
|
||||
@@ -26,6 +31,14 @@ class Pawn extends Piece {
|
||||
"Pawn.png"
|
||||
);
|
||||
}
|
||||
|
||||
ArrayList<Coord> moves(Coord coord) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
moves.add(
|
||||
new Coord(coord.x, coord.y + (this.col.equals(Col.BLACK) ? 1 : -1))
|
||||
);
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
||||
class Rook extends Piece {
|
||||
@@ -36,6 +49,13 @@ class Rook extends Piece {
|
||||
"Rook.png"
|
||||
);
|
||||
}
|
||||
|
||||
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));
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
||||
class Knight extends Piece {
|
||||
@@ -46,6 +66,21 @@ class Knight extends Piece {
|
||||
"Knight.png"
|
||||
);
|
||||
}
|
||||
|
||||
ArrayList<Coord> moves(Coord coord) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
moves.add(new Coord(coord.x + 1, coord.y + 2));
|
||||
moves.add(new Coord(coord.x + 2, coord.y + 1));
|
||||
moves.add(new Coord(coord.x + 1, coord.y - 2));
|
||||
moves.add(new Coord(coord.x + 2, coord.y - 1));
|
||||
moves.add(new Coord(coord.x - 1, coord.y - 2));
|
||||
moves.add(new Coord(coord.x - 2, coord.y - 1));
|
||||
moves.add(new Coord(coord.x - 1, coord.y + 2));
|
||||
moves.add(new Coord(coord.x - 2, coord.y + 1));
|
||||
moves.add(new Coord(coord.x - 1, coord.y - 2));
|
||||
moves.add(new Coord(coord.x - 2, coord.y - 1));
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
||||
class Bishop extends Piece {
|
||||
|
@@ -4,5 +4,5 @@ package chess;
|
||||
class Player extends Actor {
|
||||
Player(Util.Col col, Board board) { super(col, board); }
|
||||
|
||||
void click(Coord coord) { this.board.move(coord, new Coord(4, 4)); }
|
||||
void click(Coord coord) { this.board.displayMoves(coord); }
|
||||
}
|
||||
|
Reference in New Issue
Block a user