Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tic tac toe AI Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RicardoB




PostPosted: Thu Sep 02, 2010 9:27 pm   Post subject: Tic tac toe AI Help

Hi,

I want to implement my current program to use AI so I can do human vs CPU.

First I will like to hear some ideas from where to start I know i Need the concepts of min max, also alpha beta pruning. I have to overlook 3 ways on the tree.
Ill post my code...


file:///H:/tictactoe.java

I dont have a clear idea how to do it. NO im not asking for someone to do it for me or code it for me I need ideas pseudo code .. from there on i will start.. thank you.
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Thu Sep 02, 2010 9:37 pm   Post subject: RE:Tic tac toe AI Help

I'm sorry that I cannot help you with your problem, but I just wanted to point out that your "attached" file is not actually attached. You can't just paste the local location of the file, you must upload it using the upload button when you create or edit a post.
TerranceN




PostPosted: Thu Sep 02, 2010 9:44 pm   Post subject: RE:Tic tac toe AI Help

Well what do you do when you play Tic Tac Toe? Go through it step by step and there's your ai.

If that isn't enough then work backwards from the winning move all the way to the starting move. What patterns do you see? How can your ai use those patterns to its advantage?

Also the path to a file on your computer will not allow us to get to it. Just use java tags and just paste your code like this
code:

[syntax="java"]
// your code here
[/syntax]

which will then look like this
Java:

// your code here


Hope that helps.
RicardoB




PostPosted: Thu Sep 02, 2010 11:15 pm   Post subject: Re: Tic tac toe AI Help

Im sorry i didnt look at that here it is.

Java:

import java.util.*;

/**
 *
 * @
 */

public class tictactoe {
 
  private static char[][] board = new char [3][3]; // char variable of a 2D array
  private static final char player1 = 'X'; // char variable to set the X's for the first player
  private static final char player2 = 'O'; // char variable to set the O's for the second player
  private static boolean playerTurn=true; // boolean variable to  check the turn of the player
 
  /**
   * @param args the command line arguments
   */

  public static void main(String[] args) {
   
    Scanner console = new Scanner(System.in);
   
    int tries=0; // counter of possible moves
    boolean inserted=false; // boolean to check if inserted
    boolean win=false; // boolean to check for winner
    boolean noMatch=false;
   
    System.out.println("The Classic Tic-Tac-Toe Game");
   
    System.out.println("Press Y if you want to play: ");
    char response = console.nextLine().charAt(0);
   
    while(response=='Y'|| response=='y')
    {
      response='n';
      System.out.println("Player 1, please enter your name: ");
      String p1 = console.nextLine();
      System.out.println("Player 2, please enter your name: ");
      String p2 = console.nextLine();
      iniBoard(); // builds the board
     
      while(tries<=9) // while to do the maximum possible moves for the game till it ends
      {
        playerTurn=true;
        System.out.print(p1 + " you are the X's. Please enter the position from 1 to 9: ");
        int p1Index=0;
         try // try to check the answer of player 1
          {
           
            p1Index = console.nextInt();
          }
          catch (InputMismatchException exp) // catch if the answer is not a valid move
          {
            console.nextLine();
            noMatch=true;
          }
        inserted=insertBoard(p1Index); // inserts the X to the position of the player1
       
        while(inserted==false || noMatch) // while the position of the user given it will check if is already occupied and ask for another position.
        {
          noMatch=false;
          System.out.println("Try another place");
          System.out.print(p1 + " you are the X's. Please enter the position from 1 to 9: ");
          try
          {
           
            p1Index = console.nextInt();
           
          }
         
          catch (InputMismatchException exp)
          {
            console.nextLine();
            noMatch=true;
          }
         
            inserted=insertBoard(p1Index); // inserts the x
            printBoard(); // prints the board
           
        }
        playerTurn=false;
        win=winner(); // cehcks for the winner
        tries++;
        if(tries==9 & win==false) // if to check if there are no more tries and there is no winner then is a draw game
        {
          System.out.println("Draw Game");
          break;
        }
        if(win==true) // if to check if player 1 is a winner if there is a win method
        {
          System.out.println("You Won " +p1);
          break;
        }
       
        else // else ask for the next move of the player 2
        {
         
          System.out.print(p2 + " you are the O's. Please enter the position from 1 to 9: ");
          int p2Index=0;
         try
          {
           
            p2Index = console.nextInt();
          }
          catch (InputMismatchException exp)
          {
            console.nextLine();
            noMatch=true;
          }
          inserted=insertBoard(p2Index);
          printBoard();
         
           while(inserted==false || noMatch) // while the position of the user given it will check if is already occupied and ask for another position.
        {
          noMatch=false;
          System.out.println("Try another place");
          System.out.print(p2 + " you are the O's. Please enter the position from 1 to 9: ");
          try
          {
           
            p2Index = console.nextInt();
           
          }
         
          catch (InputMismatchException exp)
          {
            console.nextLine();
            noMatch=true;
          }
         
            inserted=insertBoard(p2Index);
            printBoard();
           
        }
          playerTurn=false;
         
          win=winner();
          tries++;
          if(tries==9 & win==false)
          {
            System.out.println("Draw Game");
            break;
          }
          if(win==true)
          {
            System.out.println("You Won "+p2);
            break;
          }
        }     
      }
    }
    // end of main
  }
  // iniBoard to initialize the board with digits from 1-9
  public static void iniBoard()
  {
    int i=1;
    for(int x=0; x<board.length; x++)
    {
      for(int y=0; y<board[x].length; y++)
      {
        char c=Character.forDigit(i,10);
        board[x][y]=c;
        i++;
        System.out.print(" " +(board[x][y])+ " ");
      }
      System.out.println();
    }
  }
  // method printBoard to print the board game
  public static void printBoard()
  {
    int i=1;
    for(int x=0; x<board.length; x++)
    {
      for(int y=0; y<board[x].length; y++)
      {
        System.out.print(" " +(board[x][y])+ " ");
      }
      System.out.println();
    }
  }
  // boolean method insertBoard with parameter pIndex integer to know where to insert the X's and the O's
  public static boolean insertBoard(int pIndex)
  {
    int row=0;
    int col=0;
    // if to check when the user inputs a position less then zero orgreater than 10 and output is not a valid move
    if(pIndex<=0 || pIndex>=10)
    {
      System.out.println("This is not valid move");
      return false;
    }
    // switch to represent each case for the possible moves from 1-9 if none of this case appply then is not a valid move
    switch(pIndex)
    {
      case 1: row=0;
      col=0;
      break;
      case 2: row=0;
      col=1;
      break;
      case 3: row=0;
      col=2;
      break;
      case 4: row=1;
      col=0;
      break;
      case 5: row=1;
      col=1;
      break;
      case 6: row=1;
      col=2;
      break;
      case 7: row=2;
      col=0;
      break;
      case 8: row=2;
      col=1;
      break;
      case 9: row=2;
      col=2;
      break;
     
      default: System.out.println("This is not valid move");
     
    }
    // if to place in the correct position the X's
    if(playerTurn && (board[row][col]!=player1 && board[row][col]!=player2))
    {
      board[row][col]=player1;
      return true;
    }
    // if to place in the correct position the O's
    if(playerTurn==false && (board[row][col]!=player1 && board[row][col]!=player2))
    {
      board[row][col]=player2;
      return true;
    }
    else
      return false;
   
  }
   // boolean method to check for the possible ways of win the game
  public static boolean winner()
  {
    for(int i=0; i<board.length; i++)
    {
      // Horizontal way to win a game
      if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
      {
        return true;   
      }
      //Vertical way to win a game
      if (board[0][i] == board[1][i] && board[0][i] == board[2][i])
      {
        return true;
      }
    }
    //Diagonal  way to win a game
    if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
    {
      return true;
    } 
    if (board[0][2] == board[1][1] && board[0][2] == board[2][0])
    {
      return true;   
    }
    return false;
  }
  // end of program
}

Nai




PostPosted: Fri Sep 03, 2010 5:46 pm   Post subject: Re: Tic tac toe AI Help

I had to do a similar project in grade 10 using Turing. I pretty much just created the AI by first covering the basics (if the cpu has 2 in a row then take the third, if the player has two then block him) and then I googled tic tac toe strategies and did trial and error for the rest of it.

If your interested in seeing my turing code you can find it here: http://compsci.ca/v3/viewtopic.php?p=188183#188183
RicardoB




PostPosted: Mon Sep 06, 2010 5:04 am   Post subject: RE:Tic tac toe AI Help

I have been working on it and some other things but ill try to check it since it has gui's and i dont need them but same concept ill post what i have hope someone can give me like pseudo code or ideas to do the min max or the pruning
Barbarrosa




PostPosted: Mon Sep 06, 2010 12:49 pm   Post subject: Re: Tic tac toe AI Help

I've written Tic Tac Toe 3x. All you really need to do for AI is cause it to place a valid move on the board while alternating turns with the player. You can implement these strategies pretty easily:
http://en.wikipedia.org/wiki/Tic-tac-toe#Strategy
You can disable some strategies to make the game easier.
RicardoB




PostPosted: Mon Sep 06, 2010 7:15 pm   Post subject: RE:Tic tac toe AI Help

Well I have made the progress to do it with little AI which is just makign the cpu generate random integers and being a valid move.

Now i want to do the Min Max... but im stuck....
Sponsor
Sponsor
Sponsor
sponsor
RicardoB




PostPosted: Mon Sep 06, 2010 10:06 pm   Post subject: Re: Tic tac toe AI Help

Barbarrosa @ Mon Sep 06, 2010 12:49 pm wrote:
I've written Tic Tac Toe 3x. All you really need to do for AI is cause it to place a valid move on the board while alternating turns with the player. You can implement these strategies pretty easily:
http://en.wikipedia.org/wiki/Tic-tac-toe#Strategy
You can disable some strategies to make the game easier.

I coming with an idea based on your response which i think it might be useful but did you considered before advising me that i do need to use the min max and pruning to complete the assignment. If you considered this then it might work if i can come up wit a good idea that which im thinking of ill let you know if this is what u mean.


Note.. for people who might i say i want them to do it for me thats not the case lol.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: