[英]Simple C Tic Tac Toe Input Errors
我目前正在嘗試學習C,並決定制作一個簡單的Tic Tac Toe“游戲”。
我並不想做任何花哨的事情,我只想將用戶的位置和他們給定的角色放在我的網格中。 我還沒有開始使用數組或其他任何東西,所以我使用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;
}
替換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;
}
如果將scanf_s
更改為scanf
則此代碼在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);
不過,我現在無法驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.