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;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import javalib.impworld.WorldScene;
import javalib.worldimages.EmptyImage;
@@ -10,6 +11,8 @@ import javalib.worldimages.RectangleImage;
class Board {
Map<Coord, Piece> board;
Board() { this.board = new HashMap<Coord, Piece>(); }
WorldScene draw() {
WorldScene scene =
new WorldScene(Util.boardW * Util.scale, Util.boardW * Util.scale);
@@ -31,6 +34,9 @@ class Board {
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.
@@ -51,11 +57,10 @@ class Coord {
}
// Whether two Coords are equal.
public Boolean equals(Coord that) {
return this.x == that.x && this.y == that.y;
public boolean equals(Object that) {
if (!(that instanceof Coord)) return false;
else return ((Coord)that).x == this.x && ((Coord)that).y == this.y;
}
public int hashCode() {
return 31 * Integer.hashCode(this.x) + Integer.hashCode(this.y);
}
public int hashCode() { return 31 * Integer.hashCode(this.x + this.y); }
}

View File

@@ -4,4 +4,20 @@ import tester.Tester;
class Examples {
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);
}
}