diff --git a/chess/src/chess/Board.java b/chess/src/chess/Board.java index 0c42164..c67a436 100644 --- a/chess/src/chess/Board.java +++ b/chess/src/chess/Board.java @@ -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 board; + Board() { this.board = new HashMap(); } + 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); } } diff --git a/chess/src/chess/Main.java b/chess/src/chess/Main.java index e33438d..77d2452 100644 --- a/chess/src/chess/Main.java +++ b/chess/src/chess/Main.java @@ -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); + } }