Search in sources :

Example 1 with TicTacToeState

use of aima.core.environment.tictactoe.TicTacToeState in project aima-java by aimacode.

the class TicTacToeDemo method startMinimaxDemo.

private static void startMinimaxDemo() {
    System.out.println("MINI MAX DEMO\n");
    TicTacToeGame game = new TicTacToeGame();
    TicTacToeState currState = game.getInitialState();
    AdversarialSearch<TicTacToeState, XYLocation> search = MinimaxSearch.createFor(game);
    while (!(game.isTerminal(currState))) {
        System.out.println(game.getPlayer(currState) + "  playing ... ");
        XYLocation action = search.makeDecision(currState);
        currState = game.getResult(currState, action);
        System.out.println(currState);
    }
    System.out.println("MINI MAX DEMO done");
}
Also used : XYLocation(aima.core.util.datastructure.XYLocation) TicTacToeGame(aima.core.environment.tictactoe.TicTacToeGame) TicTacToeState(aima.core.environment.tictactoe.TicTacToeState)

Example 2 with TicTacToeState

use of aima.core.environment.tictactoe.TicTacToeState in project aima-java by aimacode.

the class TicTacToeDemo method startAlphaBetaDemo.

private static void startAlphaBetaDemo() {
    System.out.println("ALPHA BETA DEMO\n");
    TicTacToeGame game = new TicTacToeGame();
    TicTacToeState currState = game.getInitialState();
    AdversarialSearch<TicTacToeState, XYLocation> search = AlphaBetaSearch.createFor(game);
    while (!(game.isTerminal(currState))) {
        System.out.println(game.getPlayer(currState) + "  playing ... ");
        XYLocation action = search.makeDecision(currState);
        currState = game.getResult(currState, action);
        System.out.println(currState);
    }
    System.out.println("ALPHA BETA DEMO done");
}
Also used : XYLocation(aima.core.util.datastructure.XYLocation) TicTacToeGame(aima.core.environment.tictactoe.TicTacToeGame) TicTacToeState(aima.core.environment.tictactoe.TicTacToeState)

Example 3 with TicTacToeState

use of aima.core.environment.tictactoe.TicTacToeState in project aima-java by aimacode.

the class TicTacToeTest method testMinmaxValueCalculation.

@Test
public void testMinmaxValueCalculation() {
    MinimaxSearch<TicTacToeState, XYLocation, String> search = MinimaxSearch.createFor(game);
    Assert.assertTrue(epsilon > Math.abs(search.maxValue(state, TicTacToeState.X) - 0.5));
    Assert.assertTrue(epsilon > Math.abs(search.minValue(state, TicTacToeState.O) - 0.5));
    // x o x
    // o o x
    // - - -
    // next move: x
    // x
    state.mark(0, 0);
    // o
    state.mark(1, 0);
    // x
    state.mark(2, 0);
    // o
    state.mark(0, 1);
    // x
    state.mark(2, 1);
    // o
    state.mark(1, 1);
    Assert.assertTrue(epsilon > Math.abs(search.maxValue(state, TicTacToeState.X) - 1));
    Assert.assertTrue(epsilon > Math.abs(search.minValue(state, TicTacToeState.O)));
    XYLocation action = search.makeDecision(state);
    Assert.assertEquals(new XYLocation(2, 2), action);
}
Also used : XYLocation(aima.core.util.datastructure.XYLocation) TicTacToeState(aima.core.environment.tictactoe.TicTacToeState) Test(org.junit.Test)

Example 4 with TicTacToeState

use of aima.core.environment.tictactoe.TicTacToeState in project aima-java by aimacode.

the class TicTacToeTest method testHashCode.

@Test
public void testHashCode() {
    TicTacToeState initialState1 = game.getInitialState();
    TicTacToeState initialState2 = game.getInitialState();
    Assert.assertEquals(initialState1.hashCode(), initialState2.hashCode());
    TicTacToeState state1 = game.getResult(initialState1, new XYLocation(0, 0));
    Assert.assertNotSame(state1.hashCode(), initialState2.hashCode());
    TicTacToeState state2 = game.getResult(initialState2, new XYLocation(0, 0));
    Assert.assertEquals(state1.hashCode(), state2.hashCode());
}
Also used : XYLocation(aima.core.util.datastructure.XYLocation) TicTacToeState(aima.core.environment.tictactoe.TicTacToeState) Test(org.junit.Test)

Example 5 with TicTacToeState

use of aima.core.environment.tictactoe.TicTacToeState in project aima-java by aimacode.

the class TicTacToeApp method proposeMove.

/** Uses adversarial search for selecting the next action. */
private void proposeMove() {
    AdversarialSearch<TicTacToeState, XYLocation> search;
    XYLocation action;
    switch(strategyCombo.getSelectionModel().getSelectedIndex()) {
        case 0:
            search = MinimaxSearch.createFor(game);
            break;
        case 1:
            search = AlphaBetaSearch.createFor(game);
            break;
        case 2:
            search = IterativeDeepeningAlphaBetaSearch.createFor(game, 0.0, 1.0, 1000);
            break;
        default:
            search = IterativeDeepeningAlphaBetaSearch.createFor(game, 0.0, 1.0, 1000);
            ((IterativeDeepeningAlphaBetaSearch<?, ?, ?>) search).setLogEnabled(true);
    }
    action = search.makeDecision(currState);
    searchMetrics = search.getMetrics();
    currState = game.getResult(currState, action);
    update();
}
Also used : XYLocation(aima.core.util.datastructure.XYLocation) IterativeDeepeningAlphaBetaSearch(aima.core.search.adversarial.IterativeDeepeningAlphaBetaSearch) TicTacToeState(aima.core.environment.tictactoe.TicTacToeState)

Aggregations

TicTacToeState (aima.core.environment.tictactoe.TicTacToeState)5 XYLocation (aima.core.util.datastructure.XYLocation)5 TicTacToeGame (aima.core.environment.tictactoe.TicTacToeGame)2 Test (org.junit.Test)2 IterativeDeepeningAlphaBetaSearch (aima.core.search.adversarial.IterativeDeepeningAlphaBetaSearch)1