Assistance in Object Oriented Bulls&Cows game.

Hey guys I have been stuck at a point in this program where I cant figure what to do next.
The homework I was given gave some codes for use to follow on but some of those code dont make sense.
I will attach my CowsBulls.h, CowsBulls.cpp, main.cpp below.
I really dont get how to proceed further.



Below is the headerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include <string>

class BullsAndCows
{
private:
	int fSecretNumbers[9];
	int fBulls;
	int fCows;

public:
	BullsAndCows();

	void start();
	void guess(std::string aNumberString);
	int getBulls() const; //getter for bulls
	int getCows() const; // getter for cows
};


Below is the cpp file for the header file
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
57
58
59

#include "BullsAndCows.h"
using namespace std;

BullsAndCows::BullsAndCows()
{
	fBulls = 0;
	fCows = 0;
}

void BullsAndCows::start()
{
	int i = 9;
	while (i >= 1)
	{
		int j = rand() % i;
		i--;
		swap(fSecretNumbers[i], fSecretNumbers[j]);

	}
}

void BullsAndCows::guess(std::string aNumberString)
{
	fBulls = 0;
	fCows = 0;
	for (int i = 0; i <= 3; i++)
	{
		int lBulls = 0;
		int lCows = 0;
		int lTest = aNumberString[i] - '0';
		if (fSecretNumbers[i] == lTest)
		{
			lBulls = lBulls + 1;
		}
		else
		{
			for (int j = 0; j <= 3; j++)
			{
				if (fSecretNumbers[j] == lTest)
				{
					lCows = lCows + 1;
					break;
				}
			}
		}
	}

}

int BullsAndCows::getCows() const
{
	return fCows;
}

int BullsAndCows::getBulls() const
{
	return fBulls;
}



Below is the Main.cpp file
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

#include "BullsAndCows.h"

#include <iostream>

#include <cstdlib>

using namespace std;

int main()
{
	BullsAndCows lGame;
	bool lPlayAgain = true;

	while (lPlayAgain)
	{
		cout << "New Game" << endl;
		lGame.start();

		string lInput;
		do
		{
			cout << "Make a guess: ";
			cin >> lInput;
			lGame.guess(lInput);

			cout << "Number of bulls: " << lGame.getBulls() << ", number of cows: " << lGame.getCows() << endl;
		} while (lGame.getBulls() != 4);
		cout << "New game, Y/N? ";
		cin >> lInput;
		if (lInput[0] != 'Y')
		{
			lPlayAgain = lInput[0] == 'y';
		}//recheck
	}
	cout << "Game over. Good bye." << endl;

	return 0;
}


Im really confused on how the getCows or getBulls work.
Im really confused on how the getCows or getBulls work.
What is the problem? They return the value of fBulls/fCows defined on line 8/9.

fBulls/fCows are supposed to be set in the member function guess(), but they aren't.
@coder777 (sorry not sure how to directly tag someone in a post or maybe there aren't any)

I'm supposed to get an output looking like this ,
----------------------------------------
New game
Make a guess: 1234
Number of bulls: 0, number of cows: 3
Make a guess: 5678
Number of bulls: 0, number of cows: 1
Make a guess: 2813
Number of bulls: 1, number of cows: 3
Make a guess: 2381
Number of bulls: 4, number of cows: 0
New game, Y/N? n
Game over. Good bye.
-------------------------------------

but my output is ,
----------------------------------
New Game
Make a guess: 1234
Number of bulls: 0, number of cows:0
Make a guess: 8635
Number of bulls: 0, number of cows:0
Make a guess: 1674
Number of bulls: 0, number of cows:0
-------------------------------------
Maybe I couldn't explain clearly about that, sorry not really good at it.
You could say most of the code in here was given inside my homework instructions already but really not sure what im missing. haha well there definitely something wrong because the reply is coming at 0 all the time.
In function guess(...): Remove the variables lBulls/lCows (Note: l) and replace where they are assigned with fBulls/fCows (Note: f)
Your start() method swaps the secret numbers, but you never initialize them. I don't think you really need to swap them at all, just pick random digits for each position.

You also never call srand() to initialize the random number generator (RNG). Without calling srand(), rand() will always return the same sequence of numbers. You should call srand() once at the beginning of main() if you want your game to have random behavior.


Topic archived. No new replies allowed.