Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************
- * *
- * Written by Mikaiyl i3nigma <[email protected]> *
- * *
- * This program simulates a derby style horse race. *
- * *
- *******************************************************/
- #include <iostream>
- #include <stdio.h>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- bool winner = false;
- /************************
- * Create Horse Class *
- ************************/
- class Horse {
- private:
- char name;
- int position = 0;
- public:
- Horse(void);
- void incriment(void);
- void printP(void);
- void setName(char n);
- void isWinner(void);
- int getPos(void);
- };
- /* Horse constructor, set default name to '0' */
- Horse::Horse(void){
- name = '0';
- }
- /* Method to move horse randomly */
- void Horse::incriment(void){
- if(rand() % 100 > 50 && position < 20){
- position++;
- } else {}
- }
- /* Print horse's position */
- void Horse::printP(void){
- char p[20];
- for(int i = 0; i < 22; i++){
- if(i == position){
- p[i] = name;
- } else {
- p[i] = '.';
- }
- }
- printf("%s\n", p);
- }
- /* Set horse's name */
- void Horse::setName(char n){
- name = n;
- }
- /* Get horses position */
- int Horse::getPos(void){
- return position;
- }
- /* Check for winning horse */
- void Horse::isWinner(void){
- if( winner == false && position == 20){
- printf("Horse %c wins!\n", name);
- winner = !winner;
- }
- }
- int main(){
- /* Seed rand() */
- srand(time(NULL));
- /* Create five instances of horse class */
- Horse a, b, c, d, e;
- /* Name the horses */
- a.setName('0');
- b.setName('1');
- c.setName('2');
- d.setName('3');
- e.setName('4');
- do {
- /* Randomly incriment the horses */
- a.incriment();
- b.incriment();
- c.incriment();
- d.incriment();
- e.incriment();
- /* Print the horses */
- a.printP();
- b.printP();
- c.printP();
- d.printP();
- e.printP();
- /* Check for a winner */
- a.isWinner();
- b.isWinner();
- c.isWinner();
- d.isWinner();
- e.isWinner();
- /* Prompt user to continue */
- cout << "Press enter for another turn:";
- cin.ignore();
- } while( a.getPos() < 20 || b.getPos() < 20 || c.getPos() < 20 || d.getPos() < 20 || e.getPos() < 20);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment