fork download
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. static void graph(int *board) {
  5. puts("");
  6. for (int y = 0; y < 3; ++y) {
  7. for (int x = 0; x < 3; ++x) {
  8. printf("|%s",
  9. board[y * 3 + x] == 1 ? "o" : board[y * 3 + x] == 2 ? "x" : " ");
  10. }
  11. puts("|");
  12. }
  13. }
  14. static void input(int *board, int count) {
  15. int x, y;
  16. char buf[BUFSIZ];
  17. do {
  18. printf("x y: ");
  19. fgets(buf, BUFSIZ, stdin);
  20. if (sscanf(buf, " %d%d", &x, &y) != 2) continue;
  21. if (x < 1 || 4 <= x) continue;
  22. if (y < 1 || 4 <= y) continue;
  23. if (board[(y - 1) * 3 + (x - 1)]) continue;
  24. board[(y - 1) * 3 + (x - 1)] = count % 2 + 1;
  25. break;
  26. } while (true);
  27. }
  28. static bool check_board_full(int *board) {
  29. bool full = true;
  30. for (int i = 0; i < 3 * 3; ++i)
  31. full = (full && !!board[i]);
  32. return full;
  33. }
  34. static bool check_rows(int *board) {
  35. for (int i = 0; i < 3; ++i) {
  36. if (board[i * 3] && board[i * 3] == board[i * 3 + 1]
  37. && board[i * 3 + 1] == board[i * 3 + 2]) return true;
  38. }
  39. for (int i = 0; i < 3; ++i) {
  40. if (board[i] && board[i] == board[3 + i] && board[3 + i] == board[6 + i])
  41. return true;
  42. }
  43. if (board[0] && board[0] == board[3 + 1] && board[3 + 1] == board[6 + 2])
  44. return true;
  45. if (board[2] && board[2] == board[3 + 1] && board[3 + 1] == board[6])
  46. return true;
  47. return false;
  48. }
  49. static bool check(int *board) {
  50. if (check_rows(board)) return true;
  51. return check_board_full(board);
  52. }
  53. int main(void) {
  54. int board[3 * 3] = { 0 };
  55. int count = 0;
  56. for (graph(board); !check(board); input(board, count++), graph(board))
  57. ;
  58. if (check_rows(board)) {
  59. if (count % 2) puts("o win");
  60. else puts("x win");
  61. }
  62. puts("draw");
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 5444KB
stdin
1 1
2 2
3 3
1 2
3 2
3 1
1 3
2 3
2 1
stdout
| | | |
| | | |
| | | |
x y: 
|o| | |
| | | |
| | | |
x y: 
|o| | |
| |x| |
| | | |
x y: 
|o| | |
| |x| |
| | |o|
x y: 
|o| | |
|x|x| |
| | |o|
x y: 
|o| | |
|x|x|o|
| | |o|
x y: 
|o| |x|
|x|x|o|
| | |o|
x y: 
|o| |x|
|x|x|o|
|o| |o|
x y: 
|o| |x|
|x|x|o|
|o|x|o|
x y: 
|o|o|x|
|x|x|o|
|o|x|o|
draw