Having trouble alternating users in chip game.

Hey, I am having trouble alternating users when I ask them to take chips from the pile. The program compiles and runs, it is just not near complete. I'm just stuck.

It seems to only ask player 2 to take chips, it needs to ask player 1, then player 2, then player 1 again, and so on.

You can find the section of code that needs help in the function "int ask_move(bool player1turn, int chips_in_pile)"

Thanks!
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  /* Game of Chips */

#include <iostream>
using namespace std;

int init_game(); 
int ask_move(bool, int);
bool is_move_legal(int, int); 
void declare_winner(bool);

int main() 
{ 
	bool player1turn = true; // This variable keeps track of whose turn it is. // When true it is player 1’s turn and when // false it is player 2’s turn.
	bool game_over = false; // will be set to true once the chips are gone. int chips_in_pile; int chips_taken;
	
	int chips_in_pile = init_game();
	
	while (game_over == false) 
	{ 
	
		int chips_taken = ask_move(player1turn, chips_in_pile);		
		chips_in_pile = chips_in_pile - chips_taken;
		
		if (chips_in_pile == 0) 
		{ 
			game_over = true; 
			declare_winner(player1turn); 
		} 
		else 
		{ 
			cout << "There are " << chips_in_pile << " chips left." << endl << endl;
			player1turn = !player1turn; 
		}
		
	} 
	
	return(0);
}



void declare_winner(bool player1turn)
{  
/* This function is called by the main program once the game is over.

It outputs a message congratulating the winning player.

Nothing is returned by this function. 
*/
}



bool is_move_legal(int chips_taken, int chips_in_pile) 
{ 
/* This function is called by the ask_move function.

It looks at the number of chips in the pile and the number of chips the player wants to take and 
determines if the proposed move is legal. If the proposed move is legal this function will return true
and if the proposed move is not legal this function will return false.

Remember, a player must take at least 1 chip and no more than half of the chips remaining in the pile 
unless the pile has only one chip left in which case the only legal move is to take the one remaining 
chip. 
*/ 
}


int ask_move(bool player1turn, int chips_in_pile)
{ 
/* This function is called by the main program.
	
It asks the correct player how many chips she would like to take. 
Then it calls is_move_legal which determines if the proposed move is legal. 
If the proposed move is NOT legal then it will print out a message saying the move is not legal
and it will ask repeatedly until the player enters a legal move.
 
Once a legal move is entered this function returns the number of chips taken. 
*/
	
	int chips_taken;
	int game_over;
	while(chips_taken)
	
	{
	cout<<"Player 1, how many chips would you like to take?";
	cin>>chips_taken;
	}
	
	{
	cout<<"Player 2, how many chips would you like to take?";
	cin>>chips_taken;
	}	
return chips_taken;
 }


 
int init_game() 
{ 
/* This function is called by the main program.

It asks the user how many chips are in the pile at the start of the game. 
If the user enters a number less than 2 or greater than 50 it will tell him that he must enter a number between 2 and 50.

Once a valid number of chips is entered the function will return the number of chips. 
*/ 

int chips_in_pile;

int redo = (chips_in_pile<=1 || chips_in_pile>=51);
   while ((chips_in_pile<=1 || chips_in_pile>=51) == redo)
   
   {
   

	cout<<"How many chips would you like to start with? (2-50)"<<endl;
			cin>> chips_in_pile;
		
		if(chips_in_pile>=1 && chips_in_pile<=51)
			{
			cout<<"You have chosen to start with "<<chips_in_pile<<" chips."<<endl;
			}
		else
			
	 cout << "Please choose again (2-50)"<<endl;
	 
	}	
return chips_in_pile;		
}
Line 81: chips_taken is an uninitialized variable (garbage).
Line 83: You're testing this uninitialized variable.
Line 94: You're returning garbage.
Ok so I figured out my initial question, but now i'm having trouble calling the "is_move_legal()" function. I feel like im really close but im missing something.

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Game of Chips */

#include <iostream>
using namespace std;

int init_game(); 
int ask_move(bool, int);
bool is_move_legal(int, int); 
void declare_winner(bool);

int main() 
{ 
	bool player1turn = true; // This variable keeps track of whose turn it is. // When true it is player 1’s turn and when // false it is player 2’s turn.
	bool game_over = false; // will be set to true once the chips are gone. int chips_in_pile; int chips_taken;
	
	int chips_in_pile = init_game();
	
	while (game_over == false) 
	{ 
	
		int chips_taken = ask_move(player1turn, chips_in_pile);		
		chips_in_pile = chips_in_pile - chips_taken;
		
		if (chips_in_pile == 0) 
		{ 
			game_over = true; 
			declare_winner(player1turn); 
		} 
		else 
		{ 
			cout << "There are " << chips_in_pile << " chips left." << endl << endl;
			player1turn = !player1turn; 
		}
		
	} 
	
	return(0);
}



void declare_winner(bool player1turn)
{  
/* This function is called by the main program once the game is over.

It outputs a message congratulating the winning player.

Nothing is returned by this function. 
*/
cout<<"Congratulations player "<<player1turn<<". You are the winner! (╯°□°)╯"<<endl;

}



bool is_move_legal(int chips_taken, int chips_in_pile) 
{ 
/* This function is called by the ask_move function.

It looks at the number of chips in the pile and the number of chips the player wants to take and 
determines if the proposed move is legal. If the proposed move is legal this function will return true
and if the proposed move is not legal this function will return false.

Remember, a player must take at least 1 chip and no more than half of the chips remaining in the pile 
unless the pile has only one chip left in which case the only legal move is to take the one remaining 
chip. 
*/ 
int game_over;

	while (game_over == false)
	{
		if (chips_taken>=1 && chips_taken<=chips_in_pile/2);
		{
		game_over = false;
		}
		if (chips_taken<1 || chips_taken>=chips_in_pile/2)		//I cannot figure out why function int ask_move() isn't calling int game_over.
		{
		cout<<"Invalid amount."<<endl;
		}
	}
return chips_in_pile;


}


int ask_move(bool player1turn, int chips_in_pile)
{ 
/* This function is called by the main program.
	
It asks the correct player how many chips she would like to take. 
Then it calls is_move_legal which determines if the proposed move is legal. 
If the proposed move is NOT legal then it will print out a message saying the move is not legal
and it will ask repeatedly until the player enters a legal move.
 
Once a legal move is entered this function returns the number of chips taken. 
*/
  int chips_taken;
   

	cout<<"Player "<<player1turn<<", how many chips would you like to take?"<<endl;
	cin>>chips_taken;
	
	
return chips_taken;
 }


 
int init_game() 
{ 
/* This function is called by the main program.

It asks the user how many chips are in the pile at the start of the game. 
If the user enters a number less than 2 or greater than 50 it will tell him that he must enter a number between 2 and 50.

Once a valid number of chips is entered the function will return the number of chips. 
*/ 

  int chips_in_pile;

  int redo = (chips_in_pile<=1 || chips_in_pile>=51);
    while ((chips_in_pile<=1 || chips_in_pile>=51) == redo)
   
   {
   

	cout<<"How many chips would you like to start with? (2-50)"<<endl;
			cin>> chips_in_pile;
		
		if(chips_in_pile>=1 && chips_in_pile<=51)
			{
			cout<<"You have chosen to start with "<<chips_in_pile<<" chips."<<endl;
			}
		else
			
	 cout << "Please choose again (2-50)"<<endl;
	 
	}	
return chips_in_pile;		
}
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
//A Question : if player1 takes all chips on first turn, he wins immediately !!? 

#include <iostream>
using namespace std;

int init_game(); 
int ask_move(bool, int);
bool is_move_legal(int, int); 
void declare_winner(bool);

int main() 
{ 
	bool player1turn = true;
	bool game_over = false;
	
	int chips_in_pile = init_game();
	
	while (!game_over)
	{ 
	
		int chips_taken = ask_move(player1turn, chips_in_pile);
		chips_in_pile = chips_in_pile - chips_taken;
		
		if (chips_in_pile == 0) 
		{ 
			game_over = true; 
			declare_winner(player1turn); 
		} 
		else 
		{ 
			cout << "There are " << chips_in_pile << " chips left." << endl << endl;
			player1turn = !player1turn; 
		}
		
	} 
	
	return(0);
}


void declare_winner(bool player1turn)
{
cout<<"Congratulations player "<<player1turn<<". You are the winner! (?°?°)?"<<endl;
}


bool is_move_legal(int chips_taken, int chips_in_pile) ///////////
{ 
	if (chips_taken<=chips_in_pile && chips_taken>=0)
		return true;
	else 
		return false;
}


int ask_move(bool player1turn, int chips_in_pile)
{
  int chips_taken;
  bool ok;
  do
  {
	if (player1turn) cout <<"player 1's turn, How many chips would you like to take?\n"; //
	else cout <<"player 2's turn, How many chips would you like to take?\n\n"; //
	cin>>chips_taken;
	ok = is_move_legal(chips_taken, chips_in_pile); ////
	if (!ok) cout << "Wrong Choice, Try again!\n"; ///
  } while(!ok);
	
return chips_taken;
}


int init_game() 
{
  int chips_in_pile;

  //int redo = (chips_in_pile<=1 || chips_in_pile>=51); // ??    
   do //use do-while loop
   {
	cout<<"How many chips would you like to start with? (2-50)"<<endl;
	cin>> chips_in_pile;
		
	if(chips_in_pile>=1 && chips_in_pile<=51)		
		cout<<"You have chosen to start with "<<chips_in_pile<<" chips."<<endl;
		
	else			
	 cout << "Please choose again (2-50)"<<endl;
	 
	} while (chips_in_pile<1 || chips_in_pile>51);

return chips_in_pile;		
}
Topic archived. No new replies allowed.