2012-01-15 106 views
1

我一直在嘗試在C中編寫一個井字棋遊戲,除了我得到了一些我不明白的錯誤。我知道這仍然需要一些工作,但現在我只想在添加之前運行該程序。有人能幫我嗎?這裏是我的代碼:井字棋遊戲中的錯誤

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

以下是編譯器給我的錯誤:

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編譯器忽略縮進。 (但是第一條警告信息應該是顯而易見的,即使對於新手也是如此。) – 2012-01-15 20:00:10

+0

你的大括號不均衡(並且有大量的大括號缺失)。 – Mat 2012-01-15 20:02:56

+0

只是一個提示 - 你不需要像使用「board」一樣明確初始化全局變量(儘管它不會受到傷害) - 全局變量和靜態變量如果沒有明確初始化,會自動清零。請注意,這不適用於像'move'或'user1','user2'這樣的本地非靜態變量 - 除非初始化它們,否則這些變量的初始值是未定義的。 – bdonlan 2012-01-15 20:02:58

回答

0

您試圖掃描一個整數,但scanf函數參數期望一個字符串(字符數組)。嘗試%d而不是%s。這就是十進制數字的格式字符串。

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

你必須使用「{」和「}」,因爲你有2行。

1
  1. 使用move,而不是在你的scanf聲明move[10],當你將它傳遞給函數。 move指的是數組,move[10]只是表示該數組中的第10個位置。
  2. 戴上牙套{}周圍的代碼在你的if/else塊,如果他們不是一個單一的代碼行以上(或最好始終,但是這是一個風格問題。)
0

如果你想把一個以上如果後面的指令,你應該在{}關閉它。 否則,編譯器認爲只有第一個是有條件的,其餘的都應該完成。

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,並且可能會給您「訪問衝突」錯誤,因爲我和j可能不在界限。

此外,你的邏輯是錯誤的,因爲顯然你不佔據一個位置就贏不了。

1

我發現了一些錯誤...

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

你想在這裏做什麼?如果你想讀一個字符串,使用

scanf("%s", move); 

如果你想讀的數組的第10位只有一個字符,請使用您的陣列被宣佈爲移動

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

注[10 ],所以它的位置從移動[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之前聲明de函數。

你不必在那裏實現它,只需聲明。例如:

void all_locations_filled(); 

int main (void) 
{ 
... 
} 

在過去的功能:

bool won_the_game(userU) 

你必須定義 「userU」 的類型。

你也忘了在main的結尾處關閉大括號「}」。