ProcessGuess Function Checking Order

I’m not really sure which order to put the checks in is best, but I think that the order is pretty important because if we put the check with a higher possibility of being true in the beginning, the run time will be faster, as we don’t have to go through any other checks. For example, if the chance of winning in a guessing game is 80%, we would first check for a win.

2 Likes

I think that the order shown in the tutorial video is ideal with how you desire the game flow to be, because it goes from the response that you ideally expect, in this case, a correct answer, to the one you least desire, an incorrect one. By placing the if statement for the correct input first you are telling the compiler to check for that first. If that doesn’t happen, it returns and goes through the code again until it meets the next if statement, which first checks for the correct number of letters before removing a life, which would reflect in the latter case an incorrect response.

I also think checking order has to do with optimization, here’s my current take on it:

{
    //if !IsIsogram
        //print out should be isogram
        //ask for new input     
        //RETURN
    
    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("Length should be %i."), HiddenWord.Len()); 
        PrintLine(TEXT("Try again:"));     
        return;
    }
    
    if (Guess == HiddenWord)
    {
        ClearScreen();
        PrintLine(TEXT("You win!"));      
        EndGame();
        return;
    }
        
    PrintLine(TEXT("%i lives left."), --Lives); //decrement lives and display to player
        
    if (Lives == 0)
    {
        ClearScreen();
        PrintLine(TEXT("You lost the game."));
        PrintLine(TEXT("The word was %s."), *HiddenWord);
        EndGame();      
        return;
    }              
    
    //show player the bulls and the cows
    PrintLine(TEXT("Try again!"));            

    return;                               
}

yeah, next lecture proved me wrong lol

Privacy & Terms