Пример #1
0
  /*
   * Creates a tree of moves to take with their evaluated scores
   */
  public void createSearchSpace(int level, State state, Board board, boolean myTurn) {
    int[] openColumns = board.getOpenColumns();
    state.nextMoves = new State[openColumns.length];

    for (int i = 0; i < openColumns.length; i++) {
      int move = openColumns[i];
      board.handleMove(myTurn, move);
      int score = heuristic.getScore(board);
      state.nextMoves[i] = new State(move, board, score);

      if (level != depth && board.hasWinner() == false)
        createSearchSpace(level + 1, state.nextMoves[i], board, !myTurn);

      board.undoMove(move);
      board.setHasWinner(false);
    }
  }
  @Test
  public void test() {
    Board test_board; // Spielbrett

    test_board = new Board(7, 6);

    // Test 1 : Spieler X gewinnt in einer Spalte
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.X, 0);

    assertTrue("Patt: findWinner() gibt Player.X", test_board.findWinner() == Player.X);

    // Test 2 : Spieler O gewinnt in einer Spalte
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.O, 0);

    assertTrue("Patt: findWinner() gibt Player.=O", test_board.findWinner() == Player.O);

    // Test 3 : Spieler X gewinnt in einer Zeile
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.X, 4);

    assertTrue("Patt: findWinner() gibt Player.X", test_board.findWinner() == Player.X);

    // Test 4 : Spieler O gewinnt in einer Zeile
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.O, 4);

    assertTrue("Patt: findWinner() gibt Player.=O", test_board.findWinner() == Player.O);

    // Test 5 : Spieler X gewinnt diagonal
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.X, 3);

    assertTrue("Patt: findWinner() gibt Player.=X", test_board.findWinner() == Player.X);

    // Test 6 : Spieler O gewinnt diagonal
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.O, 3);

    assertTrue("Patt: findWinner() gibt Player.=X", test_board.findWinner() == Player.O);

    // Test 7 Spielsituation: erste Möglichkeit Patt

    test_board = new Board(7, 6);

    // Spalte 1 füllen
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.O, 0);
    // Spalte 2 füllen
    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.X, 1);

    // Spalte3 füllen
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.O, 2);

    // Spalte 4 füllen
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.X, 3);

    // Spalte 5 füllen
    test_board.makeDrop(Player.X, 4);
    test_board.makeDrop(Player.O, 4);
    test_board.makeDrop(Player.O, 4);
    test_board.makeDrop(Player.X, 4);
    test_board.makeDrop(Player.O, 4);
    test_board.makeDrop(Player.O, 4);

    // Spalte 6 füllen
    test_board.makeDrop(Player.O, 5);
    test_board.makeDrop(Player.X, 5);
    test_board.makeDrop(Player.X, 5);
    test_board.makeDrop(Player.O, 5);
    test_board.makeDrop(Player.X, 5);
    test_board.makeDrop(Player.X, 5);

    // Spalte 7 füllen
    test_board.makeDrop(Player.X, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.X, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);

    // kontrolliert, ob das gewünschte Ergebnis in diesem Testfall eintritt
    assertTrue("Patt: findWinner() soll null zurückgeben", test_board.findWinner() == null);
    assertTrue("Patt: isFull() gibt true zurück", test_board.isFull());
    assertTrue("Patt: isGameDraw() gibt true zurück", test_board.isGameDraw());
    assertFalse("Patt: hasWinner() gibt false zurück", test_board.hasWinner());
    assertTrue("Patt: isEndSituation() gibt true zurück", test_board.isEndSituation());

    // Test 7 Spielsituation: zweite Möglichkeit für einen Patt

    test_board = new Board(7, 6);

    // Spalte 1 füllen
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.X, 0);
    test_board.makeDrop(Player.O, 0);
    test_board.makeDrop(Player.X, 0);
    // Spalte 2 füllen
    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.X, 1);
    test_board.makeDrop(Player.O, 1);
    test_board.makeDrop(Player.X, 1);

    // Spalte3 füllen
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.X, 2);
    test_board.makeDrop(Player.O, 2);
    test_board.makeDrop(Player.X, 2);

    // Spalte 4 füllen
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.O, 3);
    test_board.makeDrop(Player.X, 3);
    test_board.makeDrop(Player.O, 3);

    // Spalte 5 füllen
    test_board.makeDrop(Player.X, 4);
    test_board.makeDrop(Player.O, 4);
    test_board.makeDrop(Player.O, 4);
    test_board.makeDrop(Player.X, 4);
    test_board.makeDrop(Player.X, 4);
    test_board.makeDrop(Player.O, 4);

    // Spalte 6 füllen
    test_board.makeDrop(Player.O, 5);
    test_board.makeDrop(Player.X, 5);
    test_board.makeDrop(Player.O, 5);
    test_board.makeDrop(Player.X, 5);
    test_board.makeDrop(Player.O, 5);
    test_board.makeDrop(Player.X, 5);

    // Spalte 7 füllen
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.X, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.X, 6);

    // kontrolliert, ob das gewünschte Ergebnis in diesem Testfall eintritt
    assertTrue("Patt: findWinner() soll null zurückgeben", test_board.findWinner() == null);
    assertTrue("Patt: isFull() gibt true zurück", test_board.isFull() == true);
    assertTrue("Patt: isGameDraw() gibt true zurück", test_board.isGameDraw() == true);

    // Test 9 : Spieler wirft in eine volle Spalte
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.X, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.X, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);
    test_board.makeDrop(Player.O, 6);

    assertTrue(
        "Spalte ist voll: findFirstEmptyField gibt null zurück",
        test_board.findFirstEmtpyFieldInColumn(6) == null);

    // Test 10 : Spieler wirft in eine falsche Spalte (7)
    test_board = new Board(7, 6);

    test_board.makeDrop(Player.X, 7);

    assertTrue(
        "falsche Spalte: findFirstEmptyField gibt null zurück",
        test_board.findFirstEmtpyFieldInColumn(7) == null);
  }