Chess.
This commit is contained in:
@@ -14,8 +14,7 @@ class Board {
|
|||||||
Board() { this.board = new HashMap<Coord, Piece>(); }
|
Board() { this.board = new HashMap<Coord, Piece>(); }
|
||||||
|
|
||||||
WorldScene draw() {
|
WorldScene draw() {
|
||||||
WorldScene scene =
|
WorldScene scene = new WorldScene(Util.size, Util.size);
|
||||||
new WorldScene(Util.boardW * Util.scale, Util.boardW * Util.scale);
|
|
||||||
for (int i = 0; i < Util.boardW; i++)
|
for (int i = 0; i < Util.boardW; i++)
|
||||||
for (int j = 0; j < Util.boardW; j++) {
|
for (int j = 0; j < Util.boardW; j++) {
|
||||||
Piece piece = this.board.get(new Coord(i, j));
|
Piece piece = this.board.get(new Coord(i, j));
|
||||||
@@ -24,11 +23,12 @@ class Board {
|
|||||||
(piece == null) ? new EmptyImage() : piece.draw(),
|
(piece == null) ? new EmptyImage() : piece.draw(),
|
||||||
new RectangleImage(
|
new RectangleImage(
|
||||||
Util.scale, Util.scale, OutlineMode.SOLID,
|
Util.scale, Util.scale, OutlineMode.SOLID,
|
||||||
(i + j) % 2 == 0 ? Color.BLACK : Color.WHITE
|
(i + j) % 2 == 0 ? Color.DARK_GRAY
|
||||||
|
: Color.LIGHT_GRAY
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
i * Util.scale + Util.scale / 2,
|
i * Util.scale + Util.halfScale,
|
||||||
j * Util.scale + Util.scale / 2
|
j * Util.scale + Util.halfScale
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,8 +51,8 @@ class Coord {
|
|||||||
// Return coordinates relative to the selected color's position on the
|
// Return coordinates relative to the selected color's position on the
|
||||||
// board. White on top, black on bottom.
|
// board. White on top, black on bottom.
|
||||||
Coord rel(Util.Col col) {
|
Coord rel(Util.Col col) {
|
||||||
return col.equals(Util.Col.BLACK)
|
return col.equals(Util.Col.WHITE)
|
||||||
? new Coord(this.x, Util.boardW - this.y)
|
? new Coord(this.x, Util.boardW - this.y - 1)
|
||||||
: this;
|
: this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package chess;
|
package chess;
|
||||||
|
|
||||||
|
import chess.Util.Col;
|
||||||
import javalib.impworld.*;
|
import javalib.impworld.*;
|
||||||
|
|
||||||
class Game extends World {
|
class Game extends World {
|
||||||
@@ -18,6 +19,8 @@ class Game extends World {
|
|||||||
this(
|
this(
|
||||||
new Player(Util.Col.WHITE), new Player(Util.Col.BLACK), new Board()
|
new Player(Util.Col.WHITE), new Player(Util.Col.BLACK), new Board()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
public WorldScene makeScene() {
|
public WorldScene makeScene() {
|
||||||
@@ -30,4 +33,72 @@ class Game extends World {
|
|||||||
this.bigBang(Util.boardW * Util.scale, Util.boardW * Util.scale, 0.01);
|
this.bigBang(Util.boardW * Util.scale, Util.boardW * Util.scale, 0.01);
|
||||||
}
|
}
|
||||||
void draw() { this.scene = this.board.draw(); }
|
void draw() { this.scene = this.board.draw(); }
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
this.board.board.put(new Coord(0, 0), new Rook(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(1, 0), new Knight(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(2, 0), new Bishop(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(3, 0), new King(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(4, 0), new Queen(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(5, 0), new Bishop(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(6, 0), new Knight(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(7, 0), new Rook(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(0, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(1, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(2, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(3, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(4, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(5, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(6, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
this.board.board.put(new Coord(7, 1), new Pawn(Util.Col.BLACK));
|
||||||
|
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(0, 0).rel(Util.Col.WHITE), new Rook(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(1, 0).rel(Util.Col.WHITE), new Knight(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(2, 0).rel(Util.Col.WHITE), new Bishop(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(3, 0).rel(Util.Col.WHITE), new King(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(4, 0).rel(Util.Col.WHITE), new Queen(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(5, 0).rel(Util.Col.WHITE), new Bishop(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(6, 0).rel(Util.Col.WHITE), new Knight(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(7, 0).rel(Util.Col.WHITE), new Rook(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(0, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(1, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(2, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(3, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(4, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(5, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(6, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
this.board.board.put(
|
||||||
|
new Coord(7, 1).rel(Util.Col.WHITE), new Pawn(Util.Col.WHITE)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,7 +15,7 @@ class Examples {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void testBoard(Tester t) {
|
void testBoard(Tester t) {
|
||||||
Piece pawn = new Pawn();
|
Piece pawn = new Pawn(Util.Col.BLACK);
|
||||||
Board board = new Board();
|
Board board = new Board();
|
||||||
board.set(new Coord(0, 0), pawn);
|
board.set(new Coord(0, 0), pawn);
|
||||||
t.checkExpect(board.get(new Coord(0, 0)), pawn);
|
t.checkExpect(board.get(new Coord(0, 0)), pawn);
|
||||||
|
@@ -8,16 +8,71 @@ enum PieceType { PAWN, ROOK, KNIGHT, BISHOP, POPE, QUEEN, KING }
|
|||||||
// A chess piece.
|
// A chess piece.
|
||||||
abstract class Piece {
|
abstract class Piece {
|
||||||
PieceType type;
|
PieceType type;
|
||||||
|
Util.Col col;
|
||||||
|
|
||||||
Piece(PieceType type) { this.type = type; }
|
Piece(PieceType type, Util.Col col) {
|
||||||
|
this.type = type;
|
||||||
|
this.col = col;
|
||||||
|
}
|
||||||
WorldImage draw() { return new EmptyImage(); };
|
WorldImage draw() { return new EmptyImage(); };
|
||||||
}
|
}
|
||||||
|
|
||||||
class Pawn extends Piece {
|
class Pawn extends Piece {
|
||||||
Pawn() { super(PieceType.PAWN); }
|
Pawn(Util.Col col) { super(PieceType.PAWN, col); }
|
||||||
WorldImage draw() {
|
WorldImage draw() {
|
||||||
return new FromFileImage(
|
return new FromFileImage(
|
||||||
"/home/jacob/Projects/CS3/chess/imgs/whitePawn.png"
|
"chess/imgs/" + (this.col == Util.Col.WHITE ? "white" : "black") +
|
||||||
|
"Pawn.png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Rook extends Piece {
|
||||||
|
Rook(Util.Col col) { super(PieceType.ROOK, col); }
|
||||||
|
WorldImage draw() {
|
||||||
|
return new FromFileImage(
|
||||||
|
"chess/imgs/" + (this.col == Util.Col.WHITE ? "white" : "black") +
|
||||||
|
"Rook.png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Knight extends Piece {
|
||||||
|
Knight(Util.Col col) { super(PieceType.KNIGHT, col); }
|
||||||
|
WorldImage draw() {
|
||||||
|
return new FromFileImage(
|
||||||
|
"chess/imgs/" + (this.col == Util.Col.WHITE ? "white" : "black") +
|
||||||
|
"Knight.png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Bishop extends Piece {
|
||||||
|
Bishop(Util.Col col) { super(PieceType.BISHOP, col); }
|
||||||
|
WorldImage draw() {
|
||||||
|
return new FromFileImage(
|
||||||
|
"chess/imgs/" + (this.col == Util.Col.WHITE ? "white" : "black") +
|
||||||
|
"Bishop.png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Queen extends Piece {
|
||||||
|
Queen(Util.Col col) { super(PieceType.QUEEN, col); }
|
||||||
|
WorldImage draw() {
|
||||||
|
return new FromFileImage(
|
||||||
|
"chess/imgs/" + (this.col == Util.Col.WHITE ? "white" : "black") +
|
||||||
|
"Queen.png"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class King extends Piece {
|
||||||
|
King(Util.Col col) { super(PieceType.KING, col); }
|
||||||
|
WorldImage draw() {
|
||||||
|
return new FromFileImage(
|
||||||
|
"chess/imgs/" + (this.col == Util.Col.WHITE ? "white" : "black") +
|
||||||
|
"King.png"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,6 +8,12 @@ class Util {
|
|||||||
// Scale of the drawn chessboard.
|
// Scale of the drawn chessboard.
|
||||||
static int scale = 64;
|
static int scale = 64;
|
||||||
|
|
||||||
|
// Half the scale.
|
||||||
|
static int halfScale = 32;
|
||||||
|
|
||||||
|
// The number of pixels.
|
||||||
|
static int size = 64 * 8;
|
||||||
|
|
||||||
// The color a piece can be.
|
// The color a piece can be.
|
||||||
enum Col { WHITE, BLACK }
|
enum Col { WHITE, BLACK }
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user