I don’t know whats wrong with this thing. I’m sure its obvious. it returns the wrong messages when entering guesses.
//FBullCowGame.cpp
//#include is literally a copy and paste of properly formatted C++ text, for access
#pragma once
#include “FBullCowGame.h”
#include
#define TMap std::map // to make syntax more Unreal friendly
using int32 = int;
FBullCowGame::FBullCowGame() { Reset(); }//default constructor
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); }
bool FBullCowGame::IsGameWon() const { return bGameIsWon; }
int32 FBullCowGame::GetMaxTries() const
{
TMap<int32, int32> WordLengthToMaxTries
{
{3,5}, {4,6}, {5,7}, {6,8}
};
return WordLengthToMaxTries[MyHiddenWord.length()];
}
void FBullCowGame::Reset()
{
const FString HIDDEN_WORD = “sin”; //this Must be an isogram
MyHiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
bGameIsWon = false;
return;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
if (!IsIsogram(Guess))
{
return EGuessStatus::Not_Isogram;
}
else if (!IsLowercase(Guess))
{
return EGuessStatus::Not_Lowercase;
}
else if (Guess.length() != GetHiddenWordLength()) // if the guess length is wrong
{
return EGuessStatus::Wrong_Length;
}
else
{
return EGuessStatus::OK;
}
}
// receives a VALID guess, incriments turn, and returns count
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
MyCurrentTry++;
FBullCowCount BullCowCount;
int32 WordLength = MyHiddenWord.length(); //assuming same length as guess
// loop through all letters in the hidden word
for (int32 i/*MyHiddenWordChar*/ = 0; i < WordLength; i++)
{
for (int32 j/*GuessChar*/ = 0; j < WordLength; j++)//compare letters against the hidden word
{
//if they match
if (Guess[j] == MyHiddenWord[i])
{
if (i == j)//if they are in the same place
{
BullCowCount.Bulls++; //increment bulls
}
else
{
BullCowCount.Cows++; //increment cows
}
}
}
}
if (BullCowCount.Bulls == WordLength)
{
bGameIsWon = true;
}
else
{
bGameIsWon = false;
}
return BullCowCount;
}
bool FBullCowGame::IsIsogram(FString Word) const
{
// treat 0 and 1 letter words as isograms
if (Word.length() <= 1)
{
return true;
}
TMap<char, bool> LetterSeen;// setup our map
for (auto Letter : Word)
{
Letter = tolower(Letter);//handle mixed case
if (LetterSeen[Letter])// if the letter is in the map
{
return false;// we do NOT have and isogram
}
else
{
LetterSeen[Letter] = true;// add the letter to the map as seen
}
}
return true; // for example in cases where /0 is entered
}
bool FBullCowGame::IsLowercase(FString Word) const
{
for (auto Letter : Word)
{
if (!islower(Letter))
{// if not a lowercase letter
return false;
}
}
return false;
}
//FBullCowGame.h
/* The game logic (no view code or direct user interaction)
The game is a simple guess the word game based on Mastermind */
#pragma once
#include
// to make the syntax Unreal friendly
using FString = std::string;
using int32 = int;
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};
enum class EGuessStatus
{
Invalid_Status,
OK,
Not_Isogram,
Wrong_Length,
Not_Lowercase
};
class FBullCowGame
{
public:
FBullCowGame();//constructor
int32 GetMaxTries() const;
int32 GetCurrentTry() const;
int32 GetHiddenWordLength() const;
bool IsGameWon() const;
EGuessStatus CheckGuessValidity(FString) const;
void Reset();
//counts bulls & cows and increases try # assuming valid guess
FBullCowCount SubmitValidGuess(FString);
private:
//see constructor for initialization
int32 MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;
bool bGameIsWon;
bool IsIsogram(FString) const;
bool IsLowercase(FString) const;
};
//Main.cpp
/*
This is the console executable that makes use of BullCowClass.
This acts as the view in a MVC pattern and is responsible for all
user interaction. For game logic see the FBullCowGame class.
*/
#pragma once
#include
#include
#include “FBullCowGame.h”
// to make syntax Unreal friendly
using FText = std::string;
using int32 = int;
// function prototypes as outside a class
void PrintIntro();
void PlayGame();
void PrintGameSummary();
bool AskToPlayAgain();
FText GetValidGuess();
FBullCowGame BCGame; //instantiate a new game, which we re-use across plays
//the entry point for our application
int32 main ()
{
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);
return 0; // exit the application
}
//introduce the game
void PrintIntro()
{
std::cout << " Welcome to Bulls and Cows\n\n A fun word game\n" << std::endl;
std::cout << " } { ___ " << std::endl;
std::cout << " (o o) (o o) " << std::endl;
std::cout << " /-------\ / \ /-------\ " << std::endl;
std::cout << " / | BULL |O O| COW | \ " << std::endl;
std::cout << " * |&----| |-----UU| * " << std::endl;
std::cout << " ^ ^ ^ ^ " << std::endl;
std::cout << " Can you guess the " << BCGame.GetHiddenWordLength();
std::cout << " letter isogram I am thinking of?\n";
std::cout << std::endl;
return;
}
// plays a single game to completion
void PlayGame()
{
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();
//loop asking for guesses while the game is NOT won
while (!BCGame.IsGameWon() && BCGame.GetCurrentTry() <= MaxTries)
{
FText Guess = GetValidGuess();
//submit valid guess to the game, and receive counts
FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << " Cows = " << BullCowCount.Cows << "\n\n";
}
PrintGameSummary();
}
//loop continually until user gives a valid guess
FText GetValidGuess()
{
FText Guess = “”;
EGuessStatus Status = EGuessStatus::Invalid_Status;
do
{
//get a guess from the player
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try: " << CurrentTry << " of " << BCGame.GetMaxTries();
std::cout << " .Enter your guess : ";
std::getline(std::cin, Guess);
// error codes \/
Status = BCGame.CheckGuessValidity(Guess);
switch (Status)
{
case EGuessStatus::Wrong_Length:
std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n\n";
break;
case EGuessStatus::Not_Isogram:
std::cout << "Please enter a word without repeating letters.\n\n";
break;
case EGuessStatus::Not_Lowercase:
std::cout << "Please enter all lowercase letters\n\n";
break;
default:
//assume the guess is valid
break;
}
std::cout << std::endl;
} while (Status != EGuessStatus::OK); //keep looping until we get no errors
return Guess;
}
bool AskToPlayAgain()
{
std::cout << "Do you want to play again with the same word (y/n)? ";
FText Response = “”;
std::getline(std::cin, Response);
return (Response[0] == ‘y’) || (Response[0] == ‘Y’);
}
void PrintGameSummary()
{
if (BCGame.IsGameWon())
{
std::cout << “WELL DONE YOU WIN!!!\n”;
}
else
{
std::cout << “You lose, too bad:(”;
}
}