Bulls and Cows

Hola, im following a tutorial on how to make the bulls and cows game, its my first time using classes and I need a little help.
Im having trouble with the reset function, it doesnt seem to work, it seems as the current try is equal to the max tries so each time i run the program, the game finishes. I tried it without the reset and it works fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include "FBullCowGame.h"

using namespace std;

void printIntro();
void playGame();
string getGuess();
bool askToPlayAgain();

FBullCowGame BCGame;


int main()
{
	bool bPlayAgain = false;
	do 
	{
		printIntro();
		playGame();
		bPlayAgain = askToPlayAgain();
	} while (bPlayAgain == true);
	return 0;
}

void playGame()
{
	BCGame.Reset();
	int MaxTries = BCGame.GetMaxTries();
	for (int i = 1; i <= MaxTries; i++)
	{
		cout << "	Your guess: " << getGuess() << endl << endl;
	}
}
void printIntro()
{
	constexpr int WORD_LENGTH = 4;
	cout << "Guess the " << WORD_LENGTH << " letter word. " << endl << endl;
}
string getGuess()
{
	int CurrentTry = BCGame.GetCurrentTry();
	cout << "Try " << CurrentTry << ". Enter word: ";
	string guess = "";
	getline(cin, guess);
	return guess;
}
bool askToPlayAgain()
{
	cout << "Play again?: ";
	string response = "";
	getline(cin, response);
	return response[0] == 'y' ? true : false;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <string>

class FBullCowGame
{
public:
	FBullCowGame();
	void Reset();
	int GetMaxTries() const;
	int GetCurrentTry() const;
	bool IsGameWon() const;
	bool CheckGuessValidity(std::string);

private:
	int MyMaxTries;
	int MyCurrentTry;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "FBullCowGame.h"


FBullCowGame::FBullCowGame()
{
	Reset();
}

void FBullCowGame::Reset()
{
	int MyMaxTries = 8;
	int MyCurrentTry = 1;
}

int FBullCowGame::GetMaxTries() const
{
	return MyMaxTries;
}

int FBullCowGame::GetCurrentTry() const
{
	return MyCurrentTry;
}

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

bool FBullCowGame::CheckGuessValidity(std::string)
{
	return false;
}
With the code you currently have, I can't see the problem, however, do not use #pragma once, instead of normal header guards. Pragma isn't supported by all compilers, but when an unsupporting compiler sees a pragma statement, it won't throw a syntax error and your program will run, leading to headers being included more than once in the same file.
You don't set MyMaxTries or MyCurrentTry ... despite your obvious intention to do so in Reset.

Remove the int from those, or you will be creating new local variables that only exist for that scope.
If you are following the lesson on Udemy then you can look at the files on GitHub for that lesson. It will probably be one of these.
https://github.com/UnrealCourse/02_BullCowGame/commits/masterbefore=f1d9f1f5af17dc8538f0e4dede2b9e833f9abb40+35
Topic archived. No new replies allowed.