Hey guys, my instructions where to write a chip game, which I have done. Except, I have one issue with my code. It starts off with Player 2, and after it has asked for the number of chips said player would like to have, it keeps repeating that question. The point of the game is that each player is supposed to pick up X amount of chips until all the chips are gone, and the player who picked up the last chip wins.
#include <iostream>
#include <iomanip>
#include <math.h>
usingnamespace std;
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;
}}
Does anyone see a flaw in the code that would make it do this?