Please someone help I can’t see where I have gone wrong the code was working just like Bens and now its just not and cannot spot the mistake.
Main.cpp
#include
#include
#include “FBullCowGame.h”
using FText = std::string;
using int32 = int;
void PrintIntro();
void PlayGame();
FText GetVaildGuess();
bool AskToPlayAgain();
FBullCowGame BCGame;
int main()
{
bool bPlayAgain = false;
do {
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);
return 0;
}
void PrintIntro() {
//Introduce the game
std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
std::cout << "\n";
std::cout << "Can you guess the " << BCGame.GetHiddenWordLength();
std::cout << " letter isogram I'm thinking of\n";
}
void PlayGame()
{
BCGame.Reset();
int32 MyMaxTries = BCGame.GetMaxTries();
//loop for number of turns
while (!BCGame.IsGameWon() && BCGame.GetCurrentTries() <= MyMaxTries)
{
FText Guess = GetVaildGuess();
FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess); //submit vaild guess
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << " Cows = " << BullCowCount.Cows << "\n\n";
}
//TODO Summarise game
}
// loop continually until the user gives a vaild guess
FText GetVaildGuess()
{
FText Guess = “”;
EWordStatus Status = EWordStatus::Invaild_Status;
do {
int32 MyCurrentTries = BCGame.GetCurrentTries();
std::cout << “Try " << MyCurrentTries;
std::cout << " Enter your Guess:”;
std::getline(std::cin, Guess);
std::cout << "\n";
EWordStatus Status = BCGame.CheckGuessValidity(Guess);
switch (Status)
{
case EWordStatus::Not_Enough_Letters:
std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
break;
case EWordStatus::Not_Isogram:
std::cout << "Please enter an Isogram.\n";
break;
case EWordStatus::Not_Lowercase:
std::cout << "Game is case sensitive. Please use lowercase only.\n";
break;
default:
break;
}
std::cout << std::endl;
} while (Status != EWordStatus::OK); // keep looping until we get no errors
return Guess;
}
bool AskToPlayAgain()
{
std::cout << “Do you wish to play again (y/n)?”;
FText Response = “”;
std::getline(std::cin, Response);
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
}
FBullCowGame.h
#pragma once
#include
using int32 = int;
using FString = std::string;
struct FBullCowCount {
int32 Bulls = 0;
int32 Cows = 0;
};
enum EWordStatus
{
Invaild_Status,
OK,
Not_Isogram,
Not_Enough_Letters,
Not_Lowercase,
};
class FBullCowGame {
public:
FBullCowGame();
int32 GetMaxTries() const;
int32 GetCurrentTries() const;
int32 GetHiddenWordLength() const;
bool IsGameWon() const;
void Reset(); // TODO make more rich return value
EWordStatus CheckGuessValidity(FString) const;
//TODO A way to count bulls and cows
FBullCowCount SubmitValidGuess(FString);
//TODO a way to count the number of turns
//TODO
private:
int32 MyCurrentTries;
int32 MyMaxTries;
FString MyHiddenWord;
bool bGameIsWon;
};
FBullCowGame.cpp
#include “FBullCowGame.h”
#include
#include
using in32 = int;
using Fstring = std::string;
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries;}
int32 FBullCowGame::GetCurrentTries() const { return MyCurrentTries;}
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); }
FBullCowGame::FBullCowGame() { Reset(); }
void FBullCowGame::Reset()
{
constexpr int32 MAX_TRIES = 10;
MyMaxTries = MAX_TRIES;
const FString HIDDEN_WORD = "cause";
MyHiddenWord = HIDDEN_WORD;
MyCurrentTries = 1;
return;
}
bool FBullCowGame::IsGameWon() const
{
return false;
}
EWordStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
if (false) // if the guess isn’t an isogram
{
return EWordStatus::Not_Isogram;
}
else if (false) // if the guess isn’ all lowercase
{
return EWordStatus::Not_Lowercase;
}
else if (Guess.length() != GetHiddenWordLength()) // if the guess lenght is wrong
{
return EWordStatus::Not_Enough_Letters;
}
else // otherwise
{
return EWordStatus::OK;
}
// return OK
}
//receives a valid guess, incriments turn, and returns count
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
MyCurrentTries++;
FBullCowCount BullCowCount;
int32 WordLength = MyHiddenWord.length();
//loop through all letters in the guess
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++)
{
for (int32 GChar = 0; GChar < WordLength; GChar++)
{
if (Guess[GChar] == MyHiddenWord[MHWChar])
{
if (MHWChar == GChar)
{
BullCowCount.Bulls++;
}
else
{
BullCowCount.Cows++;
}
}
}
} return BullCowCount;
}