틱택토 게임을 만들어보자
승리조건도 추가해야함
좌표를 입력하세요 x = 1~3 y = 1~3 1 1 ---|---|--- X | | ---|---|--- | | ---|---|--- | |
#include <stdio.h>
int main(void) {
int a, b, i;
int player_count = 0; // 플레이어 순서
char board[3][3]; // 틱택토 보드
int xwin = 0; // x승리 변수
int owin = 0; //o승리 변수
for (a = 0; a < 3; a++) { // 보드 초기화
for (b = 0; b < 3; b++) {
board[a][b] = ' ';
}
}
while (1) {
printf(" 좌표를 입력하세요 x = 1~3 y = 1~3 \n");
scanf_s("%d %d", &a, &b); // x , o 순서로
if (!(board[a - 1][b - 1] == 'X' || board[a - 1][b - 1] == 'O')) // 빈공간일때 입력
{
if (player_count % 2 == 0) {
board[a - 1][b - 1] = 'X'; // X 입력
}
else {
board[a - 1][b - 1] = 'O'; // O 입력
}
player_count++;
}
else {
printf("이미 선택된 자리입니다.\n"); // player_count를 늘리지 않기 때문에 다시 선택
}
for (i = 0; i < 3; i++) {
printf("---|---|---\n");
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
}
for (int i = 0; i < 3; i++) {
if (board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == 'X') { // 가로 승리
xwin = 1;
}
else if (board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X') { // 세로 승리
xwin = 1;
}
else if (board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') { // \ 대각선 승리
xwin = 1;
}
else if (board[2][0] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') { // / 대각선 승리
xwin = 1;
}
}
for (int i = 0; i < 3; i++) {
if (board[i][0] == 'O' && board[i][1] == 'O' && board[i][2] == 'O') { // 가로 승리
owin = 1;
}
else if (board[0][i] == 'O' && board[1][i] == 'O' && board[2][i] == 'O') { // 세로 승리
owin = 1;
}
else if (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') { // \ 대각선 승리
owin = 1;
}
else if (board[2][0] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') { // / 대각선 승리
owin = 1;
}
}
if (xwin == 1) {
printf(" X 플레이어 승리 ");
break;
}
else if (owin == 1) {
printf(" O 플레이어 승리 ");
break;
}
}
return 0;
}