Loopy with Loops

So there is an error in my loops :/
basically the code is for a mastermind game, if you can see my error before I find it feel free to help me out! I will be posting the finished version (When it is fixed!)

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>

using namespace std;

int main () {
	int n;
    int m;
    int p;
    int tmp;
    int tmp2;
    char guess[10];
    char code[10];
    
    cout << "Enter your Guess: (Example, Blue Red Yellow Green = BRYG )\n";
	cin >> guess;
    
    cout << "Enter the Code:\n";
    cin >> code;
    
    n = 0;
    m = 0;
    p = 0;
    tmp = 0;
    tmp2 = 0;
    
    
    while (( m <= 3 ) && ( n <= 3 ))
    {
    
        if ( guess[n] == code[m] )
            {
            cout << guess[n] << code[m];
            cout << "Black " << endl;
            n++;
            m++;
            }
        else
            {
            tmp = tmp + n;
            tmp2 = tmp2 + m;
            
            m = 0;
            
            do { if ( guess[n] == code[m] ) {
                        cout << "White ";
                        p++;
                    } else {m++;}
                } while ( (p != 1) && (m < 4) ); 
                
            m = 0;
            n = 0;
            
            n = n + tmp;
            m = m + tmp2;                
            }
        m++;
        n++;
    }

}


Example input : BBRR
Example input : RRBB
Last edited on
What is the actual error you're receiving?
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
#include <iostream>
//only library needed

using namespace std;

int main () {
	int n = 0, m = 0, p = 0, tmp = 0, tmp2 = 0;//declare variables like this 
    char guess[10], code[10];
    
    cout << "Enter your Guess: (Example, Blue Red Yellow Green = BRYG )\n";
	cin >> guess;
    
    cout << "Enter the Code:\n";
    cin >> code;
    
        
    while (( m <= 3 ) && ( n <= 3 ))
    {
        
        if ( guess[n] == code[m] )
        {
            cout << guess[n] << code[m];//why output this?
            cout << "Black " << endl;
            n++;
            m++;
        }
        else
        {
            tmp += n; 
            tmp2 += m;
            
            m = 0;
            
            do { if ( guess[n] == code[m] ) {
                cout << "White ";
                p++;
            } 
            else {m++;}
            }
            while ( (p != 1) && (m < 4) ); 
            
            m = 0;//not sure what this is for
            n = 0;
            
            n = n + tmp;//not sure wha this is for
            m = m + tmp2;                
        }
        m++;//not sure what this could possibly do
        n++;
    }
    return 0; // you forgot this  
}

Enter your Guess: (Example, Blue Red Yellow Green = BRYG )
Blue
Enter the Code:
Blue
BBBlack 
uuBlack 
Okay here is what I have now, thanks for all your pointers ui uiho, perhaps this next version might give a more clear understanding of what I am trying to accomplish:

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
#include <iostream>
using namespace std;

int main () {
	int n;
    int m;
    int p;
    int tmp;
    int tmp2;
    char guess[10];
    char code[10];
    
    cout << "Enter your Guess: (Example, Blue Red Yellow Green = BRYG )\n";
	cin >> guess;
    
    cout << "Enter the Code:\n";
    cin >> code;
    
    n = 0;
    m = 0;
    p = 0;
    tmp = 0;
    tmp2 = 0;
    
    
    do {
    do {
    
        tmp = guess[n];
        tmp2 = code[m];
        
        if (( n == m ) && ( tmp == tmp2))
        {cout << n << m << "Black "; p++;}
        
        if (( n == m) && ( tmp != tmp2))
        {m++;}
        
        if (( n != m) && ( tmp == tmp2))
        {cout << n << m << "White "; p++;}
        
        if (m == 3) { cout << n << m << "None "; p++;}
        else {m++;}

        } while ( p == 0 );
        p = 0;
        m = 0;
        n++;
    } while ( n <=3 );
    
    
    return 0;
}


Example of input and the error being received :)

Enter your Guess: (Example, Blue Red Yellow Green = BRYG )
RBRB
Enter the Code:
BRBR
03White 03None 10White 21White 30White


I feel like I am so so close yet so so far XD
Last edited on
Im not sure how things work on here, but I feel as if my topic may require a bump ^^; feel free to correct me if this is the wrong way to approach these boards...
If you're clear in what the problem is, you generally get a response. You've given an example of input and say there's an error. You don't say what the error is.

What is the expected result for the input you gave?

I looked up the game of Mastermind, so I'm reasonably certain I get what you're trying to do here, but you can't expect people to read your mind and know what your code with such informative names as n, m, p, tmp and tmp2 and no comments is supposed to do.

Presumably code is supposed to represent the pattern you're solving for, and you want to compare guess to code and output the appropriate number of black and white pegs.

I actually found it easier to just code the damn thing than deal with the cryptic variable names.

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 <string>
#include <iostream>
using namespace std;

int main () {

	string guess ;
	string code ;


	cout << "Enter your Guess: (Example, Blue Red Yellow Green = BRYG )\n";
	cin >> guess;

	cout << "Enter the Code:\n";
	cin >> code;


	if ( guess.size() != 4 || code.size() != 4 )
	{
		cerr << "guess and size must be 4 letters in length.\n";
		return 1 ;
	}

	unsigned blackPegs=0 ;  // black pegs indicate correct color and placement.
	unsigned whitePegs=0 ;  // white pegs indicate correct color, wrong placement.

	// true when a black or white peg is used for code[index]
	bool accountedFor[4] = { false, false, false, false } ;

	// two passes simplifies the logic.
	for ( unsigned digit=0;  digit < 4 ;  ++digit )
	{
		if ( code[digit] == guess[digit] )
		{
			++blackPegs ;
			accountedFor[digit] = true ;
		}
	}

	for ( unsigned guessDigit=0;  guessDigit < 4 ;  ++guessDigit )
	{
		for ( unsigned codeDigit = 0;  codeDigit < 4 ; ++codeDigit )
		{
			if (accountedFor[codeDigit])
				continue ;

			if ( guess[guessDigit] == code[codeDigit] )
			{
				accountedFor[codeDigit] = true ;
				++whitePegs ;
				break ;
			}
		}
	}

	cout << blackPegs << " black, " << whitePegs << " white.\n" ;
}

Thank you so much cire. That version seems to work perfectly, and you have greatly helped my understanding! I am sorry about the badly named variables and lack of comments, I will try to improve those things.
Topic archived. No new replies allowed.