I am a bit beginner in c language and I am creating a program to play tic tac toe randomly. Unfortunately some of the functions are not working well.
Code:
#include <stdio.h>
int done1[8], done2[8], chance;
void print(){
int prnt[8];
int i, j;
for (i=0; i<9; i++)
prnt[i] = 0;
for (j=0; j<9; j++){
if (done1[j]){
if (!chance)
prnt[j] = 'X';
else
prnt[j] = 'O';
}
else
if (!prnt[j])
prnt[j] = '-';
}
for (j=0; j<9; j++){
if (done2[j]){
if (chance)
prnt[j] = 'X';
else
prnt[j] = 'O';
}
else
if (!prnt[j])
prnt[j] = '-';
}
printf("\n %c | %c | %c \n %c | %c | %c \n %c | %c | %c \n", prnt[0], prnt[1], prnt[2], prnt[3], prnt[4], prnt[5], prnt[6], prnt[7], prnt[8]);
}
void input_data(){
int input;
while(1){
input = getchar();
input -= 48;
if (input < 1 || input > 9)
continue;
input--;
if (!done1[input] && !done2[input]){
done1[input] = 1;
break;
}
}
chance = 1;
}
int main (){
int j;
for (j=0; j<9; j++){
done1[j]=0;
done2[j]=0;
}
input_data();
print();
}
and this
Code:
#include <stdio.h>
int done[1][8], chance;
void print(){
int prnt[8];
int i, j;
for (i=0; i<9; i++)
prnt[i] = 0;
for (i=0; i<2; i++)
for (j=0; j<9; j++){
if (done[i][j]){
if (i==chance)
prnt[j] = 'X';
else
prnt[j] = 'O';
}
else
if (!prnt[j])
prnt[j] = '-';
}
printf("\n %c | %c | %c \n %c | %c | %c \n %c | %c | %c \n", prnt[0], prnt[1], prnt[2], prnt[3], prnt[4], prnt[5], prnt[6], prnt[7], prnt[8]);
}
void input_data(){
int input;
while(1){
input = getchar();
input -= 48;
if (input < 1 || input > 9)
continue;
input--;
if (!done[0][input] && !done[1][input]){
done[0][input] = 1;
break;
}
}
chance = 1;
}
int main (){
int i, j;
for (i = 0; i<2; i++)
for (j=0; j<9; j++)
done[i][j]=0;
input_data();
print();
}
This both are unexpectedly are having done[0][8] = 1 and done[1][0] = 1 and done1[8] = 1 without any reason. I was excepted to have simply the mark entered by the user example if user types 5 the middle one should have mark. Kindly help with this.