Permalink
Browse files

All ChessPosition and Square tests passed

  • Loading branch information...
1 parent a6c6c66 commit 8d444fee9a6b0c69c11ba6daecfcb7a794593bd2 Kestutis-Z committed Nov 6, 2012
Showing with 1,062 additions and 6 deletions.
  1. +142 −0 endgame-oracle/src/main/java/chess/ChessPosition.java
  2. +37 −0 endgame-oracle/src/main/java/chess/ChessPositionEvaluation.java
  3. +7 −0 endgame-oracle/src/main/java/chess/SideToMove.java
  4. +221 −0 endgame-oracle/src/main/java/chess/Square.java
  5. +12 −0 endgame-oracle/src/main/java/chess/SquareContainsNoPiecesException.java
  6. +13 −0 endgame-oracle/src/main/java/chess/SquareHasNoAdjacentSquareException.java
  7. +174 −0 endgame-oracle/src/test/java/chess/ChessPositionTest.java
  8. +287 −0 endgame-oracle/src/test/java/chess/SquareTest.java
  9. BIN endgame-oracle/target/classes/chess/ChessPosition.class
  10. BIN endgame-oracle/target/classes/chess/ChessPositionEvaluation$ChessPositionEvaluationWithDTM.class
  11. BIN endgame-oracle/target/classes/chess/ChessPositionEvaluation.class
  12. BIN endgame-oracle/target/classes/chess/SideToMove.class
  13. BIN endgame-oracle/target/classes/chess/Square$Direction.class
  14. BIN endgame-oracle/target/classes/chess/Square.class
  15. BIN endgame-oracle/target/classes/chess/SquareContainsNoPiecesException.class
  16. BIN endgame-oracle/target/classes/chess/SquareHasNoAdjacentSquareException.class
  17. +71 −0 endgame-oracle/target/surefire-reports/TEST-chess.ChessPositionTest.xml
  18. +84 −0 endgame-oracle/target/surefire-reports/TEST-chess.SquareTest.xml
  19. +5 −5 endgame-oracle/target/surefire-reports/TEST-tablebases.TablebaseTest.xml
  20. +4 −0 endgame-oracle/target/surefire-reports/chess.ChessPositionTest.txt
  21. +4 −0 endgame-oracle/target/surefire-reports/chess.SquareTest.txt
  22. +1 −1 endgame-oracle/target/surefire-reports/tablebases.TablebaseTest.txt
  23. BIN endgame-oracle/target/test-classes/chess/ChessPositionTest.class
  24. BIN endgame-oracle/target/test-classes/chess/SquareTest.class
@@ -0,0 +1,142 @@
+package chess;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.EnumBiMap;
+
+import tablebases.Tablebase;
+
+/** Representation of chess position: side to move, White and Black
+ * pieces, and their respective squares. */
+public class ChessPosition {
+ private BiMap<Piece, Square> piecesWithSquares;
+ private SideToMove sideToMove;
+
+ private ChessPosition(BiMap<Piece, Square> piecesWithSquares,
+ SideToMove sideToMove) {
+ this.piecesWithSquares = piecesWithSquares;
+ this.sideToMove = sideToMove;
+ }
+
+ private ChessPosition(Tablebase tablebase,
+ List<Square> squares, SideToMove sideToMove) {
+ piecesWithSquares = EnumBiMap.create(Piece.class, Square.class);
+ if (!allSquaresAreDifferent(squares)) {
+ throw new IllegalArgumentException("Some of the squares provided " +
+ "are the same: " + squares.toString());
+ }
+ List<Piece> pieces = tablebase.getAllPieces();
+ if (pieces.size() > squares.size()) {
+ throw new IllegalArgumentException("Pieces from " +
+ "tablebase " + tablebase + " were not provided " +
+ "enough of corresponding squares");
+ }
+ for (int i = 0; i < pieces.size(); i++) {
+ piecesWithSquares.put(pieces.get(i), squares.get(i));
+ }
+ this.sideToMove = sideToMove;
+ }
+
+ private static boolean allSquaresAreDifferent(List<Square> squareList) {
+ Set<Square> squareSet = new HashSet<>(squareList);
+ return squareList.size() == squareSet.size();
+ }
+
+ public static ChessPosition createFromPiecesToSquaresMap(BiMap<Piece,
+ Square> piecesWithSquares, SideToMove sideToMove) {
+ return new ChessPosition(piecesWithSquares, sideToMove);
+ }
+
+ public static ChessPosition createFromTablebase(Tablebase tablebase,
+ List<Square> squares, SideToMove sideToMove) {
+ return new ChessPosition(tablebase, squares, sideToMove);
+ }
+
+ public BiMap<Piece, Square> getPiecesWithSquares() {
+ return piecesWithSquares;
+ }
+
+ public void setPiecesWithSquares(EnumBiMap<Piece, Square> piecesWithSquares) {
+ this.piecesWithSquares = piecesWithSquares;
+ }
+
+ public SideToMove getSideToMove() {
+ return sideToMove;
+ }
+
+ public void setSideToMove(SideToMove sideToMove) {
+ this.sideToMove = sideToMove;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime
+ * result
+ + ((piecesWithSquares == null) ? 0 : piecesWithSquares
+ .hashCode());
+ result = prime * result
+ + ((sideToMove == null) ? 0 : sideToMove.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ ChessPosition other = (ChessPosition) obj;
+ if (piecesWithSquares == null) {
+ if (other.piecesWithSquares != null)
+ return false;
+ } else if (!piecesWithSquares.equals(other.piecesWithSquares))
+ return false;
+ if (sideToMove != other.sideToMove)
+ return false;
+ return true;
+ }
+
+ public List<Piece> getWhitePieces() {
+ List<Piece> whitePieces = new ArrayList<>();
+ for (Piece p : piecesWithSquares.keySet()) {
+ if (p == Piece.BLACK_KING)
+ break;
+ whitePieces.add(p);
+ }
+ return whitePieces;
+ }
+
+ public List<Piece> getBlackPieces() {
+ List<Piece> blackPieces = new ArrayList<>();
+ for (Piece p : piecesWithSquares.keySet()) {
+ if (p.getPieceColour() == PieceColour.BLACK)
+ blackPieces.add(p);
+ }
+ return blackPieces;
+ }
+
+ public List<Square> getWhiteSquares() {
+ List<Square> whiteSquares = new ArrayList<>();
+ for (Piece p : getWhitePieces()) {
+ whiteSquares.add(piecesWithSquares.get(p));
+ }
+ return whiteSquares;
+ }
+
+ public List<Square> getBlackSquares() {
+ List<Square> blackSquares = new ArrayList<>();
+ for (Piece p : getBlackPieces()) {
+ blackSquares.add(piecesWithSquares.get(p));
+ }
+ return blackSquares;
+ }
+
+}
@@ -0,0 +1,37 @@
+package chess;
+
+/** Game-theoretical value of position obtained from tablebase. */
+public enum ChessPositionEvaluation {
+
+ DRAW, WHITE_WINS, BLACK_WINS, ILLEGAL, NOT_EVALUATED;
+
+
+ /** Simple data structure representing the evaluation of
+ * some chess position, which include the additional data member -
+ * distance-to-mate. */
+ public static class ChessPositionEvaluationWithDTM {
+ private ChessPositionEvaluation chessPositionEvaluation;
+ /** A non-negative number of moves to mate in a position, theoretically won
+ * by White or Black. */
+ private int distanceToMate;
+
+ public ChessPositionEvaluation getChessPositionEvaluation() {
+ return chessPositionEvaluation;
+ }
+
+ public void setChessPositionEvaluation(
+ ChessPositionEvaluation chessPositionEvaluation) {
+ this.chessPositionEvaluation = chessPositionEvaluation;
+ }
+
+ public int getDistanceToMate() {
+ return distanceToMate;
+ }
+
+ public void setDistanceToMate(int distanceToMate) {
+ this.distanceToMate = distanceToMate;
+ }
+
+ }
+
+}
@@ -0,0 +1,7 @@
+package chess;
+
+/** Side-to-move in a chess position.
+ * <dt><b>Precondition:</b><dd>Must correspond with the enum PieceColour. */
+public enum SideToMove {
+ WHITE, BLACK
+}
Oops, something went wrong. Retry.

0 comments on commit 8d444fe

Please sign in to comment.