I\'m working on a game of chips project in my C++ class and my code isn\'t worki
ID: 3759047 • Letter: I
Question
I'm working on a game of chips project in my C++ class and my code isn't working, wondering if anyone can double check it for me
thanks in advance!!
I'm getting these errors:
proj2.cpp:58:2: error: expected expression
else
^
proj2.cpp:66:10: error: use of undeclared identifier 'player1turn'
do { if (player1turn);
^
proj2.cpp:68:2: error: expected expression
else {cout<<"Player 2- how many chips would you like to take?" <<endl;
^
proj2.cpp:77:1: error: expected 'while' in do/while loop
int init_game()
^
proj2.cpp:66:1: note: to match this 'do'
do { if (player1turn);
^
proj2.cpp:86:2: error: expected '}'
}
^
proj2.cpp:55:1: note: to match this '{'
{
^
5 errors generated.
/* Game of Chips */
#include
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<<"Player 1 has won the game! Congratulations."< else
cout<<"Player 2 has won the game! Congratulations."<
}
bool is_move_legal(int chips_taken, int chips_in_pile)
{
if (chips_taken<=chips_in_pile && chips_in_pile>=0){
return true;
else
return false;
}
int ask_move(bool player1turn, int chips_in_pile);
bool correct;
do { if (player1turn);
cout<<"Player 1 - how many chips would you like to take?" < else {cout<<"Player 2- how many chips would you like to take?" < cin>>chips_taken;
correct= is_move_legal(chips_taken, chips_in_pile);
if (!correct) cout << "Sorry that was not a legal move. Try again." < } while(!correct);
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";
cin >> chips_in_pile;
}
return (chips_in_pile)
}
outputs:
How many chips do you want to start with? (2-50 inclusive) 98
Sorry you must enter a number between 2 and 50 inclusive
Try again
How many chips do you want to start with? (2-50 inclusive) -7
Sorry you must enter a number between 2 and 50 inclusive
Try again
How many chips do you want to start with? (2-50 inclusive) 9
Player 1 - how many chips would you like to take? 6
Sorry that was not a legal move. Try again.
Player 1 - how many chips would you like to take? 4
There are 5 chips left.
Player 2 - how many chips would you like to take? 2
There are 3 chips left.
Player 1 - how many chips would you like to take? 3
Sorry that was not a legal move. Try again.
Player 1 - how many chips would you like to take? 1
There are 2 chips left.
Player 2 - how many chips would you like to take? 0
Sorry that was not a legal move. Try again.
Player 2 - how many chips would you like to take? 1
There are 1 chips left.
Player 1 - how many chips would you like to take? 1
Congratulations Player 1! You won the game of chips.
Explanation / Answer
/* 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;
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<<"Player 1 has won the game! Congratulations.";
else
cout<<"Player 2 has won the game! Congratulations.";
}
bool is_move_legal(int chips_taken, int chips_in_pile)
{
if (chips_taken<=chips_in_pile && chips_in_pile>=0){
return true;
}
else
return false;
}
int ask_move(bool player1turn, int chips_in_pile) {
bool correct;
int chips_taken;
do {
if (player1turn)
cout<<"Player 1 - how many chips would you like to take?";
else
cout<<"Player 2- how many chips would you like to take?";
cin>>chips_taken;
correct= is_move_legal(chips_taken, chips_in_pile);
if (!correct)
cout << "Sorry that was not a legal move. Try again.";
}while(!correct);
return chips_taken;
}
int init_game()
{
int chips_in_pile;
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";
cin >> chips_in_pile;
}
return (chips_in_pile);
}