Link to home
Start Free TrialLog in
Avatar of ladyiou
ladyiou

asked on

shorter program code

heres some code that i have for a tic tac toe game that 3x3 using a 2d array and plays against the computer as well as against another opponent.  problem is its not finished and i am having trouble with it and it is too long.(in programming language C) need immediate help with this one please somebody.

would someone mind helping and like send me a shorter code that will do the same thing even print out the board and clear screen?


 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int getrand(); /* this returns a random number for me */
int checkwin(); /* when this runs it checks to see if there is a win */
int checkdraw(); /* this checks to make sure there isn't a draw */
int checkspace(); /* this just checks the space to see if it's occupied */
void clrboard(); /* this sets everyhting in the board array to blank spaces*/
void printboard(); /* prints the board to the screen */
void clrscr(); /* clears the screen */
int menu(); /* this shows the main menu */
void oneplayer(); /* this plays against the computer */
void twoplayer(); /* this plays against another player */
void compmove(); /* this is the computer AI .. it's crazy smart */


char board[3][3]; /* this array just holds the info for the board */

/*******************************************************************************
The main loop
*******************************************************************************/
int main(void)
{
menu();
return(0);
}

/*******************************************************************************
Gets a random number from min to max
*******************************************************************************/
int getrand(int min, int max)
{
static int init = 0;
int rc;

if (init == 0)
{
 srand(time(NULL));
 init = 1;
}

rc = (rand() % (max - min + 1) + min);

return(rc);
}

/*******************************************************************************
Checks for a win
*******************************************************************************/
int checkwin(void)
{
if ((board[0][2] == 'X') && (board[1][1] == 'X') && (board[2][0] == 'X') ||    
    (board[0][0] == 'X') && (board[1][1] == 'X') && (board[2][2] == 'X') ||    
    (board[0][0] == 'X') && (board[0][1] == 'X') && (board[0][2] == 'X') ||    
    (board[1][0] == 'X') && (board[1][1] == 'X') && (board[1][2] == 'X') ||    
    (board[2][0] == 'X') && (board[2][1] == 'X') && (board[2][2] == 'X') ||    
    (board[0][0] == 'X') && (board[1][0] == 'X') && (board[2][0] == 'X') ||    
    (board[0][1] == 'X') && (board[1][1] == 'X') && (board[2][1] == 'X') ||    
    (board[0][2] == 'X') && (board[1][2] == 'X') && (board[2][2] == 'X'))
{
 clrscr();
 printboard();
 printf("\nX wins!");  
 return(1);
}

else if ((board[0][2] == 'O') && (board[1][1] == 'O') && (board[2][0] == 'O') ||
         (board[0][0] == 'O') && (board[1][1] == 'O') && (board[2][2] == 'O') ||
         (board[0][0] == 'O') && (board[0][1] == 'O') && (board[0][2] == 'O') ||
         (board[1][0] == 'O') && (board[1][1] == 'O') && (board[1][2] == 'O') ||
         (board[2][0] == 'O') && (board[2][1] == 'O') && (board[2][2] == 'O') ||
         (board[0][0] == 'O') && (board[1][0] == 'O') && (board[2][0] == 'O') ||
         (board[0][1] == 'O') && (board[1][1] == 'O') && (board[2][1] == 'O') ||
         (board[0][2] == 'O') && (board[1][2] == 'O') && (board[2][2] == 'O'))
{
 clrscr();
 printboard();
 printf("\nO wins!");
 return(1);
}          

else
 return(0);
}

/*******************************************************************************
This checks to see if there is a draw
*******************************************************************************/
int checkdraw(void)
{
if ((board[0][0] != ' ') &&
    (board[1][0] != ' ') &&
    (board[2][0] != ' ') &&
    (board[0][1] != ' ') &&
    (board[1][1] != ' ') &&
    (board[2][1] != ' ') &&
    (board[0][2] != ' ') &&
    (board[1][2] != ' ') &&
    (board[2][2] != ' '))
{
clrscr();
printboard();
printf("DRAW GAME.");
return(1);
}

else
 return(0);
}

/*******************************************************************************
This checks to see if the space passed to it is occupied
*******************************************************************************/
int checkspace(int x,int y)
{
if ((board[x][y] == 'X') || (board[x][y] == 'O')) return(1);
else return(0);
}
/*******************************************************************************
This sets everything in the board array to blank spaces
*******************************************************************************/
void clrboard(void)
{
int x,y;
for (x=0;x<=2;x++)
{
 board[x][0] = ' ';
 for (y=0;y<=2;y++)
 {
  board[x][y] = ' ';
 }
}
}
/*******************************************************************************
This prints the playing board on the screen
*******************************************************************************/
void printboard(void)
{
printf("  a   b   c\n");
printf("1 %c | %c | %c\n", board[0][0], board[1][0], board[2][0]);
printf(" -----------\n");
printf("2 %c | %c | %c\n", board[0][1], board[1][1], board[2][1]);
printf(" -----------\n");
printf("3 %c | %c | %c\n", board[0][2], board[1][2], board[2][2]);
}

/*******************************************************************************
Clears the Screen
*******************************************************************************/
void clrscr(void)
{
int i;
for (i=1;i<=25;i++)
{
 printf("\n");
}
}

/*******************************************************************************
Prints the menu on the screen
*******************************************************************************/
int menu()
{
int choice; /* this just holds the answer for the menu */
char playagain = 'y'; /* y to play again, n to quit */
 
 do
 {  
 clrscr();

 printf("     T  I  C\n");
 printf("    -----------\n");
 printf("     T  A  C\n");
 printf("    -----------\n");
 printf("     T  O  E");
 printf("\n\n");
 printf("(1) Single Player\n");
 printf("(2) Two Player Game\n");
 printf("(0) Quit\n\n");
 printf("Choice : ");

 scanf("%d", &choice);
 switch (choice)
 {
 case 1   : oneplayer();
            printf("\nWould you like to play again (y\\n) : ");
            scanf(" %c", &playagain);
            break;
 case 2   : twoplayer();
            printf("\nWould you like to play again (y\\n) : ");
            scanf(" %c", &playagain);
            break;
 case 0   : printf("Thank you for playing TIC TAC TOE.\n");
            playagain = 'n';
            break;
 default  : printf("ERROR!\n");
            break;
 }
 
}
while (playagain == 'y');
}

/*******************************************************************************
This lets you play against the computer
*******************************************************************************/
void oneplayer(void)
{
int win = 0;
int draw = 0;
char move[2]; /* this just gives me two spaces for an answer */

clrboard();

do
{
clrscr();
printboard();
printf("What is your move?(ex: 3c): ");
getchar();
move[0] = getchar();
move[1] = getchar();
 
 switch (move[0])
  {
   case '1' : if ((move[0] == '1') && (move[1] == 'a'))
              {
               if ((checkspace(0,0) == 0))
               {
                board[0][0] = 'X';
                compmove();
               }
               break;
              }
           
              else if ((move[0] == '1') && (move[1] == 'b'))
              {
               if ((checkspace(1,0) == 0))
               {
                board[1][0] = 'X';
                compmove();
               }
               break;
              }
           
              else if ((move[0] == '1') && (move[1] == 'c'))
              {
               if ((checkspace(2,0) == 0))
               {
                board[2][0] = 'X';
                compmove();
               }
               break;
              }
   case '2' : if ((move[0] == '2') && (move[1] == 'a'))
              {
               if ((checkspace(0,1) == 0))
               {
                board[0][1] = 'X';
                compmove();
               }
               break;
              }
           
              else if ((move[0] == '2') && (move[1] == 'b'))
              {
               if ((checkspace(1,1) == 0))
               {
                board[1][1] = 'X';
                compmove();
               }
               break;
              }
           
              else if ((move[0] == '2') && (move[1] == 'c'))
              {
               if ((checkspace(2,1) == 0))
               {
                board[2][1] = 'X';
                compmove();
               }
               break;
              }
   case '3' : if ((move[0] == '3') && (move[1] == 'a'))
              {
               if ((checkspace(0,2) == 0))
               {
                board[0][2] = 'X';
                compmove();
               }
               break;
              }      
           
              else if ((move[0] == '3') && (move[1] == 'b'))
              {
               if ((checkspace(1,2) == 0))
               {
                board[1][2] = 'X';
                compmove();
               }
               break;
              }  
           
              else if ((move[0] == '3') && (move[1] == 'c'))
              {
               if ((checkspace(2,2) == 0))
               {
                board[2][2] = 'X';
                compmove();
               }
               break;
              }
  }
win = checkwin();
draw = checkdraw();
}
while ((win == 0) && (draw == 0));
}

/*******************************************************************************
This just lets two people play against each other
*******************************************************************************/
void twoplayer(void)
{
int player = 1;
int win = 0;
int draw = 0;
char move[2]; /* this just gives me two spaces for an answer */

clrboard();

do
{
clrscr();
printboard();
printf("What is your move?(ex: 2b): ");
getchar();
move[0] = getchar();
move[1] = getchar();

 switch (move[0])
 {
  case '1' : if ((move[0] == '1') && (move[1] == 'a'))
             {
              if ((checkspace(0,0) == 0) && (player == 1))
              {
               board[0][0] = 'X';
               player = 2;
              }
              else if ((checkspace(0,0) == 0) && (player == 2))
              {
               board[0][0] = 'O';
               player = 1;
              }
              break;
             }
             else if ((move[0] == '1') && (move[1] == 'b'))
             {
              if ((checkspace(1,0) == 0) && (player == 1))
              {
               board[1][0] = 'X';
               player = 2;
              }
              else if ((checkspace(1,0) == 0) && (player == 2))
              {
               board[1][0] = 'O';
               player = 1;
              }
              break;
             }
             else if ((move[0] == '1') && (move[1] == 'c'))
             {
              if ((checkspace(2,0) == 0) && (player == 1))
              {
               board[2][0] = 'X';
               player = 2;
              }
              else if ((checkspace(2,0) == 0) && (player == 2))
              {
               board[2][0] = 'O';
               player = 1;
              }
              break;
             }
  case '2' : if ((move[0] == '2') && (move[1] == 'a'))
             {
              if ((checkspace(0,1) == 0) && (player == 1))
              {
               board[0][1] = 'X';
               player = 2;
              }
              else if ((checkspace(0,1) == 0) && (player == 2))
              {
               board[0][1] = 'O';
               player = 1;
              }
              break;
             }
             else if ((move[0] == '2') && (move[1] == 'b'))
             {
              if ((checkspace(1,1) == 0) && (player == 1))
              {
               board[1][1] = 'X';
               player = 2;
              }
              else if ((checkspace(1,1) == 0) && (player == 2))
              {
               board[1][1] = 'O';
               player = 1;
              }
              break;
             }
             else if ((move[0] == '2') && (move[1] == 'c'))
             {
              if ((checkspace(2,1) == 0) && (player == 1))
              {
               board[2][1] = 'X';
               player = 2;
              }
              else if ((checkspace(2,1) == 0) && (player == 2))
              {
               board[2][1] = 'O';
               player = 1;
              }
              break;
             }
  case '3' : if ((move[0] == '3') && (move[1] == 'a'))
             {
              if ((checkspace(0,2) == 0) && (player == 1))
              {
               board[0][2] = 'X';
               player = 2;
              }
              else if ((checkspace(0,2) == 0) && (player == 2))
              {
               board[0][2] = 'O';
               player = 1;
              }
              break;
             }
             else if ((move[0] == '3') && (move[1] == 'b'))
             {
              if ((checkspace(1,2) == 0) && (player == 1))
              {
               board[1][2] = 'X';
               player = 2;
              }
              else if ((checkspace(1,2) == 0) && (player == 2))
              {
               board[1][2] = 'O';
               player = 1;
              }
              break;
             }
             else if ((move[0] == '3') && (move[1] == 'c'))
             {
              if ((checkspace(2,2) == 0) && (player == 1))
              {
               board[2][2] = 'X';
               player = 2;
              }
              else if ((checkspace(2,2) == 0) && (player == 2))
              {
               board[2][2] = 'O';
               player = 1;
              }
              break;
             }
 }
 
win = checkwin();
draw = checkdraw();
}
while ((win == 0) && (draw == 0)); /* do while there is no win or draw */
}

/*******************************************************************************
This is the computer AI .. niftay
*******************************************************************************/
void compmove(void)
{
int r; /* this is used for random numbers */
int done; /* this is = 1 when the computer has moved */

done = 0;

do
{
/*
this first bit of code checks to see if it can make a win.

for
#|#|#
-----
 | |
-----
 | |
*/
 if ((board[0][0] == 'O') && (board[1][0] == 'O') && (checkspace(2,0) == 0))
 {
  board[2][0] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][0] == 'O') && (board[2][0] == 'O') && (checkspace(1,0) == 0))
 {
  board[1][0] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][0] == 'O') && (board[2][0] == 'O') && (checkspace(0,0) == 0))
 {
  board[0][0] = 'O';
  done = 1;
  break;
 }
/*
for
#| |
-----
 |#|
-----
 | |#
*/
 else if ((board[0][0] == 'O') && (board[1][1] == 'O') && (checkspace(2,2) == 0))
 {
  board[2][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][0] == 'O') && (board[2][2] == 'O') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'O') && (board[2][2] == 'O') && (checkspace(0,0) == 0))
 {
  board[0][0] = 'O';
  done = 1;
  break;
 }
/*
for
#| |
-----
#| |
-----
#| |
*/
 else if ((board[0][0] == 'O') && (board[0][1] == 'O') && (checkspace(0,2) == 0))
 {
  board[0][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][0] == 'O') && (board[0][2] == 'O') && (checkspace(0,1) == 0))
 {
  board[0][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][1] == 'O') && (board[0][2] == 'O') && (checkspace(0,0) == 0))
 {
  board[0][0] = 'O';
  done = 1;
  break;
 }
/*
for
 |#|
-----
 |#|
-----
 |#|
*/
 else if ((board[1][0] == 'O') && (board[1][1] == 'O') && (checkspace(1,2) == 0))
 {
  board[1][2] = 'O';
  done = 1;
  break;
 }  
 else if ((board[1][0] == 'O') && (board[1][2] == 'O') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'O') && (board[1][2] == 'O') && (checkspace(1,0) == 0))
 {
  board[1][0] = 'O';
  done = 1;
  break;
 }
/*
for
 | |#
-----
 | |#
-----
 | |#
*/
 else if ((board[2][0] == 'O') && (board[2][1] == 'O') && (checkspace(2,2) == 0))
 {
  board[2][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[2][0] == 'O') && (board[2][2] == 'O') && (checkspace(2,1) == 0))
 {
  board[2][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[2][1] == 'O') && (board[2][2] == 'O') && (checkspace(2,0) == 0))
 {
  board[2][0] = 'O';
  done = 1;
  break;
 }
/*
for
 | |#
-----
 |#|
-----
#| |
*/
 else if ((board[2][0] == 'O') && (board[1][1] == 'O') && (checkspace(0,2) == 0))
 {
  board[0][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[2][0] == 'O') && (board[0][2] == 'O') && (checkspace(1,1) == 0))
 {
 board[1][1] = 'O';
 done = 1;
 break;
 }
 else if ((board[1][1] == 'O') && (board[0][2] == 'O') && (checkspace(2,0) == 0))
 {
  board[2][0] = 'O';
  done = 1;
  break;
 }
/*
for
 | |
-----
#|#|#
-----
 | |
*/
 else if ((board[0][1] == 'O') && (board[1][1] == 'O') && (checkspace(2,1) == 0))
 {
  board[2][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][1] == 'O') && (board[2][1] == 'O') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'O') && (board[2][1] == 'O') && (checkspace(0,1) == 0))
 {
  board[0][1] = 'O';
  done = 1;
  break;
 }
/*
for
 | |
-----
 | |
-----
#|#|#
*/
 else if ((board[0][2] == 'O') && (board[1][2] == 'O') && (checkspace(2,2) == 0))
 {
  board[2][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][2] == 'O') && (board[2][2] == 'O') && (checkspace(1,2) == 0))
 {
  board[1][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][2] == 'O') && (board[2][2] == 'O') && (checkspace(0,2) == 0))
 {
  board[0][2] = 'O';
  done = 1;
  break;
 }
/*
Now the computer has ran a million ifs to see if it can make a win, and if it
gets this far, it can't make a win so it's going to check and see if X needs to
be blocked, if so .. it will.  This is just a copy and past of the above code
but insted of checking for O's it will check for X's.

for
#|#|#
-----
 | |
-----
 | |
*/
 else if ((board[0][0] == 'X') && (board[1][0] == 'X') && (checkspace(2,0) == 0))
 {
  board[2][0] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][0] == 'X') && (board[2][0] == 'X') && (checkspace(1,0) == 0))
 {
  board[1][0] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][0] == 'X') && (board[2][0] == 'X') && (checkspace(0,0) == 0))
 {
  board[0][0] = 'O';
  done = 1;
  break;
 }
/*
for
#| |
-----
 |#|
-----
 | |#
*/
 else if ((board[0][0] == 'X') && (board[1][1] == 'X') && (checkspace(2,2) == 0))
 {
  board[2][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][0] == 'X') && (board[2][2] == 'X') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'X') && (board[2][2] == 'X') && (checkspace(0,0) == 0))
 {
  board[0][0] = 'O';
  done = 1;
  break;
 }
/*
for
#| |
-----
#| |
-----
#| |
*/
 else if ((board[0][0] == 'X') && (board[0][1] == 'X') && (checkspace(0,2) == 0))
 {
  board[0][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][0] == 'X') && (board[0][2] == 'X') && (checkspace(0,1) == 0))
 {
  board[0][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][1] == 'X') && (board[0][2] == 'X') && (checkspace(0,0) == 0))
 {
  board[0][0] = 'O';
  done = 1;
  break;
 }
/*
for
 |#|
-----
 |#|
-----
 |#|
*/
 else if ((board[1][0] == 'X') && (board[1][1] == 'X') && (checkspace(1,2) == 0))
 {
  board[1][2] = 'O';
  done = 1;
  break;
 }  
 else if ((board[1][0] == 'X') && (board[1][2] == 'X') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'X') && (board[1][2] == 'X') && (checkspace(1,0) == 0))
 {
  board[1][0] = 'O';
  done = 1;
  break;
 }
/*
for
 | |#
-----
 | |#
-----
 | |#
*/
 else if ((board[2][0] == 'X') && (board[2][1] == 'X') && (checkspace(2,2) == 0))
 {
  board[2][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[2][0] == 'X') && (board[2][2] == 'X') && (checkspace(2,1) == 0))
 {
  board[2][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[2][1] == 'X') && (board[2][2] == 'X') && (checkspace(2,0) == 0))
 {
  board[2][0] = 'O';
  done = 1;
  break;
 }
/*
for
 | |#
-----
 |#|
-----
#| |
*/
 else if ((board[2][0] == 'X') && (board[1][1] == 'X') && (checkspace(0,2) == 0))
 {
  board[0][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[2][0] == 'X') && (board[0][2] == 'X') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'X') && (board[0][2] == 'X') && (checkspace(2,0) == 0))
 {
  board[2][0] = 'O';
  done = 1;
  break;
 }
/*
for
 | |
-----
#|#|#
-----
 | |
*/
 else if ((board[0][1] == 'X') && (board[1][1] == 'X') && (checkspace(2,1) == 0))
 {
  board[2][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][1] == 'X') && (board[2][1] == 'X') && (checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][1] == 'X') && (board[2][1] == 'X') && (checkspace(0,1) == 0))
 {
  board[0][1] = 'O';
  done = 1;
  break;
 }
/*
for
 | |
-----
 | |
-----
#|#|#
*/
 else if ((board[0][2] == 'X') && (board[1][2] == 'X') && (checkspace(2,2) == 0))
 {
  board[2][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[0][2] == 'X') && (board[2][2] == 'X') && (checkspace(1,2) == 0))
 {
  board[1][2] = 'O';
  done = 1;
  break;
 }
 else if ((board[1][2] == 'X') && (board[2][2] == 'X') && (checkspace(0,2) == 0))
 {
  board[0][2] = 'O';
  done = 1;
  break;
 }
/*
Now if it can't win and X doesn't need to be blocked it will first check to see
if the center has been taken. If it hasn't it will take it.  It will then try
to block the three corner strategy.  
*/
else if ((checkspace(1,1) == 0))
 {
  board[1][1] = 'O';
  done = 1;    
  break;
 }

else if ((board[0][0] == 'X') &&
         (board[2][2] == 'X') ||
         (board[2][0] == 'X') &&
         (board[0][2] == 'X'))
 {                        
  r = getrand(1,4);
  switch (r)
  {
   case 1 : if ((checkspace(1,0) == 0))
            {
             board[1][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,1) == 0))
            {
             board[2][1] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(1,2) == 0))
            {
             board[1][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,1) == 0))
            {
             board[0][1] = 'O';
             done = 1;
             break;
            }
   case 2 : if ((checkspace(2,1) == 0))
            {
             board[2][1] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(1,2) == 0))
            {
             board[1][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,1) == 0))
            {
             board[0][1] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(1,0) == 0))
            {
             board[1][0] = 'O';
             done = 1;
             break;
            }
   case 3 : if ((checkspace(1,2) == 0))
            {
             board[1][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,1) == 0))
            {
             board[0][1] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(1,0) == 0))
            {
             board[1][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,1) == 0))
            {
             board[2][1] = 'O';
             done = 1;
             break;
            }
   case 4 : if ((checkspace(0,1) == 0))
            {
             board[0][1] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(1,0) == 0))
            {
             board[1][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,1) == 0))
            {
             board[2][1] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(1,2) == 0))
            {
             board[1][2] = 'O';
             done = 1;
             break;
            }  
  }
  break;
 }
 
/*
Now if it can't win, X doesn't need to be blocked, the center has been taken and
it doesn't need to block the three corner strategy it will check to see if it
needs to block the three side peice strategy.
*/
else if ((board[0][1] == 'X') && (board[1][2] == 'X') && (checkspace(0,2) == 0))
{
 board[0][2] = 'O';
 done = 1;
 break;
}
else if ((board[0][1] == 'X') && (board[1][0] == 'X') && (checkspace(0,0) == 0))
{
 board[0][0] = 'O';
 done = 1;
 break;
}
else if ((board[2][1] == 'X') && (board[1][0] == 'X') && (checkspace(2,0) == 0))
{
 board[2][0] = 'O';
 done = 1;
 break;
}
else if ((board[2][1] == 'X') && (board[1][2]) && (checkspace(2,2) == 0))
{
 board[2][2] = 'O';
 done = 1;
 break;
}

/*
Now if it can't win, X doesn't need to be blocked, the center has been taken and
it doesn't need to block the three corner strategy or the three side peice
strategy, it will pick a random corner.
*/  
else if ((checkspace(0,0) == 0) ||
         (checkspace(2,0) == 0) ||
         (checkspace(2,2) == 0) ||
         (checkspace(0,2) == 0))
{  
  r = getrand(1,4);
  switch (r)
  {
   case 1 : if ((checkspace(0,0) == 0))
            {
             board[0][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,0) == 0))
            {
             board[2][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,2) == 0))
            {
             board[2][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,2) == 0))
            {
             board[0][2] = 'O';
             done = 1;
             break;
            }
   case 2 : if ((checkspace(2,0) == 0))
            {
             board[2][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,2) == 0))
            {
             board[2][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,2) == 0))
            {
             board[0][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,0) == 0))
            {
             board[0][0] = 'O';
             done = 1;
             break;
            }
   case 3 : if ((checkspace(2,2) == 0))
            {
             board[2][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,2) == 0))
            {
             board[0][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,0) == 0))
            {
             board[0][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,0) == 0))
            {
             board[2][0] = 'O';
             done = 1;
             break;
            }
   case 4 : if ((checkspace(0,2) == 0))
            {
             board[0][2] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(0,0) == 0))
            {
             board[0][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,0) == 0))
            {
             board[2][0] = 'O';
             done = 1;
             break;
            }
            else if ((checkspace(2,2) == 0))
            {
             board[2][2] = 'O';
             done = 1;
             break;
            }
         
  }
  break;
}
/*
Now, if none of the corners are open, put an O in one of the side spots
*/
else if ((checkspace(1,0) == 0) ||
        (checkspace(2,1) == 0) ||
        (checkspace(1,2) == 0) ||
        (checkspace(0,1) == 0))
{  
  r = getrand(1,4);
  switch (r)
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ladyiou
ladyiou

ASKER

??? lost could you explain cause i dont see anything that helps me much in code writing and i that may be b/c i know very little about it. so if you dont mind could you please explain yourself and the website.
it has compact source code for playing tic tac toe ... ranging from 3X3 to 7X7 ....
you asked for short code for ttt, it has that ... or is that you are asking for code for submitting as your homework assignment ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How goes the war against code bloat? Still looking for methods to cut line counts?

-bcl
ttt is just "noughts and crosses" isn't it? if so:

are you trying to make an ai that wins or just places x's down until it wins by chance?

if you play ttt a few times with you friends you will pretty soon realise that whoever goes 1st can only win or draw, the 2nd player can only lose or draw. put this into a simple algorithm and that's gotta be easier.