Android Open Source - Tic-Tac-Toe A I Player






From Project

Back to project page Tic-Tac-Toe.

License

The source code is released under:

MIT License

If you think the Android project 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 com.comp1008.AmmanVedi.TicTacToe;
/* www  .  ja v  a  2 s  . c om*/
import java.util.Random;

public class AIPlayer extends Object {
  
  private int difficulty;
  private GameState gs;
  private Random easygen = new Random();
  // 1 = easy
  // 2 = medium 
  // 3 = hard

  
  public AIPlayer(int d)
  {
    //set the difficulty when you create the AI Player
    difficulty = d;
  }
  
  public void setDifficulty(int sd)
  {
    difficulty = sd;
  }
  
  //take the state
  //decide upon best move based on difficulty
  //return a tuple of the player num and the 
  //decision made by the function (sector)
  //use the tuple in GameState.updateState()
  
  public Tuple aiMove(GameState cState)
  {
    int decision = -1;
    gs = cState;
    
    if(difficulty == 3)
    {
      decision = hardLogic(cState.getRawState());
    }
    
    if(difficulty == 2)
    {
      decision = mediumLogic(cState.getRawState());
    }
    
    if(difficulty == 1)
    {
      decision = easyLogic(cState.getRawState());
    }
      
    //call AI player 2
    //user is player 1

    return new Tuple(2, decision);

  }
  
  private int hardLogic(int[][] board)
  {

    //logic (win > block > proceed > fork)
    
    
    //does the ai have a winning move?
    if(gs.hasConcecutiveWithWinner(2) != -1)
    {
      //if yes then take it
      return gs.hasConcecutiveWithWinner(2);
      
    }else
    {
      //if no then check if player 1 does
      if(gs.hasConcecutiveWithWinner(1) != -1)
      {
        //if yes then block by moving to their position
        return gs.hasConcecutiveWithWinner(1);
      }
    }
    
    return easyLogic(gs.getRawState());
    

  }
  
  private int mediumLogic(int[][] board)
  {

    //logic (win > block > proceed > fork)
    
    
    //does the ai have a winning move?
    if(gs.hasConcecutiveWithWinner(2) != -1)
    {
      //if yes then take it
      return gs.hasConcecutiveWithWinner(2);
      
    }else
    {
      return easyLogic(gs.getRawState());
    }

  }
  
  private int easyLogic(int[][] board)
  {
    
    int a;
    int b;
    
    do
    {
      a = easygen.nextInt(3);
      b = easygen.nextInt(3);
      System.out.println("heere : " + a + " " + b);
      
    }while(board[a][b] != 0);
    //logic
    
    return this.indexToSector(a, b);
  }
  
  private int indexToSector(int a, int b)
  {

    if(a == 0 && b == 0 )
    {
      return 0;
    }

    if(a == 0 && b == 1 )
    {
      return 1;
    }

    if(a == 0 && b == 2 )
    {
      return 2;
    }

    if(a == 1 && b == 0 )
    {
      return 3;
    }

    if(a == 1 && b == 1 )
    {
      return 4;
    }

    if(a == 1 && b == 2 )
    {
      return 5;
    }

    if(a == 2 && b == 0 )
    {
      return 6;
    }

    if(a == 2 && b == 1 )
    {
      return 7;
    }

    if(a == 2 && b == 2 )
    {
      return 8;
    }

    return -1;

  }
  

  
  

}




Java Source Code List

com.comp1008.AmmanVedi.TicTacToe.AIDrawView.java
com.comp1008.AmmanVedi.TicTacToe.AIPlayer.java
com.comp1008.AmmanVedi.TicTacToe.DifficultySelect.java
com.comp1008.AmmanVedi.TicTacToe.DrawView.java
com.comp1008.AmmanVedi.TicTacToe.GameState.java
com.comp1008.AmmanVedi.TicTacToe.HighScores.java
com.comp1008.AmmanVedi.TicTacToe.SplashScreen.java
com.comp1008.AmmanVedi.TicTacToe.TicTacToeActivity.java
com.comp1008.AmmanVedi.TicTacToe.Tuple.java