Bulls and Cows, my extra features

I tried to go ahead and implement some hints from the game,
the idea was to give the player the change on each game to get one hint which is the first and last letter.
but to get the hint you’ll get your lives halved, so I needed to figure out what to do when the lives were an odd number, and what to do when the player asks for the hint twice during gameplay, so I ended up with this code:
In the header:

	bool bHintGave;

In the cpp file, inside the declaration of SetupGame():

    bHintGave = false;
    PrintLine(TEXT("You can ask for a hint once by typing \n\"gethint\"\nBut your lives will be halved!"));

and then inside the ProcessGuess ():

        // Case where the player asks for a hint

        if(Guess == "gethint" && bHintGave == true)
        {
            PrintLine(TEXT("The Hidden Word starts with \"%c\" and ends\nwith \"%c\""), HiddenWord[0], HiddenWord[HiddenWord.Len()-1]);
            PrintLine(TEXT("Since you've already asked for your hint\nYou will not loose any lives"), Lives);
            PrintLine(TEXT("Press enter to continue..."));
            return;
        }

        if (Guess == "gethint")
        {
            PrintLine(TEXT("The Hidden Word starts with \"%c\" and ends\nwith \"%c\""), HiddenWord[0], HiddenWord[HiddenWord.Len()-1]);
                if(Lives%2 != 0 && Lives >= 1)
                {
                    Lives /= 2;
                    Lives++;
                    PrintLine(TEXT("You had odd number of lives \nso you have been blessed with the gift of Rounding Up!"));
                }
                else
                {
                    Lives /= 2;
                }
            PrintLine(TEXT("You now have %i lives left"), Lives);
            bHintGave = true;
            // Case where the player has no lives left so it loses the game
            if (Lives <= 0)
                {
                    EndGame();
                }
            return;
        }

So far it is working on my end, perhaps there would be a more elegant way to do it where I don’t use twice the if statement depending if they asked once or more times.
Also I think something can be done to avoid having to implement the endgame inside the hint if. I did it because I don’t know other way to tell the game that if they reach 0 lives because they ask for their hint when they had 1 life left they should loose the game.

2 Likes

I love your thought process! You are thinking like a programmer!

Privacy & Terms