@Rob
Main.cpp
/* This is the console executable that makes use of the BullCow class
This acts as the view in a MVC pattern, and is responsible for all
user interaction. For game logic see the FBullCowGame class.
*/
#include <iostream>
#include <string>
#include "FBullCowGame.h"
using Ftext = std::string;
using int32 = int;
void PrintIntro();
void PlayGame();
Ftext GetGuess();
bool AskToPlayAgain();
FBullCowGame BCGame; // instantiate a new game
// the entry point for our application
int main()
{
bool bPlayAgain = false;
do {
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);
return 0; // exit the application
}
// introduce the game
void PrintIntro()
{
constexpr int32 WORD_LENGTH = 9;
std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
std::cout << "Can you guess the " << WORD_LENGTH;
std::cout << " letter isogram I'm thinking of?\n";
std::cout << std::endl;
return;
}
void PlayGame()
{
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();
// loop for the number of turns asking for guesses
// TODO change from FOR to WHILE loop once we are validating tries
for (int32 count = 1; count <= MaxTries; count++) {
Ftext Guess = GetGuess(); //TODO make loop check for valid guesses
// submit valid guess to the game, and receieve counts
FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
// print # of bulls and cows
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << ". Cows = " << BullCowCount.Cows << std::endl;
}
//TODO summarize game
}
// get a guess from the player
Ftext GetGuess()
{
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try " << CurrentTry << ". Enter your guess: ";
Ftext Guess = "";
std::getline(std::cin, Guess);
return Guess;
}
bool AskToPlayAgain()
{
std::cout << "Do you want to play again? (y/n)?";
Ftext Response = "";
std::getline(std::cin, Response);
return Response[0] == 'y' || (Response[0] == 'Y');
return false;
}
FBullCowGame.h
#pragma once
#include <string>
using FString = std::string;
using int32 = int;
// all values initialized to 0
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};
class FBullCowGame
{
public:
FBullCowGame(); //Constructor
int32 GetMaxTries() const;
int32 GetCurrentTry () const;
bool IsGameWon() const;
void Reset(); // TODO make a more rich return value.
bool CheckGuessValidity(FString);
FBullCowCount SubmitGuess(FString); // counts bulls & cows, and increases try # assuming valid guess
// ^^ ignore Private class right now. focus on interface above ^^
private:
// See constructor for intialization
int32 MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;
};
FBullCowGame.cpp
#include "FBullCowGame.h"
using int32 = int;
FBullCowGame::FBullCowGame()
{
Reset();
}
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
void FBullCowGame::Reset()
{
constexpr int32 MAX_TRIES = 8;
MyMaxTries = MAX_TRIES;
const FString HIDDEN_WORD = "ant";
MyHiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
return;
}
bool FBullCowGame::IsGameWon() const
{
return false;
}
bool FBullCowGame::CheckGuessValidity(FString)
{
return false;
}
// receives a VALID guess, increments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
// compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
// if they match then
if (Guess[GChar] == MyHiddenWord[MHWChar]) {
if (MHWChar == GChar) { // if they're in the same place
BullCowCount.Bulls++; // incriment bulls
}
else {
BullCowCount.Cows++; // must be a cow
}
}
}
}
return BullCowCount;
}