IsIsogram, playing without loops

Hello everyone :slight_smile:
I took a C course as a freshman at the university and I studied a few other languages by myself. I didn’t use my programming skills much in my job, so they’re quite rusty :grin: and this is one of the reasons I’m taking the course from the beginning. The other reason is that I want to get to know UE peculiarities at a slow, nice pace. That said I definitely know what a loop is but I asked myself what would have I done if I were a kid unaware of loops, just as if I only knew the tools already provided by the course. I ended up writing IsIsogram that way. Obviously, I did this because I enjoy playing around :upside_down_face: and because we’re using just a single HiddenWord which is ‘cake’ and it is conveniently short to play with it. Otherwise, I wouldn’t be showing anything this session because my code was pretty much identical to that of the lessons :neutral_face:

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    SetupGame(); // Initialising the Game 

//    PrintLine(TEXT("The HiddenWord is: %s \n"), *HiddenWord, HiddenWord.Len()); //Debug line to know the HiddenWord    
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{   
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();
    } 
    else
    {
        ProcessGuess(Input);
    }
}

void UBullCowCartridge::SetupGame()
{
    HiddenWord = TEXT("cake"); // set HiddenWorld
    Lives = HiddenWord.Len();  // Set Lives
    bGameOver = false;

    // Welcome the Player
    PrintLine(TEXT("Welcome!"));

    // Prompt Player for Guess
    PrintLine(TEXT("\nTry to guess a %i letter word!\nYou have %i lives\n"), HiddenWord.Len(), Lives);
    PrintLine(TEXT("Type your guess and\npress enter to continue\n"));
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("\nPress enter to continue\n"));
}

void UBullCowCartridge::ProcessGuess(FString Guess)
{
    if ( Guess == HiddenWord)
    {
        ClearScreen();
        PrintLine(TEXT("\nCongratz! You won!"));
        EndGame();
        return;
    }
        
    if (HiddenWord.Len() != Guess.Len())
    {   
        ClearScreen(); 
        PrintLine(TEXT("\nThe hidden word is %i letters long\n"), HiddenWord.Len());           
        PrintLine(TEXT("Try guessing again.\nYou have %i lives remaining"), Lives);               
        return;
    }

    if (!IsIsogram(Guess))
    {
        PrintLine(TEXT("\nYour guess is not an isogram\nGuess again!\nYou still have %i lives remaining"), Lives);
        return;
    }

    --Lives;
    ClearScreen();
    PrintLine(TEXT("\nNot the hidden word\n"));
    PrintLine(TEXT("you lost a life! You have %i more lives\n"), Lives);

    if(Lives <= 0)
    {
        ClearScreen();
        PrintLine(TEXT("\nSorry, you have no lives left."));
        PrintLine(TEXT("\nThe hidden word was %s"), *HiddenWord);
        EndGame();
        return;
    }
    
    // show player the bulls and caws
    //if (CommonLetters) {you have %i caws: there are %i common letters with the HiddenWord}
    //if (CommoneLetters are in the right place) {You have %i bulls: there are %i common letters with the HiddenWord in the right position}
}
bool UBullCowCartridge::IsIsogram(FString Word)
{   
    //check all elements of Word to spot if someone is duplicated
    //as soon as it finds a duplicate the function returns false
    
    if(Word[0]==Word[1])
    { 
        return false; 
    }

    if(Word[0]==Word[2])
    { 
        return false; 
    }

    if(Word[0]==Word[3])
    { 
        return false; 
    }

    if(Word[1]==Word[2])
    { 
        return false; 
    }

    if(Word[1]==Word[3])
    { 
        return false; 
    }

    if(Word[2]==Word[3])
    { 
        return false; 
    }

    return true;
}

Thanks for reading :grin:

1 Like

I’m happy you are back playing around with programming. Great job with your code.

1 Like

Thank you! :blush:

Wow, looks like you know a thing or two! :+1:

1 Like

hahaha thanks :sweat_smile:
something like that :grin:

1 Like

Privacy & Terms