2 player NIM c++

So i'm studying for my final and going over problems in the book.
i missed the class so im lost. ive done this far, but i can't really figure out after this.

1. if ( n >=1 || n <=4), else doesnt work. what i expected here is that if i put numbers not between 1<n<4, it would say invalid but, it doesnt.
2. boolean doesnt work. it wouldnt stop at 0 and say you win.
3. and id like to know how to distinguish the winner from player 1 or 2.
thank you for your time :)

Write a two-player version of the game of Nim. In the game, players take turns removing from 1 to 4 sticks from a pile of 13. The player who picks up the last stick wins the game.


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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	const int total = 13;
	int n, mtotal;
	bool winner = false;

	mtotal = total;

while (!winner)
{ if(mtotal >= 0)
 cout << "There are" << mtotal << "sticks" << endl;
 cout << "player 1, pick a number between 1 and 4" << endl;
 cin >> n;
 if ( n >=1 || n <=4)
 {cout << "you have removed" << n << " stick(s)" << endl;
 mtotal -= n;
 }
 else
	 {cout << " invalid number" << endl;}

 
 if(mtotal >= 0)
cout << "There are" << mtotal << "sticks" << endl;
cout << "player 2, pick a number between 1 and 4" << endl;
cin >> n;
if ( n >=1 || n <=4)
 {cout << "you have removed" << n << " stick(s)" << endl;
mtotal -= n;}
 else
{cout << " invalid number" << endl;}


}


if (mtotal=1) 
 {winner = true;
cout <<" player 1 wins" << endl;}

 system("pause");


 
}	
Last edited on
1. iif ( n >=1 || n <=4), else doesnt work. what i expected here is that if i put numbers not between 1<n<4, it would say invalid but, it doesnt.


Thats an "or", you want an "and" there.


2. boolean doesnt work. it wouldnt stop at 0 and say you win.

I don't know what you meant with this, but

 
if (mtotal=1) 


Is always true because '=' is an assignment. You want a comparison ('=='). The assignment will return the value that was assigned - so in this case, 1. In C (and consequently, C++) any number other than 0 is true.
1)

The range for if ( n >=1 || n <=4) is all real numbers. Split it into steps:

1
2
3
4
5
6
7
8
9
10
11
if ( n >= 1 )
{
    cout << "you have removed" << n << " stick(s)" << endl;
    mtotal -= n;
}

 if ( n <= 4 )
{
    cout << "you have removed" << n << " stick(s)" << endl;
    mtotal -= n;
}


This is what your code means. As you can see, any number you can use will trigger the code. Instead, you should use if ( n >= 1 && n <= 4) if you want to check whether or not 'n' is 1, 2, 3 or 4.

Last edited on
2)

if (mtotal=1)
You forgot an = sign.
ty so much :),

thanks to you guys, 1) works. ty and ty :)

for number 2, when the mtotal reaches 0, i want the program to get off the loop and say you win.

im not really sure about boolean. i just copied the form from internet so i dont know if i have a valid statement.

i have changed to if (mtotal==0) and if (mtotal ==1) but no luck so far
Last edited on
mtotal ==0 should work for the check. It's possible that mtotal doesn't actually reach 0 though.
it did work indeed.
ty ty ty!!
gosh ty so much guys
im ready for my test
I'm also trying to complete this exact program for class. But I'm having a difficult time creating it. How did you get the program to stop once player1 or player2 won? It just keeps on going...
Topic archived. No new replies allowed.