Guessing Game

Hi there. So I am supposed to write a program that plays a guessing game with the user. There are three pegs which the computer generates a 'colour' for (number from 1-5) and the user has to guess the correct colour for each peg. The colours that the computer generates cannot be the same (example: both peg 1 and 2 cannot have the same colour of 3). after the user guesses the program outputs how many pegs matched and how many colours matched (max of 3 each, and if all three pegs are correct, then all three colours are as well).

my code is always letting me win on the first try with whatever numbers I input and it's telling me that I have twice as many colours that are correct.

here is what the program is currently giving me:

~~~~~Mastermind~~~~~
Guess each pegs colour correctly to win!
Guess 1 - 5.

Guess 1
First peg: 1

Second Peg: 3

Third Peg: 5

You have 3 correct peg(s) and 6 correct colour(s)
You have broken the code in 1 guesses!Program ended with exit code: 0

If anyone could help point out what I might be missing that would be a huge help!!

Here is my code:
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
  // This program will play mastermind. A guessing game where the user gueses the colours of different pegs


#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
int firstpeg, secondpeg, thirdpeg;

// Generates random numbers used for the colour
int randomcolour()
{
    int a;
    srand(time(NULL));
    a = rand() % 5 + 1;
    
    return a;
}

// Checks if the guesses match the pegs
int checkpegs(int a, int b, int c)
{
    int points;
    
    if (a == firstpeg)
    {
        points+=1;
    }
    if (b == secondpeg)
    {
        points+=1;
    }
    if (c == thirdpeg)
    {
        points+=1;
    }
    
    return points;
}

// Checks if the colours match the pegs
int checkcolours(int a, int b, int c)
{
    int colour;
    
    if (a == firstpeg || a == secondpeg || a == thirdpeg)
    {
        colour+=1;
    }
    if (b == firstpeg || b == secondpeg || b == thirdpeg)
    {
        colour+=1;
    }
    if (c == firstpeg || c == secondpeg || c == thirdpeg)
    {
        colour+=1;
    }
    
    return colour;
}

int main()
{
    cout << "~~~~~Mastermind~~~~~\nGuess each pegs colour correctly to win!\n Guess 1 - 5.\n\n";
    int count, win, colour;
    
    int peg1, peg2, peg3;
    peg1 = randomcolour();
    do
    {
        peg2 = randomcolour();
    } while (peg2 == peg1);
    do
    {
        peg3 = randomcolour();
    } while (peg3 == peg1 || peg3 == peg2);
    
    do
    {
        count++;
        
        cout << "Guess " << count << endl;
        cout << "   First peg: ";
        cin >> firstpeg;
        cout << "\n Second Peg: ";
        cin >> secondpeg;
        cout << "\n Third Peg: ";
        cin >> thirdpeg;
        
        win = checkpegs(firstpeg, secondpeg, thirdpeg);
        colour = checkcolours(firstpeg, secondpeg, thirdpeg);
        
        cout << "\nYou have " << win << " correct peg(s) and " << colour << " correct colour(s)";
        
    } while (win != 3);
    
    cout << "\nYou have broken the code in " << count << " guesses!";
        
    return 0;
}
closed account (NUj6URfi)
Try colour = 0 when initializing it
this sort of fixed it.... it says you have 3 correct colours now... but I still win the first guess
but I still win the first guess

Are you confusing firstpeg, secondpeg, thirdpeg (why are these global?) and peg1, peg2, peg3?

As you're passing firstpeg, secondpeg, thirdpeg to checkcolours(), it seems you're comparing the guesses with themselves, rather than against the random colors (peg1, peg2, peg3.)

(Maybe renaming firstpeg, etc to guess1, etc would probably make things easier to follow? And changing a, b, c to clearer names definitely would. Variable names are important!)

And:
- colour isn't the only variable you need to 0 before use. As a general rule, you should initialize all variable before first use.
- and you should be calling srand() once at the start of a program, not every time you want a random number (see my following post.)

Andy

PS It might help if you up the warning level on your compiler!?
Last edited on
If the code below generates the random peg "colors" in the way you do (calling srand(time(NULL)) before rand each time) then it takes 11421077 calls to randomcolour() to get 3 unique colors.

call 1 of randomcolour() returned 1
call 2164159 of randomcolour() returned 4
call 11421077 of randomcolour() returned 2
pegs = 1, 4, 2


If you use the alternative code path (#define CALL_SRAND_ONCE so that srand(time(NULL)) is called once at program startup), then it's 3 calls:

call 1 of randomcolour() returned 2
call 2 of randomcolour() returned 3
call 3 of randomcolour() returned 5
pegs = 2, 3, 5


You see, every time you call srand() with the same seed it starts the pseudo random sequence from the same point. And as processors work very quickly, they will make lots of calls per second (which is the precision of the value returned by time()) So your randomcolour() function will return the same value for up to a whole second.

(Actually, 11421077 calls is bit of an underestimate as the debug output would have slowed things down a bit.)

Andy

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 <ctime>
#include <cstdio>
#include <cstdlib>

using namespace std;

//#define CALL_SRAND_ONCE
#define DEBUG_CODE

// Generates random numbers used for the colour
int randomcolour()
{
#ifdef DEBUG_CODE
    static int callcount = 0;
    static int lastuniquenumber = 0;
#endif
    int a;
#ifndef CALL_SRAND_ONCE
    srand(time(NULL));
#endif
    a = rand() % 5 + 1;
#ifdef DEBUG_CODE
    ++callcount;
    if(lastuniquenumber != a)
    {
        cout << "call " << callcount << " of randomcolour() returned "
             << a << "\n";
        lastuniquenumber = a;
    }
#endif
    
    return a;
}


int main()
{
#ifndef CALL_SRAND_ONCE
    srand(time(NULL));
#endif

    int peg1, peg2, peg3;
    peg1 = randomcolour();
    do
    {
        peg2 = randomcolour();
    } while (peg2 == peg1);
    do
    {
        peg3 = randomcolour();
    } while (peg3 == peg1 || peg3 == peg2);

    cout << "pegs = " << peg1 << ", " << peg2 << ", " << peg3 << "\n";

    return 0;
}
Last edited on
Topic archived. No new replies allowed.