Sign in to follow this  

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

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}
}



Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 5
6 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 8
10 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 6
6 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 6
9 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 6
5 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 10
8 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 1
0 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


Share this post


Link to post
Share on other sites
If ya check out my DICE() function it works great. Like when playing RPG's we are always saying 2d4 or 3d6 my funtion does that. DICE(2,4)gives a roll of 3 four sided dice or DICE(3,6) will give a roll of 3 six sided dice. Thanks for all of the neat random stuff. Keep it coming.:)

Share this post


Link to post
Share on other sites
Here is my little "dice rolling" widget from my personal library.


# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;

int random(int,int);

int main()
{
srand((unsigned int)time(NULL));
int randInt;
randInt = random(1,6);
cout << randInt << endl;
return 0;
}

int random(int min, int max)
{
return (int)((max - min) * rand()/(float)RAND_MAX + min);
}



Outputs a random integer with customizable upper and lower bounds. For example this one outputs a random integer between 1 and 6 inclusive. This method provides a more uniform distribution than modulus(%) methods.

Share this post


Link to post
Share on other sites

This topic is 4690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this