Help with figuring out what's wrong with my game? (Mastermind)

I'm coding up a game called mastermind. It's in a really simple state.
Basically there are four spots. Each spot has a letter assigned to them, and the player guesses the order of the letter of the four spots. On the right, it will show an 'O' if the letter is in the right place, or an 'X' if a letter is in the wrong place.

The game was working fine until I started trying to put a block of code into its own function. I've been looking at this for a while and I can't figure out why it doesn't work.

The problem is, the first guess the player makes will show the Xs and Os accurately. Then any guess the player makes after that will always show all Xs, implying that none of the players guesses match the letter assigned to the board.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
 
using namespace std;
 
string setBoard();
 
int main()
{
    string guess; //the player's guess will be stored
    string spot; //where colors will be randomly assigned
    string chkSpot; //checks how accurate the player's guess is
    char again='y'; //Variable that user input will determine if he plays again
 
    spot=setBoard();
 
    cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
    cout<<"== [ "<<"?"<<" "<<"?"<<" "<<"?"<<" "<<"?"<<" ]";
    cout<<"   -- CHOICES: R B G Y -- "<<endl;
    do{
        cout<<"Enter your guess: ";
        getline(cin,guess);
 
        //Checks for valid input
        while(guess.size()!=4)
        {   cout<<"Invalid input. Please type in four letters"<<endl;
            cout<<"Enter your guess: ";
            getline(cin,guess);
        }
 
    //for loop checks each spot to see how it corresponds with the player's
    //guess, and then will assign O or X to a variable that will output
    //during the game so that the player can know how accurate his guess was
        for (int j=0; j<4; j++)
        {
            (guess[j]==spot[j]) ? (chkSpot[j]='O') : (chkSpot[j]='X');
        }
 
    //if statement checks if user guessed the colors assigned to each
    //spot on the board correctly. If the player is successful, tell em.
    //if not, tell em.
            if (chkSpot[0]=='O'&&chkSpot[1]=='O'&&chkSpot[2]=='O'&&chkSpot[3]=='O')
            {
                cout<<"YOU WIN"<<endl;
 
                cout<<"|| ["<<spot[0]<<"]["<<spot[1]<<"]["<<spot[2]<<"]["<<spot[3]<<"] || ";
                cout<<"== [ "<<chkSpot[0]<<" "<<chkSpot[1]<<" "<<chkSpot[2]<<" "<<chkSpot[3]<<" ]"<<endl;
                cout<<endl;
                cout<<"Would you like to play again on a new board? Enter 'y' for yes, or anything else to quit."<<endl;
                spot=setBoard();
                cin>>again;
            }
            else
            {
 
            cout<<"|| ["<<"?"<<"]["<<"?"<<"]["<<"?"<<"]["<<"?"<<"] || ";
            cout<<"== [ "<<chkSpot[0]<<" "<<chkSpot[1]<<" "<<chkSpot[2]<<" "<<chkSpot[3]<<" ]";
            cout<<"   -- CHOICES: R B G Y -- "<<endl;
            }
 
    }while(again=='y'||again=='Y');
 
    return 0;
}//end of main
 
string setBoard(){
    string spot; //where colors will be randomly assigned
    string chkSpot; //checks how accurate the player's guess is
    int boardSet; //where random number assignment (1-4) will be stored
    int repeat[4]={}; //tests for number repetition
srand(time(NULL));
 
    //for loop for randomizing the board
    for (int i=0; i<4; i++)
    {
        boardSet=(rand() % 4) + 1;
 
        //first if test will test if the random number is identical to any previous random number.
        //if the random number IS equal to any previous ones, it will decrement 'i'
        if ((boardSet==repeat[0])||(boardSet==repeat[1])||(boardSet==repeat[2])||(boardSet==repeat[3]))
        {
            //decrements so that the for loop will run an extra loop so that it can
            //assign a number that hasn't already been taken
            i--;
        }
        //if the random number is unique, it assigns it to a spot on the board
        else{
        //each number 1-4 corresponds to a color that will be assigned to a spot
        //on the board
        switch (boardSet)
        {
            case 1: {
                spot[i]='R'; //Assigns red to a spot
                chkSpot[i]=spot[i]; //Assigns red to check for if the user guessed correctly.
                repeat[i]=boardSet; //assigns number 1 in repeat variable so it can test for future repeats
            break;
            }
            case 2: {
                spot[i]='B'; //Assigns blue to a spot
                chkSpot[i]=spot[i]; //Assigns blue to check for if the user guessed correctly.
                repeat[i]=boardSet; //assigns number 2 in repeat variable so it can test for future repeats
                break;
            }
            case 3: {
                spot[i]='Y'; //Assigns yellow to a spot
                chkSpot[i]=spot[i]; //Assigns yellow to check for if the user guessed correctly.
                repeat[i]=boardSet; //assigns number 3 in repeat variable so it can test for future repeats
                break;
            }
            case 4: {
                spot[i]='G'; //Assigns green to a spot
                chkSpot[i]=spot[i]; //Assigns green to check for if the user guessed correctly.
                repeat[i]=boardSet; //assigns number 4 in repeat variable so it can test for future repeats
                break;
            }
        }//ends switch
        }//ends else statement
    }//ends for loop
    return spot;
}


EDIT: I continued to look at it and I realized that for some reason, the board spots are getting replaced with Xs and Os sometime after the user makes his first guess...
Last edited on
Line 39 results in undefined behavior. chkSpot is an empty string containing no letters, therefore you cannot access or assign to the ith letter of chkSpot as said letter does not exist.

In setBoard you do the same thing with two different variables that are local to that function, chkSpot and spot meaning that lines 96, 97, 102, 103, 108, 109, 114, and 115 result in undefined behavior as you try to access or assign to things that don't exist.

Try initializing those variables with strings of an appropriate length.
The line 39 :
(guess[j]==spot[j]) ? (chkSpot[j]='O') : (chkSpot[j]='X');

Should be :
chkSpot += (guess[j] == spot[j]) ? ('O') : ('X');
Topic archived. No new replies allowed.