Fixed maps.

This commit is contained in:
Jacob Signorovitch
2025-05-01 12:44:23 -04:00
parent 5ae4277958
commit be47db6361
2 changed files with 26 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
package chess; package chess;
import java.awt.Color; import java.awt.Color;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javalib.impworld.WorldScene; import javalib.impworld.WorldScene;
import javalib.worldimages.EmptyImage; import javalib.worldimages.EmptyImage;
@@ -10,6 +11,8 @@ import javalib.worldimages.RectangleImage;
class Board { class Board {
Map<Coord, Piece> board; Map<Coord, Piece> board;
Board() { this.board = new HashMap<Coord, Piece>(); }
WorldScene draw() { WorldScene draw() {
WorldScene scene = WorldScene scene =
new WorldScene(Util.boardW * Util.scale, Util.boardW * Util.scale); new WorldScene(Util.boardW * Util.scale, Util.boardW * Util.scale);
@@ -31,6 +34,9 @@ class Board {
return scene; return scene;
} }
void set(Coord coord, Piece piece) { this.board.put(coord, piece); }
Piece get(Coord coord) { return this.board.get(coord); }
} }
// Measured from top left. // Measured from top left.
@@ -51,11 +57,10 @@ class Coord {
} }
// Whether two Coords are equal. // Whether two Coords are equal.
public Boolean equals(Coord that) { public boolean equals(Object that) {
return this.x == that.x && this.y == that.y; if (!(that instanceof Coord)) return false;
else return ((Coord)that).x == this.x && ((Coord)that).y == this.y;
} }
public int hashCode() { public int hashCode() { return 31 * Integer.hashCode(this.x + this.y); }
return 31 * Integer.hashCode(this.x) + Integer.hashCode(this.y);
}
} }

View File

@@ -4,4 +4,20 @@ import tester.Tester;
class Examples { class Examples {
void testItAll(Tester t) { new Game().run(); } void testItAll(Tester t) { new Game().run(); }
void testCoord(Tester t) {
Coord coord = new Coord(1, 1);
Coord cod = new Coord(1, 1);
Coord cor = new Coord(1, 2);
t.checkExpect(coord.equals(cod), true);
t.checkExpect(coord.equals(cor), false);
t.checkExpect(coord.hashCode() == cod.hashCode(), true);
t.checkExpect(coord.hashCode() == cor.hashCode(), false);
}
void testBoard(Tester t) {
Piece pawn = new Pawn();
Board board = new Board();
board.set(new Coord(0, 0), pawn);
t.checkExpect(board.get(new Coord(0, 0)), pawn);
}
} }