#include #include #include #include #include #include #include "Race.h" #include "Horse.h" // race as in event and race as in track Race::Race(){ length = 32; // default length numHorses = 4; // default number of horses horses = new Horse[numHorses]; // make some horses } Race::Race(int length){ this->length = length; // defined length this->numHorses = 4; // default number of horses horses = new Horse[numHorses]; // make some horses } // formatted output std::string Race::raceToString(){ std::string out = ""; int i = 0; for (i = 0; i < numHorses; i++){ out = out + laneToString(i) + "\n"; } return out; } std::string Race::laneToString(int lane){ std::string out = ""; out = out + std::to_string(lane + 1) + "|"; int i = 0; for (i = 0; i < length; i++){ // prints the horses number at its latest position if (i == horses[lane].getPosition()){ out = out + std::to_string(lane + 1); // prints a lane visual } else { out = out + "-"; } // end if } // end for out = out + "|"; // out = out + std::endl; // newline and stream flush return out; } // prints one lane void Race::printLane(int lane){ // which lane int i = 0; for (i = 0; i < length; i++){ // prints the horses number at its latest position if (i == horses[lane].getPosition()){ std::cout << lane; // prints a lane visual } else { std::cout << "-"; } // end if } // end for std::cout << std::endl; // newline and stream flush } // all the horses flip a coin and advance accordingly void Race::step(){ int i = 0; // all horses in the race for (i = 0; i < numHorses; i++){ horses[i].advance(); // horses know how to advance } } // prints each lane void Race::printAllLanes(){ int i = 0; for (i = 0; i < numHorses; i++){ printLane(i); } } // checks if any horses have won or tied bool Race::isFinished(){ int i = 0; // all horses in the race for (i = 0; i < numHorses; i++){ if (horses[i].getPosition() == length){ return true; // one or more winners } } return false; } // who won the race? int Race::getWinner(){ int i = 0; for (i = 0; i < numHorses; i++){ // if horse at i finished... if (horses[i].getPosition() == length){ return (i + 1); // winner by number } } return -1; } // prints winner(s) void Race::printWinners(){ std::cout << "horse(s)"; int i = 0; // check all the horses for (i = 0; i < numHorses; i++){ // if horse at i finished... if (horses[i].getPosition() == length){ std::cout << " " << i; // ...horse at i won! } } std::cout << " won (tied)!" << std::endl; } // log the winner void Race::logWinner(){ std::ofstream fout ("winners.log", std::ios::app); fout << std::to_string(getWinner()); // log by number not index fout.close(); }