I added extra functions and update to my "Bull & Cows"

I added a function which checks if the Guess of the player only contain English letters:

bool UBullCowCartridge::ContainsNonLetters(const FString& Guess)const{
    for(int32 i=0;i<Guess.Len();i++){
        if(!((Guess[i]>='a'&&Guess[i]<='z')||(Guess[i]>='A'&&Guess[i]<='Z'))){
            return true;
        }
    }
     return false;
}

then changed the ProcessGuess to the following:

void UBullCowCartridge::ProcessGuess(const FString& Guess){
        if(HiddenWord.Equals(Guess)){
            PrintLine(TEXT("Congrats, you entered the hidden word."));
            EndGame();
        }else{
            if(Lives>0){
                //Bulls & Cows
                //============
                if(HiddenWord.Len()!=Guess.Len()){
                    PrintLine(FString::Printf(TEXT("The hidden word is %i letter in length, try again!"),HiddenWord.Len()));
                }else//Check
                if(ContainsNonLetters(Guess)){
                    PrintLine(TEXT("Your guess must have only english letters!"));
                }else 
                if(!IsIsogram(Guess)){
                    PrintLine(TEXT("Your guess is not an Isogram, try again!"));
                }
                else{
                    --Lives;
                    if(Lives!=0){
                        PrintLine(TEXT("Wrong Guess, try again!"));
                        //Show Bulls & Cows
                        int32 Bull,Cow;
                        FBullCowCount Count=GetBullCows(Guess);
                        PrintLine(TEXT("You have %i Bulls and %i Cows"),Count.Bulls,Count.Cows);
                        PrintLine(TEXT("You have %i lives left."),Lives);
                    }
                        
                    else{
                        ClearScreen();
                        PrintLine(TEXT("No more lives left, you lost the game."));
                        PrintLine(TEXT("The Hidden word was: %s"),*HiddenWord);
                        EndGame();
                    }
                }
            
            }
        } 
}

also my IsIsogram function I tried a different direction:

bool UBullCowCartridge::IsIsogram(const FString& Guess)const{
    char ch=Guess[0];
    int32 arr[26]={};
    for(int32 i=0;i<Guess.Len();i++){
        if(Guess[i]>='a'&&Guess[i]<='z'){
            arr[Guess[i]-'a']++;
        }else if(Guess[i]>='A'&&Guess[i]<='Z'){
            arr[Guess[i]-'A']++;
        }else{
            return false;
        }
    }
    for(int32 i=0;i<26;i++){
        if(arr[i]>1)return false;
    }
    return true;
}
1 Like

Interesting little feature. How did you figure out how to make it?

Privacy & Terms