need help fixing

I'm new to C++ programming and I was wondering how can I fix this program?

Rules of game:
• The game starts out with the user choosing the initial number of chips. It must be between 2 and
50 inclusively.
• Players alternate turns removing no more than half of the remaining
chips
• Whichever player takes the last chip 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
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
  
  
#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 == 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)
{
   if (player1turn = true)
  {
	cout << " Congratulations Player 1! You won the game of chips." << endl;
  }
	else
	{
	cout << "Congratulations Player 2! You won the game of chips. " << endl;
  
  }
  
}

bool is_move_legal(int chips_taken, int chips_in_pile)
{
 if (chips_taken !>= (chips_in_pile/2))
 {
 cout << "Sorry that was not a legal move. Try again. ";
	cin >> chips_taken;
	is_move_legal(chips_taken)	
 }
	else 
	{
	 return (chips_taken)
	}
  }

int ask_move(bool player1turn, int chips_in_pile)
{
  if (player1turn = true)
  {
	cout << "Player 1 - how many chips would you like to take? ";
	cin >> chips_taken;
	is_move_legal(chips_taken)
	else
	{
	cout << "Player 2 - how many chips would you like to take? ";
	cin >> chips_taken;
	is_move_legal(chips_taken)
  
  }
  return (chips_taken)
}

int init_game()
{
 
 cout << "How many chips do you want to start with? (2-50 inclusive)";
 cin >> chips_in_pile;
	if ((chips_in_pile <= 2) || (chips_in_pile >= 50))
	{cout << "Sorry you must enter a number between 2 and 50 inclusive, Try again";
	cim >> chips_in_pile;
	}
 return (chips_in_pile)
}

Last edited on
Is there more code? Can you post it? What you've posted so far ends abruptly in ask_move. The code so far looks really good. It's very well structured. A few comments:

Line 18: I never like comparing against true or false. Just say while (!game_over)

Line 43: This is a super-common bug. You're using = (assignment) instead of == (comparison). The code is legal but what it does is assign true to player1turn and then execute the if based on the result of the assignment. Since you assigned true, the if condition is always true. As with line 18, I'd just change this to if (player1turn)

Line 57: !>= is not legal. "Not greater than or equal to" is the same as "less than" so you could just say if (chips_taken < (chips_in_pile/2))

The one place where the structure is odd is in is_move_legal(). It sounds like this function is just checks the move and returns true/false depending on whether it's legal. In reality it prompts the user for more input if the move is bad. Ick. It's better to put all that logic inside a loop in ask_move(). Something like:
1
2
3
4
5
6
while (true) {
    cout << "enter a move: ";
    cin >> chips_taken;
    if (chips_taken <= chips_in_pile/2) break; // the input is valid;
    cout << "Sorry, you can't take more than " << chips_in_pile/2 << " chips\n";
};

there isn't any more code but thanks for help
Topic archived. No new replies allowed.