Guessing game multiplayer

I wrote the code for many mini games and they all work fine, except for the one, where a player plays against another player.
I removed all of the code for the other mini games.
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <sstream>
#include <windows.h>

void playervsplayer();
void ClearScreen();

int main()
{
    srand(static_cast<unsigned int>(time(NULL)));
    bool running = true;
    int menuchoice, singleplayer, multiplayer;
    std::string input;

    do
    {
        while(true)
        {
            ClearScreen();

            std::cout << ":: Enter an option ::" << "\n\n";
            std::cout << "1. Single Player game" << "\n";
            std::cout << "2. Two Player game" << "\n";
            std::cout << "3. Automated game" << "\n";
            std::cout << "4. Exit" << "\n\n";
            getline(std::cin, input);
            std::stringstream myStream(input);
            if (myStream >> menuchoice)
            break;
            std::cout << "Please enter a valid number" << "\n";
            std::cout << "Press Enter to continue..." << "\n";
            std::cin.clear();
            std::cin.ignore(10000, '\n');
        }

        switch(menuchoice)
        {
            case 2:
                while(true)
                {
                    ClearScreen();
                    std::cout << "Choose who would you like to play against:" << "\n";
                    std::cout << "1. Your friend" << "\n";
                    std::cout << "2. The computer" << "\n";
                    getline(std::cin, input);
                    std::stringstream myStream(input);
                    if (myStream >> multiplayer)
                    break;
                    std::cout << "Please enter a valid number" << "\n";
                    std::cout << "Press Enter to continue..." << "\n";
                    std::cin.clear();
                    std::cin.ignore(10000, '\n');
                }

                switch(multiplayer)
                {
                case 1: playervsplayer(); break;
                
                default:
                std::cout << "Please choose one of the two games" << "\n\n";
                std::cin.clear();
                std::cin.ignore(10000, '\n');
                break;
                }
            break;

            case 4: running = false; break;

            default:
            std::cout << "Please choose one of the 3 types of games" << "\n\n";
            std::cin.clear();
            std::cin.ignore(10000, '\n');
            break;
        }
    } while (running);

    return 0;
}

void playervsplayer()
{
    std::string input, player1, player2;
    unsigned short player1guess, player2guess, gumballs = 0, turn = 0;
    gumballs = rand() % 1000 + 1;

    ClearScreen();
    std::cout << "Player 1, please enter your name: ";
    getline(std::cin, player1);
    std::cout << "\n" << "Welcome, " << player1 << "." << "\n\n";

    std::cout << "Player 2, please enter your name: ";
    getline(std::cin, player2);

        while (player2 == player1)
        {
            std::cout << "Player 1 has already chosen this name!" << "\n";
            std::cout << "Player 2, please enter your name: ";
            getline(std::cin, player2);
        }

    std::cout << "\n" << "Welcome, " << player2 << "." << "\n\n";

    std::cout << "In this game there is a gumball jar, which can hold up to 1000 gumballs." << "\n";
    std::cout << "Each player will have their turns to guess the amount of gumballs." << "\n";
    std::cout << "The player who gets it right first, wins!" << "\n\n";

    std::cout << "Let's begin!" << "\n\n";
    std::cout << "Press any key to continue..." << "\n";
    std::cout << "(Now where's the 'any' key?)" << "\n\n\n";
    std::cin.clear();
    std::cin.ignore(10000, '\n');

    std::cout << "Let's see who shall start... ";
    turn = rand() % 2 + 1;

    while (turn == 1)
    {
        std::cout << "It's " << player1 << "'s turn!" << "\n";
        while(true)
        {
            std::cout << "Enter your guess: ";
            getline(std::cin, input);
            std::stringstream myStream(input);
            if (myStream >> player1guess)
            break;
            std::cout << "Invalid number, please try again" << "\n";
        }

        if (player1guess > gumballs)
        {
        std::cout << "Nope, there are less!" << "\n\n";

        turn = 2;
        }

        else if (player1guess < gumballs)
        {
        std::cout << "Nope, there are more!" << "\n\n";
        turn = 2;
        }

        else
        {
        std::cout << "Hooray! You got it! You guessed the right amount of gumballs!!!" << "\n";
        std::cout << "Congratulations, " << player1 << ", you win!";
        std::cin.clear();
        std::cin.ignore(10000, '\n');
        }
    }

    while (turn == 2)
    {
        std::cout << "It's " << player2 << "'s turn!" << "\n";
        while(true)
        {
            std::cout << "Enter your guess: ";
            getline(std::cin, input);
            std::stringstream myStream(input);
            if (myStream >> player2guess)
            break;
            std::cout << "Invalid number, please try again" << "\n";
        }

        if (player2guess > gumballs)
        {
        std::cout << "Nope, there are less!" << "\n\n";
        turn = 1;
        }

        else if (player2guess < gumballs)
        {
        std::cout << "Nope, there are more!" << "\n\n";
        turn = 1;
        }

        else
        {
        std::cout << "Hooray! You got it! You guessed the right amount of gumballs!!!" << "\n";
        std::cout << "Congratulations, " << player2 << ", you win!";
        std::cin.clear();
        std::cin.ignore(10000, '\n');
        }
    }
}

void ClearScreen()
{
 //.. Let's save some space, shall we :P
}


My problem is that once a player guesses a number it either goes back to the main menu, or it let's the second player take a guess and then goes back to the main menu. This problem persists in both the full code and in this example.

I think that somehow the compiler is ignoring the while (turn == 1) and the while (turn == 2) loops.

P.S. I used this article for the getline() methods: http://www.cplusplus.com/forum/articles/6046/
Last edited on
Hi Vidminas
I don't have a Windows box to compile this on but it looks like a basic looping problem in your playervsplayer function.
I assume this line chooses either 1 or 2 at random and stores it in the variable, turn:
turn = rand() % 2 + 1;

Now see what follows:
1
2
3
4
5
6
7
8
while (turn == 1)
{
    // one turn only for player 1 - if they guess wrong, turn = 2
}
while (turn == 2)
{
    // one turn only for player 2 - if they guess wrong, turn = 1
}


Those while loops will execute at most once. If player 2 goes first, player 1 won't even get a look in.
You need an outer loop surrounding that whole routine. Something like:
1
2
3
4
5
6
7
8
bool playing_game = true;
turn = rand() % 2 + 1;
while ( playing_game )
{
    while (turn == 1)
    {
    // etc.
}


When a player wins the game, set playing_game to false to exit the outer loop.
Hope that helps.
Last edited on
Vidminas, don't even need while loops for those, just if statements inside a loop like keineahnung said.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool playing_game = true;
turn = rand() % 2 + 1;
while ( playing_game )
{
    if (turn == 1)
    {
        //etc.
    }
    else //turn == 2
    {
        //etc.
    }
}


I think you also need std::cin.clear(); after these lines:

1
2
3
if (myStream >> player1guess)
    break;
std::cout << "Invalid number, please try again" << "\n";

I already had a do { } while (running); condition for my main menu, so instead i changed the while loops to if statements like you told me, then I put everything into a while(true) loop and added break; in case someone won.
It works now like it should :)
Topic archived. No new replies allowed.