#include <stdio.h>
int main(){
int j;
int k;
char pos[10] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
int i;
printf(" %c | %c | %c \n", pos[1], pos[2], pos[3]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", pos[4], pos[5], pos[6]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", pos[7], pos[8], pos[9]);
for(int i = 0; i < 9; i++){
scanf("%d",&k);
while (pos[k] != ' ' || k > 9 || k < 0){
printf("You gave the wrong input, try again:");
scanf("%d", &k);
}
if(i % 2 == 0){
pos[k] = 'X';
printf("Player X: %d\n", k);
}
else{
pos[k] = '0';
printf("Player 0:%d\n", k);
}
printf(" %c | %c | %c \n", pos[1], pos[2], pos[3]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", pos[4], pos[5], pos[6]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", pos[7], pos[8], pos[9]);
for (int j = 0; j < 2; j++){
if(pos[j*3 + 1] == pos[j*3 + 2] && pos[j*3 +2] == pos[j*3 + 3] && pos[j*3 + 1] != ' '){
printf("%c Wins!", pos[j*3 + 1]);
return 0;
}
}
for(int j = 1; j <= 3; j += 2){
if(pos[j] == pos[j + (5-i)] && pos[j] == pos[j + 2*(5-i)] && pos[j] != ' '){
printf("%c Wins!", pos [j]);
return 0;
}
}
}
printf("It's a draw!");
return 0;
}
For some reason it detects/prints when a player has won delayed