this is main cpp
/*
this is console executable, that makes use of the bullcow class
this acts as the view in MVC Pattren, and is responsible for all user
interaction. for game logic see the FBullCowGame Class
*/
#include
#include
#include"FBullCowGame.h"
FBullAndCow BCGame;
using FText = std::string;
using int32 = int;
// introduce the game
void printintro()
{
std :: cout << "welcome to bulls and cows a fun words game.\n";
std :: cout << "can you guess the " << BCGame.GetHiddenWordLength();
std :: cout << " letter Isogram I'm thinking of ? " << std :: endl ;
return;
}
// taking guess from the user
FText getguess()
{
FBullAndCow BCGame;
int32 CurrentTry = BCGame.GetCurrentTry();
std :: cout <<"Try " << CurrentTry << ". \nenter your guess: ";
FText guess;
std :: getline (std :: cin, guess);
return guess;
}
// play the game
void Playgame()
{
// instantiate a new game
FBullAndCow BCGame;
BCGame.Restplaygame();
int32 MaxTries = BCGame.GetMaxTries();
std ::cout << std :: endl;
//loop for the number of times asking for guesses
// TODO Change it from for to while loop
for (int32 x = 0; x < MaxTries; x++)
{
FText guess;
std :: cout << std :: endl;
guess = getguess(); // TODO Make and check the valid Guess
//repeat it the guess back to them
// & and submit valid guess to the game and recive counts
FBullCowCount BullCowCount = BCGame.SubmitGuess(guess);
// print number of Bulls and Cows
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << " Cows = " << BullCowCount.Cows;
std::cout << std::endl;
std :: cout << "your guess was: " << guess << std :: endl;
// TODO Add a Game Summary
}
return;
}
//ask the player if they want to play again
bool asktoplayagain()
{
std :: cout << âDo you want to play again (y/n)? â;
FText answer =ââ;
std :: getline(std :: cin, answer);
std :: cout << std :: endl;
return ( answer[0] == âyâ ) || ( answer[0] == âYâ );
}
int main()
{
printintro();
std :: cout << std :: endl;
do
{
Playgame();
std :: cout << std :: endl;
}
while (asktoplayagain());
return 0;
}
this is Methods header.file
#pragma once
#include
using int32 = int;
using FString = std::string;
// all values intialised to zero
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};
class FBullAndCow
{
public:
// constructor
FBullAndCow();
void Restplaygame(); // TODO make it more rich Value
int32 GetMaxTries() const;
int32 GetCurrentTry() const;
bool IsGameWon() const;
int32 GetHiddenWordLength() const;
bool CheckGuessIsValid (FString) const ; // TODO make it more rich Value
FBullCowCount SubmitGuess(FString);
private:
// see constructor for initialisation
int32 MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;
};
this is methods file
#include âFBullCowGame.hâ
using int32 = int;
using FString = std::string;
FBullAndCow::FBullAndCow()
{
Restplaygame();
}
void FBullAndCow::Restplaygame()
{
constexpr int32 NumberOfTries = 8;
MyMaxTries = NumberOfTries;
const FString Hidden_word = "king";
MyHiddenWord = Hidden_word;
MyCurrentTry = 1;
return;
}
int32 FBullAndCow::GetMaxTries() const
{
return MyMaxTries;
}
int32 FBullAndCow::GetCurrentTry() const
{
return MyCurrentTry;
}
int32 FBullAndCow::GetHiddenWordLength() const
{
return MyHiddenWord.length();
}
bool FBullAndCow::IsGameWon() const
{
return false;
}
bool FBullAndCow::CheckGuessIsValid(FString) const
{
return false;
}
//recives a valid guess increments try and return count
FBullCowCount FBullAndCow::SubmitGuess(FString guess)
{
//incriment the try number
MyCurrentTry++;
// setup return variable
FBullCowCount BullCowCount;
// loop through all letter in the guess
int32 HiddenWorldLength = MyHiddenWord.length();
for (int32 MyHiddenWordChar = 0 ; MyHiddenWordChar < HiddenWorldLength ; MyHiddenWordChar++ )
{
// compare letters against the hidden word
for (int32 GuessChar = 0; GuessChar < HiddenWorldLength; GuessChar++)
{
//if they match
if (guess[GuessChar] == MyHiddenWord[MyHiddenWordChar])
{
//if they are in the same place
if (MyHiddenWordChar == GuessChar)
{
// incriment bulls
BullCowCount.Bulls++;
}
// if not in the same place
else
{
//incriment cows
BullCowCount.Cows++;
}
}
}
}
return BullCowCount;
}