Android Open Source - android-tic-tac-toe Min Max






From Project

Back to project page android-tic-tac-toe.

License

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.

Java Source Code

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);
  }
}




Java Source Code List

org.shaon.android.tictactoe.TicTacToeApplication.java
org.shaon.android.tictactoe.activity.SettingsActivity.java
org.shaon.android.tictactoe.activity.TicTacToeActivity.java
org.shaon.android.tictactoe.board.Board.java
org.shaon.android.tictactoe.board.Cell.java
org.shaon.android.tictactoe.exception.InvalidTurn.java
org.shaon.android.tictactoe.model.ActionState.java
org.shaon.android.tictactoe.model.Action.java
org.shaon.android.tictactoe.model.AlphaBetaSearch.java
org.shaon.android.tictactoe.model.MinMax.java
org.shaon.android.tictactoe.model.PlayerConfig.java
org.shaon.android.tictactoe.model.SearchAlgorithm.java
org.shaon.android.tictactoe.model.State.java
org.shaon.android.tictactoe.model.TerminatingCondition.java
org.shaon.android.tictactoe.model.WinningCombination.java