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)"
/* Game of Chips */
#include <iostream>
usingnamespace 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;
}
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.
/* Game of Chips */
#include <iostream>
usingnamespace 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;
}
//A Question : if player1 takes all chips on first turn, he wins immediately !!?
#include <iostream>
usingnamespace 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)
returntrue;
elsereturnfalse;
}
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;
}