Tic-Tac-Toe Judge
Completed
Read a 3×3 board of X, O and .. Print X wins, O wins, draw or in progress.
Input
Three lines.
Output
One line.
Example
Input
XXX O.O ...
Output
X wins
Editor
C
Hints
Check rows, columns and both diagonals for each player.
Solution explanation
A global board keeps the helper simple (no 2D array parameter needed).
Show solution
#include <stdio.h>
char b[3][4];
int wins(char p) {
for (int i = 0; i < 3; i++) {
if (b[i][0] == p && b[i][1] == p && b[i][2] == p) return 1;
if (b[0][i] == p && b[1][i] == p && b[2][i] == p) return 1;
}
if (b[0][0] == p && b[1][1] == p && b[2][2] == p) return 1;
if (b[0][2] == p && b[1][1] == p && b[2][0] == p) return 1;
return 0;
}
int main() {
int empty = 0;
for (int i = 0; i < 3; i++) {
scanf("%s", b[i]);
for (int j = 0; j < 3; j++) {
if (b[i][j] == '.') empty = 1;
}
}
if (wins('X')) printf("X wins\n");
else if (wins('O')) printf("O wins\n");
else if (empty) printf("in progress\n");
else printf("draw\n");
return 0;
}