Created
July 3, 2017 23:08
-
-
Save richagren/1f4c44fac362246413513ea00809907c to your computer and use it in GitHub Desktop.
Error checking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "FBullCowGame.h" | |
using int32 = int; | |
FBullCowGame::FBullCowGame() { Reset(); } | |
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; } | |
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; } | |
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); } | |
void FBullCowGame::Reset() { | |
constexpr int32 MAX_TRIES = 8; | |
const FString HIDDEN_WORD = "planet"; | |
MyMaxTries = MAX_TRIES; | |
MyHiddenWord = HIDDEN_WORD; | |
MyCurrentTry = 1; | |
return; | |
} | |
bool FBullCowGame::IsGameWon() const { | |
return false; | |
} | |
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const { | |
if (false) { // If the guess isn't an isogram | |
return EGuessStatus::Not_Isogram; // return an error | |
} | |
else if (false) { //if the guess isn't all lowercase | |
return EGuessStatus::Not_Lowercase; // return an error | |
} | |
else if (Guess.length() != GetHiddenWordLength()) { // if the guess lenth is wrong | |
return EGuessStatus::Wrong_Length; // return an error | |
} | |
else { | |
return EGuessStatus::OK; | |
} | |
//otherwise | |
//return OK | |
} | |
//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 thru 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 they are in the same place | |
if (MHWChar == GChar) { | |
BullCowCount.Bulls++; // increment bulls | |
} | |
else { | |
BullCowCount.Cows++; // increment cows | |
} | |
} | |
} | |
} | |
return BullCowCount; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <string> | |
using FString = std::string; | |
using int32 = int; | |
// all values initiailized to zero | |
struct FBullCowCount { | |
int32 Bulls = 0; | |
int32 Cows = 0; | |
}; | |
enum class EGuessStatus { | |
OK, | |
Not_Isogram, | |
Wrong_Length, | |
Not_Lowercase | |
}; | |
class FBullCowGame { | |
public: | |
FBullCowGame(); // constructor | |
int32 GetMaxTries() const; | |
int32 GetCurrentTry() const; | |
int32 GetHiddenWordLength() const; | |
bool IsGameWon() const; | |
EGuessStatus CheckGuessValidity(FString) const; | |
void Reset(); // TODO make a more rich return value | |
FBullCowCount SubmitGuess(FString); | |
// Please try and ignore this and focus on the interface above ^^ | |
private: | |
int32 MyCurrentTry; // See Constructor for initialization value | |
int32 MyMaxTries; | |
FString MyHiddenWord; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This is the console executable that makes use of the BullCow class. | |
This acts as the view in an MVC pattern, and is responsible for all user interaction.*/ | |
#include <iostream> | |
#include <string> | |
#include "FBullCowGame.h" | |
using int32 = int; | |
using FText = std::string; | |
void PrintIntro(); | |
void PlayGame(); | |
FText GetGuess(); | |
bool AskToPlayAgain(); | |
FBullCowGame BCGame; //instantiate a new game | |
//the entry point of our application | |
int main() { | |
bool bPlayAgain = false; | |
do { | |
PrintIntro(); | |
PlayGame(); | |
// TODO Add a game summary | |
bPlayAgain = AskToPlayAgain(); | |
} | |
while (bPlayAgain); | |
return 0; // exit the application | |
} | |
void PrintIntro() { | |
//introduce the game | |
std::cout << "Welcome to Bulls and Cows, a fun word game.\n"; | |
std::cout << "Can you guess the " << BCGame.GetHiddenWordLength(); | |
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 umber of turns | |
// 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 checking valid | |
EGuessStatus Status = BCGame.CheckGuessValidity(Guess); | |
// submit valid guess to the game | |
FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess); | |
// print number of bulls and cows | |
std::cout << "Bulls = " << BullCowCount.Bulls; | |
std::cout << " Cows = " << BullCowCount.Cows << std::endl; | |
std::cout << std::endl; | |
} | |
} | |
FText GetGuess() { | |
int32 CurrentTry = BCGame.GetCurrentTry(); | |
// get a guess from the player | |
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'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment