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()
{
std::cout << BCGame.GetCurrentTry();
do
{
PrintIntro();
PlayGame();
} while (AskToPlayAgain());
return 0; // exit the application
}
// introduce the game
void PrintIntro()
{
constexpr int32 WORD_LENGTH = 5;
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 am 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 loops once we are validating tries
for (int32 count = 1; count <= MaxTries; count++)
{
FText Guess = GetGuess(); // TODO make loop checking valid
// submit valid guess to the game
// print number of bulls and cows
std::cout << "Your guess was: " << Guess << std::endl;
std::cout << std::endl;
}
// TODO summarise game
}
// get a guess from the player
FText GetGuess()
{
int32 MyCurrentTry = BCGame.GetCurrentTry();
std::cout << "Try " << MyCurrentTry << ". Enter your try: ";
FText Guess = "";
std::getline(std::cin, Guess);
return Guess;
}
bool AskToPlayAgain()
{
std::cout << "Do you want to play again, Yes or No? ";
FText Response = "";
std::getline(std::cin, Response);
std::cout << std::endl;
return (Response[0] == 'y') || (Response[0] == 'Y');
}
FBullCowGame.h:
#pragma once
#include <string>
using FString = std::string;
using int32 = int;
// all values initialized to 0
struct BullCowCount
{
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); // TODO make a more rich return value.
BullCowCount SubmitGuess(FString);
// please try and ignore this and focus on the interface above ^^
private:
// See constructor for initialisation
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 = "planet";
MyCurrentTry = HIDDEN_WORD;
MyCurrentTry = 1; //TODO make try count increase for each successful attempt
return;
}
bool FBullCowGame::IsGameWon() const
{
return false;
}
bool FBullCowGame::CheckGuessValidity(FString)
{
return false;
}
// recieve a VALID guess, increments turn, and returns count
BullCowCount FBullCowGame::SubmitGuess(FString)
{
//increment the turn number
MyCurrentTry++;
//setup return variable
BullCowCount BullCowCount;
//loop through all letters in the guess
// compare letters against the hidden word
return BullCowCount;
}
Most of the errors went away after I restarted VS, then I realized I defined HIDDEN_WORD as MyCurrentTry, instead of MyHiddenWord.
In essence is that basically telling me you’re trying to define a string with an integer and you can’t?