我正试图编写一个内存游戏,以使我的C编程技能更好,但我现在面临一个运行时错误,我真的不知道如何修复它。游戏规则如下:“Simple Simon是一款内存测试游戏。在这款游戏中,电脑会在屏幕上显示一系列数字,时间很短。然后你必须记住它们,当数字从屏幕上消失时,你必须输入相同的数字序列。当你连续成功3次时,这个过程会重复一个较长的数字序列,供你尝试。目标是尽可能长时间地继续这个过程。”游戏从3位数开始,一直平稳地运行到5位数。此时,在用户输入(guessedNumArr[])与要猜测的序列(numToGuess[])对应的块中,它会更改索引处的numToGuess[]数量。这一变化,我真的无法解释自己,使用户自动失去游戏。
这是代码:
#include <stdio.h>
#include "stdbool.h"
#include "time.h"
#include "stdlib.h"
#include "ctype.h"
int main() {
//declaring variables
bool play = true;
time_t now;
int try=1, anotherGame, difficulty=5, numToGuess[difficulty];
int count=0, guessedNum, guessedNumArr[difficulty], singleScore=0, totalScore=0;
//initializing the game
puts("\nSimple Simon is a memory-test game. In this game, the\n"
"computer displays a sequence of digits on the screen for a short period of time.\n"
"You then have to memorize them, and when the digits disappear from the screen,\n"
"you must enter exactly the same sequence of digits. "
"Each time you succeed 3 times in a row, the process repeats with a longer sequence of digits for you to try.\n"
"The objective is to continue the process for as long as possible\n"
"Press enter to play");
scanf("%c", &anotherGame);
do {
qui:;
const unsigned int DELAY = 2;
//Creating the array of nums to guess
srand(time(NULL));
for (int i = 0; i < difficulty; i++) {
int casualNum = rand()%10;
numToGuess[i] = casualNum;
printf("%d ", numToGuess[i]);
}
//hiding the nums to guess
now=clock();
for (; clock() - now < DELAY * CLOCKS_PER_SEC;);
puts("\n");
//scanning player guessed nums
for (int k = 0; k < difficulty; k++) {
if (k < 1) {
puts("Try to enter the same exact sequence of numbers, remember to put a space in between the numbers:\n");
}
scanf("%u", &guessedNum);
guessedNumArr[k] = guessedNum;
}
//counting how much nums player guessed correctly
for (int j = 0; j < difficulty; j++) {
/* this two prinf() show where the error is, as you can see the program will change the element of numToGuess[0] and i really don't know why */
printf("%d --- ", guessedNumArr[j]);
printf("%d\n", numToGuess[j]);
if (guessedNumArr[j] == numToGuess[j]) {
++count;
}
}
++try;
//scoring a point if player guessed all nums and detecting if player lost the game
if (count == difficulty) {
singleScore++;
totalScore++;
count = 0;
if(singleScore<3){
goto qui;
}
} else {
char Y_N;
printf("sorry, you lost. You reached a score of: %d.\nDo you want to play again? Y or N\n", totalScore);
bool i = true;
while (i == true) {
scanf("%s", &Y_N);
if (tolower(Y_N) == 'y') {
try = 0;
totalScore=0;
singleScore=0;
difficulty=3;
count = 0;
i = false;
goto jump;
} else if (tolower(Y_N) == 'n') {
printf("Game ended");
play = false;
i = false;
goto jump;
} else {
printf("say whaaat?, plz tell me 'Y' or 'N'");
}
}
}
//
singleScore = 0;
int gotItwrong=0;
here:;
char yOrN;
if(gotItwrong==0){puts("You guessed correctly 3 times in a row, enter a 'Y' to keep playing or enter a 'N' to stop the game\n");}
scanf("%s", &yOrN);
if (tolower(yOrN) == 'y') {
difficulty++;
count = 0;
} else if (tolower(yOrN) == 'n') {
printf("Game ended, your score was: %d", totalScore);
play = false;
} else {
printf("say what?, plz tell me 'Y' or 'N'");
++gotItwrong;
goto here;
}
jump:;
}while(play==true);
return 0;
}`
发布于 2022-11-01 14:36:57
我尝试了你的代码,并遇到了问题,只是让游戏开始。除了关于初始化数组大小和未初始化难度值的评论之外,我还做了一些调整,以使游戏能够像描述中提到的那样发挥作用。下面是您的程序的一个版本与这些调整。
#include <stdio.h>
#include "stdbool.h"
#include "time.h"
#include "stdlib.h"
#include "ctype.h"
#define MAX_DIFFICULTY 100
int main()
{
//declaring variables
bool play = true;
time_t now;
int try=1, difficulty=5, numToGuess[MAX_DIFFICULTY]; /* Use defined constant to set difficulty limit */
int count=0, guessedNumArr[MAX_DIFFICULTY], singleScore=0, totalScore=0;
char anotherGame[1], guessedNum[MAX_DIFFICULTY * 2 + 1];
char temp;
//Initializing the game
puts("\nSimple Simon is a memory-test game. In this game, the\n"
"computer displays a sequence of digits on the screen for a short period of time.\n"
"You then have to memorize them, and when the digits disappear from the screen,\n"
"you must enter exactly the same sequence of digits. "
"Each time you succeed 3 times in a row, the process repeats with a longer sequence of digits for you to try.\n"
"The objective is to continue the process for as long as possible\n"
"Press enter to play");
fgets(anotherGame, 1, stdin); /* Makes pressing enter work */
do
{
qui:
;
const unsigned int DELAY = 2;
//Creating the array of nums to guess
srand(time(NULL));
for (int i = 0; i < difficulty; i++)
{
int casualNum = rand()%10;
numToGuess[i] = casualNum;
printf("%d ", numToGuess[i]);
}
//Hiding the nums to guess
now=clock();
for (; clock() - now < DELAY * CLOCKS_PER_SEC;);
puts("\n");
printf("Try to enter the same exact sequence of numbers, remember to put a space in between the numbers:\n");
scanf("%c", &temp); /* Clears the buffer so that a guess can be entered */
scanf("%[^\n]",guessedNum); /* This allows the user to enter in all of the digits on the same line with a space as instructed */
//Scanning player guessed nums
for (int k = 0; k < difficulty * 2 - 1; k++)
{
if ((k %2) == 0) /* Places every other character into the guess array */
{
guessedNumArr[k / 2] = guessedNum[k] - '0'; /* Converts the character into an integer value */
}
}
//Counting how much nums player guessed correctly
for (int j = 0; j < difficulty; j++)
{
/* this two prinf() show where the error is, as you can see the program will change the element of numToGuess[0] and i really don't know why */
printf("%d --- ", guessedNumArr[j]);
printf("%d\n", numToGuess[j]);
if (guessedNumArr[j] == numToGuess[j])
{
++count;
}
}
++try;
//scoring a point if player guessed all nums and detecting if player lost the game
if (count == difficulty)
{
printf("Good - you guessed correctly\n");
singleScore++;
totalScore++;
count = 0;
if(singleScore<3)
{
goto qui;
}
}
else
{
char Y_N;
printf("Sorry, you lost. You reached a score of: %d.\nDo you want to play again? Y or N\n", totalScore);
bool i = true;
while (i == true)
{
scanf(" %c", &Y_N);
if (tolower(Y_N) == 'y')
{
try = 0;
totalScore=0;
singleScore=0;
difficulty=3;
count = 0;
i = false;
goto jump;
}
else if (tolower(Y_N) == 'n')
{
printf("Game ended\n");
play = false;
i = false;
goto jump;
}
else
{
printf("Say whaaat? Plz tell me 'Y' or 'N'\n");
}
}
}
//
singleScore = 0;
int gotItwrong=0;
here:
;
char yOrN;
if(gotItwrong==0)
{
puts("You guessed correctly 3 times in a row, enter a 'Y' to keep playing or enter a 'N' to stop the game\n");
}
scanf(" %c", &yOrN);
if (tolower(yOrN) == 'y')
{
difficulty++;
count = 0;
}
else if (tolower(yOrN) == 'n')
{
printf("Game ended, your score was: %d\n", totalScore);
play = false;
}
else
{
printf("Say what?, plz tell me 'Y' or 'N'\n");
++gotItwrong;
goto here;
}
if (difficulty >= MAX_DIFFICULTY)
{
printf("You've reached the upper limit - thank you for playing\n");
play = false;
}
jump:
;
}
while(play==true);
return 0;
}有些事要注意。
首先,
以下是游戏的样本测试。
@Dev:~/C_Programs/Console/GuessingGame/bin/Release$ ./GuessingGame
Simple Simon is a memory-test game. In this game, the
computer displays a sequence of digits on the screen for a short period of time.
You then have to memorize them, and when the digits disappear from the screen,
you must enter exactly the same sequence of digits. Each time you succeed 3 times in a row, the process repeats with a longer sequence of digits for you to try.
The objective is to continue the process for as long as possible
Press enter to play
7 2 3 4 3
Try to enter the same exact sequence of numbers, remember to put a space in between the numbers:
7 2 3 4 3
7 --- 7
2 --- 2
3 --- 3
4 --- 4
3 --- 3
Good - you guessed correctly
7 8 2 6 9 也许还有其他的调整可以做,以加强这个游戏,但给这个版本的游戏一个尝试,看看它是否符合您的项目精神。
发布于 2022-11-02 15:42:48
嘿@NoDakker,谢谢你花时间看我的代码。我试用了你的版本,但结果很糟糕。在架构级别上可能存在一些差异,或者不允许代码在不同的机器上工作。
这就是我通过使用C23编译器和调试器运行代码所得到的结果:
Simple Simon is a memory-test game. In this game, the
computer displays a sequence of digits on the screen for a short period of time.
You then have to memorize them, and when the digits disappear from the screen,
you must enter exactly the same sequence of digits. Each time you succeed 3 times in a row, the process repeats with a l
onger sequence of digits for you to try.
The objective is to continue the process for as long as possible
Press enter to play
5 5 2 7 4
Try to enter the same exact sequence of numbers, remember to put a space in between the numbers:
5 5 2 7 4
-16 --- 5
-16 --- 5
-16 --- 2
-16 --- 7
-48 --- 4
Sorry, you lost. You reached a score of: 0.
Do you want to play again? Y or N`注意,它开始时,我没有按enter键。
https://stackoverflow.com/questions/74275206
复制相似问题