Android Open Source - android-tic-tac-toe Player Config






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  v a  2s.c o  m
import java.util.Date;
import java.util.Random;

import org.shaon.android.tictactoe.model.State.Player;

/**
 * Player Configuration class. Helps keep trace of artificially intelligent
 * player and human player. By default {@link Player#X} is artificially intelligent
 * player and {@link Player#O} is human player. 
 * 
 * @author fahad
 */
public class PlayerConfig {
  /**
   * The {@link Player} instance of artificially intelligent player.
   * Defaults to {@link Player#X}  
   */
  private Player aiPlayer;
  
  /**
   * The {@link Player} instance of human player.
   * Defaults to {@link Player#O}  
   */
  private Player humanPlayer;
  
  /**
   * Constructor 
   * 
   * @param aiPlayer Player to set
   */
  public PlayerConfig(Player aiPlayer) {
    setAiPlayer(aiPlayer);
  }
  
  /**
   * Construct player configuration with default value. 
   */
  public PlayerConfig() {
    this(Player.X);
  }

  
  /**
   * Get the {@link Player} instance for human player.
   *  
   * @return Human player
   */
  public Player getHumanPlayer(){
    return humanPlayer;
  }
  
  /**
   * Get the {@link Player} instance for artificially intelligent player.
   *  
   * @return Artificially intelligent player
   */
  public Player getAiPlayer() {
    return aiPlayer;
  }
  
  /**
   * Set the {@link Player} instance for artificially intelligent player.
   * And human player is set to the opposite.
   * Defaults to {@link Player#X}. 
   * 
   * @param aiPlayer Player to set. 
   */
  public void setAiPlayer(Player aiPlayer) {
    this.aiPlayer = aiPlayer;
    this.humanPlayer = opposite(aiPlayer); 
  }
  
  /**
   * Get opposite player of provided player. 
   * 
   * @param player Player to find opposite
   * @return Opposite player
   */
  public static Player opposite(Player player) {
    return player.equals(Player.X) ? Player.O : Player.X;
  }
  
  /**
   * Select a player randomly.
   * 
   * @return Randomly selected player.
   */
  public static Player randomPlayer(){
    Player[] playerArray = new Player[]{Player.X, Player.O};
    
    Date date = new Date();
    Random random = new Random(date.getTime());
    
    return playerArray[random.nextInt(2)];
  }
}




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