Sign in to follow this  

Game function won't work

This topic is 4688 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

OK I am creating a newbie turn-based horse racing game. I am trying to get each section to work before I add the next. This part is supposed to generate a list of 3 new horses for the player to choose. For the life of me I can't get it to compile. This code is the section that I am having trouble with. It looks as if it should compile, but what do I know as I am new to this monster called C++. This project is probabbly over my head but I thought it might be good to stop reading books and get my feet wet.
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;

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

int main() {

BRONZE_H(2);// 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()%3];}
else {return colors_2[rand()%3];}

}

// Generate 3 horses

void BRONZE_H(int star){

 switch(star){
 
 case 1:
 { 
   HANDLE  hConsole;       
       hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 10);
	for(int i = 5; i > 0; i--)
	{
		cout  << i << "\b" ;
		Sleep(500);
	}
 cout << "\n\t\tThree new colts has been born\n";
 
 cout << " Colt #1\n";
 cout << "\t\t\t ---------------\n";
 cout << "\t\t\tTYPE: PAINT\n";
 cout << "Color:" << GET_COLOR(1);
 cout << "Stamina:" << DICE(3,7) +18;
 cout << "Fortitude:" << DICE(3,7)+18;
 cout << "Top Speed:" << DICE(3,7)+18;
 cout << "Acceleration:" << DICE(3,7)+18;
 cout << " Press 1 to buy this colt";
 
 Sleep(500);
 
 cout << " Colt #2\n";
 cout << "\t\t\t ---------------\n";
 cout << "\t\t\tTYPE: PAINT\n";
 cout << "Color:" << GET_COLOR(1);
 cout << "Stamina:" << DICE(3,7) +18;
 cout << "Fortitude:" << DICE(3,7)+18;
 cout << "Top Speed:" << DICE(3,7)+18;
 cout << "Acceleration:" << DICE(3,7)+18;
 cout << " Press 2 to buy this colt";
  Sleep(500);
 
 cout << " Colt #3\n";
 cout << "\t\t\t ---------------\n";
 cout << "\t\t\tTYPE: PAINT\n";
 cout << "Color:" << GET_COLOR(1);
 cout << "Stamina:" << DICE(3,7) +18;
 cout << "Fortitude:" << DICE(3,7)+18;
 cout << "Top Speed:" << DICE(3,7)+18;
 cout << "Acceleration:" << DICE(3,7)+18;
 cout << " Press 3 to buy this colt";
 break;
 //222222222222222222222222222222222222222222222
  case 2:
 { 
   HANDLE  hConsole;       
       hConsole = GetStdHandle(STD_OUTPUT_HANDLE);       
        SetConsoleTextAttribute(hConsole, 10);
	for(int i = 5; i > 0; i--)
	{
		cout  << i << "\b" ;
		Sleep(500);
	}
 cout << "\n\t\tThree new colts has been born\n";
 
 cout << " Colt #1\n";
 cout << "\t\t\t ---------------\n";
 cout << "\t\t\tTYPE: PAINT\n";
 cout << "Color:" << GET_COLOR(1);
 cout << "Stamina:" << DICE(3,7) +18;
 cout << "Fortitude:" << DICE(3,7)+24;
 cout << "Top Speed:" << DICE(3,7)+18;
 cout << "Acceleration:" << DICE(3,7)+24;
 cout << " Press 1 to buy this colt";
 
 Sleep(500);
 
 cout << " Colt #2\n";
 cout << "\t\t\t ---------------\n";
 cout << "\t\t\tTYPE: PAINT\n";
 cout << "Color:" << GET_COLOR(1);
 cout << "Stamina:" << DICE(3,7) +18;
 cout << "Fortitude:" << DICE(3,7)+24;
 cout << "Top Speed:" << DICE(3,7)+18;
 cout << "Acceleration:" << DICE(3,7)+24;
 cout << " Press 2 to buy this colt";
  Sleep(500);
 
 cout << " Colt #3\n";
 cout << "\t\t\t ---------------\n";
 cout << "\t\t\tTYPE: PAINT\n";
 cout << "Color:" << GET_COLOR(1);
 cout << "Stamina:" << DICE(3,7) +18;
 cout << "Fortitude:" << DICE(3,7)+24;
 cout << "Top Speed:" << DICE(3,7)+18;
 cout << "Acceleration:" << DICE(3,7)+24;
 cout << " Press 3 to buy this colt";
 break;
 }
}

Share this post


Link to post
Share on other sites
I haven't tested it, but it compiles

#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <ctime>
#include <vector>
#include <algorithm>
#include <string>//you forgot to put #include <string>
using namespace std;

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

int main() {

BRONZE_H(2);// 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()%3];}
else {return colors_2[rand()%3];}

}

// Generate 3 horses

void BRONZE_H(int star){

switch(star){

case 1:
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
for(int i = 5; i > 0; i--)
{
cout << i << "\b" ;
Sleep(500);
}
cout << "\n\t\tThree new colts has been born\n";

cout << " Colt #1\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+18;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+18;
cout << " Press 1 to buy this colt";

Sleep(500);

cout << " Colt #2\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+18;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+18;
cout << " Press 2 to buy this colt";
Sleep(500);

cout << " Colt #3\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+18;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+18;
cout << " Press 3 to buy this colt";
}break;//break outside of case, and you were missing a }
//222222222222222222222222222222222222222222222
case 2:
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
for(int i = 5; i > 0; i--)
{
cout << i << "\b" ;
Sleep(500);
}
cout << "\n\t\tThree new colts has been born\n";

cout << " Colt #1\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+24;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+24;
cout << " Press 1 to buy this colt";

Sleep(500);

cout << " Colt #2\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+24;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+24;
cout << " Press 2 to buy this colt";
Sleep(500);

cout << " Colt #3\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+24;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+24;
cout << " Press 3 to buy this colt";

}break;//break outside of case
}
}//added a }


Share this post


Link to post
Share on other sites
Your missing two closing brackets. One at the end of your case 1: statement and one to close your switch.

Also you need to include <string> if you want to use strings. The source would become :
#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() {

BRONZE_H(2);// 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()%3];}
else {return colors_2[rand()%3];}
}

// Generate 3 horses

void BRONZE_H(int star){

switch(star){

case 1:
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
for(int i = 5; i > 0; i--)
{
cout << i << "\b" ;
Sleep(500);
}
cout << "\n\t\tThree new colts has been born\n";

cout << " Colt #1\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+18;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+18;
cout << " Press 1 to buy this colt";

Sleep(500);

cout << " Colt #2\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+18;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+18;
cout << " Press 2 to buy this colt";
Sleep(500);

cout << " Colt #3\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+18;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+18;
cout << " Press 3 to buy this colt";
break;
}


case 2:
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
for(int i = 5; i > 0; i--)
{
cout << i << "\b" ;
Sleep(500);
}
cout << "\n\t\tThree new colts has been born\n";

cout << " Colt #1\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+24;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+24;
cout << " Press 1 to buy this colt";

Sleep(500);

cout << " Colt #2\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+24;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+24;
cout << " Press 2 to buy this colt";
Sleep(500);

cout << " Colt #3\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
cout << "Color:" << GET_COLOR(1);
cout << "Stamina:" << DICE(3,7) +18;
cout << "Fortitude:" << DICE(3,7)+24;
cout << "Top Speed:" << DICE(3,7)+18;
cout << "Acceleration:" << DICE(3,7)+24;
cout << " Press 3 to buy this colt";
break;
}
}
}




There's some more stuff that isn't a 100% legal but this gets your program compiled. Next time please use the [ source] [ /source] tags.

EDIT : I'm a slow typer. hothead beat me with 7 mins !!!

Share this post


Link to post
Share on other sites
Make sure your code is nicely indented, while you're at it.

Oh, and let's remove some redundancy shall we?


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

// let's give these functions some nicer names :) Lowercase would be good for
// a start; there's no need to shout. (ALL_CAPS_NAMES_LIKE_THIS are usually
// used for macros.)

string getColor(int whichSet);
int rollDice(int numDice, int sidesPerDie);
void displayHorses(int whichColours, int howMany) {
void initColors();

// I am making the horse colours global because they should be initialized only
// once (the RNG seeding should happen only once too).

vector<string> colors[2]; // also, I'm using an array, because of the
// parallelism. Simililarly named variables distinguished by a number suggest
// this kind of change.


int main() {
srand(time(0));
// Initialize colour data
initColors();
// I moved the Windows stuff into here because it doesn't really
// have anything to do with creating horses.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 10);
for(int i = 5; i > 0; i--) {
cout << i << "\b" ;
Sleep(500);
}
cout << "\n\t\tThree new colts has been born\n";

// Display 3 new horses.
void displayHorses(1, 3);
system("PAUSE");
return 0;
}

void initColors() {
// Later on we should initialize these from a file or something.
// Although if you really want these to be hard-coded (so that you always
// know how many there are), you may as well use an array. You can use
// random_shuffle() and other such algorithms with plain arrays, you know :)
// random_shuffle(array_name, array_name + number_of_elements);
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");
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");
random_shuffle(colors_1.begin(),colors_1.end());
random_shuffle(colors_2.begin(),colors_2.end());
}

int rollDice(int numDice, int sidesPerDie) {
int score = 0;
for (int i = 0; i < numDice; i++) {
// There are better ("more random") approaches to this, but I'll let it
// slide for now.
score += rand() % sidesPerDie + 1;
}
return score;
}

// Get horse color
string getColor(int whichSet) {
// Note we don't need the if/else any more.
// You could put in an assert here if you're worried about bad input.
// Note that the valid values are now 0 and 1, not 1 and 2.
return colors[whichSet][rand()%3];}
}

// Create some "horses" and display their info. I hope you don't mind that I
// made this a bit more powerful ;) Anyway, this is the major change - one
// block of cout'ing instead of 3, and calculations instead of switch logic.
void displayHorses(int whichColours, int howMany) {
for (int i = 1; i <= howMany; i++) {
// For each horse, print some info.
// I assume you do intend to roll seven-sided dice ;)
cout << " Colt #" << i << "\n";
cout << "\t\t\t ---------------\n";
cout << "\t\t\tTYPE: PAINT\n";
// I'm aware that in your code, the passed-in variable affects the stat
// rolling, and not the colour choice. Your assignment: fix that, *without*
// doing a dumb copy-and-paste job.

cout << "Color:" << getColor(whichColours);
cout << "Stamina:" << rollDice(3,7)+18;
cout << "Fortitude:" << rollDice(3,7)+18;
cout << "Top Speed:" << rollDice(3,7)+18;
cout << "Acceleration:" << rollDice(3,7)+18;
cout << " Press " << i << " to buy this colt" << endl;
Sleep(500);
}
}



Control-V is not your friend. When you create parallel bits of stuff like that, if you want to change one you will likely have to change them all - and sometimes you forget; and sometimes there are necessary subtle differences and you lose track of them, etc. etc. Just Don't Freakin' Do It (TM). [smile]

Share this post


Link to post
Share on other sites

This topic is 4688 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