Back to project page android-tic-tac-toe.
The source code is released under:
MIT License
If you think the Android project android-tic-tac-toe listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.shaon.android.tictactoe.model; //from w w w . ja v a 2s .c o m import java.util.ArrayList; import java.util.List; import org.shaon.android.tictactoe.model.State.Player; public class AlphaBetaSearch implements SearchAlgorithm { /** * Player configuration object. */ private PlayerConfig playerConfig; /** * Contracts a AlphaBetaSearch object. */ public AlphaBetaSearch() { playerConfig = new PlayerConfig(); } /** * Set the {@link Player} instance for artificially intelligent player. * Delegates {@link PlayerConfig#setAiPlayer(Player)} * * @param aiPlayer Player instance to set. */ public void setAiPlayer(Player aiPlayer) { playerConfig.setAiPlayer(aiPlayer); } /** * Get player configuration object. * * @return Player configuration object */ public PlayerConfig getPlayerConfig() { return playerConfig; } /** * <pre>alphaBetaSearch * function ALPHA-BETA-SEARCH(state) returns an action * inputs: state, current state in game * * v <- MAX-VALUE (state, -INF, +INF) * * return the action in SUCCESSORS(state) with value v * </pre> * * @param state * @return */ public List<ActionState> alphaBetaSearch(State state){ if(state.isFullyEmpty()) { return state.successorList(playerConfig.getAiPlayer()); } int v = maxValue(state, Integer.MIN_VALUE, Integer.MAX_VALUE); List<ActionState> maxActionStates = new ArrayList<ActionState>(); for(ActionState as : state.successorList(playerConfig.getAiPlayer())) { if(as.getState().getSateValue() == v){ maxActionStates.add(as); } } return maxActionStates; } /** * <pre> * * function MAX-VALUE (state, a, b) returns a utility value * * input: * state, current state in game * a, the value of the best alternative for MAX along the path to state * b, the value of the best alternative for MIN along the path to state * * if TERMINAL-TEST(state) then return UTILITY(state) * * v <- -INF * for a, s in SUCCESSORS(state) do * v <- MAX(v, MIN-VALUE(s, a, b)) * if v >= b then return v * a <- MAX(a, v) * * return v * </pre> * * @param state * @return Maximum value among succor list of state. */ private int maxValue(State state, int a, int b) { TerminatingCondition tc = state.checkFinished(); if(tc != null) { return tc.utility(playerConfig.getAiPlayer()); } int v = Integer.MIN_VALUE; for(ActionState as : state.successorList(playerConfig.getAiPlayer())) { int minValue = minValue(as.getState(), a, b); as.getState().setSateValue(minValue); v = Math.max(v, minValue); if (v >= b) { return v; } a = Math.max(a, v); } return v; } /** * <pre> * * function MIN-VALUE (state, a, b) returns a utility value * * input: * state, current state in game * a, the value of the best alternative for MAX along the path to state * b, the value of the best alternative for MIN along the path to state * * if TERMINAL-TEST(state) then return UTILITY(state) * * v <- +INF * * for a, s in SUCCESSORS(state) do * v <- MIN(v, MAX-VALUE(s, a, b)) * * if v <= a then return v * b <- MIN(b, v) * * return v * </pre> * * @param state * @return Minimum value among succor list of state. */ private int minValue(State state, int a, int b){ TerminatingCondition tc = state.checkFinished(); if(tc != null) { return tc.utility(playerConfig.getAiPlayer()); } int v = Integer.MAX_VALUE; for(ActionState as : state.successorList(playerConfig.getHumanPlayer())) { int maxValue = maxValue(as.getState(), a, b); as.getState().setSateValue(maxValue); v = Math.min(v, maxValue); if( v <= a) { return v; } b = Math.min(b, v); } return v; } @Override public List<ActionState> search(State state) { return alphaBetaSearch(state); } }