Skip to content

Commit

Permalink
BC24 Using if Statements in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tristem committed Jan 12, 2016
1 parent 0f6b87a commit ae0a805
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
21 changes: 17 additions & 4 deletions BullCowGame/FBullCowGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void FBullCowGame::Reset()
constexpr int32 MAX_TRIES = 8;
MyMaxTries = MAX_TRIES;

const FString HIDDEN_WORD = "planet";
const FString HIDDEN_WORD = "ant";
MyHiddenWord = HIDDEN_WORD;

MyCurrentTry = 1;
Expand All @@ -30,16 +30,29 @@ bool FBullCowGame::CheckGuessValidity(FString)
}

// receives a VALID guess, incriments turn, and returns count
BullCowCount FBullCowGame::SubmitGuess(FString)
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// incriment the turn number
MyCurrentTry++;

// setup a return variable
BullCowCount BullCowCount;
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; j < HiddenWordLength; j++) {
// if they match then
if (Guess[i] == MyHiddenWord[i]) {
if (i == j) { // if they're in the same place
BullCowCount.Bulls++; // incriment bulls
}
else {
BullCowCount.Cows++; // must be a cow
}
}
}
}
return BullCowCount;
}
4 changes: 2 additions & 2 deletions BullCowGame/FBullCowGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using FString = std::string;
using int32 = int;

// all values intialised to zero
struct BullCowCount
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
Expand All @@ -22,7 +22,7 @@ class FBullCowGame

void Reset(); // TODO make a more rich return value.
bool CheckGuessValidity(FString); // TODO make a more rich return value.
BullCowCount SubmitGuess(FString);
FBullCowCount SubmitGuess(FString);


// ^^ Please try and ignore this and focus on the interface above ^^
Expand Down
6 changes: 4 additions & 2 deletions BullCowGame/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ void PlayGame()
for (int32 count = 1; count <= MaxTries; count++) {
FText Guess = GetGuess(); // TODO make loop checking valid

// submit valid guess to the game
// submit valid guess to the game, and receive counts
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 << "Your guess was: " << Guess << std::endl;
std::cout << std::endl;
}

Expand Down
Binary file modified Section_02.VC.db
Binary file not shown.

3 comments on commit ae0a805

@siddrox
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition will be if (Guess[j] == MyHiddenWord[i]) in place of if (Guess[i] == MyHiddenWord[i])

@JustSvK
Copy link

@JustSvK JustSvK commented on ae0a805 Apr 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have trouble with Visual Studio, after this lecture. It always says me errors Those are the files what aren't allright
this is the main.cpp

/* This is a console executable, that makes use of the BullCow class
This acts as the view in a MVC pattern, and it's responsible for all
for all user interaction. For game logic see FBullCowGame
*/
#include
#include
#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 point32 for our application
int32 main()
{
Printintro();
PlayGame();
// TODO summarise game
AskToPlayAgain();
bool bPlayAgain = false;
do {
Printintro();
PlayGame();
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);

return 0; // exit the applicaiton

}
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
constexpr int32 NUMBER_OF_TURNS = 5;
for (int32 count = 1; count <= MaxTries; count++) {
	FText Guess = GetGuess(); // TODO make loop checking valid

	// 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 << "Your guess was: " << Guess << std::endl;
}

}
void Printintro()
{
// introduce the game
constexpr int32 WORD_LENGHT = 5;
std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
std::cout << "Can you guess the " << WORD_LENGHT;
std::cout << " letter isogram I am thinking of?\n";
std::cout << std::endl;
return;
}
FText GetGuess()
{
int32 GetCurrentTry = BCGame.GetCurrentTry();
// get a guess from the player
std::cout << "Try " << GetCurrentTry << ". 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');

}

This is the FBullCowGame.h

class FBullCowGame {
public:
FBullCowGame(); // constructor
int32 GetMaxTries() const;
int32 GetCurrentTry() const;
bool IsGameWon() const;
bool CheckGuessValidity(FStirng); // TODO make a more rich return value.
void Reset(); // TODO make a more rich return value.
// Counts bulls and cows, and increases try # assuming vaild guesses
FBullCowCount FBullCowCount::SubmitGuess(FStirng);

// ^^ Please try and ignore this and focus on the interface above ^^
private:
// see constructor for initialisation
int32 MyCurrentTry;
int32 MyMaxTries;
FStirng MyHiddenWord;
};

This is the FBullCowGame.cpp

#include "FBullCowGame.h"

using FStirng = std::string;
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 FStirng HIDDEN_WORD = "planet";
MyHiddenWord = HIDDEN_WORD;

MyCurrentTry = 1;

return;

}

// recives a VALID guess, incriments turn, and return count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop trough all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MWHChar = 0; MWHChar < HiddenWordLength; MWHChar++) {
// compare letters againts the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
// if they match then
if (Guess[MWHChar] == MyHiddenWord[MWHChar]) {
if (MWHChar == GChar) { // if they're in the same place
BullCowCount.Bulls++; // incriment bulls
}
else {
BullCowCount.Cows++; // must be a cow
}
}
}
}
return BullCowCount;
}

bool FBullCowGame::IsGameWon () const
{
return false;
}

bool FBullCowGame::CheckGuessValidity(FStirng)
{
return false;
}

Please if you can help me, write it in comments. Thanks!

@incpo
Copy link

@incpo incpo commented on ae0a805 Nov 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same problem, idk what I need to do, heeelp me

Please sign in to comment.