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. 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.
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.
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...