I need some tips from a pro for my newbie game.

Started by
10 comments, last by Matt Apple 19 years, 2 months ago
I"M STUCK :( I am trying to make a newbie turn-based horse racing console game. This is the first game I have every attempted. I am guessing that one way of learning game programming is to jump in and learn as I go; furthermore I am sick of reading boring books:). The following code compiles and runs smooth but it is only the horse generation portion. If you copy the code and then run the program you will see that it leaves one with the option to purchase a colt. This leaves me with a problem. I don’t know how to store the colt in a memory space so I can retrieve it at will. What I am ultimately looking for is for the character to have an inventory that will display his current money and horse and perhaps an option to either race, train or feed the horse. Now I am not expecting someone to program the code, but perhaps an explanation of what I need to do and why. Furthermore, some suggestions to improve the existing code would be appreciated greatly. I would greatly appreciate any and all advice regarding this issue. Remember I am a green newbie.:)


#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <ctime>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

string GET_COLOR(int pick);
int DICE(int n, int x);
void BRONZE_H(int star);

int main() {
srand(time(0));
BRONZE_H(1);// call function to display 3 new horses



system("PAUSE");
return 0;
}



//dice****************
int DICE(int n, int x){
 int i, score = 0;
 
 for (i = 1; i <= n; i++){
 score += rand()% x + 1;}
   return score; }

// Get horse color
string GET_COLOR(int pick){
 vector<string> colors_1;
colors_1.push_back("Bay");
colors_1.push_back("Chestnut");
colors_1.push_back("Grey");
colors_1.push_back("Palamino");
colors_1.push_back("Black");
colors_1.push_back("Brown");
vector<string> colors_2;
colors_2.push_back("Bay");
colors_2.push_back("Light Chestnut");
colors_2.push_back("Dark Chestnut");
colors_2.push_back("Light Grey");
colors_2.push_back("Dark Grey");
colors_2.push_back("Black");
colors_2.push_back("Roan");
srand(time(0));
random_shuffle(colors_1.begin(),colors_1.end());
random_shuffle(colors_2.begin(),colors_2.end());

if(pick == 1){

return colors_1[rand()%4];}
else {return colors_2[rand()%4];}

}

// Generate 3 horses

void BRONZE_H(int star){

 switch(star){
 
 case 1:
 { 
 cout << "\n\t\t\tThree new colts have been born\n\n";
   HANDLE  hConsole;       
       hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 15);
	for(int i = 3; i > 0; i--)
	{
		cout  << i << "\b" ;
		Sleep(500);
	}
 
 
 cout << "\t\t\t\tColt #1\n";
 cout << "\t\t\t----------------------\n";
 cout << "\t\t\tTYPE: Paint\n";
 cout << "\t\t\tColor: " << GET_COLOR(1)<< "\n";
 cout << "\t\t\tStamina: " << DICE(7,3) +18 << "\n";
 cout << "\t\t\tFortitude: " << DICE(7,3)+18<< "\n";
 cout << "\t\t\tTop Speed: " << DICE(7,3)+18<< "\n";
 cout << "\t\t\tAcceleration: " << DICE(7,3)+18<< "\n\n";
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 11);
 cout << "\t\t\tPress 1 to buy this colt\n\n";
 
 Sleep(800);
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 15);
 
 cout << "\t\t\t\tColt #2\n";
 cout << "\t\t\t----------------------\n";
 cout << "\t\t\tTYPE: Paint\n";
 cout << "\t\t\tColor: " << GET_COLOR(1) <<"\n";
 cout << "\t\t\tStamina: " << DICE(3,7) +18<< "\n";
 cout << "\t\t\tFortitude: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tTop Speed: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tAcceleration: " << DICE(3,7)+18<< "\n\n";
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 11);
 cout << "\t\t\tPress 2 to buy this colt\n\n";
  Sleep(800);
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 15);
 
 cout << "\t\t\t\tColt #3\n";
 cout << "\t\t\t----------------------\n";
 cout << "\t\t\tTYPE: Paint\n";
 cout << "\t\t\tColor: " << GET_COLOR(1)<< "\n";
 cout << "\t\t\tStamina: " << DICE(3,7) +18<< "\n";
 cout << "\t\t\tFortitude: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tTop Speed: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tAcceleration: " << DICE(3,7)+18<< "\n\n";
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 11);
 cout << "\t\t\tPress 3 to buy this colt\n\n";
 }break;
 //222222222222222222222222222222222222222222222
  case 2:
 { 
 cout << "\n\t\t\tThree new colts have been born\n\n";
   HANDLE  hConsole;       
       hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 15);
	for(int i = 3; i > 0; i--)
	{
		cout  << i << "\b" ;
		Sleep(500);
	}
 cout << " \t\t\t\tColt #1\n";
 cout << "\t\t\t----------------------\n";
 cout << "\t\t\tType: Paint\n";
 cout << "\t\t\tColor: " << GET_COLOR(1)<< "\n";
 cout << "\t\t\tStamina: " << DICE(3,7) +18<< "\n";
 cout << "\t\t\tFortitude: " << DICE(3,7)+24<< "\n";
 cout << "\t\t\tTop Speed: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tAcceleration: " << DICE(3,7)+24<< "\n\n";
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 11);
 cout << "\t\t\tPress 1 to buy this colt\n\n";
 
 Sleep(800);
       
       hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 15);
 cout << "\t\t\t\tColt #2\n";
 cout << "\t\t\t----------------------\n";
 cout << "\t\t\tType: Paint\n";
 cout << "\t\t\tColor: " << GET_COLOR(1) << "\n";
 cout << "\t\t\tStamina: " << DICE(3,7) +18<< "\n";
 cout << "\t\t\tFortitude: " << DICE(3,7)+24<< "\n";
 cout << "\t\t\tTop Speed: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tAcceleration: " << DICE(3,7)+24<< "\n\n";
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 11);
 cout << "\t\t\tPress 2 to buy this colt\n\n";
  Sleep(800);
      
       hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 15);
 
 
 cout << "\t\t\t\tColt #3\n";
 cout << "\t\t\t----------------------\n";
 cout << "\t\t\tType: Paint\n";
 cout << "\t\t\tColor: " << GET_COLOR(1) <<"\n";
 cout << "\t\t\tStamina: " << DICE(3,7) +18<< "\n";
 cout << "\t\t\tFortitude: " << DICE(3,7)+24<< "\n";
 cout << "\t\t\tTop Speed: " << DICE(3,7)+18<< "\n";
 cout << "\t\t\tAcceleration: " << DICE(3,7)+24<< "\n\n";
 hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 11);
 cout << "\t\t\tPress 3 to buy this colt\n\n";
 
 }break;
}
}



Advertisement
First, you should create a structure to hold your horse's characteristics.
Then you should write a function that assigns random values to each of these.
Then a function which displays one horse's characteristics.

That would be a start. [smile]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Or you could bundle them all into a nice class ;)
Could you elaborate a bit more please. What do you mean by structure? How can a creating a class help me create an inventory? If I store them in a string they will come out all in a mess correct?

Again I appreciate all advice.
I personally like the structure idea. It would be something like so:

struct horse { //various attributes}


And then you could make instances of this class for every horse in existence. And say you want to keep track of the player's stable then simply create an array of the type horse.

That would be the best. After this, create a few functions to handle this type. Like a report function that lists the attributes, an assignment function to create new horses, and so on and so on.
ok That's got my mind working. Thanks everyone, I appreciate the help. I'm off to study structures and classes.:)
You can create random values with the rand function.

srand(time(0));int random_number = rand();




if you want a number between 0 and 5:

const int MAX_NUMBER = 5;srand(time(0));int random_number = static_cast <double> (rand()) / MAX_NUMBER;





A random number between 5 and 10:

const int MAX_NUMBER = 10;const int START_NUMBER = 5;srand(time(0));int random_number = static_cast <double> (rand()) / MAX_NUMBER + START_NUMBER;




Hope this helps for the random stuff in your program.

Incorrect, rand() returns a integer between 0 and RAND_MAX, so dividing it by, e.g. 5, cannot yield a number between 0 and 5...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
He probably meant:
srand((unsigned)time(NULL));int random_number = rand()%Max_Number;
In addition the logics for that function are incorrect. You must compensate for that fact you are not starting at 0. Here is an example:

#include <stdio.h>#include <time.h>#include <stdlib.h>int main(int argc, char* argv[]){	const int MAX_NUMBER = 10;	const int START_NUMBER = 5;	srand(time(0));	int random_number = rand() % (MAX_NUMBER - START_NUMBER + 1) + START_NUMBER;	for(int x=0;x<500;x++)	{		printf("%i ",random_number);		random_number = rand() % (MAX_NUMBER - START_NUMBER + 1) + START_NUMBER;	}	return 0;}


Output is;
10 6 8 7 5 5 7 8 5 10 8 6 6 8 5 5 10 7 9 7 6 8 8 8 6 6 9 8 8 8 6 8 8 8 5 5 8 7 5 7 5 7 7 8 9 9 5 6 7 9 5 9 8 9 10 5 6 7 10 6 9 9 9 5 7 10 10 7 8 8 8 10 9 6 5 56 9 9 7 8 8 10 6 10 7 5 5 6 10 8 6 5 8 10 7 5 9 10 9 9 5 8 7 5 7 7 5 10 6 7 5 810 5 8 8 10 10 9 6 9 9 7 10 7 10 7 5 9 5 10 5 6 5 7 9 8 8 8 10 8 10 6 7 5 5 9 66 7 9 8 5 8 8 10 10 6 8 9 6 8 5 10 7 6 5 7 8 6 6 9 7 5 7 8 7 5 5 6 8 8 8 10 7 10 5 7 7 9 8 6 5 8 5 5 9 5 5 9 10 8 7 8 10 9 10 9 9 10 8 8 8 7 7 6 5 10 10 7 10 69 8 9 6 7 6 6 5 5 7 8 6 9 5 8 7 10 10 6 10 8 7 6 8 6 8 7 9 10 5 9 7 8 6 8 5 7 65 7 5 7 6 8 8 5 7 6 10 9 8 6 10 7 5 6 8 9 9 5 8 9 9 7 10 8 6 8 9 5 10 10 10 10 7 9 9 10 5 5 10 10 5 9 9 6 9 5 5 9 8 9 10 8 7 9 6 6 7 6 10 10 9 9 10 10 9 8 9 108 5 5 9 6 6 10 8 9 8 8 7 8 7 5 7 8 8 5 7 7 7 7 7 9 7 5 8 8 9 7 5 10 9 9 10 5 6 9 5 5 10 10 8 7 10 8 6 10 9 9 9 6 8 8 6 10 5 8 9 7 6 9 9 6 9 5 5 10 8 6 9 7 9 9 6 10 6 5 5 7 7 8 5 6 7 8 6 7 8 8 5 8 8 9 10 9 6 8 9 8 5 6 10 10 5 10 7 8 10 7 5 10 5 9 9 9 10 8 8 6 7 5 8 6 7 8 6 5 7 5 10 9 6 6 8 8 10 8 8 7 9 9 7 10 7 7 6 7 10 6 8 5 10 6 7 5 8 7 9 10 5 8 10 7 8 9 Press any key to continue

This topic is closed to new replies.

Advertisement