[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

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)
0
ladyiou
Asked:
ladyiou
2 Solutions
 
sunnycoderCommented:
0
 
ladyiouAuthor Commented:
??? 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.
0
 
sunnycoderCommented:
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 ?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bcladdCommented:
(1) You should use loops. If you think about it, there are really only eight "rows" where a win can be found or blocked (three horizontal, three vertical, and two diagonal). So if you come up with some way to express each of those "rows" you could use one loop across the eight and divide your next move logic size by 8.

How to track a "row"...I am getting sick of writing "row" in quotes. I am going to call a sequence of three adjacent cells a "tic" and lose the quote marks. What is a tic? It is a starting position but it is also more than that because three tics start at board[0][0] (one horizontal, one vertical, and one diagonal). So a tic is represented by its starting row, its starting column, and a direction. We'll  express a direction as a change in row and a change in column, call them dRow and dCol. So the three tics at board[0][0] could be expressed as:

// starts at 0,0 and column goes up by 1 for each successive square: 0,0; 0,1; 0,2
{ row = 0, col = 0, dRow = 0, dCol = 1 }
// starts at 0, 0 and row goes up by 1 for each successive square: 0,0; 1,0; 2,0
{ row = 0, col = 0, dRow = 1, dCol = 0 }
// starts at 0, 0 and row AND column go up by 1 for each successive square: 0,0; 1,1; 2,2
{ row = 0, col = 0, dRow = 1, dCol = 1 }

The other 5 tics are defined similarly. Only odd one is the other diagonal:
// starts at 2, 0 and row goes down by one AND column goes up by 1 for each successive square: 2,0; 1,1; 0,2
{ row = 0, col = 0, dRow = -1, dCol = 1 }

The negative 1 is odd to some people.

You can define all the tics so that they start in their left-most, top-most position (top-most if there are multiple entries with the same left-most column).

How could this junk be of any use? Well, assume we have a struct called tic that has four fields as described above. We make an array of 8 tics, call it allTics. Then we can check for a win by seeing if all the elements along a given tic are the same:

char winnerCh = ' '; // set to space
for (int index = 0; index < 8; ++index) {
  int r = allTics[index].row;
  int c = allTics[index].col;
  int dR = allTics[index].dRow;
  int dC = allTics[index].dCol;
  if ((board[r][c] == board[r+dR][c+dC]) && (board[r][c] == board[r + 2 * dR][c + 2 * dC]) && (board[r][c] != ' ')) {
    winnerCh = board[r][c];
    break;
  }
}

Now I know that that is actually longer than your checkwin routine. It is an attempt to take something you know how to do and express it using the loops and tics. You can use the same thing when you are checking for a tic with a single open element (so your if statements get replaced with a for loop across allTics).

If this is too complex then just look at your code and make it the best that you can and ignore this advice. If you have questions about what I am talking about, feel free to ask.

-bcl

0
 
bcladdCommented:
How goes the war against code bloat? Still looking for methods to cut line counts?

-bcl
0
 
lewyCommented:
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now