PDA

View Full Version : Calling C++ programers! Need help making a golf game


Kain
01-11-04, 12:16 PM
I need to make a golf game for my C++ class. It is due on Tuesday. This is what the game needs to do: In this game the user will play either nine or eighteen holes of golf. The computer should choose random numbers for appropriate length and par for the hole. The player should choose from a selection of, say, three woods and five irons, all of which will hit the ball a random distance within a given range. The computer should then tell the player his new distance to the hold and allow a new club to be selected. The process should be repeated until the player completes the hole. The computer, of course, should keep track of the number of strokes. Considerations: advising the player when he/she is "on the green," establishing a range for successful completion of a putt, what to do when a player overshoots the hole, and adding random slices, hooks, and fairway hazards. Report results back to the user at the end. The program that I have written so far doesn't really work. I still need to work out how I will calculated the distance and how I will actually make the game work. Can someone please help?

This is what I have so far:

#include <iostream.h>
#include <lvprandom.h>

void Hole()
{
int hole;
cout << "Would you like to play a 9 or 18-hole game? ";
cin >> hole;

if (hole == 9)
{
int hole1 = ((50+lvprandom(200));
int hole2 = ((50+lvprandom(320))-hole1);
int hole3 = ((50+lvprandom(540))-hole2);
int hole4 = ((50+lvprandom(680))-hole3);
int hole5 = ((50+lvprandom(800))-hole4);
int hole6 = ((50+lvprandom(1200))-hole5);
int hole7 = ((50+lvprandom(1400))-hole6);
int hole8 = ((50+lvprandom(1600))-hole7);
int hole9 = ((50+lvprandom(1800))-hole8);
}

else if (hole == 18)
{
int hole1 = ((50+lvprandom(200));
int hole2 = ((50+lvprandom(400))-hole1);
int hole3 = ((50+lvprandom(600))-hole2);
int hole4 = ((50+lvprandom(800))-hole3);
int hole5 = ((50+lvprandom(1000))-hole4);
int hole6 = ((50+lvprandom(1200))-hole5);
int hole7 = ((50+lvprandom(1400))-hole6);
int hole8 = ((50+lvprandom(1600))-hole7);
int hole9 = ((50+lvprandom(1800))-hole8);
int hole10 = ((50+lvprandom(2000))-hole9);
int hole11 = ((50+lvprandom(2200))-hole10);
int hole12 = ((50+lvprandom(2400))-hole11);
int hole13 = ((50+lvprandom(2600))-hole12);
int hole14 = ((50+lvprandom(2800))-hole13);
int hole15 = ((50+lvprandom(3000))-hole14);
int hole16 = ((50+lvprandom(3200))-hole15);
int hole17 = ((50+lvprandom(3400))-hole16);
int hole18 = ((50+lvprandom(3600))-hole17);
}

else
{
while (hole != 9 || 18)
{
cout << "Please enter either 9 or 18: ";
cin >> hole;
}
}
}

void Instructions()
{
cout << "These are the Instructions." << endl;
cout << endl;
Hole();
}

void Clubs(int club)
{
int distance, club;
if club=1
distance=20+lvprandom(40);
else if club=2
distance=40+lvprandom(60);
else if club=3
distance=60+lvprandom(80);
else if club=4
distance=80+lvprandom(100);
else if club=5
distance=100+lvprandom(120);
else if club=6
distance=200+lvprandom(50);
else
cout << "Please enter a club number from 1 to 6: ";
cin >> club;
{

void Introduction()
{
char answer;
cout << "Welcome to Golf 2004!" << endl;
cout << endl;
cout << "Would you like to read the Instructions? y/n: ";
cin >> answer;
if (answer == 'y')
Instructions();
else
{
while (answer != 'y' || 'n")
cout << "Please enter either y or n: ";
cin >> answer;
}

void Distance()
{
int hole, counter, distance;
cout << "Would you like to play a 9 or 18-hole game? ";
cin >> hole;

for (counter=1, counter<hole, counter++)
{
distance =
cout << "Distance to hole " << counter << " is " << distance << " yards."

int main()
{
randomize();
Introduction();

return(0);
}

Sazar
01-11-04, 12:49 PM
since I am in the middle of a bbq I will try and put out an basic flow...

instead of having 2 lists with 9 holes and then 18 holes declared... list all 18 and then depending on the cin... reference first 9 items or reference all 18 items...

secondly.. when a shot is hit you are stating a random distance... whatever number is generated calculate next shot using hole yardage - random number == distance for next shot... store this number since this will become hole yardage for next shot and the next shots distance will be subtracted from it... follow ?

anyways... this will continue till distance == 0 then proceed to next hole and repeat... just have a function that does this so you just have to check to see what hole you are playing and what not...

considering the hazards... you should be tracking strokes and the program will be storing strokes in memory (hopefully you have this under consideration)... anyways when playing and if hazards are random, put up message saying player is in said hazard... if water or OB, add 1 to stroke and replay (therefore distance to hole remains same) or drop (therefore distance to hole is reduced)

since there is no stated criteria for how a hazard or slice or hook affects game... there should be no need to do anything other than just have an output saying "player so and so... you f!cked up :D "

now when you are starting the game you NEED to establish an input for the club and the distance you assign to each club... the club distances should probably not be variables...

therefore do your select hole number spiel then when game starts... ask enter club number 1-whatever, type s for sand wedge, w for pitching wedge, p for putter and what not...

just have a basic algorithm before you start programming... it'll make it all easier :D

/me off to get drunk...

:beer:

Kain
01-11-04, 10:23 PM
Thanks!

But I am still kind of confused on how to actually display the distance to the hole (and based on which club they select) subtract the distance hit to show how far they are from the hole now. I know how it works, but not sure how to put in C++ stuff.

By the way, how many functions do I really need? Do I need one for the clubs and distance and all? Or can I do all that in the "int main?"

Thanks again!

de><ta
01-12-04, 07:05 AM
Just glancing at your code I'd say,
#1- use freaking arrays!!!
#2- build it up using a class strutcure for a course. let the course's constructor take a int type value which is the number of holes. And then in the particular course_type class, have methods such as
hit(type_of_club)
report() - returns distance etc.

#3- currently what you are doing looks more like a procedural program, switch over too object orientation and the whole task is a lot easier.

I think I have a free hour today, I'll see how I can help out.

Cheers

ALobpreis
01-13-04, 10:55 AM
That code can't run! There are several problems... For example:

if club=1
Should be "if (club == 1)"

for (counter=1, counter<hole, counter++)
Should be "for (...; ...; ...)"

The control loops for keyboard input are not very well. Also, as said above, arrays needed!!! :)

The code in general is not clear. Look at the main, there's no sequence of steps, each function calls another and it's hard to follow the execution order.

Is this program for today??? :eek: I may help you, but probably at night, not now... (I'm at work)