I'm almost finished with this but got stuck in trapping it when
1)there is a winner
2) a player attempts to enter a position that has been previously filled

#include<stdio.h>
#include<conio.h>

display();
game(int turn, int board[3][3]);
output(int board[3][3]);

main()
{int turn=1, board[3][3]={0};
 clrscr();


  display();
  while(turn <= 9)
  {game(turn, board);
   turn++;
   output(board);
   if(turn < 9)
   game(turn, board);
   output(board);
   turn++;
  }
 getch();
}

display()
{printf("Player 1: X\nPlayer 2: O");
 gotoxy(15,3); printf("1 2 3");
 gotoxy(15,4); printf("4 5 6");
 gotoxy(15,5); printf("7 8 9");
}

game(int turn,int board[3][3])
{int a=1,b,p=1,q=3, xo;
   do
     {if (turn % 2 == 1)
       printf("\nP1(X): ");
       if(turn % 2 == 0)
       printf("\nP2(O): ");
       scanf("%d", &xo);
     }while(xo<1 || xo>9 );

   for(b=0 ;b<3 ;b++)
      {for(;a<=q ;a++)
	  {if(a == xo)
	      {if(turn % 2 == 1)
	        board[b][a-p]=1;
	        if(turn % 2 == 0)
	        board[b][a-p]=2;
	      }
	  }
       q+=3;
       p+=3;
      }
}


output(int board[3][3])
{
int i, j, x, y=3;
  for(i=0;i<3;i++)
    {x=15;
     for(j=0;j<3;j++)
       {switch(board[i][j])
	  {case 0: gotoxy(x,y); printf("_");
	   x+=2;
	   break;
	   case 1: gotoxy(x,y); printf("X");
	   x+=2;
	   break;
	   case 2: gotoxy(x,y); printf("O");
	   x+=2;
	   break;
	  }
	}
     y++;
     }
}

our instructions were to use arrays and reuse arrays if possible..

help guys..
TIA

Basic error in your logic. You can't say for sure that the game is not over NOW, (after the opponent or yourself, has made a move), unless you check, first. I use a function named IsWin(), but the name is not important.

You must include logic to check if the game is over, and call it, after every move. Counting the number of turns that have been played is OK, but not enough.

Also, I don't see anything to test if a move that is entered, is legal or not. What if the player enters an illegal move? What if the human wants to cheat, you know, like politicians. :p IsLegal() should be called first after every move, then IsWin() is called to see if the last move was a winning move (for either player).

Also, I don't see anything to test if a move that is entered, is legal or not. (for either player).

Thanks for the reply.
Do you mean when the player enters an out of range number like ...0 or 10...?
it's already here.

do
     {if (turn % 2 == 1)
       printf("\nP1(X): ");
       if(turn % 2 == 0)
       printf("\nP2(O): ");
       scanf("%d", &xo);
     }while(xo<1 || xo>9 );

my only problem now is that when a player attempts to enter a position that has been previously filled.

my only problem now is that when a player attempts to enter a position that has been previously filled.

Bingo! :)

If the array index for the board is != EMPTY, then you can't move there. You can #define EMPTY to be any value you like, including just zero.

So:

1) test if the move that has been entered is legal. If not, reject it and ask for another move.

2) test if the move that you now accept as legal, has ended the game - either by # of moves made (there are no more places that can take a move), or by winning/losing the game.

You'll definitely want to make these functions separate. If you try and do them "straight up" in the game's while loop, you'll flounder from all the logic and lines of code that will be needed.

Also, your merry little program will look like crap from having all this stuff crammed into it, in one place. Not what you want.

Even a simple game like TTT, has a good bit of logic needed for a program to play it. So many things that we humans do "automatically", are completely beyond the computers ability without the most detailed logic being coded up for it.

Let's look at #1, is the move legal:

Pretty straight forward logic. If the square is empty for the requested move, then the move is legal. Normally a function return value of 0 indicates a "good" return, and anything else is "not good", but we want to turn that around in this case:

So our IsLegal() function will return 1 if it's a legal move, and 0, if it's not.

int IsLegal(int row, int col)   {  //add whatever parameters you need, of course
   int i, legal = 0;
   
   if(board[row][col] == 0)   {
      if(moveNum % 2 == 1)  // X players move 
         board[row][col] = 'X'; //or 1 or whatever you want for first players mark
      else
         board[row][col] = 'O';  //or 2 or whatever you want for the second players mark
      
      legal = 1;
   }
   return legal;
}

//And in our game loop we'd have something like:
if(IsLegal(moveRow, moveCol)
   GameOver();  //Is the game over, now?
...

etc.

If your board array isn't global, then you'll need to send the board array address to IsLegal() (and most of the other functions you'll make for this program, iirc). A copy of the board, won't do.

This article has been dead for over six months. Start a new discussion instead.