I'm trying to code a memory game to get better with my C programming skills, but I'm now facing a run time error and I really have no clue how to fix it.
These are the game rules: "
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 the same sequence of digits. When 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 "
The game starts with a 3 digit sequence and it runs smoothly till it gets to a sequence of 5 digits. At this point, in the block that confront the user input (guessedNumArr[]
) with the sequence to guess (numToGuess[]
), it changes the number of numToGuess[]
at index [0]. This change that I really can not explain to myself makes the user automatically lose the game.
This is the code: `
#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();
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;
}
`
CodePudding user response:
I tried out your code and ran into issues just getting the game to go. Along with the comments about initializing array sizes and the difficulty value not being initialized, I made a few tweaks to get the game to function as noted in the description. Following is a version of your program with those tweaks.
#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();
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;
}
Some things to note.
- First off, a value was defined to provide for a maximum difficulty level for the game which is subsequently used to initialize and size variable arrays that will be used in the game.
- The use of "fgets" was added to make pressing the enter key work to start the game.
- When initially testing the game, entering the guess with a space between the digits did not work as required; one had to key in the digit and then press enter for each digit guessed. Changing the entry to a character array using "scanf" and then parsing the entered array allowed for the entry of data as instructed.
- As a guardrail, a test to see if the maximum difficulty level had been reached to gently end the game.
Following is a sample test of the game.
@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
There are probably other tweaks that can be done to enhance this game, but give this version of your game a try and see if it meets the spirit of your project.
CodePudding user response:
Hey @NoDakker thnx for taking the time and looking through my code. I tried out your version of the game but with bad results. There's probably some difference on an architectural level or something that don't allow the code to work on a different machine.
This is what i get by running your code, im using a C23 compiler and debugger btw:
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`
notice that it started without me pressing the enter key.