How to display if user is correct or close to combination?

I've been working on Mastermind for sometime now. I've gotten teh numbers to display and move up to the next line when the first line is complete. However, I do not know how to display how many the user has gotten right. For example:

7781 **--

the * shows that the user is right and the -- shows that they haven't gotten the other two.

Here is the code that I have so far:

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
#include<iostream>
#include<Windows.h>
#include<time.h>
#include <conio.h>
#include<fstream>


using namespace std;

int get_digit();
void goto_xy(int col, int row);
void hide_ans(int line);
void display_question_marks(int r);  // r --> rows, t --- number of tries



int main()
{
	srand(time(0));
	

	int answer[4], col, guess[4] = {0}, i, rows = 17, tries = 1,Number,num;
	
	


	hide_ans(rows);					
	//	system("COLOR 02");

	display_question_marks(rows);

	col = 20;
	while(tries <= 7)
	{
		for(i = 0; i <4; i++)
		{
			goto_xy(col, rows);
			cin >> guess[i];
			col = col + 5;
		}
		
		
		col = 20;
		rows = rows - 2;
		tries = tries + 1;
		display_question_marks(rows);

	}

		

	
	cout<<"The answer is : "<<endl;	
		
	system("pause");
	
	return 0;
}



void goto_xy(int c, int r)	// Function moves cursor to correct column and row (screen)
{ 

    COORD coord;
    coord.X = c;
    coord.Y = r;
				//Use a built-in function that will use the "coord" coordinates and place the cursor
				// at the requird location on the screen.

     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 

     return;
}
	
int get_digit()				// generates random digits 1 - 9
{
	int num;

	num = rand() % 8 + 1;

	return(num);
}


void hide_ans(int line)			// display title, hides the answer
{								// prints row numnbers  line --> rows
	int col, i, ro;

	col = 20;
	ro = 3;

	goto_xy(25, 0);
	cout << "**Master Mind**\n";
	cout<<"         Guess the correct number sequence to win!"<<endl;
	

	
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3); //replace the number with a number for the color you want
	for (i = 0; i < 4; i++)
	{
		goto_xy(col, ro);
		cout << (char)219;
		col = col + 5;
	}

	for(i = 1; i <= 7; i++)			// prints number of attempts
	{
		goto_xy(15, line);
		cout << i;
		line = line - 2;
	}
	
	return;
}
void display_question_marks(int r) 
{
	int col, i, ro,guess;
		
	col = 15;
	ro = 17;
	
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4); //replace the 0 with a number for the color you want
	col = col + 5;

	for(i = 0; i < 4; i++)		// prints questions marks on current attempt
	{
		goto_xy(col, r);
		cout << "?";
		col = col + 5;

	}
	
		
	return;

	
}
closed account (o3hC5Di1)
Hi there,

In pseudocode:


string output = "";

looping through all values of answer[4]
   if answer[n] == guess[n] then output += "*"
   else output += "-"

show output to screen


Sorry, I'd rather make you think about it than give you the solution.

Do let us know if you require further help.

All the best,
NwN


I have written :

1
2
3
4
5
6
7
if ((answer[i]==guess[i]))
	{
		cout<<"*"<<guess[i]<<endl;
	}
	else  ((guess[i]+='-'));
	{
		}


It slightly works but it displays numbers at the end and doesn't really count how many are right :/. Anyway to fix this?
closed account (o3hC5Di1)
Hi there,

Getting there, very good attempt, here's some corrections:

1
2
3
4
5
6
7
8
9
10
if (answer[i]==guess[i])  //double parenthesis are not necessary in this case
	{
		cout<<"*"<<guess[i]<<endl;  //there's you're number output
	}
         /* in a regular else you don't provide an expression, it simply means
          * "code to do when above if is not true"
	else
	{
		guess[i] = '-';  //so the code to execute should go here.
        } */



If you want to count how many of the answer were right, you would declare a variable that is incremented by one when answer[i]==guess[i] .

Hope that makes sense to you?

All the best,
NwN
Topic archived. No new replies allowed.