Need help with this pls!!!!! Cant figure it out

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string.h>
#include <time.h>

using namespace std;


const int MAXDIGITS = 8;


// picks a number between 1 and "maxValidDigit" (inclusive)
int pickDigit (int maxValidDigit) {

	return 1 + int((rand() / (RAND_MAX + 1.0)) * maxValidDigit);

}


// selects a code (Hint: use pickDigit)
//*************************************
// write function "pickCode" here
//*************************************

// see if the user entered "quit" in any mixture of upper / lower case
bool eqQuit(char guess[]) {
	int i, len = strlen(guess);
	char quit[]= "quit";
	
	// if the length isn't 4, it's not quite
	if (len!=4) {
		return false;
	}
	
	// length is 4, so convert to all lower case and compare to string "quit"
	for (i=0;i<len;i++) {
		guess[i]=tolower(guess[i]);
		// check ith character
		if (guess[i]!=quit[i]) {
			return false;
		}
	}
	
	// must be "quit"
	return true;
}

// returns "true" if guess successfully obtained.
// returns "false" if "quit" entered.
bool readGuess (int numberOfDigits, int guess[], int highestValidDigit) {

	char guessString [MAXDIGITS + 2];
	bool allValid;
	int i;

	for (;;) {

		cout << "Enter guess (\"quit\" to end game): ";
		cin >> setw(MAXDIGITS + 2) >> guessString;
		cin.ignore (INT_MAX, '\n'); // discard reset of input line

		if (eqQuit(guessString)) {

			return false;

		}

		if (strlen(guessString) != (unsigned) numberOfDigits) {

			cout << "*** Invalid code (bad length) ***" << endl;

		} else {

			allValid = true; // make assumption
			for (i = 0; i < numberOfDigits; i++) {
				if (!isdigit(guessString[i])) {
					allValid = false; break;
				}
				guess[i] = guessString[i] - '0';
				if ((guess[i] == 0) || (guess[i] > highestValidDigit)) {
					allValid = false; break;
				}
			}

			if (allValid) {
				return true; // code successfully obtained
			}

			cout << "*** Invalid code (bad digit) ***" << endl;

		}

	}

}


// analyzes the guess by the user to figure out how many blacks and whites
// the user gets to help him/her make a better next guess
//*********************************************
// write function analyzeGuess here
//*********************************************


int main () {

	int digits, highestValidDigit,
	    code[MAXDIGITS], guess[MAXDIGITS], blacks, whites;
	char reply;

   // "seed" the random number generator with the current system
   // time. this ensures that we won't get exactly the same
   // sequnce of "random" numbers every time we run the program.
   srand (time(NULL));

	//****************************************************************************
	// Insert code that reads in the number of digits and the highest valid digit.
	// This code should loop until valid values are entered.
	//****************************************************************************

	do {

        // get the code that the user must guess
		pickCode (digits, code, highestValidDigit);

		/*
		// useful when debugging
		cout << "The code is ";
		for (int i = 0; i < digits; i++) {
			cout << code[i];
		}
		cout << endl;
		*/

		for (;;) {

			if (!readGuess (digits, guess, highestValidDigit)) { // user has quit
                system("PAUSE");
                return 0;
			}

			// work out how many blacks and whites should be displayed
			analyzeGuess (digits, code, guess, blacks, whites);
			
			if (blacks == digits) {
				cout << "Congratulations - you have broken the code!!" << endl;
				break;
			}

			cout << "Blacks : " << blacks << " Whites: " << whites << endl;

		}

		cout << "Do you wish to play again (Y/N): ";
		cin >> reply;
		cin.ignore (INT_MAX, '\n'); // discard rest of input line

	} while (toupper(reply) == 'Y');
	
	system("PAUSE");
	return 0;

}
Last edited on
Cant figure out how to write function analyze guess...
Please wrap your code in [code] tags
@Zaita , i have done that
http://www.cplusplus.com/forum/articles/1295/
Please provide a better description of how your analyse function will work. And what you have attempted so far.
Topic archived. No new replies allowed.