So I have completed my TripleX Game but I don’t like the random numbers generator proposed during the course. Main reason is that when we fail a level the values change opening an opportunity to cheat the game by constantly failing in order to get easier questions. I’ve already changed the rng a bit by expanding the range of the numbers ( Adding difficulty level to difficulty level ) since I felt like its a bit too easy and I wondered if maybe i can solve this ‘cheating problem’ by creating a function that would act like an on/off switch for seeding the rand using real time, which makes the numbers actually random. And here lies the problem. I don’t know if such an operation of enabling and disabling srand is actually possible. I would be happy to hear any thoughts you have about my code, the presentation of it etc.
(Sorry bout the poor image quality. I was testing out the Polacode)
That’s incredibly narrow and hard to read.
What do you mean by this?
Like a way to stop the numbers from randomising if the player fails the level so the numbers in this particular level would stay the same.
You don’t need to mess with rand
. Just put a loop in the PlayGame function if the guess isn’t correct.
So I tried putting a while loop inside my PlayGame but I’m encountering an error “Control may reach end of non-void function” and I’m not really sure how to fix it. Could you explain why this error shows ?
So there’s actually two cases of undefined behaviour (UB) here. You have the following
int GuessA, GuessB, GuessC;
These variables are uninitialised and hold indeterminate values. Reading of interdeminate values is UB which is what you do on the following two lines.
non-void functions must return a value. In the while condition if CodeSum == GuessSum
and CodeProd == GuessProd
- which whilst unlikely, is still a non-zero chance since UB has already been invoked - then this function doesn’t reach any of the return statements so the function wouldn’t return a value and would also invoke UB.
If you fix the first issue by moving them after you get values for A,B,C then that would still cause a problem. Whilst we can logicially see that’s impossible it’s still required that all control paths can return a value. So you can fix this by having a return statement after the while loop.
You’re going to run into another issue but here are some (hopefully) helpful info.
- Return statements return from the function i.e. exit them. If they get it wrong are you going to get the result you’re after?
- Maybe you want something else for the condition like lives? Or an infinite loop might be useful?
- There’s a do-while loop which is the same as a while loop but instead it checks at the end of the loop instead of the start of it. The syntax is
do { //code } while(condition);
With the help of my father, who is a programmer, I fixed the while loop issue and we came up with the same idea of having the return statement after the while loop. Unfortunately this created another problem since I want to include lives into the game and having no return false statement makes it a bit hard. Also the fact that C++ does not have just an ‘or’ statement (as is one or the other must be correct or wrong) complicates things a lot for me. Here is what I’ve got so far. I feel like I’m making my life unnecessarily hard with some of this code. On my own I implemented the do while loop as you suggested earlier.
Update on what I’ve got so far. I’ve managed to implement working lives counter into my game but at the expense of allowing the numbers to randomise after failing a level. Still haven’t figured out how to make both of them work
Well you had a good go but this is one way to do it
bool PlayGame(int Difficulty)
{
PrintIntroduction(Difficulty);
// Generate Code
const int CodeA = rand() % Difficulty + Difficulty;
const int CodeB = rand() % Difficulty + Difficulty;
const int CodeC = rand() % Difficulty + Difficulty;
const int CodeSum = CodeA + CodeB + CodeC;
const int CodeProduct = CodeA * CodeB * CodeC;
// Print CodeSum and CodeProduct to the terminal
std::cout << "+ There are 3 numbers in the code";
std::cout << "\n+ The codes add-up to: " << CodeSum;
std::cout << "\n+ The codes multiply to give: " << CodeProduct << std::endl;
int Lives = 5;
do
{
// Store player guess
int GuessA, GuessB, GuessC;
std::cin >> GuessA >> GuessB >> GuessC;
const int GuessSum = GuessA + GuessB + GuessC;
const int GuessProduct = GuessA * GuessB * GuessC;
// Check if the players guess is correct
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << "\n*** Well done agent! You have extracted a file! Keep going! ***";
return true;
}
else
{
std::cout << "\n*** You entered the wrong code! Careful agent! Try again! ***";
std::cout << "\n*** You have " << --Lives << " remaining ***\n";
}
} while (Lives > 0);
return false;
}
If you want the lives to persist then you would need to pass that into the function.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.