Beispiel #1
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}));
 }
Beispiel #2
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.");
     }
   }
 }
Beispiel #3
0
 /**
  * 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;
 }
Beispiel #4
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;
     }
   }
 }