Hi,
So I know in Python you can use inline if statements i.e. numTry = ‘try!’ if len(guesses) <= 1 else ‘tries!’
Is it possible to do the same in C++? I had a look and came up with a similar version using the ternary operator ?:
I am working on the BullCowGame and want to neaten up the ‘Lives’ section of the game. Currently, I depreciate the lives with every wrong answer saying ‘you have X lives left’. However, when it gets to 1 life left, I want to say the word ‘life’.
So, I tried this in my setup function to enable me to use it to format an FString. I did declare it in the headerfile as a FString member variable as well:
//*** In SetupGame() Function section of code to define variables***
HiddenWord = TEXT("Texts"); //Set the HiddenWord from List
Lives = HiddenWord.Len(); //Set Lives
NumLives = Lives == 1 ? TEXT("life") : TEXT("lives"); //change 'lives' to 'life' when lives = 1
Try = TEXT("\nTry again!");
bGameOver = false;
//*** In OnInput() Function section of code to see if the word length matches and to depreciate a life if not ***
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else //Checking PlayerGuess
{
if (Input == HiddenWord)
{
PrintLine(TEXT("You Win!"));
EndGame();
}
else
{
if (Input.Len() != HiddenWord.Len())
{
--Lives;
PrintLine(TEXT("Incorrect Guess. You have %i %s left.\n%s"), Lives, *NumLives, *Try);
//PrintLine (TEXT("%i"), Lives);
}
}
}
if (Lives == 1) // changes 'try again' text to nothing when Lives == 1
{
Try = TEXT("\n");
}
else if (Lives < 1) // checks if no lives and ends game
{
PrintLine(TEXT("You Have Lost..."));
EndGame();
}
It doesn’t want to work, not sure if this is the right way to do this? The ‘Try’ formatting works perfectly, but not the inline if statement
Cheers,
Nic