Why wouldn't my code run?

I am making a mastermind game that will have a user input a password or have the computer randomly generate it, it will then try to guess at the answer, and return how many tries it took. Below is my code up to this point. The problem is that my code would not run. What can I do to fix it. Thank you for your help


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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
using namespace std;

int generateAnswer();
void trim (int guesses[], int guess, int &length, int black, int white);
void evaluate( int guess, int answer, int &black, int &white);
int nthDigit( int combination, int position );			// identify a digit
void clearNthDigit( int &combination, int position );	// clear a digit
int methodicalEliminate (int answer);
char play;
char response;
int answer;
const int size = 4096;

int main() // Plays the game
{
	play = 'y';
	while (play == 'y')
	{
		int answer = generateAnswer();
		cout<<"It took methodicalEliminate "<< methodicalEliminate(answer)<<" tries";
		cout<<"Would you like to plasy Mastermind? ";
		cin >> play;
	}

return 0;
}

int generateAnswer() 
/*
Either lets the user at the keyboard choose the mystery combination,
or gives the option to have the computer generate a random combination.
*/

{ 
	cout<<"Do you want to create the mystery combination? Respond with y for yes or n for computer."<<endl;
    cin>>response;
    if(response=='y'){
        cout<<"Enter your combination.(4 digits ranging from 1-8.)"<<endl;
        cin>>answer;
    }
	else{
        answer=1000*(1+rand()%8)+100*(1+rand()%8)+10*(1+rand()%8)+1*(1+rand()%8);
        cout<<"The combination that is trying to be guessed is "<<answer;
    }
    return answer;
}

int generateSearchSpace(int guesses[], int& length)
/*
Populates an array with all possible combinations of four-digit
values in the range 1 to 8.
*/
{
	int a = 0;
	
		for (int b = 1; b <=8; b++)
			for (int c = 1; c <=8; c++)
				for (int d = 1; d <= 8; d++)
					for (int e = 1; e <=8; e++)
					{
					guesses[a] = 1000*b + 100*c + 10*d + 1*e;
					a++;
					}
		return length = a;
}

void trim (int guesses[], int guess, int &length, int black, int white)
/*
Trim function analyzes the response to a particular guess and then eliminates
any values from the list of possibilities that are no longer
possible answers.
*/
{
		int newLength = 0;
        int trimBlack = 0;
        int trimWhite = 0;
 
        for(int i = 0; i < length; i++)
        {
 
                evaluate(guesses[i], guess, trimBlack, trimWhite);
                if(trimBlack==black&&trimWhite==white)	// If they are matched, keep them because one of them could be the right answer.
				{
				guesses[newLength]=guesses[i];
				newLength++;
				
				}
		length=newLength;		
				
			
		}		
				
}				
				
int methodicalEliminate (int answer)
/*
beginning with a list of all possible candidate answers
continually guesses the first element in the list, and
trim answers accordingly, until an answer is found
*/
{
	int guesses[size];
    int length = 0;
    int black = 0;
    int white = 0;
    int count = 0;
    int currentGuess;
    generateSearchSpace(guesses, length);
    while(black< 4)
    {       
		currentGuess=guesses[0];
        evaluate(currentGuess,answer,black,white);
		for(int i = 0; i < length; i++)
        {
            trim(guesses, currentGuess, length, black, white);
            
        }
        count++;
    }
	return count; // returns how many times did this function take to guess the right answer.
}


void evaluate( int guess, int answer, int &black, int &white )
/*
evaluates a combination by comparing it with the answer
Correctness is indicated by black pegs (correct digit in correct position)
and white pegs (correct digit in incorrect position)
*/
{
	black = 0;			
	white = 0;
	for (int i=1; i<=4; i++)
		if (nthDigit( guess, i ) == nthDigit( answer, i ) )
		{
			black++;						
			clearNthDigit( guess, i );		
			clearNthDigit( answer, i );
		}
	for (int i=1; i<=4; i++)
		for (int j=1; j<=4 && nthDigit(guess,i)>0; j++)
											
			if (nthDigit( guess,i ) == nthDigit( answer, j ) )
			{
				white++;					
				clearNthDigit( guess, i );  
				clearNthDigit( answer, j );
			}
}
		
int nthDigit( int combination, int position )
/*
identified the n'th digit of a combination

*/

{
	int current;			
	while (position < 4)
	{
		combination /= 10;		
		position++;				
	}

	return (combination % 10);

}

void clearNthDigit( int &combination, int position )
/*
clears the n'th digit of a combination to zero, so it will no longer match
digits must be counted in the same manner as nthDigit above.
*/
{
	int current;			// position currently being examined
	int digitValue;			
	current = 4;			
	digitValue = 1;			
	while (position < current)
	{
		digitValue *= 10;	// previou digit has 10 times the value
		current--;			// digitValue refers to previous digit
	}
	// subtract the digit minus itself to make it into a zero digit
	combination -= digitValue * nthDigit( combination, position );
}
What do you mean "it won't run"?

I was able to compile and execute your code.
Whether it is doing what you intended I do not know.

Have you tried using a debugger?
Or placing cout statements in your program at key points to show what it is doing?
When you run, it would not do anything. It's supposed to return how many tries it took for methodicalEliminate to guess the answer. However, in my case, it would only say "Give me the number. And Guessing 2222 e.g"
Your program gets stuck in the while loop in this function.

If it is stuck in this loop it must mean black is always less than 4.

Two One reason why.

1 - In evaluate() black and white are always set to 0. Edit actually this code is needed
1 - In methodicalEliminate() you check only the first guess currentGuess=guesses[0];


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
int methodicalEliminate (int answer)
/*
beginning with a list of all possible candidate answers
continually guesses the first element in the list, and
trim answers accordingly, until an answer is found
*/
{
	int guesses[size];
    int length = 0;
    int black = 0;
    int white = 0;
    int count = 0;
    int currentGuess;
    generateSearchSpace(guesses, length);
    while(black< 4)
    {       
		currentGuess=guesses[0];
        evaluate(currentGuess,answer,black,white);
		for(int i = 0; i < length; i++)
        {
            trim(guesses, currentGuess, length, black, white);
            
        }
        count++;
    }
	return count; // returns how many times did this function take to guess the right answer.
}
Last edited on
Topic archived. No new replies allowed.