#include <stdio.h>
char board[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
void printBoard() {
for (int i = 0; i < 3; i++) {
printf("---|---|---\n");
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
}
printf("---|---|---\n");
}
int checkWin(char player) {
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) return 1;
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) return 1;
}
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) return 1;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) return 1;
return 0;
}
int isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') return 0;
}
}
return 1;
}
int main() {
char currentUser = 'X';
int x, y;
while (1) {
printBoard();
if (checkWin('X')) {
printf("사용자(X)가 승리했습니다!\n");
break;
}
if (checkWin('O')) {
printf("컴퓨터(O)가 승리했습니다!\n");
break;
}
if (isBoardFull()) {
printf("비겼습니다!\n");
break;
}
if (currentUser == 'X') {
printf("(x, y) 좌표: ");
scanf("%d %d", &x, &y);
if (x < 0 || x > 2 || y < 0 || y > 2 || board[x][y] != ' ') {
printf("오류: 잘못된 위치이거나 이미 놓여진 자리입니다. 다시 입력하세요.\n");
continue;
}
board[x][y] = 'X';
currentUser = 'O';
} else {
printf("컴퓨터가 자동으로 다음 수를 결정합니다.\n");
int compX = -1, compY = -1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
compX = i;
compY = j;
break;
}
}
if (compX != -1) break;
}
if (compX != -1 && compY != -1) {
board[compX][compY] = 'O';
printf("(x, y) 좌표: %d %d\n", compX, compY);
}
currentUser = 'X';
}
}
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: