Skip to content

Commit

Permalink
BC30 Warm Fuzzy Feelings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tristem committed Jan 14, 2016
1 parent 0bf9c9b commit 775f950
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
17 changes: 7 additions & 10 deletions BullCowGame/FBullCowGame.cpp
Expand Up @@ -47,19 +47,16 @@ EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const


// receives a VALID guess, incriments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)

This comment has been minimized.

Copy link
@caixarel

caixarel Nov 11, 2018

I do not understand how to do the conection between the (Fstring Guess) and the MyHiddenWord.
here we have :
Guess[GChar]
how does the program knows that we are refering to the MyHiddenWord.??

This comment has been minimized.

Copy link
@SuperWig

SuperWig Nov 12, 2018

Guess is the parameter of the function meaning we would pass it a string when calling it

std::string Guess = GetGuess();
BCGame.SubmitValidGuess(Guess);

(It should be noted that Guess in the function in SubmitValidGuess is a copy of the argument and is a different variable)

MyHiddenWord is a member variable so we have access to it in this function. The function works by comparing the characters in each string - string being an array of characters - and their position.

if (Guess[GChar] == MyHiddenWord[MHWChar]) {
    if (MHWChar == GChar) { 

Which is what these two lines of code are doing. GChar and MHWChar are just ints, so the first line is checking if the characters are the same and then the second line is checking if the ints are the same.

So this is what is happening on each loop. Using HW = “ant” and Guesss = “and”

HWChar = 0, GChar = 0,
a nt == a nd; -> 0 == 0
HWChar = 0, GChar = 1,
a nt == a n d
HWChar = 0, GChar = 2,
a nt == an d

HWChar = 1, GChar = 0,
a n t == a nd

This comment has been minimized.

Copy link
@Spawn58

Spawn58 Nov 27, 2018

I am having a problem with the same thing. The debugger is saying Guess[MHWChar] == MyHiddenWord[MHWChar] Array index is out of bounds. Cannot understand how to fix it

This comment has been minimized.

Copy link
@SuperWig

SuperWig Nov 27, 2018

You're using the wrong variable. Check the code, you aren't using GChar.

This comment has been minimized.

Copy link
@Spawn58

Spawn58 Nov 27, 2018

saying the same thing but it is saying MHWChar is not identified. WHen the game is debugging I am getting a 1 on the first line. Believe that is a bug which is strange.

This comment has been minimized.

Copy link
@SuperWig

SuperWig Nov 27, 2018

GitHub isn't the best place to discuss this. Please make a post in the Udemy Q&A along with your code.

{
// incriment the turn number
MyCurrentTry++;

// setup a return variable
FBullCowCount BullCowCount;

// loop through 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++) {
int32 WordLength = MyHiddenWord.length(); // assuming same length as guess

// loop through all letters in the hidden word
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) {
// compare letters against the guess
for (int32 GChar = 0; GChar < WordLength; GChar++) {
// if they match then
if (Guess[GChar] == MyHiddenWord[MHWChar]) {
if (MHWChar == GChar) { // if they're in the same place
Expand Down
2 changes: 1 addition & 1 deletion BullCowGame/FBullCowGame.h
Expand Up @@ -35,7 +35,7 @@ class FBullCowGame
EGuessStatus CheckGuessValidity(FString) const;

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

// ^^ Please try and ignore this and focus on the interface above ^^
private:
Expand Down
8 changes: 5 additions & 3 deletions BullCowGame/main.cpp
Expand Up @@ -53,7 +53,7 @@ void PlayGame()
FText Guess = GetValidGuess();

// submit valid guess to the game, and receive counts
FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);

std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << ". Cows = " << BullCowCount.Cows << "\n\n";
Expand All @@ -65,12 +65,12 @@ void PlayGame()
// loop continually until the user gives a valid guess
FText GetValidGuess()
{
FText Guess = "";
EGuessStatus Status = EGuessStatus::Invalid_Status;
do {
// get a guess from the player
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try " << CurrentTry << ". Enter your guess: ";
FText Guess = "";
std::getline(std::cin, Guess);

// check status and give feedback
Expand All @@ -86,10 +86,12 @@ FText GetValidGuess()
std::cout << "Please enter all lowercase letters.\n";
break;
default:
return Guess;
// assume the guess is valid
break;
}
std::cout << std::endl;
} while (Status != EGuessStatus::OK); // keep looping until we get no errors
return Guess;
}

bool AskToPlayAgain()
Expand Down
Binary file modified Section_02.VC.db
Binary file not shown.

6 comments on commit 775f950

@DeliciousChickenSandwich

Choose a reason for hiding this comment

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

thanks for including this :D

@anxious77
Copy link

@anxious77 anxious77 commented on 775f950 May 14, 2017

Choose a reason for hiding this comment

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

Edit: my fault! I had a scoping mistake.. and didn't remove the declaration of "Status" in the do while loop of GetValidGuess... so I always had a "Invalid_Status" while returning Guess at the bottom of the function.

If I just make a break on default I won't get out of the loop when I enter a correct isogram. It will always ask me for "Please enter Try 1...". And yes I added "return Guess" on and the bottom of the function.

@Cloyde
Copy link

@Cloyde Cloyde commented on 775f950 Oct 20, 2017

Choose a reason for hiding this comment

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

Hi, I made a mistake and I don't understand what it wants me to change, is is saying that it is missing a definition of Guess. The Guess at the bottom is underlined in red. The only thing I had to change was transfer the return Guess; to the bottom.


FText GetValidGuess()
{
EGuessStatus Status = EGuessStatus::Invalid_Status;
do {
FText Guess = "";
// get a guess from the player
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try "<< CurrentTry << ". Enter your guess ";
std::getline(std::cin, Guess);

	// check status and give feedback
	Status = BCGame.CheckGuessValidity(Guess);
	switch (Status) {
	case EGuessStatus::Wrong_Length:
		std::cout << "Plaease enter a " << BCGame.GetHiddenWordLenght() << " letter word.\n";
		break;
	case EGuessStatus::Not_Isogram:
		std::cout << "Please enter a word without repeating letters. \n";
		break;
	case EGuessStatus::Not_Lowercase:
		std::cout << "Please enter all lowercase letters.\n";
		break;
	default:
		
		//assume the guess is valid
		break;
	}
	std::cout << std::endl;
} while (Status != EGuessStatus::OK);	//keep looping until we get no errors
return Guess;

}

Help!

@kuschneider
Copy link

Choose a reason for hiding this comment

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

@Cloyde
Try to declarate Guess outside the do while loop, because you declarated it inside, so it's not valid outside the loop where you try to return its value.

@Cloyde
Copy link

@Cloyde Cloyde commented on 775f950 Oct 20, 2017

Choose a reason for hiding this comment

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

@kuschneider

Jeah I found the conclusion, it was all about the hierarchy, just what you mean I think.

Thx

@sid-mewtwo
Copy link

Choose a reason for hiding this comment

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

I have not made any changes to the code other than what has been in the videos. When I renamed SubmitGuess to SubmitValidGuess, that somehow made the try number stop incrementing. I'm extremely confused as the cause and effect have little to no correlation?

`// receives a VALID guess, increments turn, and returns cout
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
// increment the try number
MyCurrentTry++;

// setup a return variable
FBullCowCount BullCowCount;

// loop through all letters in the guess
int32 WordLength = MyHiddenWord.length();

for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++){

	// compare letters against the hidden guess
	for (int32 GChar = 0; GChar < WordLength; GChar++) {

		// if they match then
		if (Guess[GChar] == MyHiddenWord[MHWChar]) {
			
			if (MHWChar == GChar) { //if they're in the same place
				BullCowCount.Bulls++; //increment bulls
			}
			else {
				BullCowCount.Cows++;	//increment cows
			}
		}
	}
}
return BullCowCount;

}
`
This is the code for submitting a valid guess.

Please sign in to comment.