2014-01-28 52 views
2

我目前正在嘗試學習C,並決定做一個簡單的井字遊戲「遊戲」。簡單的C井字遊戲輸入錯誤

我不想做任何奇特的事情,我只想把用戶的位置和他們給定的角色放在我的網格中。我還沒有開始使用數組或任何東西,所以我使用printf組合了一個網格。

問題出現在我試圖實際運行遊戲時,角色不起作用。我可以輸入正確的輸入,但它只是使該位置變爲空白。我如何使這個switch語句存儲我想要的字符?

#include <stdio.h> 
    #include <stdlib.h> 

    int main() 
    { 
    // Initialize and define all variables and characters 
    char c1='.', c2='.', c3='.', c4='.', c5='.', c6='.', c7='.', c8='.', c9='.'; // Board  characters 
    char given; // User-input character 
    int pos; // User-input position for the character 
    int turnCount; // Gives a limit on game length 

    for (turnCount = 0; turnCount < 9; turnCount++) 
    { 
     // Display lines 
     printf("%c %c %c 1 2 3\n", c1, c2, c3); // First line 
     printf("%c %c %c 4 5 6\n", c4, c5, c6); // Second line 
     printf("%c %c %c 7 8 9\n", c7, c8, c9); // Third line 

     // Asks for input, scans input for position and character to use 
     printf("Choose a position and character, without spaces.\n"); 
     scanf_s("%i%c", &pos, &given); 

     // Matches the input position with a character, defines the character 
     switch (pos) 
     { 
     case 1: c1 = given; break; 
     case 2: c2 = given; break; 
     case 3: c3 = given; break; 
     case 4: c4 = given; break; 
     case 5: c5 = given; break; 
     case 6: c6 = given; break; 
     case 7: c7 = given; break; 
     case 8: c8 = given; break; 
     case 9: c9 = given; break; 
     } // End switch 
    } // End for - game should be over 
    return 0; 
} 
+0

總是很好地檢查'scanf_s()'的結果。 – chux

回答

3

更換scanf_s("%i%c", &pos, &given);scanf("%i%c", &pos, &given);將解決你的問題。這是因爲scanf_s需要字符輸入的緩衝區大小,而scanf不需要。另一種方法是包含緩衝區大小:scanf_s("%i%c", &pos, &given,1);

我測試了兩個,他們在我的Windows系統上工作。

編輯: 只是爲了好玩,我使用數組實現了這一點。這種方式有點清潔。

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    // Initialize and define all variables and characters 
    char gameBoard[3][3]; 
    char given; // User-input character 
    int pos; // User-input position for the character 
    int turnCount; // Gives a limit on game length 
    int i; 
    //set the gameboard to all '.' 
    for(i=0;i<9;i++) 
    { 
     gameBoard[i/3][i%3] = '.'; 
    } 

    for (turnCount = 0; turnCount < 9; turnCount++) 
    { 
     // Display lines 
     printf("%c %c %c 1 2 3\n", gameBoard[0][0], gameBoard[0][1], gameBoard[0][2]); // First line 
     printf("%c %c %c 4 5 6\n", gameBoard[1][0], gameBoard[1][1], gameBoard[1][2]); // Second line 
     printf("%c %c %c 7 8 9\n", gameBoard[2][0], gameBoard[2][1], gameBoard[2][2]); // Third line 

     // Asks for input, scans input for position and character to use 
     printf("Choose a position and character, without spaces.\n"); 
     scanf_s("%i%c", &pos, &given,1); 

     // sets the character on the gameboard. 
     gameBoard[(pos-1)/3][(pos-1)%3] = given; 
    } // End for - game should be over 
    return 0; 
} 
+0

但爲什麼呢? – ooga

+1

因爲scanf_s需要爲字符聲明緩衝區大小。 Fyi - 我在Windows上測試過它,它工作正常。 – George

+0

我知道,但你應該在你的答案中提及! :) – ooga

3

此代碼似乎如果我改變scanf_sscanf在Linux上工作。我沒有Windows系統來測試它,但我認爲scanf_s函數調用並不合適。

據這裏:http://faculty.edcc.edu/paul.bladek/CS131/scanf_s.htm

Unlike scanf, scanf_s requires the buffer size to be specified for all input 
parameters of type c, C, s, S, or [. The buffer size is passed as an additional 
parameter immediately following the pointer to the buffer or variable 

因此,也許你應該嘗試這樣您的通話scanf_s

scanf_s("%i%c",&pos, &given, 1); 

我現在不能確認這一點,雖然...