Pick up sticks game!

Hello there! So I've recently made a pick up stick game and Im pretty lost. The code keeps going past 0 and I'm not sure how to stop it.. Im also trying to say that whoever gets the last stick wins, but I am not sure how to go about that.

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
  //
//  daniel03123.cpp
//  
//
//  Created by Daniel Dalisay on 10/14/15.
//
//

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int total = 21;
    int n, remainder;
    bool winner = false;
    
    remainder = total;
    
    while (!winner)
    {
        if (remainder >= 0)
        cout << "There are " << remainder << " stone(s)." << endl;
        cout << "Player 1, pick a number between 1 and 3." << endl;
        cin >> n;
        if ( n > 0 && n < 4)
        {
            cout << "You have removed " << n << " stone(s)." << endl;
            remainder -= n;
        }
        else
        {
            cout << "Invalid number" << endl;
            cout << "Please enter a number between 1 and 3." << endl;
            cin >>n;
            cout << "You have removed " << n << " stone(s)." <<endl;
            remainder -=n;
        }
        
        
        if (remainder >= 0)
            cout << "There are " << remainder << " stone(s)." << endl;
            cout << "Player 2, pick a number between 1 and 3." << endl;
            cin >> n;
        if ( n > 0 && n < 4)
        {
            cout << "You have removed " << n << " stone(s)." << endl;
            remainder -= n;}
        else
        {
            cout << "Invalid number" << endl;
            cout << "Please enter a number between 1 and 3." << endl;
            cin >>n;
            cout << "You have removed " << n << " stone(s)." <<endl;
            remainder -=n;
        }
        
    }
    return 0;
    
}
Last edited on
Hi,

You didn't change the value of winner so it keeps looping :+)

Also, you probably want some braces around lines 24 to 26 and 43 to 45, but then consider what happens if that condition is not true. You may need to reorganise things a bit to get the logic right - you haven't specified what happens when remainder is less than zero, or equal to zero for that matter.

Good Luck !!
Thanks! Got some of those fixed. Reason why I didn't put what happens when remainder is 0 was cause Im pretty stumped on how to indicate which player won :c
Yep I'm stumped. Still not sure why it keeps looping. If it doesn't loop, it goes past 0 for some reason.
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
//
//  daniel03123.cpp
//  
//
//  Created by Daniel Dalisay on 10/14/15.
//
//

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int total = 21;
    int n, remainder;
    bool winner = false;
    
    remainder = total;
    
    while (!winner)
    {
        if (remainder >= 0)
        {
            cout << "There are " << remainder << " stone(s)." << endl;
            cout << "Player 1, pick a number between 1 and 3." << endl;
            cin >> n;
        }
            if ( n > 0 && n < 4)
            {
                cout << "You have removed " << n << " stone(s)." << endl;
                remainder -= n;
            }
            else
            {
                cout << "Invalid number" << endl;
                cout << "Please enter a number between 1 and 3." << endl;
                cin >>n;
                cout << "You have removed " << n << " stone(s)." <<endl;
                remainder -=n;
            }
            if (remainder >= 0)
            {
                cout << "There are " << remainder << " stone(s)." << endl;
                cout << "Player 2, pick a number between 1 and 3." << endl;
                cin >> n;
            }
            if ( n > 0 && n < 4)
            {
                cout << "You have removed " << n << " stone(s)." << endl;
                remainder -= n;}
            else
            {
                cout << "Invalid number" << endl;
                cout << "Please enter a number between 1 and 3." << endl;
                cin >>n;
                cout << "You have removed " << n << " stone(s)." <<endl;
                remainder -=n;
            }
            if (remainder == 0)
            {
                winner = true;
                cout << "Player 1 wins!";
            }
    }
    return 0;
    
}
Also not sure how to indicate which player has won. Maybe I need more variables o_O
Last edited on
Topic archived. No new replies allowed.