HELP Doing Tic Tac Toe

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Following is my code, it compiles properly but not giving me the out put as desires. This is the game that supposed to run between two computers and it not has to be intelligent just random moves okay take a look at my code and help me out Sorry
its not in c++ :(
<pre class="prettyprint #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//#include <stdbool.h>
/* A Global variable to use everywhere */
char xarray[3][3];

/* Funtion Prototypes */
int Number_Generator();
void Print_Board();
void initialize_Board();
void Move_X();
void Move_O();
char GetWinner();
//void Full_Board();
//char Checker_X();
//char Checker_O();



int main(){

int count=0; //temp variable for testing
char Player1[10],Player2[10],Value=NULL,User;
bool test = false;

srand(time(NULL));
initialize_Board();
do{

Move_X();
Print_Board();
Value = GetWinner();
if(Value == X){
printf("X Won !!n");
test = true;
break;
}
else{
printf("Press Enter to Continue:");
scanf("%c",&User);
}
if(User == n){

Move_O();
Print_Board();
}
Value = GetWinner();
if(Value == O){
printf("O Won !!n");
test = true;
break;
}
else{
printf("Press Enter to Continue:");
scanf("%c",&User);
}
}while( User == n && test == false);

return 1;
}


/* Function to Generate Locations on Board */
int Number_Generator(){

int rNumber;
rNumber=rand()%3;
return rNumber;
}

/* Function to Print Board */
void Print_Board(){
int i;
printf("n");
for(i=0;i<3;i++){
printf("%c | %c | %c n",xarray[1],xarray[2],xarray[3]);
if(i<2)
printf("--+---+--n");

}
printf("n");
}

/* Function to initialize an Empty Board */
void initialize_Board(){
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
xarray[j]=NULL;
}

/* Function to Get X on the board */
void Move_X(){
int x,y;
x=Number_Generator();
y=Number_Generator();
if (xarray[x][y] == ){
xarray[x][y]= X;
}
else{
Move_X();
}
}

/* Function to Get O on the board */
void Move_O(){
int x,y;
x=Number_Generator();
y=Number_Generator();
if (xarray[x][y]==){
xarray[x][y]= O;
}
else{
Move_O();
}
}

/* return winner (X or O, or if no winner) */
char GetWinner(){ // renamed...
/* Row/Column Checker */

for(int i=0;i<3;i++){

if((xarray[0] == xarray[1]) && (xarray[1] == xarray[2]) && xarray[0] != NULL){
return xarray[0];
}

if((xarray[0] == xarray[1]) && (xarray[1] == xarray[2]) && xarray[0] != NULL){
return xarray[0];
}
}

/* Diagonal Checkers */

if((xarray[0][0] == xarray[1][1]) && (xarray[1][1] == xarray[2][2]) && xarray[0][0] != NULL){
return xarray[0][0];
}

if((xarray[0][2] == xarray[1][1]) && (xarray[1][1] == xarray[2][0]) && xarray[0][2] != NULL){
return xarray[0][2];
}

//return ; // assumes is used for empty cells
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top