Hey guys,
I went and wrote an implementation of how I think the game should run before I went and checked ahead to see how the lecturer has done it. Please give me feed back on my implementation and give me any questions you may have.
main.cpp
#include "FBullCowGame.h"
using FText = std::string;
int main()
{
FBullCowGame BullCowGame;
FBullCowCount BullCowCount;
FText MyGuess = "";
bool FirstTime = true;
do
{
if (FirstTime)
{
BullCowGame.PrintIntro();
FirstTime = false;
}
std::cout << "\nYou have " << (BullCowGame.GetMaxTries() - BullCowGame.GetCurrentTry()) << " turns left. \nPlease enter your guess: ";
std::getline(std::cin, MyGuess);
std::cout << std::endl;
BullCowCount = BullCowGame.SubmitGuess(MyGuess);
BullCowGame.PrintGuessResults(MyGuess, BullCowCount);
} while (BullCowGame.PlayAgain(BullCowGame.GetCurrentTry(), BullCowCount, FirstTime));
return 0;
}
FBullCowGame.cpp
#pragma once
#include <string>
#include <iostream>
using FString = std::string;
using int32 = int;
const FString HIDDEN_WORD = "planets";
constexpr int32 WORD_LENGTH = 7;
constexpr int32 GUESS_NUMBER = 10;
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};
class FBullCowGame
{
public:
FBullCowGame();
void PrintIntro();
void Reset();
int32 GetMaxTries();
int32 GetCurrentTry();
FBullCowCount SubmitGuess(FString);
void PrintGuessResults(FString, FBullCowCount);
bool IsGameWon(FBullCowCount);
bool CheckGuessValidity(FString);
bool PlayAgain(int32, FBullCowCount, bool&);
private:
int32 MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;
};
FBullCowGame.h
#include "FBullCowGame.h"
FBullCowGame::FBullCowGame() : MyHiddenWord(HIDDEN_WORD), MyCurrentTry(0), MyMaxTries(GUESS_NUMBER)
{
}
void FBullCowGame::PrintIntro()
{
std::cout << "Welcome to Bulls and Cows, a fun word game.\n" << "Can you guess the " << WORD_LENGTH << " letter word that I'm thinking of?" << std::endl;
return;
}
void FBullCowGame::Reset()
{
}
int32 FBullCowGame::GetMaxTries()
{
return MyMaxTries;
}
int32 FBullCowGame::GetCurrentTry()
{
return MyCurrentTry;
}
FBullCowCount FBullCowGame::SubmitGuess(FString InGuess)
{
MyCurrentTry++;
FBullCowCount BullCowCount;
if (CheckGuessValidity(InGuess) != true)
{
std::cout << "Your word isn't the right length!" << std::endl;
return BullCowCount;
}
for (int32 i = 0; i < WORD_LENGTH; i++)
{
for (int32 j = 0; j < WORD_LENGTH; j++)
{
if (MyHiddenWord[i] == InGuess[j])
{
if (i == j)
{
BullCowCount.Bulls++;
}
else
{
BullCowCount.Cows++;
}
}
}
}
return BullCowCount;
}
void FBullCowGame::PrintGuessResults(FString CurrentGuess, FBullCowCount CurrentCount)
{
if(CheckGuessValidity(CurrentGuess) && IsGameWon(CurrentCount) == false) std::cout << "For the guess of " << CurrentGuess << " you got the following:\n Bulls: " << CurrentCount.Bulls << "\n Cows: " << CurrentCount.Cows << std::endl;
return;
}
bool FBullCowGame::IsGameWon(FBullCowCount InCount)
{
if (InCount.Bulls == WORD_LENGTH)
{
return true;
}
else
{
return false;
}
}
bool FBullCowGame::CheckGuessValidity(FString InGuess)
{
if (InGuess.length() != WORD_LENGTH)
{
return false;
}
else
{
return true;
}
}
bool FBullCowGame::PlayAgain(int32 CurrentGuessNumber, FBullCowCount CurrentCount, bool& FirstTime)
{
if ((CurrentGuessNumber < (GUESS_NUMBER)) && (IsGameWon(CurrentCount) == false))
{
return true;
}
else
{
if (IsGameWon(CurrentCount))
{
std::cout << "You won! Congradulations! The word was " << HIDDEN_WORD << " and you managed to guess it in " << MyCurrentTry << " guesses!" << std::endl;
}
else
{
std::cout << "\nYou failed! Eat ****! The word was " << HIDDEN_WORD << "!" << std::endl;
}
FString TempGuess = "";
while (true)
{
std::cout << "Do you want to play again? Y/N";
std::getline(std::cin, TempGuess);
if ((TempGuess[0] == 'N') || (TempGuess[0] == 'n'))
{
return false;
}
else if ((TempGuess[0] == 'Y') || (TempGuess[0] == 'y'))
{
FirstTime = true;
return true;
}
else
{ }
}
}
}