Need Help with "Game"

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.

This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <limits>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

// Define game definition constants.
const int Max_Coins_Total=25;
const int Max_Coins_Pickup=3;

// Define player characteristics.
const int Player=0;
const int Computer=1;
const char 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);
		}
		else if (Max_Coins_To_Pickup<Coins_To_Pickup||Coins_On_Table<Coins_To_Pickup)
			cout <<"You cannot pick up " <<Coins_To_Pickup<< " coins"<<endl<<endl;
		else if (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.

Can anybody offer some advice?
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.