저는 C 언어로 tic-tac-toe 게임을 코딩하려고했는데, 이해할 수없는 오류가 있습니다. 이 작업은 여전히 약간의 작업이 필요하다는 것을 알고 있지만 지금은 추가하기 전에 프로그램을 실행하고 싶습니다. 누군가 나를 도울 수 있습니까? 여기Tic-Tac-Toe 게임에서 오류가 발생했습니다.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int board[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
int main (void)
{
int const user1 = 1;
int const user2 = 2;
char move[10];
while (! all_locations_filled()) {
printf("User-1, please enter your move:");
scanf("%s", move[10]);
if(valid_location(move[10]))
mark_location(user1, move[10]);
display_board(board[3][3]);
else if(won_the_game(user1)
printf("Congratulations User-1, You Won the Game!");
break;
else
printf("Invalid Move");
printf("User-2, please enter your move:");
scanf("%s", move[10]);
if(valid_location(move[10]))
mark_location(user2, move[10]);
display_board();
else if(won_the_game(user2)
printf("Congratulations User-2, You Won the Game!");
break;
else
printf("Invalid Move");
return 0;
}
bool valid_location(char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0 || strcmp(str[10], "up") == 0 || strcmp(str[10], "upperRight") == 0 || strcmp(str[10], "left") == 0 || strcmp(str[10], "center") == 0 || strcmp(str[10], "right") == 0 || strcmp(str[10], "lowerLeft") == 0 || strcmp(str[10], "down") == 0 || strcmp(str[10], "lowerRight") == 0)
return true;
}
void mark_location(int userU, char str[10]) {
int strcmp(x, y);
if (strcmp(str[10], "upperLeft") == 0)
board[0][0] = userU;
else if (strcmp(str[10], "up") == 0)
board[0][1] = userU;
else if (strcmp(str[10], "upperRight") == 0)
board[0][2] = userU;
else if (strcmp(str[10], "left") == 0)
board[1][0] = userU;
else if (strcmp(str[10], "center") == 0)
board[1][1] = userU;
else if (strcmp(str[10], "right") == 0)
board[1][2] = userU;
else if (strcmp(str[10], "lowerLeft") == 0)
board[2][0] = userU;
else if (strcmp(str[10], "down") == 0)
board[2][1] = userU;
else if (strcmp(str[10], "lowerRight") == 0)
board [2][2] = userU;
}
char display_board(int array[][]) {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if (array[i][j] == 0)
print("-");
else if (array[i][j] == 1)
print("x");
else if (array[i][j] == 2)
print("o");
}
void all_locations_filled() {
int i, j;
for (i=0; i<3; ++i)
for (j=0; j<3; ++j)
if board[i][j] == 0
return false;
return true;
}
bool won_the_game(userU) {
int i, j;
if (board[0][j] == userU)
return true;
else if (board[1][j] == userU)
return true;
else if (board[2][j] == userU)
return true;
else if (board[i][0] == userU)
return true;
else if (board[i][1] == userU)
return true;
else if (board[i][2] == userU)
return true;
else
return false;
}
컴파일러는 나에게주는 오류가 있습니다 : 여기 내 코드는 당신은 정수를 스캔하려고하지만는 scanf 인수가 문자열 (char 배열을) 기대
tictactoe.c: In function ‘main’:
tictactoe.c:19: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
tictactoe.c:24: error: expected expression before ‘else’
tictactoe.c:115: error: expected declaration or statement at end of input
tictactoe.c:115: error: expected declaration or statement at end of input
힌트 : C 컴파일러는 들여 쓰기를 무시합니다. (하지만 첫 번째 경고 메시지는 랭크 초보자에게도 분명해야합니다.) –
중괄호는 균형이 맞지 않습니다 (누락 된 톤이 있습니다). – Mat
팁 - 'board'와 같이 전역 변수를 명시 적으로 0으로 초기화 할 필요는 없습니다. (상처를주지는 않지만) 전역 변수와 정적 변수는 명시 적으로 초기화하지 않으면 자동으로 0으로 설정됩니다. 이것은'move' 나'user1','user2'와 같은 로컬 비 정적 변수에는 적용되지 않습니다. 초기화하지 않으면 초기화되지 않은 값이 정의됩니다. – bdonlan