Skip to content

Instantly share code, notes, and snippets.

@JackDraak
Created January 26, 2016 08:27
Show Gist options
  • Save JackDraak/8ca087914175d18140f5 to your computer and use it in GitHub Desktop.
Save JackDraak/8ca087914175d18140f5 to your computer and use it in GitHub Desktop.
Main.cpp - I/O example
#pragma once
#include <algorithm>
#include <string>
#include "FBullCowGame.h"
// Required for UnrealEngine-friendly syntax:
using FText = std::string;
using int32 = int;
// Function prototypes, as outside class:
FText GetValidGuess();
void MasterControlProgram();
void PrintTurnSummary(FBullCowCounts BullCowCounts, FString& Guess);
// Instantiate a new game named BCGame, which is recycled through each turn and round (or phase):
FBullCowGame BCGame;
// The entry-point for the applciation:
int main()
{
do { MasterControlProgram(); } while (bAskToPlayAgain());
return 0;
}
// Core game I/O handler method:
void MasterControlProgram()
{
BCGame.Reset();
PrintRoundIntro();
while (!BCGame.IsPhaseWon() && BCGame.GetTurn() <= BCGame.GetMaxTries())
{
FText Guess = GetValidGuess();
FBullCowCounts BullCowCounts = BCGame.ProcessValidGuess(Guess);
PrintTurnSummary(BullCowCounts, Guess);
BCGame.IncrementTry();
}
PrintPhaseSummary();
return;
}
// Output - Print game introduction, instruction and status text:
void PrintRoundIntro()
{
std::cout << "\nA secret " << BCGame.GetIsogram().length() << "-letter isogram has been selected just for you... Therefore,";
std::cout << "\nThis round you'll need to earn " << BCGame.GetIsogram().length() << " Bulls in one guess to win. Good luck!\n\n";
}
// I/O - Get a valid guess from the player, loop until satisfied:
FText GetValidGuess()
{
EGuessStatus Status = EGuessStatus::Invalid_Status;
FText Guess = "";
do
{
// Acquire input:
std::cout << std::endl << "Please enter a " << BCGame.GetIsogram().length() << " letter guess, #" << BCGame.GetTurn();
std::cout << " of " << BCGame.GetMaxTries() << ": ";
std::getline(std::cin, Guess);
// Provide helpful feedback if the guess is unexpected:
Status = BCGame.CheckGuessValidity(Guess);
// ...yada yada... basically it loops here until the player input is validated
return Guess;
}
// Output - After a guess is validated, print the results: Guess# of #, Bull# Cow#
void PrintTurnSummary(FBullCowCounts BullCowCounts, FString& Guess)
{
std::cout << "\nGuess Result " << BCGame.GetTurn() << "/" << BCGame.GetMaxTries() << ": " << Guess << ", has:\n";
// yada yada, stuff about player score
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment