Mastermind COde

I am unsure on how to fill out the rest of the code. I need arrays for the number of tries and spots.

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
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main(){
	int guess;
	srand( time(NULL) );
	int number = rand() % 10000;
	
	cout<< "Welcome to the game of Mastermind. ";
	cout<< "You have 12 chances to guess a combination of ";
	cout<< "4 non-repeating digits from 1 to 8."<<endl;
	
	cout<< "Enter your digits as xxxx. " <<endl;
	cout<< "The computer chose: "<< number <<endl;

	if(guess != number)
		do{
			cout<< "Enter your guess: ";
			cin>> guess;
			cout<<"Correct Digits: ";
			cout<<"Correct Spots: ";
			cout<<"Guesses left: ";
		}while(guess != number);
		
		
	if(number == guess){
		cout<<"you win";
		cout<<"you got it in "<< tries<< "moves";
		}
	else
		cout<<"you lost, the answer was"<<number;
}

int correctdigits(int a[],int g[], int size[]){
	int count = 0;
	for (int i = 0; i < 4; i++){
		for (int j = 0; j < 4; j++){
			if(a[i] == g[i])
				count++;
		}
	}
}


int correctspots(int a[], int g[], int size){
	int count = 0;
		for(int i = 0; i < size; i++){
			if(a[i] == g[i])
				count++;
		}
		return count;
}
1. Explain what the program is supposed to do.
2. What part do you not understand?
3. Explain what you have tried to do for the solution.

I will gladly help if you give more details to understand what the problem is.

In your code the indentation is incorrect here:
1
2
3
4
5
6
7
8
int correctspots(int a[], int g[], int size){
	int count = 0;
		for(int i = 0; i < size; i++){
			if(a[i] == g[i])
				count++;
		}
		return count;
}


it should be:

1
2
3
4
5
6
7
8
int correctspots(int a[], int g[], int size){
    int count = 0;
    for(int i = 0; i < size; i++){
        if(a[i] == g[i])
            count++;
    }
    return count;
}


refer to this article for indentation rules: http://www.cplusplus.com/forum/beginner/65839/
Last edited on
In the classic game of Mastermind, a random set of 4 digits (from 1-6) are selected by the computer. A player will have 12 attempts to guess the pattern of digits. The computer will respond after every guess the user makes--with a number of correct digits that was made, and the number of spots the digits are in.
The player will have 12 attempts to guess the pattern of digits.

I am supposed to record the array of moves the user made. And there is more.

What I did so far is provide a simple structure of a welcome screen, and functions for correct digits and spots but I need help into turning it into a full working code.
Lets do small parts at a time.
1
2
srand( time(NULL) );
int number = rand() % 10000;


This is not going to provide you with a set of 4 digits from 1-6. it would be 0-9999.

a way you could do this is:
1
2
3
4
int num1 = rand() % 5 + 1 // 1 to 6
int num2 = rand() % 5 + 1
int num3 = rand() % 5 + 1
int num4 = rand() % 5 + 1


Each time a guess is incorrect you could make a variable called counter as shown:
1
2
3
4
int totalGuesses = 12;
for(int i = 0; i < totalGuesses; ++i) {
    // logic for each num in here
}
Last edited on
6+1 is 1 to 7 right?
also i dont know how to incorporate guesses into the code. would it go in int main?
also how do i incorporate num1,2,3,4 into 1 actual variable number
for rand reference: http://www.cplusplus.com/reference/cstdlib/rand/
for stringstream: http://www.cplusplus.com/reference/sstream/stringstream/
atoi (attempts to converts char* to int): http://www.cplusplus.com/reference/cstdlib/atoi/



incorporation that you could use a string stream then convert it into a number/string as shown:

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
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {
    stringstream stream1;
    int fullNumber;
    int nums[4] = {};
    srand( time(NULL) );
    nums[0] = rand() % 5 + 1; // 1 to 6
    nums[1] = rand() % 5 + 1;
    nums[2] = rand() % 5 + 1;
    nums[3] = rand() % 5 + 1;
    
    for(int i = 0; i < 4; ++i) {
        stream1 << nums[i]; // put each value into the string stream
    }
    string fullString = stream1.str(); // as a string
    fullNumber = atoi(stream1.str().c_str()); // convert to string then char*
    cout << fullNumber << endl; // print full number for validation.
    return 0;
}
}

Last edited on
Here is a possible solution

also i messed up it should have been for rand()

 
int num = rand() % 6 + 1;


full solution:
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
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {
    stringstream stream1;
    int nums[4] = {};
    string guess;
    int guessedAllowed = 12;
    
    srand( time(NULL) );
    nums[0] = rand() % 6 + 1; // 1 to 6
    nums[1] = rand() % 6 + 1;
    nums[2] = rand() % 6 + 1;
    nums[3] = rand() % 6 + 1;
    
    for(int i = 0; i < 4; ++i) {
        stream1 << nums[i]; // put each value into the string stream
    }
    
    string fullString = stream1.str(); // as a string
    cout << fullString << endl; // remove this later just shows you string
    
    for(int i = 0; i < guessedAllowed; ++i) {
        int correct = 0;
        cout << "Enter a 4 letter key(xxxx): ";
        getline(cin, guess);
        if(guess.size() != 4) { // to only allow 4 characters.
            cout << "Please enter 4 numbers" << endl;
            i--;
            continue; // so it does not count an invalid num
        }
        if(guess[0] == fullString[0]) {
            cout << "First letter correct" << endl;
            correct++;
        }
        if(guess[1] == fullString[1]) {
            cout << "Second letter correct" << endl;
            correct++;
        }if(guess[2] == fullString[2]) {
            cout << "Third letter correct" << endl;
            correct++;
        } if(guess[3] == fullString[3]) {
            cout << "Last letter correct" << endl;
            correct++;
        }
        if(correct == 4) {
            cout << "You win! Exiting." << endl;
            exit(0);
        }
        if(correct == 0) cout << "NONE CORRECT!"<< endl;
    }
    cout << "YOU LOSE D:" << endl;
    return 0;
}
Last edited on
I dont use sstream and string, do you want me to elaborate more on the details of the project?
Yeah sure that would help.
can you open your pms
Topic archived. No new replies allowed.