'IsIsogram' problems.Please help!


1.BullCowartridge.cpp

// 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();

}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter

{   

    if (bGameOver) //If the game is over then ClearScreen() and Setupgame().

    {

        ClearScreen();

        SetupGame();

    }

    else//If the game isn't over then check the player guess 

    {   

        ProcessGuess(Input);

    }

    

}

void UBullCowCartridge::SetupGame()

{   

    //Welcoming the player.

    PrintLine(TEXT("Welcome to Bulls and Cows!"));

    HiddenWord = TEXT("cake");//Set the hidden word.

    Lives = HiddenWord.Len();

    bGameOver = false;

    PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());

    PrintLine(TEXT("You have only %i lives."), Lives);

    PrintLine(TEXT("Type in your guess and\n press enter to continue....."));

    const TCHAR HW[] = TEXT("cake");

    PrintLine(TEXT("First letter of the hidden word is : %c"), HiddenWord[0]);

    PrintLine(TEXT("Last letter of the word is : %c"), HiddenWord[3]);

}

void UBullCowCartridge::EndGame()

{

    bGameOver = true;

    PrintLine(TEXT("Press enter to play again."));

    

}

void UBullCowCartridge::ProcessGuess(FString Guess)

{ 

    if (HiddenWord == Guess)//Cheking the player guess.

    {

        PrintLine(TEXT("Your answer is correct!"));

        EndGame();

        return;

    }

    if (Guess.Len() != HiddenWord.Len())

             {

                 PrintLine(TEXT("Sorry,the word has %i letters."), HiddenWord.Len());

                 PrintLine(TEXT("You have %i lives remaining!"), Lives);

                 EndGame();

                 return;

             }

        //Cheking if it is a isogrm.

      if (!IsIsogram(Guess))

    {

        PrintLine(TEXT("No repeating letters,Guess again!"));

        return;

    }

    PrintLine(TEXT("You lost a live!"));

    --Lives;

     if(Lives <= 0)

            {

              EndGame();

              PrintLine(TEXT("You have no lives!"));

              PrintLine(TEXT("The hidden word was '%s'!"), *HiddenWord);

              ClearScreen();

              return;

            }

            //show the player bulls and cows

            PrintLine(TEXT("Guess again,you have %i lives left."), Lives);

}  

bool UBullCowCartridge::IsIsogram(FString Word)

{   

    int32 Index = 0;

    int32 Comparison = Index + 1;

    for (Comparison < Word.Len(); Comparison++)

    {

        if (Word[Index] == Word[Comparison])

        {

            return false;

        }

    }

    return true;

    // for (  = 0;  < (); ++)

    // {

    //     PrintLine(TEXT("%c"), Word[Index]);

    // }

    

    // For each letter 

    // star at element [0]

    // compare aginst each letter

    // untill we reach [Word.Len() -1]

    // if any are the same return false.

}

2.BullCowCartridge.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#include "Console/Cartridge.h"

#include "BullCowCartridge.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))

class BULLCOWGAME_API UBullCowCartridge : public UCartridge

{

    GENERATED_BODY()

    public:

    virtual void BeginPlay() override;

    virtual void OnInput(const FString& Input) override;

    void SetupGame();

    void EndGame();

    void ProcessGuess(FString Guess);

    bool IsIsogram(FString Word);

    // Your declarations go below!

    private:

    FString HiddenWord;

    int32 Lives;

    bool bGameOver;

};

Compile fails.Please help!

This is incorrect for a couple of reasons.

  • A for statement has 3 parts and you only have two.
  • The first part is for initialising a variable to be used within the loop, you have Comparison < Word.Len() which isn’t being used hence the message. This means Comparison++ is being used for the condition and you have nothing for the increment part hence the other part of that message about missing a semicolon.
  • With fixing that to be
    for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
    
    Isn’t going to correctly determine isograms. With the word being “been” this loop would do
    b == e // Word[0] == Word[1]
    b == e // Word[0] == Word[2]
    b == n // Word[0] == Word[3]
    
    And then end, “e” was never compared with the other “e” so this would falsely report that “been” is an isogram. You need another loop.

Thank you.I had solved the problem.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms