How am I doing?

Hello. I am trying to learn c++, and the first project I decided to do on my own now functions as I intend. (A simple mastermind program) I would love it if you guys could look over the code and tell me if I am headed in the right direction. Better to learn what's wrong before it becomes habit right?

So any improvements you can suggest (That aren't too complicated at least) would be great. Thanks!

I divided my project into 3 files for some reason, but I will put them all together into one and comment out the #include's that don't make sense to post it here.

Rules are same as regular Mastermind, but with numbers 1-6 instead of colors. Scores print to the right. B is the same as black peg in the original game (right place, right color), and W is same as white (right color, wrong place).

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Mastermind.h

class Mastermind
{
	int maxGuesses;
	int guesses;
	int solution[4];
	int guess[4];
	int scoreW;
	int scoreB;
	int* board_Guesses;
	char* board_Scores;

public:
	
	bool isPlaying;
	Mastermind (int guessLimit);
	~Mastermind ();
	void GetInput ();
	void ScoreCheck ();
	int Print ();
	void Record ();
};

//main.cpp

#include <iostream>
//#include "Mastermind.h"

int main (void) {
	using std::cout;
	using std::cin;
	int temp;
	cout << "How many guesses would you like?\n";
	cin >> temp;
	Mastermind game(temp);
	game.Print();

	while (game.isPlaying) {
		game.GetInput();
		game.ScoreCheck();
		game.Record();
		game.Print();
	}
}

//Mastermind.cpp

//#include "Mastermind.h"
//#include <iostream>
#include <cstdlib>
#include <ctime>

void Mastermind::GetInput () { 
	using std::cout;
	using std::cin;
	
	do {
		cout << "Type your guess (4 digits seperated by spaces. 1-6)\n";
		cin >> guess[0] >> guess[1] >> guess[2] >>guess[3];
	} while ((guess[0] < 1 || guess[0] > 6) || //Make sure guess is valid
			 (guess[1] < 1 || guess[1] > 6) ||
			 (guess[2] < 1 || guess[2] > 6) ||
			 (guess[3] < 1 || guess[3] > 6));
	guesses++;
}

void Mastermind::ScoreCheck () {
	bool matchedS[4];
	bool matchedG[4];
	scoreB = 0;
	scoreW = 0;
	
	for (int i=0; i<4; i++) { //Check scoreB first, since it takes precedence over W
		if (guess[i] == solution[i]) {
			scoreB ++;
			matchedS[i] = 1;
			matchedG[i] = 1;
		}
		else {
			matchedG[i] = 0;
			matchedS[i] = 0;
		}
	}
	for (int i=0; i<4; i++) {
		if (matchedS[i]) continue; //Skip checking solution[i] if it is already accounted for
		for (int n=0; n<4; n++) {
			if (matchedG[n]) continue; //Skip checking guess[n] if it is already accounted for
			else if (solution[i] == guess[n]) {
				scoreW ++;
				matchedS[i] = 1;
				matchedG[n] = 1;
				break; //Stop checking guess[n] once a match is found for it
			}
		}
	}
}

void Mastermind::Record () {
	for (int g=0; g<4; g++)
		board_Guesses[g + 4 * (guesses - 1)] = guess[g];
	for (int b=0; b<scoreB; b++) //Add "B" scores first.
		board_Scores[b + 4 * (guesses - 1)] = 'B';
	for (int w=scoreB; w<scoreB+scoreW; w++) //...Followed by "W" Scores.
		board_Scores[w + 4 * (guesses - 1)] = 'W';
}

int Mastermind::Print () {
	using std::cout;
	
	for (int i=maxGuesses; i>0; i--) {
		if (i < 10) cout << " " << i << "|| ";
		else cout << i << "|| ";
		for (int c=0; c<4; c++)
			cout << board_Guesses[(i-1) * 4 + c] << " ";
		cout << "||";
		for (int c=0; c<4; c++)
			cout << board_Scores[(i-1) * 4 + c];
		cout << "\n";
	}
	if (scoreB == 4) {
		cout << "You Won!!!!\n" << "It took you: " << guesses << " Guesses";
		isPlaying = 0;
		return (0);
	}
	else if (guesses >= maxGuesses) {
		cout << "\nYou Lost :(\n" << " The solution was: ";
		for (int s=0; s<4; s++)
			cout << solution [s] << " ";
		isPlaying = 0;
		return (0);
	}
}

Mastermind::Mastermind  (int guessLimit) : maxGuesses(guessLimit) {
	using std::rand;
	using std::srand;
	using std::time;
	
	isPlaying = 1;
	guesses = 0;
	board_Guesses = new int[maxGuesses*4];
	board_Scores = new char[maxGuesses*4];
	srand(time(0));
	
	for (int i = 0; i < 4; i++) 
		solution[i] = rand() % 6 + 1;
}

	Mastermind::~Mastermind() {
	delete[] board_Guesses;
	delete[] board_Scores;
}



It works, so obviously I succeeded on some level. If there is anything glaringly wrong with my code, please point it out. Also was my use of classes acceptable? Should I have used more individual classes? Any other comments/criticisms?

Thanks!

PS I know that getting input could be done much better, but I didn't want to fiddle with that part anymore. :p

Also I was going to use multidimensional arrays to store the board/scores, but after a bit of googling came to the conclusion that dynamic multidimensional arrays bring more complications than I wanted to deal with.
Last edited on
Topic archived. No new replies allowed.