2012-01-15 4 views
1

저는 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 
+1

힌트 : C 컴파일러는 들여 쓰기를 무시합니다. (하지만 첫 번째 경고 메시지는 랭크 초보자에게도 분명해야합니다.) –

+0

중괄호는 균형이 맞지 않습니다 (누락 된 톤이 있습니다). – Mat

+0

팁 - 'board'와 같이 전역 변수를 명시 적으로 0으로 초기화 할 필요는 없습니다. (상처를주지는 않지만) 전역 변수와 정적 변수는 명시 적으로 초기화하지 않으면 자동으로 0으로 설정됩니다. 이것은'move' 나'user1','user2'와 같은 로컬 비 정적 변수에는 적용되지 않습니다. 초기화하지 않으면 초기화되지 않은 값이 정의됩니다. – bdonlan

답변

0

. %s 대신 %d을 시도하십시오. 10 진수의 형식 문자열입니다.

2
if(valid_location(move[10])) 
     mark_location(user1, move[10]); 
     display_board(board[3][3]); 

두 줄이 있으므로 "{"와 "}"를 사용해야합니다.

1
  1. 사용 move 대신하여 scanfmove[10], 그리고 때 함수에 전달하고 있습니다. move은 배열을 가리키며, move[10]은 해당 배열의 10 번째 위치를 의미합니다. 그들은 단 한 줄의 코드보다 더 있다면
  2. 넣고는 if/else 블록의 코드 주위에 {} 중괄호 (바람직하게는 항상 또는, 그러나 그것은 스타일의 문제입니다.)
0

하나를 다음 더 넣어하려는 경우 지시문 뒤에 if, {}으로 닫아야합니다. 그렇지 않으면 컴파일러는 첫 번째 조건 만 수행 중이라고 생각하고 나머지는 어쨌든 완료해야한다고 생각합니다.

0

scanf("%s", move);하지 scanf("%s", move[10]);

if(valid_location(move))하지 if(valid_location(move[10]))

mark_location(user1, move);하지 mark_location(user1, move[10]);

if (strcmp(str, "upperLeft") == 0)하지 if (strcmp(str[10], "upperLeft") == 0)

등 등

당신이 할 배열을 사용할 때마다 대괄호를 써서 C로 배열을 만든다. 기본적으로 두 가지 상황에서 대괄호를 사용합니다. 대괄호에 배열의 크기가 포함 된 배열을 선언하면 대괄호에 인덱스가 포함 된 배열 요소에 액세스합니다.

아마도이 말을 듣고 싶지는 않을 것이지만 코드에는 다른 점이 많습니다. 아마 책을 읽고 좀 더 간단하게 시작해야 할 것입니다.

0

컴파일러 오류를 처리 한 후에는 초기화되지 않은 변수 i와 j를 읽는 함수 won_the_game을보고 싶을 수 있으며 i와 j가있을 가능성이 있으므로 "액세스 위반"오류가 발생할 수 있습니다 범위.

또한 논리가 잘못되었습니다. 분명히 한 위치 만 차지하면 승리하지 못하기 때문입니다.

1

몇 가지 오류를 발견했습니다 ...

scanf("%s", move[10]); 

여기서 무엇을 하시겠습니까? 당신이 문자열을 읽으려면 당신이 배열의 10 번째 위치에있는 단 하나 개의 문자를 읽으려면,

scanf("%s", move); 

를 사용 [10 배열이 움직임으로 선언

scanf("%c", &move[9]); 

주 사용 ], 따라서 위치는 이동 [0]에서 이동 [9]로 이동합니다. 위치 이동 [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"); 

당신은 아마 의미 : 여기

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"); 

과 :

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; 
} 

당신은()는 "만약"에를 잊어 버렸습니다. 다음과 같아야합니다.

if (board[i][j] == 0) 

또한 함수를 호출하기 전에 함수를 선언해야합니다. 그래서 main 앞에 함수를 선언하십시오.

신고하기 만하면됩니다. 신고하기 만하면됩니다. 마지막 기능에서

void all_locations_filled(); 

int main (void) 
{ 
... 
} 

: 예를 들어

bool won_the_game(userU) 

당신은 "userU"의 유형을 정의해야합니다.

메인의 끝 부분에서 중괄호 "}"를 닫는 것을 잊었습니다.

관련 문제