Bull cows

here is my code

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

#include "BullCowCartridge.h"

#include "HiddenWordList.h"

void UBullCowCartridge::BeginPlay() // When the game starts

{

    Super::BeginPlay();

    SetUpGame();

}

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

{

    if (bGameOver)

    {

        ClearScreen();

        SetUpGame();

    }

    else //checking PLayer Guess

    {

        ProcessGuess(PlayersInput);

    }

}

void UBullCowCartridge::SetUpGame()

{

    //welcome the player

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

    HiddenWord = GetValidWords(WordList)[FMath::RandRange(0, GetValidWords(WordList).Num() - 1)];

    Lives = HiddenWord.Len();

    bGameOver = false;

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

    PrintLine (TEXT("You have %i lives. Type in your guess"), Lives);

    PrintLine (TEXT("Press enter to continue..."));//prompt player for guess

    PrintLine (TEXT("The Hidden word is: %s."), *HiddenWord);//debug line

}

void UBullCowCartridge::EndGame()

{

    bGameOver = true;

    PrintLine (TEXT("\nPress enter to play again"));

}

void UBullCowCartridge::ProcessGuess(FString Guess)

{

    if (Guess == HiddenWord)

    {

        PrintLine (TEXT("You win!!!"));

        EndGame();

        return;

    }

    //Remove life

    PrintLine(TEXT("You have lost a life."));

    --Lives;

    //check if its the right number of letters

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

    {

        PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());

        PrintLine(TEXT("Sorry, try again. \nYou have %i lives remaining."), Lives);

    }

    if (Lives <= 0)

    {

        ClearScreen();

        PrintLine(TEXT("You have no lives left."));

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

        EndGame();

        return;  

    }

    //check if its an isogram

    if (! IsIsogram(Guess))

    {

        PrintLine(TEXT("No repeating letters."));

        return;

    }

    //show the players the bulls and cows

    int32 Bulls, Cows;

    GetBullCows(Guess, Bulls, Cows);

    PrintLine(TEXT("You have %i bulls and %i cows"), Bulls, Cows);

}

bool UBullCowCartridge::IsIsogram(FString Word) const

{

    for (int32 Index = 0; Index < Word.Len(); Index++)

    {

        for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)

        {

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

            {

                return false;

            }

            

        }

        

    }

    return true;

}

TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& Word) const

{

    TArray<FString> ValidWords;

    for (FString Words : Word)

    {

        if (Words.Len() >= 4 && Words.Len() <= 10)

        {

            if (IsIsogram(Words))

            {

                ValidWords.Emplace(Words);

            }

        }

    }  

    return ValidWords;

}

void UBullCowCartridge::GetBullCows(const FString& Guess, int32& BullCount, int32&CowCount) const

{

    BullCount = 0;

    CowCount = 0;

    // for every index of the guess is same as the index of the hidden word, bull count ++

    // if not a bull was it a cow. If yes cow count ++

    for (int32 GuessIndex[] = 0; int32 GuessIndex[] < Guess.Len(); GuessIndex++)

    {

        if (Guess[GuessIndex] == HiddenWord[GuessIndex])

        {

            BullCount ++;

            continue

        }

        for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)

        {

            if (Guess[GuessIndex] == HiddenWord[HiddenIndex])

            {

                CowCount ++;

            }

        }     

    }

}

Here is the error statement from unreal.

continue on line 231 is missing a semi colon, however your code and the error message do not line up could you send a copy of the actual .cpp file. I believe that it was formatted differently when you typed it out on this forum.

205: for (int32 GuessIndex[] = 0; int32 GuessIndex[] < Guess.Len(); GuessIndex++);

on line 205, you initialized GuessIndex twice and declared it as an array of int32, instead just declare it as a int32 and don’r declare it twice.

for(int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++);

The file and the error messages are cryptic because the line numbers don’t match the error message

I believe that will resolve all the issues associated with this .cpp file

Works thank you

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

Privacy & Terms