Write a two person chip game. The game will start with a pile of chips.
Each player will have the chance to pick up as few as 1 chip and at most half of the pile of chips. This will continue until one of the players gets the last chip. Whomever gets the last chip is the winner of the game.
int main ()
{
int totalchips;
int maxchips;
int chips;
string current;
string player1;
string player2;
cout << "Rules: The game starts with a pile of chips."<<endl;
cout << " Each player may only take at most half of the chips."<<endl;
cout << " The player that gets the last chip wins. Good luck!"<<endl;
cout<< endl;
cout << "Player 1 please enter your first name:";
cin >> player1;
cout << "Player 2 please enter your first name:";
cin >> player2;
cout << endl;
cout <<"How many chips would you like to start with?";
cin >> totalchips;
current = player1
cout << endl << current << " how many of the remaining " << totalchips << " chip(s) would you like ("<<maxchips<< " max): ";
cin >> chips;
while (chips > maxchips || chips <=0)
{
cout <<"invalid number of chips. Try again:"<<endl;
cin >> chips;
}
if(current == player1)
{
current = player2;
}
else
{
current = player1;
}
while (totalchips>=0)
{
maxchips=totalchips/2;
}
if (totalchips%2==1)
{
maxchips+=1;
}
if (totalchips == 0)
{
cout << " Congratulations "<<current << "! You won!"endl;
}
system ("pause");
return 0;
}
the line i highlight in bold is where i got an error message and i can't figure out whats the problem
This line here... 'maxchips' was never set to anything. How can the computer know whether or not chips > maxchips when you never told it what maxchips was?
thanks for the help i have inizlized maxchips. and the program can run now, but heres the problem.
1. The program whould start off with player 2 instead of player 1
2. after the player select hom many chips they want then they just repeat the same question over and over again.
ex. Player 2 how many of the remaing 200 chips would you like (100 max): 50
Player 2 how many of the remaing 150 chips would you like (75 max): 50
Player 2 how many of the remaing 150 chips would you like (75 max): 45
Player 2 how many of the remaing 150 chips would you like (75 max): 20
int main ()
{
int totalchips;
int maxchips;
int chips;
string current;
string player1;
string player2;
cout << "Rules: The game starts with a pile of chips."<<endl;
cout << " Each player may only take at most half of the chips."<<endl;
cout << " The player that gets the last chip wins. Good luck!"<<endl;
cout<< endl;
cout << "Player 1 please enter your first name: ";
cin >> player1;
cout << "Player 2 please enter your first name: ";
cin >> player2;
cout << endl;
cout <<"How many chips would you like to start with? ";
cin >> totalchips;
maxchips=totalchips/2;
if (totalchips%2==1)
{
maxchips+=1;
}
current = player1;
if(current == player1)
{
current = player2;
}
else
{
current = player1;
}
cout << endl << current << " how many of the remaining " << totalchips << " chip(s) would you like ("<<maxchips<< " max): ";
cin >> chips;
while (chips > maxchips || chips <=0)
{
cout <<"invalid number of chips. Try again:"<<endl;
cin >> chips;
}
for (totalchips = totalchips - chips; totalchips > 0; )
{
maxchips=totalchips/2;
cout << endl << current << " how many of the remaining " << totalchips << " chip(s) would you like ("<<maxchips<< " max): ";
cin >> chips;
}
if (totalchips == 0)
{
cout << " Congratulations "<<current << "! You won!" <<endl;
}
system ("pause");