示例#1
0
 /**
  * Test that the <code>Piece</code> sets itself up exactly as many times as it needs, when some of
  * its initial positions correspond to unusable cells.
  */
 public void testSetUpLoopUnusable() {
   List<int[]> unusable = new ArrayList<int[]>(2);
   unusable.add(new int[] {6, 0});
   unusable.add(new int[] {1, 7});
   Board newBoard =
       new RectangularBoard(
           RectangularBoard.defaultLength, RectangularBoard.defaultHeight, unusable);
   Piece newKnight = new Knight(true, newBoard);
   try {
     newKnight.setUp();
     assertEquals(
         "Piece did not set up correctly", newKnight, newBoard.getPiece(new int[] {1, 0}));
     newKnight = new Knight(true, newBoard);
     newKnight.setUp();
     fail("Piece did not fail while setting up, as it should have");
   } catch (InvalidInitialPositionException e1) {
     try {
       newKnight = new Knight(false, newBoard);
       newKnight.setUp();
       assertEquals(
           "Piece did not set up correctly", newKnight, newBoard.getPiece(new int[] {6, 7}));
       newKnight = new Knight(false, newBoard);
       newKnight.setUp();
       fail("Piece did not fail while setting up, as it should have");
     } catch (InvalidInitialPositionException e2) {
       assertEquals("Number of Pieces in Board is incorrect", 1, newBoard.getPieces(true).size());
       assertEquals("Number of Pieces in Board is incorrect", 1, newBoard.getPieces(false).size());
       return;
     }
     fail("Black Piece set itself up successfully to an unusable cell.");
   }
   fail("White Piece set itself up successfully to an unusable cell.");
 }
示例#2
0
文件: Rook.java 项目: ipince/pawned
 /**
  * Returns a list with the valid Plies of this <code>Rook</code>, as specified in the overview.
  *
  * @return A <code>List</code> of valid <code>Plies</code> for this <code>Rook</code>; if the
  *     <code>Rook</code> is not currently in a <code>Board</code>, the <code>List</code> will be
  *     empty.
  */
 public List<Ply> getPlies() {
   checkRep();
   Board board = getBoard();
   // Create list of valid cells to move to
   List<int[]> validCells = new LinkedList<int[]>();
   List<Ply> validPlies = new ArrayList<Ply>();
   if (board.contains(this)) {
     int[] pos = board.getPosition(this);
     int[] temp = new int[2];
     // Get positions where rook can move (horizontally or vertically)
     for (int i = 0; i < 4; i++) {
       // 0 = north; 1 = east; 2 = south; 3 = west
       temp = oneCellNext(pos, i);
       while (board.isUsable(temp)
           && (board.isEmpty(temp)
               || ((board.getPiece(temp).isWhite() && !isWhite())
                   || (!board.getPiece(temp).isWhite() && isWhite())))) {
         validCells.add(temp.clone());
         if (!board.isEmpty(temp)
             && ((board.getPiece(temp).isWhite() && !isWhite())
                 || (!board.getPiece(temp).isWhite() && isWhite()))) break;
         temp = oneCellNext(temp, i);
       }
     }
     // Create list of valid moves
     for (int[] validCell : validCells) validPlies.add(new Move(pos, validCell));
   }
   checkRep();
   return validPlies;
 }
示例#3
0
 /** Test valid <code>Plies</code> in a normal setting (no move-preventing cells). */
 public void testPliesNormal() {
   // For white Piece
   board.addPiece(whiteKnight, new int[] {4, 5});
   Set<Ply> correctPlies = pliesAtFourFive;
   checkPlies(correctPlies, whiteKnight);
   // For black Piece
   board.removePiece(whiteKnight);
   board.addPiece(blackKnight, new int[] {4, 5});
   checkPlies(correctPlies, blackKnight);
 }
示例#4
0
文件: Rook.java 项目: ipince/pawned
 /**
  * Returns the initial position of a <code>Rook</code>. This method makes stronger assumption that
  * the <code>Rook</code> plays in a <code>RectangularBoard</code> with dimensions greater than or
  * equal to 8. Its initial position then corresponds to the initial position of rooks in a regular
  * game of chess. That is, if the <code>Rook</code> is white, the initial positions are a1 or h1,
  * and a8 or h8 otherwise.
  *
  * <p>Note that use of this method is not necessary for the correct functioning of a <code>Rook
  * </code>. In other words, a <code>Rook</code> could be placed in any arbitrary cell in a
  * 2-dimensional <code>Board</code>, and it all its functionality will work appropriately.
  */
 protected int[] initialPos() {
   Board board = getBoard();
   int row;
   int[] pos = new int[2];
   if (isWhite()) {
     row = 0;
   } else {
     row = 7;
   }
   pos[1] = row;
   pos[0] = 0;
   if (!board.isEmpty(pos)) pos[0] = 7;
   return pos;
 }
示例#5
0
 /**
  * Test valid <code>Plies</code> among unusable cells (user-defined). The <code>Plies</code>
  * should exclude those that end in unusable cells.
  */
 public void testPliesUnusableCenter() {
   // For white Piece with holes (unusable)
   List<int[]> unusable = new ArrayList<int[]>(2);
   unusable.add(new int[] {5, 7});
   unusable.add(new int[] {3, 3});
   Board holedBoard =
       new RectangularBoard(
           RectangularBoard.defaultLength, RectangularBoard.defaultHeight, unusable);
   Piece newKnight = new Knight(true, holedBoard);
   holedBoard.addPiece(newKnight, new int[] {4, 5});
   Set<Ply> correctPlies = new HashSet<Ply>();
   for (Ply ply : pliesAtFourFive)
     if (!ply.equals(new Move(new int[] {4, 5}, new int[] {5, 7}))
         && !ply.equals(new Move(new int[] {4, 5}, new int[] {3, 3}))) correctPlies.add(ply);
   checkPlies(correctPlies, newKnight);
 }
示例#6
0
 /**
  * Test valid <code>Plies</code> among unusable cells (sides). The <code>Plies</code> should be
  * restricted by the limits of the <code>Board</code>.
  */
 public void testPliesUnusableSides() {
   // For black Piece
   board.addPiece(blackKnight, new int[] {0, 7});
   Set<Ply> correctPlies = new HashSet<Ply>();
   correctPlies.add(new Move(new int[] {0, 7}, new int[] {1, 5}));
   correctPlies.add(new Move(new int[] {0, 7}, new int[] {2, 6}));
   checkPlies(correctPlies, blackKnight);
 }
示例#7
0
 /**
  * Test that the <code>Piece</code> sets itself up exactly as many times as it needs, assuming its
  * initial positions correspond to usable cells.
  */
 public void testSetUpLoopNormal() {
   // For a white Piece
   Piece newKnight = null;
   while (true) {
     try {
       newKnight = new Knight(true, board);
       newKnight.setUp();
     } catch (IllegalStateException e) { // not really needed
       fail(
           "White Piece threw an IllegalStateException while set"
               + "ting up to an occupied cell. This should not happen.");
     } catch (InvalidInitialPositionException e) {
       assertFalse(
           "White Piece was added to the Board after failing to set up",
           board.contains(newKnight));
       assertFalse(
           "A black Piece was added to the Board while testing a " + "white Piece",
           board.contains(newKnight));
       assertEquals(
           "More than two white Pieces (or less) were " + "added when setting up",
           2,
           board.getPieces(true).size());
       break;
     }
   }
   // For a black Piece
   while (true) {
     try {
       newKnight = new Knight(false, board);
       newKnight.setUp();
     } catch (IllegalStateException e) { // not really needed
       fail(
           "Black Piece threw an IllegalStateException while set"
               + "ting up to an occupied cell. This should not happen.");
     } catch (InvalidInitialPositionException e) {
       assertFalse(
           "Black Piece was added to the Board after failing to set up",
           board.contains(newKnight));
       assertEquals(
           "More than two black Pieces (or less) were " + "added when setting up",
           2,
           board.getPieces(false).size());
       break;
     }
   }
 }
示例#8
0
 /**
  * Test valid <code>Plies</code> in a complex setting, i.e. a combination of the previous tests.
  */
 public void testPliesComplex() {
   List<int[]> unusable = new ArrayList<int[]>(2);
   unusable.add(new int[] {5, 7});
   unusable.add(new int[] {3, 3});
   Board holedBoard =
       new RectangularBoard(
           RectangularBoard.defaultLength, RectangularBoard.defaultHeight, unusable);
   Piece whiteKnight = new Knight(true, holedBoard);
   Piece blackBishop = new Bishop(false, holedBoard);
   Piece newKnight = new Knight(true, holedBoard);
   holedBoard.addPiece(whiteKnight, new int[] {5, 3});
   holedBoard.addPiece(blackBishop, new int[] {2, 6});
   holedBoard.addPiece(newKnight, new int[] {4, 5});
   Set<Ply> correctPlies = new HashSet<Ply>();
   correctPlies.add(new Move(new int[] {4, 5}, new int[] {3, 7}));
   correctPlies.add(new Move(new int[] {4, 5}, new int[] {6, 6}));
   correctPlies.add(new Move(new int[] {4, 5}, new int[] {6, 4}));
   correctPlies.add(new Move(new int[] {4, 5}, new int[] {2, 4}));
   correctPlies.add(new Move(new int[] {4, 5}, new int[] {2, 6}));
   checkPlies(correctPlies, newKnight);
 }
示例#9
0
 /** Test that the <code>Piece</code> fails to be set up more than once. */
 public void testSetUpTwice() {
   // For a white Piece
   while (true) {
     try {
       whiteKnight.setUp();
     } catch (IllegalStateException e) {
       assertTrue(
           "White Piece was not added to the Board after being set up",
           board.contains(whiteKnight));
       assertFalse(
           "A black Piece was added to the Board while testing a " + "white Piece",
           board.contains(blackKnight));
       assertEquals(
           "More than one white Piece (or no Pieces) was (were) " + "added when setting up",
           1,
           board.getPieces(true).size());
       assertEquals(
           "White Piece did not set up itself at the correct position",
           whiteKnight,
           board.getPiece(new int[] {1, 0}));
       break;
     } catch (InvalidInitialPositionException e) {
       fail(
           "White Piece threw an InvalidInitialPositionException while "
               + "setting up more than once. This should not happen.");
     }
   }
   // For a black Piece
   while (true) {
     try {
       blackKnight.setUp();
     } catch (IllegalStateException e) {
       assertTrue(
           "Black Piece was not added to the Board after being set up",
           board.contains(blackKnight));
       assertEquals(
           "More than one black Piece (or no Pieces) was (were) " + "added when setting up",
           1,
           board.getPieces(false).size());
       assertEquals(
           "Black Piece did not set up itself at the correct position",
           blackKnight,
           board.getPiece(new int[] {1, 7}));
       break;
     } catch (InvalidInitialPositionException e) {
       fail(
           "Black Piece threw an InvalidInitialPositionException while "
               + "setting up more than once. This should not happen.");
     }
   }
 }
示例#10
0
 /**
  * Test valid <code>Plies</code> among <code>Piece</code>s of the same color. The <code>Plies
  * </code> should exclude those that end in a cell occupied by another <code>Piece</code> of the
  * same color.
  */
 public void testPliesSameColor() {
   // For white Piece
   Piece whitePawn = new Pawn(true, board);
   board.addPiece(whitePawn, new int[] {5, 7});
   board.addPiece(whiteKnight, new int[] {4, 5});
   Set<Ply> correctPlies = new HashSet<Ply>();
   for (Ply ply : pliesAtFourFive)
     if (!ply.equals(new Move(new int[] {4, 5}, new int[] {5, 7}))) correctPlies.add(ply);
   checkPlies(correctPlies, whiteKnight);
   // For black Piece
   board.removePiece(whitePawn);
   board.removePiece(whiteKnight);
   Piece blackPawn = new Pawn(false, board);
   board.addPiece(blackPawn, new int[] {5, 7});
   board.addPiece(blackKnight, new int[] {4, 5});
   checkPlies(correctPlies, blackKnight);
 }
示例#11
0
 /**
  * Test valid <code>Plies</code> among <code>Piece</code>s of different color. The <code>Plies
  * </code> should not exclude anything.
  */
 public void testPliesDiffColor() {
   // For white Piece
   Piece blackPawn = new Pawn(false, board);
   Piece blackBishop = new Bishop(false, board);
   board.addPiece(blackPawn, new int[] {5, 7});
   board.addPiece(blackBishop, new int[] {3, 3});
   board.addPiece(whiteKnight, new int[] {4, 5});
   Set<Ply> correctPlies = pliesAtFourFive;
   checkPlies(correctPlies, whiteKnight);
   // For black Piece
   board.removePiece(blackPawn);
   board.removePiece(blackBishop);
   board.removePiece(whiteKnight);
   Piece whitePawn = new Pawn(true, board);
   Piece whiteBishop = new Bishop(true, board);
   board.addPiece(whitePawn, new int[] {5, 7});
   board.addPiece(whiteBishop, new int[] {3, 3});
   board.addPiece(blackKnight, new int[] {4, 5});
   checkPlies(correctPlies, blackKnight);
 }
示例#12
0
 /**
  * Test that the <code>Piece</code> sets itself up in a <code>Board</code> successfully and
  * correctly. It is set up as many times as it should handle.
  */
 public void testSetUpNormal() {
   // For a white Piece
   Piece newKnight = new Knight(true, board);
   try {
     whiteKnight.setUp();
     newKnight.setUp();
   } catch (Exception e) {
     fail("White Piece failed to set up appropriately");
   }
   assertTrue(
       "White Piece was not added to the Board after being set up", board.contains(whiteKnight));
   assertTrue(
       "White Piece was not added to the Board after being set up", board.contains(newKnight));
   assertEquals(
       "More than two Pieces (or less) were added when " + "setting up",
       2,
       board.getPieces(true).size());
   assertTrue(
       "Another Piece was added to the Board",
       board.getPiece(new int[] {1, 0}).getType().equals(new String("knight"))
           && board.getPiece(new int[] {6, 0}).getType().equals(new String("knight")));
   assertEquals(
       "White Piece did not set up itself at the correct position",
       whiteKnight,
       board.getPiece(new int[] {1, 0})); // Notice order
   assertEquals(
       "White Piece did not set up itself at the correct position",
       newKnight,
       board.getPiece(new int[] {6, 0}));
   // For a black Piece
   newKnight = new Knight(false, board);
   try {
     blackKnight.setUp();
     newKnight.setUp();
   } catch (Exception e) {
     fail("Black Piece failed to set up appropriately");
   }
   assertTrue(
       "Black Piece was not added to the Board after being set up", board.contains(blackKnight));
   assertTrue(
       "Black Piece was not added to the Board after being set up", board.contains(newKnight));
   assertEquals(
       "More than two Pieces (or less) were added when " + "setting up",
       2,
       board.getPieces(false).size());
   assertTrue(
       "Another Piece was added to the Board",
       board.getPiece(new int[] {1, 7}).getType().equals(new String("knight"))
           && board.getPiece(new int[] {6, 7}).getType().equals(new String("knight")));
   assertEquals(
       "Black Piece did not set up itself at the correct position",
       blackKnight,
       board.getPiece(new int[] {1, 7})); // Notice order
   assertEquals(
       "Black Piece did not set up itself at the correct position",
       newKnight,
       board.getPiece(new int[] {6, 7}));
 }