I need to create a game where the objective is to take "Points" out of a pot and the winner ends up being the person who gets the points down to 1. I can't create the code for the "computer" player to properly execute this.
#include <iostream>
#include <limits>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
usingnamespace std;
// Define game definition constants.
constint Max_Coins_Total=25;
constint Max_Coins_Pickup=3;
// Define player characteristics.
constint Player=0;
constint Computer=1;
constchar end_of_line = '\n';
// Set up structures.
int Read_Coins_To_Pickup(int Coins_On_Table, int Max_Coins_To_Pickup);
int Compute_Coins_To_Pickup(int Coins_On_Table, int Max_Coins_To_Pickup);
void Play_Coins_Game(int Max_Coins_On_Table, int Max_Coins_To_Pickup);
// Determine game start or restart.
int main()
{
srand(unsigned(time(NULL)));
cout << "Would you like to play the number game? (Y/y)";
char answer;
cin>>answer;
while (answer=='Y'||answer=='y')
{
Play_Coins_Game(Max_Coins_Total, Max_Coins_Pickup);
cout << "Would you like to play the number game again? (Y/y)";
cin>>answer;
}
system("PAUSE");
return 0;
}
// Main game algorithm.
void Play_Coins_Game(int Coins_On_Table, int Max_Coins_To_Pickup)
{
int Player_ID=rand()%2;
do
{
int Coins_Pick;
Player_ID=(Player_ID +1) %2;
cout << Coins_On_Table << " coins are still on the table." <<endl <<endl;
if(Player_ID==Player)
{
cout <<"It is your turn." <<endl<<endl;
Coins_Pick=Read_Coins_To_Pickup(Coins_On_Table, Max_Coins_To_Pickup);
cout << "You pick up "<<Coins_Pick<< " coins." <<endl<<endl;
}
else
{
cout<<"It is the other player's turn."<<endl<<endl;
Coins_Pick=Compute_Coins_To_Pickup(Coins_On_Table, Max_Coins_To_Pickup);
cout <<"The other player picks up " <<Coins_Pick<<" coins."<<endl<<endl;
}
Coins_On_Table=Coins_On_Table - Coins_Pick;
}
while (Coins_On_Table>0);
if (Player_ID==Player)
cout<<"YOU LOSE!"<<endl;
else
cout <<"YOU WIN!"<<endl;
}
// Determine whether input is valid.
int Read_Coins_To_Pickup(int Coins_On_Table, int Max_Coins_To_Pickup)
{
bool Good_Coins_To_Pickup=false;
int Coins_To_Pickup;
do
{
cout<<"Enter the number of coins you would like to pickup: (between 1 and "<<Max_Coins_Pickup<<"): ";
cin>>Coins_To_Pickup;
if(!cin.good())
{
cout <<"You did not enter a number. Please enter a number."<<endl <<endl;
cin.clear();
cin.ignore(numeric_limits<int>::max(), end_of_line);
}
elseif (Max_Coins_To_Pickup<Coins_To_Pickup||Coins_On_Table<Coins_To_Pickup)
cout <<"You cannot pick up " <<Coins_To_Pickup<< " coins"<<endl<<endl;
elseif (Coins_To_Pickup<1)
cout <<"You cannot pick up "<<Coins_To_Pickup<< " coins"<<endl<<endl;
else
Good_Coins_To_Pickup= true;
}
while(!Good_Coins_To_Pickup);
return Coins_To_Pickup;
}
// Compute the number of coins for the computer to pick up.
int Compute_Coins_To_Pickup(int Coins_On_Table, int Max_Pickup)
{
int Coins_To_Pickup;
if(Coins_On_Table%(Max_Pickup+1)==0)
do
Coins_To_Pickup=1+rand()%Max_Pickup;
while(Coins_To_Pickup>Coins_On_Table);
else
{
Coins_To_Pickup=0;
do
Coins_To_Pickup++;
while ((Coins_On_Table-Coins_To_Pickup)%(Max_Pickup-1) !=0);
if(Coins_To_Pickup>Coins_On_Table)
Coins_To_Pickup=Coins_On_Table-1;
}
return Coins_To_Pickup;
}
The last int (lines 100-118) is the code for the computer player, but I cannot get it to try to stick at 1. It keeps trying to get to 0.
Bump, I really need help with this guys. I can't seem to get the RNG to work for the computer (104-107) and I can't get the "else" command (108-117) to properly work to make the computer try to get down to 5 and then try to maneuver to 1.