This is my program.... Can someone please help me? This is so confusing for me and nothing makes any sense. When I excute the program, it will ask for the 2 names, how chips I want to start with, then it says congrats player 2 name, you win. I have no idea what I am doing wrong.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int remainingChips, chipPile, currentChips, Chips = 0;
string player1, player2, currentPlayer;
// Rules of game
cout << "Rules: The game starts with a pile of chips. Each player may only take at\n";
cout << "most half of the chips. The player that gets the last chip wins. Good Luck!\n";
// ask player 1 for name
cout << "\nPlayer 1 please enter your name: ";
cin >> player1;
// ask player 2 for name
cout << "Player 2 please enter your name: ";
cin >> player2;
//set the current player to player 1
currentPlayer = player1;
// ask players how many chips they want to start with
// ask player 1 how many chips they want from the remaining 200 max 100
// ask player 2 how many chips they want from the remaining chips max half of remaining chips
cout << "\nHow many chips would you like to start with? ";
cin >> chipPile;
while (Chips > 1)
{
if ( chipPile % 2 == 1 )
{
cout << endl << currentPlayer << " how many of the remaining " << chipPile << " chip(s) would you like to take (" << remainingChips << " max)?";
cin >> currentChips;
}
else
{
cout << endl << currentPlayer << " how many of the remaining " << chipPile << " chip(s) would you like to take (" << remainingChips << " max)?";
cin >> currentChips;
}
}
if( currentChips >= remainingChips / 2 )
{
Chips = chipPile - currentChips;
chipPile ++;
}
if (chipPile % 2 != 0)
{
cout << "Congratulations " << player1 << "! You won!";
}
else
{
cout << "Congratulations " << player2 << "! You won!";
}
return 0;
}
Ok ..... let me try this one. Everything works except at the end. I can go thru and put it amount of chips, when I get to 1 left (1max) it keeps repeating it and doesn't declare a winner. Can anyone help me find where I went wrong ??
int main()
{
int takeChips, chipAmt, chips = 0;
string player1, player2;
// Rules of game
cout << "Rules: The game starts with a pile of chips. Each player may only take at\n";
cout << "most half of the chips. The player that gets the last chip wins. Good Luck!\n";
// ask player 1 for name
cout << "\nPlayer 1 please enter your first name: ";
cin >> player1;
cout << "\nPlayer 2 please enter your first name: ";
cin >> player2;
cout << "\nHow many chips would you like to start with? ";
cin >> chips;
while (chips > 0)
{
if (chipAmt % 2 == 0)
{
cout << player1 << " how many of the remaining " << chips << " would you like (" << (chips / 2) << " max)? ";
}
else
{
cout << player2 << " how many of the remaining " << chips << " would you like (" << (chips / 2) << " max)? ";
}
cin >> takeChips;
if (takeChips <= (chips/2))
{
chips = chips - takeChips;
chipAmt++;
}
else
{
cout << "\nERROR: invalid number of chips! Try again!";
}
}
if (chipAmt % 2 != 0)
{
cout << "\nCongratulations " << player1 << "! You won!";
}
else
{
cout << "\nCongratulations " << player2 << "! You won!";
}
return 0;
}