Mar 14, 2022 at 8:38pm UTC
#include "Horserun.cpp"
#include "Random.cpp"
Horse::Horse(std::string _name)
:name(_name)
,distance_covered(0)
{
}
void Horse::run()
{
// increment the disnatnce randomly between 0-1
distance_covered += Random::randint(Random::MAX_SEED_RUN);
}
int Horse::get_distance_covered()
{
return distance_covered;
}
bool Horse::is_race_won()
{
if (distance_covered >= Random::WINING_DISTANCE)
{
return true;
}
else
{
return false;
}
}
std::string Horse::get_distance_str()
{
std::string retval;
for (uint32_t cnt = 0; cnt < distance_covered; cnt++)
{
retval += "=";
}
retval += " " + name;
if (distance_covered == Random::WINING_DISTANCE)
{
retval += " is the Winner!";
}
return retval;
}
#include <string>
class Horse {
public :
// public method declaration
// c'tor
Horse(std::string _name);
// run method
void run();
// distance covered
int get_distance_covered();
// get distnace string
std::string get_distance_str();
// check if horse has croosed the winnning limit
bool is_race_won();
public :
// public member declaration
std::string name;
private :
int distance_covered;
};
#include <iostream>
#include <cstdlib>
#include <time.h>
class Random
{
public :
static void seed()
{
std::srand(time(0));
}
static int randint(uint32_t max)
{
return std::rand() % (max - 0);
}
static const uint32_t WINING_DISTANCE = 20;
static const uint32_t MAX_SEED_RUN = 2;
static const int HALF_SECOND = 500;
};
#include <iostream>
#include <windows.h>
#include <string>
#include "Random.cpp"
#include "Horserun.cpp"
const std::string BANNER =
"==============\n\
= Horse Race =\n\
==============\n\
";
const int NUM_HORSE_TO_RACE = 5;
int main(int argc, char* arg[])
{
// set title - Change name here
system("Horse Race");
bool noWinners = true;
// call function to generate seed
Random::seed();
// create an array of horse to be raced
Horse * racing_horses[NUM_HORSE_TO_RACE];
for (uint32_t idx = 0; idx < NUM_HORSE_TO_RACE; idx++)
{
if (0 == idx)
{
racing_horses[idx] = new Horse("ShadowFox");
}
else if (1 == idx)
{
racing_horses[idx] = new Horse("Epona");
}
else if (2 == idx)
{
racing_horses[idx] = new Horse("Roach");
}
else if (3 == idx)
{
racing_horses[idx] = new Horse("Pinkie Pie");
}
else if (4 == idx)
{
racing_horses[idx] = new Horse("Wildfire");
}
}
while (noWinners)
{
// print banner
std::cout << BANNER << std::endl;
std::cout << "====================" << std::endl;
for (uint32_t idx = 0; idx < NUM_HORSE_TO_RACE; idx++)
{
racing_horses[idx]->run();
std::cout << racing_horses[idx]->get_distance_str() << std::endl;
noWinners = noWinners && !racing_horses[idx]->is_race_won();
}
std::cout << "====================" << std::endl;
Sleep(Random::HALF_SECOND);
if (noWinners) {
system("cls");
}
}
// delete allocated array
for (uint32_t idx = 0; idx < NUM_HORSE_TO_RACE; idx++)
{
delete racing_horses[idx];
}
return 0;
}
Last edited on Mar 14, 2022 at 9:19pm UTC
Mar 15, 2022 at 2:13am UTC
To rephrase George P's #3:
3. NEVER #include
a .cpp file. This can cause multiply defined symbols at link time. Your header files should be .h files.
PLEASE EDIT YOUR POST AND APPLY CODE TAGS.
Mar 15, 2022 at 2:35am UTC
Whoa, there's an echo in here! :Þ
Mar 15, 2022 at 5:20am UTC
I didn't think your emphasis was strong enough. :)
Nor did you give an explanation of why not to do it.
Last edited on Mar 15, 2022 at 5:33am UTC
Mar 15, 2022 at 6:10am UTC
Another problem is writing 150 lines in one session, only to discover that it doesn't work.
Start small, compile often (like every 5 lines) and test often.
Like you make sure it works for one horse before copy/pasting your way to 10 of them.