I wrote this code on horse race simulation but how can i make it work with proce
ID: 3747707 • Letter: I
Question
I wrote this code on horse race simulation but how can i make it work with procedural style instead of using objects.
#include <stdio.h> //printf and NULL
#include <iostream>
#include <stdlib.h> //srand and rand
#include <string>
#include <iomanip>
using namespace std;
class Horse {
public:
int place;
bool bWon;
Horse(const int& iPos = 0) : place(iPos), bWon(false) {}
void move_horse()
{
place += rand() % 2;
}
void win()
{
bWon = true;
}
int getplace(){
return place;
}
};
class Horse_Race{
public:
Horse *h;
int length;
int iCount;
bool bEnd;
int winner;
Horse_Race(int iHorses = 2, int iLen = 10){
iCount = iHorses;
h = new Horse[iCount];
length = iLen;
bEnd = false;
}
Horse_Race()
{
delete[] h;
h = NULL;
}
void print()
{
cout << endl;
for (int i = 0; i < iCount; i++) {
for (int j = 0; j < h[i].getplace(); j++) {
cout << ".";
}
cout << "(" << i << ")";
for (int l=h[i].getplace(); l < 15; l++) {
cout << ".";
}
cout<<endl;
}
}
bool is_end()
{
return bEnd;
}
void run()
{
if (bEnd) return;
for (int i = 0; i < iCount; i++) {
h[i].move_horse();
if (h[i].getplace() == length) {
h[i].win();
bEnd = true;
winner=i;
}
}
}
void res(){
cout<<"Horse "<<winner<<" wins!";
}
};
int main(){
srand(time(NULL));
Horse_Race r(5, 15);
while(!r.is_end()) {
cout<<"Press enter for another turn:";
if(cin.get()==' '){
r.run();
r.print();
}
}
r.res();
}
Explanation / Answer
#include
#include
#include
#define NumberHorses 10
#define SpaceHorses 15
#include
void showhorse(int HorseNumber, int HorsesPosition) {
int X;
printf(" %d:", HorseNumber);
for(X = 0 ; X = 1 && y printf("Start GO! ");
srand( (unsigned)time(NULL));
showhorse(1,Ha);
showhorse(2,Hb);
showhorse(3,Hc);
showhorse(4,Hq);
showhorse(5,Hw);
showhorse(6,He);
showhorse(7,Hr);
showhorse(8,Ht);
showhorse(9,Hy);
showhorse(10,Hu);
while(Ha < SpaceHorses && Hb < SpaceHorses && Hc < SpaceHorses && Hq < SpaceHorses && Hw < SpaceHorses && He < SpaceHorses && Hr < SpaceHorses && Ht < SpaceHorses && Hy < SpaceHorses && Hu < SpaceHorses) { if((rand () % 100)>50)
++Ha;
if((rand () % 100)>50)
++Hb;
if((rand () % 100)>50)
++Hc;
if((rand () % 100)>50)
++Hq;
if((rand () % 100)>50)
++Hw;
if((rand () % 100)>50)
++He;
if((rand () % 100)>50)
++Hr;
if((rand () % 100)>50)
++Ht;
if((rand () % 100)>50)
++Hy;
if((rand () % 100)>50)
++Hu;
printf(" ");
printf(" ======================= ");
showhorse(1,Ha);
showhorse(2,Hb);
showhorse(3,Hc);
showhorse(4,Hq);
showhorse(5,Hw);
showhorse(6,He);
showhorse(7,Hr);
showhorse(8,Ht);
showhorse(9,Hy);
showhorse(10,Hu);
printf(" =======================");
printf(" ");
Sleep(1200);
}
if(Ha == 15 && Hb == 15 || Ha == 15 && Hc == 15 || Hc == 15 && Hb == 15 ) {
printf("Tie ");
}
else if (Ha == 15){
printf("Congratulations Horse 1 - Star ");
}
else if (Hb == 15){
printf("Congratulations Horse 2 - Dakota ");
}
else if (Hc == 15){
printf("Congratulations Horse 3 - Cheyenne ");
}
else if (Hq == 15){
printf("Congratulations Horse 4 - Spirit ");
}
else if (Hw == 15){
printf("Congratulations Horse 5 - Misty ");
}
else if (He == 15){
printf("Congratulations Horse 6 - Cowboy ");
}
else if (Hr == 15){
printf("Congratulations Horse 7 - Honey ");
}
else if (Ht == 15){
printf("Congratulations Horse 8 - Duke ");
}
else if (Hy == 15){
printf("Congratulations Horse 9 - Rocky ");
}
else if (Hu == 15){
printf("Congratulations Horse 10 - Lady ");
}
printf(" I hope that everybody enjoy the race, see you next time! ");
printf("============================================================== ");
printf(" By: XD Creations");
printf(" ");
printf(" ");
}
else {
printf("This horses is not in the race. Please try again and choose between 1 to 10. ");
}
system("PAUSE");
return 0;
}