Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ Game help


  • Please log in to reply

#1
AJwest

AJwest

    New Member

  • Member
  • Pip
  • 1 posts
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.


#include <iostream>
#include <iomanip>
#include <math.h>

using namespace 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;



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 red is where i got an error message and i can't figure out whats the problem
  • 0

Advertisements


#2
InsaneKaos

InsaneKaos

    Member

  • Member
  • PipPipPip
  • 120 posts
current = player1;<-----



cout << endl << current << " how many of the remaining " << totalchips << " chip(s) would you like ("<<maxchips<< " max): ";cin >> chips;

Maybe that is the problem.
  • 0

#3
OpenOutcome

OpenOutcome

    Member

  • Member
  • PipPipPip
  • 640 posts
Hello,

InsaneKaos is right. Also:

if (totalchips == 0)
{
cout << " Congratulations "<<current << "! You won!" << endl;


Good luck,

-Jason
  • 0

#4
TheBug

TheBug

    Member

  • Member
  • PipPip
  • 28 posts
I do not see any loop for the game to carry on until there are no chips left. You should check the algorithm again.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP