Hello! New problem here, same program. It used to be a different problem, but now it's that you can't die, or even get a button to become "#". Code is as follows (once I figure out what this looks like, I'll add line breaks so it doesn't stretch the forum so much):
Code:
int safestate [10]; // The safe; actually a 1d array of boolean values for whether this number was pressed yet, but for some reason or other
C doesn't have bools.
char safe [10]; // The char representation of the safe.
int num; // The number that you need to guess. I have no idea how to calculate the worst-case scenario.
int pressed; // The number that you pressed. Global because it's used by both main and both printcond and exprintcond. <- Technically a
legal English statement.
int printcond() // Reports whether the number you pressed is true. If it returns 1, it's not. If it's 2, you guessed right. If it's 3, you blew
up the safe.
{
if (pressed == num) // If the number you pressed is right...
{
return 2; // It doesn't need to do anything else because you won, and it doesn't print safe again until after it was reset.
}
if ((pressed >= 0) && (pressed <= 9) && (pressed != num)) // On the other hand, if it's wrong...
{
safestate [pressed]++; // First, it adds one to the amount of times you pressed the button.
safe [pressed] = '#'; // Then, it changes the number you pressed into a hash sign. Sort of
looks like molten lava, if you use your imagination.
if (safestate [pressed] >= 2) // However, if that changes the amount of times you pressed it to more than 1...
{
return 3; // You die. This should lift you out of both while loops in the main procedure, causing you to die.
}
num++; // If you didn't die, then num moves over one.
if (num == 10) // Rolls num over to 0 if this causes it to become 10.
{
num = 0;
}
return 1; // This sufficiently low value should cause you to still be trapped in both while loops.
}
return 0; // This should allow you to enter the second while loop without getting your safe reset.
}
int main()
{
char a; // Input for player.
setsafe(); // Sets the char representation of the safe to 0, 1, 2 ... 9. Also sets num to a random number.
setsafenum(); // Clears the bool(ish) table for how many times a number was pressed.
pressed = 11; // Sets pressed to something that won't react with printcond.
printf("<LOTS OF TEXT EXPLAINING RULES SNIPPED>");
scanf("%c", &a); // Gets the mode you want to play.
printf("\n\n"); // For good measure.
if (a == 'n') // There's another mode, but it's the exact same thing as this one except it uses exprintcond instead of printcond.
{
while (printcond() <= 2) // If you exit this, you die.
{
setsafe(); // These are for executing each
setsafenum(); // time you win; it "clears the
pressed = 11; // game table", so to speak.
while ((printcond() == 1) || (printcond() == 0)) // If you exit this, you either win or die depending on whether
printcond returns 2 or 3.
{
printf("Here's the safe:");
printsafe(); // Uses for loops to graphically display safe [0] to safe [9]. No bugs here.
printf("\n\nNow, what is the number you wish to pick?\n");
scanf("%d", &pressed); // Gets the number you want to put in.
if (pressed > 9) // This is to stay away from overflow errors. I only recently thought of this.
{
while (pressed > 9) // Enters you into a while loop until you press a number within the range.
{
printf("\nYou can't get ye flask.\n"); // This is for humor.
pressed = 11; // I forgot this last time. It wasn't pretty.
scanf("%d", &pressed);
}
}
if (printcond() == 1) // It's put in an if statement because if printcond returns 2 from the boot (impossible,
I know) you won't have to deal with this crap.
printf("\n\nSorry, you guessed incorrectly. Try again. ");
}
if (printcond() == 2)
{
printf("Hooray! You guessed it! Let's try again.\n\n"); // Should return you to the beginning of the first while
loop.
}
setsafe(); //
setsafenum(); // For good measure.
pressed = 11; // Actually, I added the top one when I forgot this was down here.
}
printf("Aww, man! The safe blew up. Since it killed you, you can't continue using this program. As such:\n\nreturn 0;"); // For
humour. I have only achieved this message once.
return 0; // Hopefully no bugs here.
}