Nim Game

After I type in a number, the program outputs the computer and then does nothing else. I am not totally sure what is the problem. I am assuming somewhere in the Nim program of the code
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
#include <string>
#include <iostream>
#include <ctime>
using namespace std;

void Nim();
int PlayerTurn(int);
int ComputerTurn(int);
int SmartComputer(int);

int PlayerTurn(int pileSize)
{
    int userInput = 0;
    bool flag = true;

    while(flag == true)
    {
        cout << "There are " << pileSize << " in the pile" << endl;
        cout << "How many do you want to take? ";
        cin >> userInput;
        if (userInput > 1 && userInput < (pileSize/2))
        {
            pileSize = pileSize - userInput;
            flag = false;
        }
        else
        {
            cout << "Error, that's not a valid move." << endl;
        }
    }
    return pileSize;
}

int Computer(int pileSize)
{
    cout << "The computer will take from the pile. " << endl;
    if (pileSize>63)
    {
        pileSize = 63;
    }
    else if (pileSize>31&&pileSize<63)
    {
        pileSize = 31;
    }
    else if (pileSize>15&&pileSize<31)
    {
        pileSize = 15;
    }
    else if (pileSize>7&&pileSize<15)
    {
        pileSize = 7;
    }
    else if (pileSize>3&&pileSize<7)
    {
        pileSize = 3;
    }
    else
    {
        pileSize = pileSize - rand() % (pileSize/2);
    }
    return pileSize;
}

void Nim()
{
	int Pile=0;
	cout << "Please input an integer greater than 10" << endl;
	cin >> Pile;
    bool turn = rand() % 2;
    if (turn = true)
    {
        cout << "The computer will go first. " << endl;
    }
    else
    {
        cout << "The player will go first. " << endl;
    }
    while(Pile!=1)
    {
		if (turn = true)
		{ 
			Pile=Computer(Pile);
                        turn=false;
		}
        else
        {
            Pile = PlayerTurn(Pile);
            turn=true;
            cout << Pile;
        }
    }
}
int main()
{
    srand(time(NULL));
    Nim();
    return 0;
}
Last edited on
Please edit your post and use code tags - http://www.cplusplus.com/articles/jEywvCM9/
Sorry about that.
a few things. = is an assignment operator. Not the "equal to" operator. More info on operators here - http://www.cplusplus.com/doc/tutorial/operators/

So inside if statements, if (turn = true) You want to check if turn is equal to true, so you need the == , so it looks like if (turn == true);

your main problem is here.

while (Pile != 1); // why is there a semicolon here? Remove it^^
Okay, I did that and changed those to ==. However, now it isn't giving the computer a turn.
Well, you have to think of what's happening inside your loop.

1
2
3
4
5
6
7
8
9
10
11
12
while (Pile != 1)
	{
		if (turn == true)
		{
			Computer(Pile);
		}
		else
		{
			Pile = PlayerTurn(Pile);
			cout << Pile;
		}
	}


Your bool turn, starts with false, and never in that loop do you change it, so it will be false forever and the player will always play. You need to alter it between true and false inside that loop so the computer gets a turn.
I need to set Pile=Computer(Pile). That will change the number of the Pile. However, when it gets down to 3 I can no longer take anything from the pile. Is that when I should end the code and say that player wins?
First of all, in the beginning, you should clarify to the player how many piles he can take, say between 1-5, so they don't start typing 10 or 20 only to try and find out what's an actual valid take.

Your problem is mainly here - if (userInput > 1 && userInput < (pileSize / 2))

say pileSize is equal to 3. And we want to take 2 from the piles so we can win, heck even 1 who cares, point is, read the condition.

userInput has to be bigger than 1 and at the same time less than pileSize/2. We said pileSize was 3. 3/2 is 1.5.

Numbers bigger 1 are as follows: 2,3,4,5 etc. 1.5 is not an option.

Edit: You're over complicating this little game. You might want to rethink the logic.
Last edited on
Topic archived. No new replies allowed.