I wrote a tic tac toe game code but it doesnt work properly. I thing it’s logic is not right somewhere. I cracked my head on it. I am a beginner so please help.
#include<stdio.h>
#include<stdlib.h>
char arr[3][3]={'a','b','c','d','e','f','g','h','i'};
int tictactoe(char user1);
void user1loc(char user1);
void user2_loc(char user2);
int check_winner(char user1, char user2);
int main(){
printf("do you prefer x or 0(zero)\n");
char user1;
scanf("%c",&user1);
tictactoe(user1);
return 0;
}
int tictactoe(char user1){
char user2;
if(user1=='x'){
user2='0';
}
else{
user2='x';
}
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%c ",arr[i][j]);
}printf("\n");
}printf("\n");
int y;
for(int p=1;p<10;p++){
if(user1=='x'){
user1loc(user1);
user2_loc(user2);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%c ",arr[i][j]);
}printf("\n");
}printf("\n");
y=check_winner(user1,user2);
if(y==1){
printf("X wins");
return 0;
}
if(y==2){
printf("0 wins");
return 0;
}
}
else{
user2_loc(user2);
user1loc(user1);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%c ",arr[i][j]);
}printf("\n");
}printf("\n");
y=check_winner(user1,user2);
if(y==1){
printf("X wins");
return 0;
}
if(y==2){
printf("0 wins");
return 0;
}
}
}
return 0;
}
void user1loc(char user1){
char z;
printf("enter your location\n");
scanf("%c",&z);
if( (z=='a') & ((arr[1][1] !='x') | (arr[1][1]!='0')) ){
arr[1][1]=user1;
}
else if( (z=='b') & ((arr[1][2] !='x') | (arr[1][2]!='0')) ){
arr[1][2]=user1;
}
else if( (z=='c') & ((arr[1][3] !='x') | (arr[1][3]!='0')) ){
arr[1][3]=user1;
}
else if( (z=='d') & ((arr[2][1] !='x') | (arr[2][1]!='0')) ){
arr[2][1]=user1;
}
else if( (z=='e') & ((arr[2][2] !='x') | (arr[2][2]!='0')) ){
arr[2][2]=user1;
}
else if( (z=='f') & ((arr[2][3] !='x') | (arr[2][3]!='0')) ){
arr[2][3]=user1;
}
else if( (z=='g') & ((arr[3][1] !='x') | (arr[3][1]!='0')) ){
arr[3][1]=user1;
}
else if( (z=='h') & ((arr[3][2] !='x') | (arr[3][2]!='0')) ){
arr[3][2]=user1;
}
else if( (z=='i')& ((arr[3][3] !='x') | (arr[3][3]!='0')) ){
arr[3][3]=user1;
}
else{
printf("space already occupied\n");
user1loc(user1);
}
}
void user2_loc(char user2){
srand(1);
int y;
goto assignran;
assignran : {
y=rand()%9+1;
}
if(y==0){
goto assignran;
}
if((y==1) & ((arr[1][1]!='x') | (arr[1][1]!='0'))){
arr[1][1]=user2;
}
else if( (y==2) & ((arr[1][2]!='x') | (arr[1][2]!='0'))){
arr[1][2]=user2;
}
else if( (y==3) & ((arr[1][3]!='x') | (arr[1][3]!='0'))){
arr[1][3]=user2;
}
else if( (y==4) & ((arr[2][1]!='x') | (arr[2][1]!='0'))){
arr[2][1]=user2;
}
else if( (y==5) & ((arr[2][2]!='x') | (arr[2][2]!='0'))){
arr[2][2]=user2;
}
else if( (y==6) & ((arr[2][3]!='x') | (arr[2][3]!='0'))){
arr[2][3]=user2;
}
else if( (y==7) & ((arr[3][1]!='x') | (arr[3][1]!='0'))){
arr[3][1]=user2;
}
else if( (y==8) & ((arr[3][2]!='x') | (arr[3][2]!='0'))){
arr[3][2]=user2;
}
else if( (y==9) & ((arr[3][3]!='x') | (arr[3][3]!='0'))){
arr[3][3]=user2;
}
else{
}
}
int check_winner(char user1, char user2) {
int y=0;
if( arr[1][1] ==arr[1][2] ==arr[1][3] ){
if(arr[1][1] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[2][1] ==arr[2][2] ==arr[2][3] ){
if(arr[2][1] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[3][1] ==arr[3][2] ==arr[3][3] ){
if(arr[3][1] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[1][1] ==arr[2][1] ==arr[3][1] ){
if(arr[1][1] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[1][2] ==arr[2][2] ==arr[3][2] ){
if(arr[1][2] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[1][3] ==arr[2][3]==arr[3][3] ){
if(arr[1][3] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[1][1] ==arr[2][2]==arr[3][3] ){
if(arr[1][1] ==user1){
y=1;
return y;
}
else{
y=2;
return y;
}
}
else if( arr[1][3] ==arr[2][2] ==arr[1][3]){
if(arr[1][3] ==user1){
y=1;
return y;
}
else{
printf("%c wins\n",user2);
y=2;
return y;
}
}
else{
y=0;
return y;
}
}