So my issue is kind of a tricky one. Once I run the game it starts fine, with the intro. But when I type in a guess, I only get 1. Then the console doesn’t say “press any key to continue…” it just doesn’t let me type anymore. I’ve tried rebuilding, and also re-opening Visual Studio. I’m also getting a Visual Studio Error.
Here is my code:
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()
{
do
{
PrintIntro();
PlayGame();
} while (AskToPlayAgain());
return 0; // exit the application
}
// introduce the game
void PrintIntro()
{
constexpr int32 WORD_LENGTH = 6;
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, and recieve counts
FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
// print number of bulls and cows
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << ". Cows = " << BullCowCount.Cows << 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 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); // TODO make a more rich return value.
FBullCowCount 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 = "ant";
MyHiddenWord = 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
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
//increment the turn number
MyCurrentTry++;
//setup return variable
FBullCowCount BullCowCount;
//loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 i = 0; i < HiddenWordLength; i++)
{
// compare letters against the hidden word
for (int32 j = 0; i < HiddenWordLength; j++) {
// if they match then
if (Guess[i] == MyHiddenWord[i]) {
if (i == j){ // if they're in the same place
BullCowCount.Bulls++;// increment bulls
}
else {
BullCowCount.Cows++;// must be a cow
}
}
}
}
return BullCowCount;
}