Beispiel #1
0
 /** Tests the clearBoard method. */
 public void testClearBoard() {
   assertEquals(board.getAllBlackPieces().size(), 16);
   assertEquals(board.getAllBlackPieces().size(), 16);
   board.resetBoard(false);
   assertEquals(board.getAllBlackPieces().size(), 0);
   assertEquals(board.getAllBlackPieces().size(), 0);
 }
Beispiel #2
0
 /**
  * Tests to make sure getCell returns null if bad arguments are given, and that it returns the
  * right cell if valid arguments are given.
  */
 public void testGetCell() {
   assertNull(board.getCell(-1, -1));
   assertNull(board.getCell(9, 9));
   assertNull(board.getCell(8, 8));
   assertNotNull(board.getCell(0, 0));
   assertNotNull(board.getCell(7, 7));
   assertNotNull(board.getCell(2, 3));
 }
Beispiel #3
0
  /** Performs a check and makes sure all conditions are met for this state. */
  public void testCheck() {
    assertTrue(gameEngine.playerHasLegalMoves(1));
    assertTrue(gameEngine.playerHasLegalMoves(2));
    assertEquals(gameEngine.determineGameLost(), 0);
    showInFrame(panel);
    this.click(board.getCells()[6][3]);
    this.click(board.getCells()[4][3]); // move pawn above queen 2 north
    this.click(board.getCells()[1][4]);
    this.click(board.getCells()[3][4]);
    assertEquals(gameEngine.determineGameLost(), 0);

    this.click(board.getCells()[7][2]);
    this.click(board.getCells()[3][6]); // move bishop 4 north-east
    assertEquals(gameEngine.determineGameLost(), 0);
    assertTrue(gameEngine.isKingInCheck(true)); // check current king
    assertFalse(gameEngine.isKingInCheck(false)); // check other king
    assertTrue(gameEngine.playerHasLegalMoves(1));
    assertTrue(gameEngine.playerHasLegalMoves(2));
    assertEquals(gameEngine.getCurrentPlayer(), 2);
    board.getCells()[1][7].getPieceOnSquare().showLegalMoves(board);
    for (int i = 0; i < board.getCells().length; i++) {
      for (int j = 0; j < board.getCells()[0].length; j++) {
        assertTrue(
            !board.getCells()[i][j].getBackground().equals(Color.PINK)
                && !board.getCells()[i][j].getBackground().equals(Color.YELLOW));
      } // makes sure no illegal moves were shown
    }
    assertEquals(
        board.getCells()[1][7].getPieceOnSquare().calculatePossibleMoves(board).size(),
        2); // this includes illegal moves
  }
Beispiel #4
0
  /** Tests the constructor and the initial setup. */
  public void testInitialSetupPerformedCorrectly() {
    assertTrue(cells[0][0].getPieceOnSquare() instanceof Rook);
    assertTrue(cells[1][0].getPieceOnSquare() instanceof Pawn);
    assertTrue(cells[1][0].getPieceOnSquare() instanceof Pawn);

    int numBlacks = board.getAllBlackPieces().size();
    int numWhites = board.getAllWhitePieces().size();

    assertEquals(32, numBlacks + numWhites);
    assertEquals(16, numWhites);
    assertEquals(16, numBlacks);
  }
Beispiel #5
0
  /**
   * Performs a checkmate and determines if all conditions are met for this end-game state. (for
   * player 2)
   */
  public void testCheckmatePlayerTwo() {
    assertTrue(gameEngine.playerHasLegalMoves(1));
    assertTrue(gameEngine.playerHasLegalMoves(2));
    assertEquals(gameEngine.determineGameLost(), 0);
    showInFrame(panel);
    this.click(board.getCells()[6][0]);
    this.click(board.getCells()[5][0]);
    this.click(board.getCells()[1][3]);
    this.click(board.getCells()[3][3]); // move pawn below queen 2 south
    this.click(board.getCells()[5][0]);
    this.click(board.getCells()[4][0]);
    assertEquals(gameEngine.determineGameLost(), 0);

    this.click(board.getCells()[0][2]);
    this.click(board.getCells()[3][5]); // move bishop 3
    this.click(board.getCells()[4][0]);
    this.click(board.getCells()[3][0]);
    assertEquals(gameEngine.determineGameLost(), 0);

    this.click(board.getCells()[0][4]);
    this.click(board.getCells()[2][2]); // move queen 2
    this.click(board.getCells()[3][0]);
    this.click(board.getCells()[2][0]);
    assertEquals(gameEngine.determineGameLost(), 0);

    this.click(board.getCells()[2][2]);
    this.click(board.getCells()[6][2]);
    assertFalse(gameEngine.playerHasLegalMoves(1));
    assertTrue(gameEngine.playerHasLegalMoves(2));
    assertEquals(gameEngine.determineGameLost(), 1);
    assertEquals(gameEngine.getCurrentPlayer(), 1);
    selectConfirmDialogOption(JOptionPane.YES_OPTION);
  }
Beispiel #6
0
 /** Tests the stalemate end-game condition and makes sure all conditions are met properly. */
 public void testStalemate() {
   assertTrue(gameEngine.playerHasLegalMoves(1));
   assertTrue(gameEngine.playerHasLegalMoves(2));
   assertEquals(gameEngine.determineGameLost(), 0);
   showInFrame(panel);
   board.resetBoard(true); // we're forcing a reset, game engine will be
   // out of sync
   new King(board, 0, 0, 0);
   new King(board, 5, 5, 1);
   click(board.getCells()[5][5]);
   click(board.getCells()[4][5]); // force the game engine to update
   assertEquals(gameEngine.determineGameLost(), -1);
   selectConfirmDialogOption(JOptionPane.NO_OPTION);
 }
Beispiel #7
0
  /**
   * Tests to make sure clearCell returns null if bad arguments are given, and that it returns the
   * right cell if valid arguments are given.
   */
  public void testClearCell() {
    Exception caughtEx = null;
    try {
      board.clearCell(-1, -1);
    } catch (Exception ex) {
      caughtEx = ex;
    }
    assertTrue(caughtEx instanceof IllegalStateException);

    caughtEx = null;
    try {
      board.clearCell(9, 9);
    } catch (Exception ex) {
      caughtEx = ex;
    }
    assertTrue(caughtEx instanceof IllegalStateException);

    assertNotNull(board.getCell(0, 0).getPieceOnSquare());
    assertNotNull(board.getCell(7, 7).getPieceOnSquare());
    assertNull(board.getCell(2, 3).getPieceOnSquare());

    board.clearCell(0, 0);
    board.clearCell(7, 7);
    board.clearCell(2, 3);

    assertNull(board.getCell(0, 0).getPieceOnSquare());
    assertNull(board.getCell(7, 7).getPieceOnSquare());
    assertNull(board.getCell(2, 3).getPieceOnSquare());
  }
Beispiel #8
0
  /** Makes sure invalid clicks are handled properly. */
  public void testIllegalMoves() {
    showInFrame(panel);
    this.click(board.getCells()[6][0]);
    this.click(board.getCells()[6][0]);
    this.click(board.getCells()[4][2]);
    assertNotNull(this.getComponent(JOptionPane.class));
    click(getComponent(JButton.class, where.textIs("OK")));
    this.click(board.getCells()[0][2]);
    assertNotNull(this.getComponent(JOptionPane.class));
    click(getComponent(JButton.class, where.textIs("OK")));

    this.click(board.getCells()[7][1]);
    this.click(board.getCells()[0][0]);
    assertNotNull(this.getComponent(JOptionPane.class));
    click(getComponent(JButton.class, where.textIs("OK")));
    this.click(board.getCells()[6][1]);
    this.click(board.getCells()[5][1]);

    this.click(board.getCells()[1][1]);
    this.click(board.getCells()[7][7]);

    assertNotNull(this.getComponent(JOptionPane.class));
    click(getComponent(JButton.class, where.textIs("OK")));
    this.click(board.getCells()[7][7]);
  }
Beispiel #9
0
  /** Tests the clearColorsOnBoard method. */
  public void testClearColorsOnBoard() {
    for (int i = 0; i < cells.length; i++) {
      for (int j = 0; j < cells[0].length; j++) {
        assertTrue(
            cells[i][j].getBackground().equals(Color.BLACK)
                || cells[i][j].getBackground().equals(Color.WHITE));
      }
    }

    cells[0][0].setBackground(Color.PINK);
    cells[5][4].setBackground(Color.GREEN);
    board.clearColorsOnBoard();

    for (int i = 0; i < cells.length; i++) {
      for (int j = 0; j < cells[0].length; j++) {
        assertTrue(
            cells[i][j].getBackground().equals(Color.BLACK)
                || cells[i][j].getBackground().equals(Color.WHITE));
      }
    }
  }
Beispiel #10
0
 /** Tests the getAllBlackPieces method. */
 public void testGetBlackPieces() {
   assertEquals(board.getAllBlackPieces().size(), 16);
   board.getCells()[0][0].clearSquare();
   assertEquals(board.getAllBlackPieces().size(), 15);
 }
Beispiel #11
0
 /** Tests the getAllWhitePieces method. */
 public void testGetWhitePieces() {
   assertEquals(board.getAllWhitePieces().size(), 16);
   board.getCells()[7][7].clearSquare();
   assertEquals(board.getAllWhitePieces().size(), 15);
 }
Beispiel #12
0
 /** Performs initial setup. */
 public void setUp() {
   board = new ChessGameBoard();
   cells = board.getCells();
 }