We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › AI tic tac toe game (with code but doesn't work..)
Page Index Toggle Pages: 1
AI tic tac toe game (with code but doesn't work..) (Read 908 times)
AI tic tac toe game (with code but doesn't work..)
Apr 19th, 2010, 4:10pm
 
PFont title, regular;
int gameState = 0;

void setup () {
 size(630, 500);
 title = createFont ("Bradley Hand ITC", 60);
 regular = createFont ("Bradley Hand ITC", 25);
}

void draw (){ //main method
 background(128);
 //title for the game, it will remain throughout the game, therefore, instead of clear screen, a white rectangle
 //will be drawn, the top starting from y=130
 textFont (title);
 text ("The Unbeatable", 110, 60);
 text ("Tic-Tac-Toe", 125, 125);

 if (gameState == 0)  {
   //introduction method invocation
   intro ();
 }
 else if (gameState == 1)  {
   chooseShape();
 }
 else if (gameState == 2)  {
   gameLoop();
 }
 else if (gameState == 3){
  playAgain();
 }

} //end of main method

void gameLoop()  {
 gameState = 2;
 //declaring variables
 int games = 0, lost = 0;
 final char choice = chooseShape ();
 int[] [] play = new int [3] [3];

 //draws the board
 drawBoard (play, choice);

 //loop for the entire game
 //use another method to repeat game!
 do
 {
   //increment number of games counter
   games++;

   //for loop for 9 turns
   for (int i = 1 ; i <= 9 ; i++) {
     //if the turn number and game number are both odd, or both even, it is the player's turn
     if ((i % 2 == 1 && games % 2 == 1) || (i % 2 == 0 && games % 2 == 0))
       play = playerTurn (play, choice);
     //or else it is the computer's turn
     else
       play = compTurn (play, i);

     //draws the board at the end of each turn
     drawBoard (play, choice);

     //if the computer had won the game, increment number of games lost counter
     if (winGame (play)) {
       text ("You Lose!", 265, 460);
       delay (1000);
       lost++;
       break;
     }

     //or if it is a tie game
     else if (i == 9 && !winGame (play)) {
       text ("Tie Game!", 265, 460);
       delay (1000);
     }
   }

   //reset board to 0
   for (int row = 0 ; row < 3 ; row++)
     for (int col = 0 ; col < 3 ; col++)
       play [row] [col] = 0;
 }
 while (playAgain ()); //play again method
}

//intro method
void intro ()
{
 //clears bottom screen, starting from below the title
 fill (255);
 rect (0, 130, 640, 370);
 fill (0);

 //set font
 textFont (regular);

 //outputs the game instructions and controls
 text ("The objective of the game is to put three ", 100, 175);
 text ("of your signs in a row, column, or diagonal.", 100, 205);
 text ("Controls: ", 200, 250);
 text ("Up", 250, 280);
 text ("Down", 250, 310);
 text ("Left", 250, 340);
 text ("Right", 250, 370);
 text ("Enter", 250, 400);
 text ("'i'", 375, 280);
 text ("'k'", 375, 310);
 text ("'j'", 375, 340);
 text ("'l'", 375, 370);
 text ("'f'", 375, 400);

 //prompting input from user to continue
 text ("Press 'f' to continue", 200, 450);
 if(keyPressed)  {
   //exit only if the correct character is entered
   if (key == 'f')
   {
     //tells the user how to get back to this screen
     text ("Press 'h' to access the help screen", 175, 490);

     //clear screen, leaving the title and the help access prompt
     fill (255);
     rect (0, 130, 640, 340);
     gameState = 1;
   }
 }
} //end of intro method

Re: AI tic tac toe game part 2
Reply #1 - Apr 19th, 2010, 4:11pm
 

//compTurn method, computer AI for playing the game
int[] [] compTurn (int[] [] play, int turn)
{
 //check first if the computer, then the player can win by a single turn,
 //if so win, or prevent the player from winning
 int sum = 0;
 for (int i = 1 ; i >= -1 ; i = i - 2)
 {
   for (int row = 0 ; row < 3 ; row++)
   {
     sum = play [row] [0] + play [row] [1] + play [row] [2];
     if (sum == 2 * i)
     {
       for (int col = 0 ; col < 3 ; col++)
         if (play [row] [col] == 0)
           play [row] [col] = 1;

       return (play);
     }
   }

   for (int col = 0 ; col < 3 ; col++)
   {
     sum = play [0] [col] + play [1] [col] + play [2] [col];
     if (sum == 2 * i)
     {
       for (int row = 0 ; row < 3 ; row++)
         if (play [row] [col] == 0)
           play [row] [col] = 1;

       return (play);
     }
   }

   sum = play [0] [0] + play [1] [1] + play [2] [2];
   if (sum == 2 * i)
   {
     for (int coordinate = 0 ; coordinate < 3 ; coordinate++)
       if (play [coordinate] [coordinate] == 0)
         play [coordinate] [coordinate] = 1;
     return (play);
   }

   sum = play [2] [0] + play [1] [1] + play [0] [2];
   if (sum == 2 * i)
   {
     for (int coordinate = 0 ; coordinate < 3 ; coordinate++)
       if (play [coordinate] [2 - coordinate] == 0)
         play [coordinate] [2 - coordinate] = 1;
     return (play);
   }

 }

 //if neither the computer nor the player could win right away,
 //the computer goes through a series of if statements to determine how to move
 //note, odd numbered turns are only possible when the computer goes first,
 //and even numbered turns are for when the player goes first

 //if this is the first turn
 if (turn == 1)
   //random corner or centre
   switch ((int) (Math.random () * 1 + 2))
   {
   case 1:
     play [1] [1] = 1;
     break;
   case 2:
     switch ((int) (Math.random () * 4 + 1))
     {
     case 1:
       play [0] [0] = 1;
       break;
     case 2:
       play [0] [2] = 1;
       break;
     case 3:
       play [2] [2] = 1;
       break;
     case 4:
       play [2] [0] = 1;
       break;
     }
   }
 //if second turn
 else if (turn == 2)
   if (play [1] [1] == -1)
     switch ((int) (Math.random () * 4 + 1))
     {
     case 1:
       play [0] [0] = 1;
       break;
     case 2:
       play [0] [2] = 1;
       break;
     case 3:
       play [2] [2] = 1;
       break;
     case 4:
       play [2] [0] = 1;
       break;
     }
   else
     play [1] [1] = 1;
 //if third turn
 else if (turn == 3)
   if (play [1] [1] == 1)
   {
     if (play [1] [0] == -1 || play [0] [1] == -1)
       play [0] [0] = 1;
     else if (play [2] [1] == -1 || play [1] [2] == -1)
       play [2] [2] = 1;
     else
       while (true)
         switch ((int) (Math.random () * 4 + 1))
         {
         case 1:
           if (play [0] [0] == 0)
           {
             play [0] [0] = 1;
             return play;
           }
         case 2:
           if (play [0] [2] == 0)
           {
             play [0] [2] = 1;
             return play;
           }
         case 3:
           if (play [2] [2] == 0)
           {
             play [2] [2] = 1;
             return play;
           }
         case 4:
           if (play [2] [0] == 0)
           {
             play [2] [0] = 1;
             return play;
           }
         }
   }
   else if (play [1] [1] == -1)
   {
     if (play [0] [0] == 1)
       play [2] [2] = 1;
     else if (play [0] [2] == 1)
       play [2] [0] = 1;
     else if (play [2] [2] == 1)
       play [0] [0] = 1;
     else
       play [0] [2] = 1;
   }
   else if (play [1] [0] == 0 && play [0] [1] == 0 && play [1] [2] == 0 && play [2] [1] == 0)
   {
     while (true)
       switch ((int) (Math.random () * 4 + 1))
       {
       case 1:
         if (play [0] [0] == 0)
         {
           play [0] [0] = 1;
           return play;
         }
       case 2:
         if (play [0] [2] == 0)
         {
           play [0] [2] = 1;
           return play;
         }
       case 3:
         if (play [2] [2] == 0)
         {
           play [2] [2] = 1;
           return play;
         }
       case 4:
         if (play [2] [0] == 0)
         {
           play [2] [0] = 1;
           return play;
         }
       }
   }
   else if ((play [0] [0] == 1 && (play [0] [1] == -1 || play [1] [0] == -1)) || (play [0] [2] == 1 && (play [0] [1] == -1 || play [1] [2] == -1)) || (play [2] [2] == 1 && (play [2] [1] == -1 || play [1] [2] == -1)) || (play [2] [0] == 1 && (play [2] [1] == -1 || play [1] [0] == -1)))
   {
     if ((play [0] [0] == 1 && play [1] [0] == -1) || (play [2] [2] == 1 && play [2] [1] == -1))
       play [0] [2] = 1;
     else if ((play [0] [0] == 1 && play [0] [1] == -1) || (play [2] [2] == 1 && play [1] [2] == -1))
       play [2] [0] = 1;
     else if ((play [0] [2] == 1 && play [0] [1] == -1) || (play [2] [0] == 1 && play [1] [0] == -1))
       play [2] [2] = 1;
     else
       play [0] [0] = 1;
   }
   else
   {
     if ((play [0] [0] == 1 && play [2] [1] == -1) || (play [2] [2] == 1 && play [1] [0] == -1))
       play [2] [0] = 1;
     else if ((play [0] [0] == 1 && play [1] [2] == -1) || (play [2] [2] == 1 && play [0] [1] == -1))
       play [0] [2] = 1;
     else if ((play [0] [2] == 1 && play [1] [0] == -1) || (play [2] [0] == 1 && play [0] [1] == -1))
       play [0] [0] = 1;
     else
       play [2] [2] = 1;
   }

Re: AI tic tac toe game (with code but doesn't work..)
Reply #2 - Apr 19th, 2010, 4:12pm
 
//if fourth turn
 else if (turn == 4)
   if ((play [0] [0] == -1 && play [2] [2] == -1) || (play [0] [2] == -1 && play [2] [0] == -1))
   {
     play [1] [0] = 1;
   }
   else if (play [1] [1] == -1)
   {
     if (play [0] [0] == -1 && play [2] [2] == 1)
       play [0] [2] = 1;
     else if (play [0] [2] == -1 && play [2] [0] == 1)
       play [0] [0] = 1;
     else if (play [2] [2] == -1 && play [0] [0] == 1)
       play [0] [2] = 1;
     else
       play [0] [0] = 1;
   }
   else if (play [0] [0] == 0 && play [2] [0] == 0 && play [0] [2] == 0 && play [2] [2] == 0)
   {
     if (play [0] [1] == -1 && play [1] [0] == -1)
       play [0] [0] = 1;
     else if (play [1] [0] == -1 && play [2] [1] == -1)
       play [2] [0] = 1;
     else if (play [2] [1] == -1 && play [1] [2] == -1)
       play [2] [2] = 1;
     else
       play [0] [2] = 1;
   }
   else
   {
     if (play [0] [0] == -1)
       play [2] [2] = 1;
     else if (play [0] [2] == -1)
       play [2] [0] = 1;
     else if (play [2] [2] == -1)
       play [0] [0] = 1;
     else
       play [0] [2] = 1;
   }

 //if fifth turn
 else if (turn == 5)
   if (play [0] [0] == 0 && play [1] [0] == 0 && play [0] [1] == 0)
     play [0] [0] = 1;
   else if (play [0] [2] == 0 && play [1] [2] == 0 && play [0] [1] == 0)
     play [0] [2] = 1;
   else if (play [2] [2] == 0 && play [1] [2] == 0 && play [2] [1] == 0)
     play [2] [2] = 1;
   else
     play [2] [0] = 1;

 //if sixth turn
 else if (turn == 6)
   if (play [1] [1] == 1)
   {
     if (play [0] [0] == 0)
       play [0] [0] = 1;
     else if (play [0] [2] == 0)
       play [0] [2] = 1;
     else if (play [2] [2] == 0)
       play [2] [2] = 1;
     else
       play [2] [0] = 1;
   }
   else
   {
     if (play [0] [0] == 1)
       if (play [0] [1] == 0)
         play [0] [1] = 1;
       else
         play [1] [0] = 1;
     else if (play [0] [2] == 1)
       if (play [0] [1] == 0)
         play [0] [1] = 1;
       else
         play [1] [2] = 1;
     else if (play [2] [2] == 1)
       if (play [2] [1] == 0)
         play [2] [1] = 1;
       else
         play [1] [2] = 1;
     else
       if (play [0] [1] == 0)
         play [0] [1] = 1;
       else
         play [2] [1] = 1;

   }

 //if seventh turn
 else if (turn == 7)
   while (true)
     switch ((int) (Math.random () * 4 + 1))
     {
     case 1:
       if (play [1] [0] == 0)
       {
         play [1] [0] = 1;
         return play;
       }
     case 2:
       if (play [0] [1] == 0)
       {
         play [0] [1] = 1;
         return play;
       }
     case 3:
       if (play [1] [2] == 0)
       {
         play [1] [2] = 1;
         return play;
       }
     case 4:
       if (play [2] [1] == 0)
       {
         play [2] [1] = 1;
         return play;
       }
     }

 //if eighth turn
 else if (turn == 8)
   for (int row = 0 ; row < 3 ; row++)
   {
     for (int col = 0 ; col < 3 ; col++)
     {
       if (play [row] [col] == 0)
       {
         play [row] [col] = 1;
         return (play);
       }
     }
   }

 //if last turn
 else if (turn == 9)
   for (int row = 0 ; row < 3 ; row++)
     for (int col = 0 ; col < 3 ; col++)
       if (play [row] [col] == 0)
       {
         play [row] [col] = 1;
         return (play);
       }
 //returns updated playing board
 return (play);
} //end of compTurn method
Re: AI tic tac toe game (with code but doesn't work..)
Reply #3 - Apr 19th, 2010, 4:12pm
 
char chooseShape ()
{
 char choice = 'X'; //x

 //set colour and font
 fill(0);
 textFont (regular);

 //prompting input
 text ("Please choose your shape", 175, 200);
 drawBox (240, 250);
 drawShape (choice, 250, 260, true);
 drawShape (choice, 350, 260, false);

 //infinite loop that exits only when the user has chosen
   if(keyPressed)  {
     if (key == 'l' && choice == 'X') //x
     {
       fill(255);
       stroke(0);
       drawBox (340, 250);
       drawShape (choice, 250, 260, true);
       drawShape (choice, 350, 260, false);
       choice = 'X'; //o
       gameState = 2;
     }
     else if (key == 'j' && choice == 'O') //o
     {
       fill(255);
       stroke(0);
       drawBox (240, 250);
       drawShape (choice, 250, 260, false);
       drawShape (choice, 350, 260, true);
       choice = 'O'; //x
       gameState = 2;
     }
   }
   return choice;
} //end of chooseShape method


//drawBox method, draws the selection box
void drawBox (int x, int y)
{
 stroke(0);
 noFill();
 rect (x, y, 50, 50);
} //end of drawBox method
Re: AI tic tac toe game (with code but doesn't work..)
Reply #4 - Apr 19th, 2010, 4:13pm
 
//playAgain method,
boolean playAgain ()
{
 gameState = 3;
 //declares variables
 boolean cont = true;

 //clear screen
 fill(255);
 rect (0, 130, 640, 340);
 fill(0);

 //set font
 textFont (regular);

 //prompt input
 text ("Would you like to play again?", 175, 200);
 drawBox (240, 250);
 text ("Yes", 245, 285);
 text ("No", 350, 285);

 //infinite loop for input
 if(keyPressed)  {
   if (key == 'f')
     gameState = 2;
   else if (key == 'l' && cont)
   {
     fill(0);
     rect (0, 250, 640, 100);
     drawBox (340, 250);
     text ("Yes", 245, 285);
     text ("No", 350, 285);
     cont = false;
   }
   else if (key == 'j' && !cont)
   {
     fill(255);
     rect (0, 250, 640, 100);
     drawBox (240, 250);
     text ("Yes", 245, 285);
     text ("No", 350, 285);
     cont = true;
   }
   else if (key == 'h')
   {
     intro ();
     stroke(0);
     text ("Would you like to play again?", 175, 200);
     drawBox (240, 250);
     text ("Yes", 245, 285);
     text ("No", 350, 285);
     cont = true;
   }
 }
 return cont;
} //end of playAgain method


//playerTurn method
int[] [] playerTurn (int[] [] play, char choice)
{
 //variable declaration
 int row = 0, col = 0;

 //draws the board
 drawBoard (play, choice);
 drawBox (215, 215);

 //infinite loop for player input
 if (keyPressed)  {
   if (key == 'f' && (play [row] [col]) == 0)
   {
     play [row] [col] = -1;
     return play;
   }
   else if (key == 'k' && row != 2)
   {
     row++;
   }
   else if (key == 'j' && col != 0)
   {
     col--;
   }
   else if (key == 'l' && col != 2)
   {
     col++;
   }
   else if (key == 'i' && row != 0)
   {
     row--;
   }
   else if (key == 'h')
     intro ();
 }
 drawBoard (play, choice);
 drawBox ((215 + 75 * col), (215 + 75 * row));
 return play;
}

//drawShape method
void drawShape (char choice, int x, int y, boolean player)
{
 //if it is not the player, draw the opposite symbol
 if (!player)
   if (choice == 'X') //x
     choice = 'X';
   else if (choice == 'O') //o
     choice = 'O';

 //set colour, draw symbol
 fill(0);
 if (choice == 'X')  { //x
   for (int i = -2 ; i < 3 ; i++)
   {
     line (x + i, y, x + i + 30, y + 30);
     line (x + i, y + 30, x + i + 30, y);
   }
 }
 else if (choice == 'O')  { //o
   stroke(0);
   noFill();
   ellipse (x + 15, y + 15, 40, 40);
 }
}

//drawBoard method, draws the game board
void drawBoard (int[] [] play, char choice)
{
 //clear screen
 fill (255);
 rect (0, 130, 640, 300);
 fill (0);

 //draws the lines
 for (int i = -2 ; i < 3 ; i++)
 {
   line (200, 275 + i, 425, 275 + i);
   line (200, 350 + i, 425, 350 + i);
   line (275 + i, 200, 275 + i, 425);
   line (350 + i, 200, 350 + i, 425);
 }

 //nested for loop to access each value in 2D array play
 for (int row = 0 ; row < 3 ; row++)
 {
   for (int col = 0 ; col < 3 ; col++)
   {
     if (play [row] [col] == 1)
       drawShape (choice, (225 + col * 75), (225 + row * 75), false);
     else if (play [row] [col] == -1)
       drawShape (choice, (225 + col * 75), (225 + row * 75), true);
   }
 }
} //end of drawBoard method
Re: AI tic tac toe game (with code but doesn't work..)
Reply #5 - Apr 19th, 2010, 4:14pm
 
//winGame method, checks if the computer has beaten the player yet
boolean winGame (int[] [] play)
{
 //declaring variables
 boolean win = false;
 int sum = 0;

 //horizontal win
 for (int i = 0 ; i < 3 ; i++)
 {
   sum = 0;
   for (int j = 0 ; j < 3 ; j++)
     sum += play [i] [j];

   if (sum == 3 && i == 0)
   {
     drawRedLine (200, 242, 1);
     win = true;
   }
   else if (sum == 3 && i == 1)
   {
     drawRedLine (200, 317, 1);
     win = true;
   }
   else if (sum == 3 && i == 2)
   {
     drawRedLine (200, 392, 1);
     win = true;
   }
 }

 //vertical win
 for (int i = 0 ; i < 3 ; i++)
 {
   sum = 0;
   for (int j = 0 ; j < 3 ; j++)
     sum += play [j] [i];

   if (sum == 3 && i == 0)
   {
     drawRedLine (242, 200, 2);
     win = true;
   }
   else if (sum == 3 && i == 1)
   {
     drawRedLine (317, 200, 2);
     win = true;
   }
   else if (sum == 3 && i == 2)
   {
     drawRedLine (392, 200, 2);
     win = true;
   }
 }

 //diagonal win, top left to bottom right
 sum = 0;
 sum = play [0] [0] + play [1] [1] + play [2] [2];
 if (sum == 3)
 {
   drawRedLine (0, 0, 3);
   win = true;
 }

 //diagonal win, top right to bottom left
 sum = 0;
 sum = play [0] [2] + play [1] [1] + play [2] [0];
 if (sum == 3)
 {
   drawRedLine (0, 0, 4);
   win = true;
 }

 return (win);
} //end of winGame method


//drawRedLine method, draws the winning red line
void drawRedLine (int x, int y, int direction)
{
 stroke (255, 0, 0);
 switch (direction)
 {
   //horizontal
 case 1:
   for (int i = -2 ; i < 3 ; i++)
   {
     line (x, y + i, x + 225, y + i);
   }
   break;

   //vertical
 case 2:
   for (int i = -2 ; i < 3 ; i++)
   {
     line (x + i, y, x + i, y + 225);
   }
   break;

   //diagonal, top left to bottom right
 case 3:
   for (int i = -2 ; i < 3 ; i++)
   {
     line (200 + i, 200, 425 + i, 425);
   }
   break;

   //diagonal, top right to bottom left
 case 4:
   for (int i = -2 ; i < 3 ; i++)
   {
     line (200 + i, 425, 425 + i, 200);
   }
   break;
 }
 fill (0);
} //end of drawRedLine method


Re: AI tic tac toe game (with code but doesn't work..)
Reply #6 - Apr 19th, 2010, 4:15pm
 
I really dun know where the problem is><..... i know it is a long one but please help me><!!
Re: AI tic tac toe game (with code but doesn't work..)
Reply #7 - Apr 19th, 2010, 10:36pm
 
Uh, for large code, you should use an external site, like Pastebin...
And we don't know either where the problem is, so a little description of the latter would help.
Page Index Toggle Pages: 1