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. j a va 2 s .c o m import java.util.ArrayList; import java.util.List; import org.shaon.android.tictactoe.model.State.Player; /** * Implementation of Min-Max Algorithm * * @author fahad */ public class MinMax implements SearchAlgorithm { /** * Player configuration object. */ private PlayerConfig playerConfig; /** * Contracts a MinMax object. */ public MinMax() { 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> * function MinMax decision (state) returns an action * inputs: state, current state in game * * v <- MAX-VALUE(state) * return the action in SUCCESSORS(state) with value v * </pre> * * @param state * @return */ public List<ActionState> minMaxDecision(State state){ if(state.isFullyEmpty()) { return state.successorList(playerConfig.getAiPlayer()); } int v = maxValue(state); 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) returns a utility value * * if TERMINAL-TEST (state) then returns UTILITY(state) * v <- -INF * for a,s in SUCCESSORS(state) do * v <- MAX (v, MIN-VALUE(s)) * * return v * </pre> * * @param state * @return Maximum value among succor list of state. */ private int maxValue(State state) { 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()); as.getState().setSateValue(minValue); v = Math.max(v, minValue); } return v; } /** * <pre> * function MIN-VALUE (state) returns a utility value * if TERMINAL-TEST (state) then returns UTILITY(state) * * v <- INF * for a,s in SUCCESSORS(state) do * v <- MIN (v, MAX-VALUE(s)) * * return v * </pre> * * @param state * @return Minimum value among succor list of state. */ private int minValue(State state){ 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()); as.getState().setSateValue(maxValue); v = Math.min(v, maxValue); } return v; } @Override public List<ActionState> search(State state) { return minMaxDecision(state); } }