Switching between players.

Hello. I'm writing code for what is essentially the nim game, however i cant for the life of me figure out how to get my code to switch between players. I've checked everything over and over, and yet still when i run the program, it just stays on player 1's turn the entire time. Can anyone please help me understand why it won't switch to player 2?




#include <iostream>
#include<time.h>
#include<stdlib.h>
#include<ctime>

using namespace std;
int player=1;

////////////////////////////////
void mainmenu()
{
cout<<"Main menu:"<<endl;
cout<<"1. Play against another person."<<endl;
cout<<"2. Play against computer."<<endl;
cout<<"3. Exit."<<endl;
}
////////////////////////////////
int switchplayer(int player)
{

if (player== 1)
{
player=2;
}
else
{
player=1;
}
return(player);
}
////////////////////




////////////////////


int main(int argc, const char * argv[])
{

int mode; int numstones; int p1=0; int p2=0; int stonestaken;
double cp;


srand(time(NULL));
cout<<"Welcome to the last stone game"<<endl;
mainmenu();
cin>>mode;
while (mode>0 && mode<3)
{
if (mode ==1 )
{
cout<<"1. Play against another person."<<endl;
string pname1; string pname2;
cout<<"Enter name for Player 1"<<endl;
cin>>pname1;
cout<<"Enter name for Player 2"<<endl;
cin>>pname2;
numstones = rand()%(11)+10;
cp= 1;

while (numstones>0)
{
if(cp==1)
{
cout<<"Number of stones: "<<numstones<<endl;
cout<<pname1<<", PLAYER 1's Turn."<<endl;
cout<<"How many stones will you take?"<<endl;
cin>>stonestaken;


}
else
{
cout<<"Number of stones: "<<numstones<<endl;
cout<<pname2<<", PLAYER 2's Turn."<<endl;
cout<<"How many stones will you take?"<<endl;
cin>>stonestaken;
}
numstones= numstones-stonestaken;
cp= player;
}

if (cp ==1)
{
cout<<"Player 2 wins!"<<endl;
}
if (cp ==2)
{
cout<<"Player 1 wins!"<<endl;
}

if (mode==2)
{
cout<<"2. Play against computer."<<endl;
}





}



mainmenu();
}


















return 0;
}
Last edited on
You never change the cp. After player 1 chooses some stones, cp = 2;. Likewise, after player 2 chooses some stones, cp = 1;.

BTW, Nim is supposed to be played with multiple piles. Otherwise the first player can always force a win by simply taking all the stones. (Or taking all but one, if taking the last stone is considered losing.)

The most common variant is three piles, which makes writing code easy:
int numstones[3];

Initialize each pile of stones with some random number, let the player choose both a pile and a number of stones, and display the number of stones in each pile.

Hint, use some functions.

Good luck!
Maybe something isn't working like intended, but there is indeed a statement changing cp.

cp is assigned to the return function player, so shouldn't this change cp?

I even went and started a new line of code to isolate the issue, and the same problem persists.

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
#include <iostream>
using namespace std;
int cm=1;
int mode(int cm)
{
    if (cm==1)
    {
        cm=2;
    }
    if (cm==2)
    {
        cm=1;
    }
    return cm;
}





int main(int argc, const char * argv[]) {
    // insert code here...
    
    
    while (cm>0&&cm<3)
    {
        if (cm==1)
        {
            cout<<"mode 1"<<endl;
            mode(cm);
        }
        else if (cm==2)
        {
            cout<<"mode 2"<<endl;
            mode(cm);
        }
        
        
        
        
        
        
    }
    
    return 0;





}



You have two variables, both named “cm”. Changing the local “cm” in mode() does not change the global “cm”.

Get rid of the mode() function — you don’t need it. Just set the next player after the player selects stones.

Hope this helps.
Line 4,14: mode() is a typed proc. The new value of cm is returned. Since cm is passed by value, the caller's instance of cm is not changed.

Line 30,35: You ignore the new value of cm returned by mode().
Topic archived. No new replies allowed.