Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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