My code with additional palindrome function

//cpp file
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include <deque>
#include <algorithm>

bool UBullCowCartridge::is_palindrome(const FString& s)
{
    std::deque<char>normal, reverse;

    for (auto& c : s) {
        if (isalpha(c)) {
            normal.push_back(toupper(c));
            reverse.push_front(toupper(c));
        }
    }
    if (normal == reverse)
        return true;
    return false;
}

bool UBullCowCartridge::is_isogram(const FString& s) {
  
    for (int32 i = 0; i < s.Len()-1; i++)
        if (s[i] == s[i + 1])
            return true;
    return false;
}


void UBullCowCartridge::BeginPlay() {
   
    Super::BeginPlay();    
    SetupGame();
  
    PrintLine(FString::Printf(TEXT("The length of the hidden word is: %i"), HiddenWord.Len()));

    if(is_palindrome(HiddenWord))
        PrintLine(TEXT("Hidden word is a palindrome"));
    if(is_isogram(HiddenWord))
        PrintLine(TEXT("Hidden word is a isogram"));
}

void UBullCowCartridge::OnInput(const FString& Input) {

    if (bGameOver) {
        ClearScreen();
        SetupGame();
    }
    else {
        ProcessGuess(Input);
    }
    
}
void UBullCowCartridge::SetupGame() {
   
    HiddenWord = TEXT("cakes");
    player_lives = HiddenWord.Len();
    bGameOver = false;
    PrintLine(FString::Printf(TEXT("Total Lives: %i"), player_lives));
        PrintLine(FString::Printf(TEXT("first two characters are %c %c\nlast character is: %c "), HiddenWord[0], HiddenWord[1],HiddenWord[HiddenWord.Len()-1]));

    PrintLine(TEXT("Guess the hidden word!!\nPress enter to continue"));
 // PrintLine(TEXT("The hidden word is: %s"), *HiddenWord); // debug
}
void UBullCowCartridge::EndGame() {
    bGameOver = true; 
    PrintLine(TEXT("Press enter to play again"));
}
void UBullCowCartridge::ProcessGuess(const FString &Input) {
    if (Input == HiddenWord)
    {
        PrintLine(TEXT("You have Won!"));
        EndGame();
    }
    else
    {
        PrintLine(TEXT("You have Lost!"));
        PrintLine(FString::Printf(TEXT("Remaining lives: %i"), --player_lives));
        if(player_lives == 0) {
            PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
            PrintLine(TEXT("You have no lives left!\nGame Over."));
            EndGame();
        }

    }
}
//header file
// 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(const FString &Input);
        bool is_palindrome(const FString& s);
	bool is_isogram(const FString& s);

// Your declarations go below!
private:
		FString HiddenWord;
		int32 player_lives;
		bool bGameOver;

};

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

Privacy & Terms