1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <cstdlib>
#include <iomanip>
#define SEED 50
using namespace std;
/* The following code stimulates a race track,
It first declars a horse class with the properties of horse number, speed, and position.
The methods the horse class has is Advance(), which advances the position dependent on the horses speed;
and SetHorse() which initializes the horses values after construction(this this is because we need a array of Horses latter on)
Next is the RaceTrack class, with Lanes,Length Horses and pointers to horses and counters.
It has a constructor which initializes everything including creating the array previously mentioned and a method called StartRace() which runs the race.
Main sets it all up with Random inputs, will make the seed dependent on time when I'm done.
*/
class Horse{
private:
int Speed;
public:
int Position;
int HorseNumber;
int Advance();
Horse();
void SetHorse(int Speed, int Position,int HorseNumber);
};
Horse::Horse(){
int Speed=0;
int Position=0;
int HorseNumber=0;
}
void Horse::SetHorse(int SpeedI, int PositionI,int I){
Speed = SpeedI;
Position = PositionI;
HorseNumber = I;
}
int Horse::Advance(){
Position+=Speed;
return Position;
}
class RaceTrack{
private:
int Length;
int Lanes;
int i;//counter
int j;//counter
Horse* Head;
Horse* Temp;
Horse* Temp2;
Horse HorseArray[7];
public:
RaceTrack(int Length, int Lanes);
void StartRace();
};
RaceTrack::RaceTrack(int LengthI, int LanesI){
Length=LengthI;
Lanes=LanesI;
i = 0;
j = 0;
Horse HorsesArray[7];
for(i=0; i<Lanes;i++){// School's IDE says memory Exception around here.
HorsesArray[i].SetHorse((rand()%100+1),0,i);
//Speed is between 1 and 11, start position is zero, and horse number is i;
}
Temp2=Temp=Head= HorseArray;
}
void RaceTrack::StartRace(){
cout.fill('.');
/* while current horse is not at the end, increase horse position if random/rand_max is greater then 50%, show output page by page*/
while((Temp->Position < Length) && (i < Lanes)){
if(Lanes%i==0){
for(j=0;j<Lanes;j++){
cout <<"|"<<setw(Temp2->Position-1)<<Temp2->HorseNumber<<setw(Length-Temp2->Position)<<setiosflags(ios::left)<<"| \n"<<setiosflags(ios::right);
Temp2 = Temp2+1;
}
Temp2 = Head;
cout << "Please hit Enter to continue. \n";
cin.ignore(1);
}
srand(SEED);//Will make seed dependent on time when done debugging.
if(rand()/RAND_MAX > .5 ) {
Temp->Advance();
}
Temp = Temp + 1;
i++;
}
for(j=0;j<Lanes;j++){
cout <<"|"<<setw(Temp2->Position-1)<<Temp2->HorseNumber<<setw(Length-Temp2->Position)<<setiosflags(ios::left)<<"| \n"<<setiosflags(ios::right);
Temp2 = Temp2+1;
}
Temp2 = Head;
cout<< "The winning Horse is: Horse Number:... " << Temp->HorseNumber <<"!! \n";
/*Ties are impossible by how Position is evaluated, first horse evaluated to have position greater then Length is the winner.*/
cout.fill(' ');
}
int main() {
srand(SEED);//Will make seed dependent on time when done debugging.
int Length = rand()%10 + 11; //Random number between 11 and 110;
int Lanes = rand()%100/2 + 2; // Random Number between 2 and 7;
RaceTrack TheRace(Length,Lanes);
TheRace.StartRace();
return 0;
}
|