Horse race

*HEADER HORSE*

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
/**
 * Header file for Horse-class
 *
 * @author Nami Shah
 * @date 2012/01/23
 */

#ifndef INC_HORSE_H
#define INC_HORSE_H

#include <string>
#include <vector>
#include <list>


using namespace std;

/**
 * Class for horse
 */
class Horse {
	private:
		string name;
		int cur_pos;
		vector<string> traject;
	public:
		Horse(const string);
		void printH();
		void resetH();
		void forwardH();
		bool isFinished();
		string getNameH();
		friend bool operator== (Horse&, Horse&);
};

#endif /* INC_HORSE_H */ 


*CPP HORSE*

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
/**
 * Source file for Horse-class
 *
 * @author Nami Shah
 * @date 2012/01/23
 */

#include <iostream>
#include <vector>
#include <cstdlib>
#include <sstream>
#include "Horse.h"

Horse::Horse(const string name){
	Horse::name = "#"+name;
	Horse::cur_pos = 0;
	Horse::traject.resize(41);
	Horse::traject[0] = Horse::name;
	for(int i=1; i<39; i++){
		Horse::traject[i] = " ";
	}
	Horse::traject[39] = "|";
	
}

void Horse::printH(){
	for(int i=1;i<41;i++){
		cout << Horse::traject[i];
	}
	cout << endl;
}
void Horse::resetH(){
	Horse::cur_pos = 0;
	Horse::traject.resize(41);
	Horse::traject[0] = Horse::name;
	for(int i=1; i<39; i++){
		Horse::traject[i] = " ";
	}
	Horse::traject[39] = "|";
	Horse::traject[40] = "";
}
void Horse::forwardH(){
	if(Horse::isFinished()){
		return;
	}

	int n = rand() % 2 + 1; //random nummer tussen 1 en 2
	for(int i=0; i<n;i++){
		Horse::traject[cur_pos] = ".";
		cur_pos++;
		if(Horse::isFinished()){
			break;
		}
	}
	Horse::traject[cur_pos] = Horse::name;
	switch(cur_pos){
		case 40:
			Horse::traject[cur_pos-1] = "|";
		break;
	}
	

}
bool Horse::isFinished(){
	switch(Horse::cur_pos){
		case 40:
			return true;
		break;
		default:
			return false;
		break;
	}

}
string Horse::getNameH(){
	return Horse::name;
}

bool operator== (Horse& horse1, Horse& horse2){
    if (horse1.name == horse2.name) {
        return true;
    }
    return false;
}


*HEADER PLAYER*

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
/**
 * Header file for Player-class
 *
 * @author Nami Shah
 * @date 2012/01/23
 */

#ifndef INC_PLAYER_H
#define INC_PLAYER_H

#include "Horse.h"


using namespace std;

/**
 * Class for player
 */
class Player {
	private:
		string name;
		list<Horse> p_horse;
	public:
		Player(const string&);
		void addHorse(const Horse);
		void removeHorse(Horse);
		void printP();
		int count();
		int countFinished();
		void forwardP();
		void resetP();
		string getNameP();
		Horse isLast();
};


#endif /* INC_PLAYER_H */ 


*CPP PLAYER*

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
/**
 * Source file for Player-class
 *
 * @author Nami Shah
 * @date 2012/01/23
 */

#include "Player.h"
#include <iostream>

using namespace std;

Player::Player(const string& name){
	Player::name = "Player "+name;
}

void Player::addHorse(const Horse horse){
	Player::p_horse.push_back(horse);

}
void Player::removeHorse(Horse horse){
	list<Horse>::iterator it = p_horse.begin();
	while(it != p_horse.end()){
		if(*it == horse){
			Player::p_horse.erase(it);
			break;
		}
		it++;
	}

}
void Player::printP(){
	cout << Player::name << ":" << endl;
	list<Horse>::iterator it = Player::p_horse.begin();
	while(it != Player::p_horse.end()){
		cout << "\t";
		it->printH();
		it++;
	}

}
int Player::count(){
	return Player::p_horse.size();
}
int Player::countFinished(){
	list<Horse>::iterator it = Player::p_horse.begin();
	int count =0;
	while(it != Player::p_horse.end()){
		if(it->isFinished()){
			count++;
		}
		it++;
	}
	return count;
}
void Player::forwardP(){

	list<Horse>::iterator it=Player::p_horse.begin();
	while(it != Player::p_horse.end()){
		it->forwardH();		
		it++;
	}
	
}

void Player::resetP(){
	list<Horse>::iterator it = Player::p_horse.begin();
	while(it != Player::p_horse.end()){
		it->resetH();
		it++;
	}
}

string Player::getNameP(){
	return Player::name;
}

Horse Player::isLast(){
	list<Horse>::iterator it = Player::p_horse.begin();
	while(it != Player::p_horse.end()){
		if(!it->isFinished()){
			break;
		}
		it++;
	}
	return *it;
}


*MAIN*

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * Main file for Paardenrace program
 *
 * @author Nami Shah
 * @date 2012/01/23
 */


#include <iostream>
#include <limits>
#include "Horse.h"
#include "Player.h"
#include <list>
#include <sstream>

using std::cout;
using std::endl;
using std::cin;


namespace derby{
    list<Player> players;   // een lijst van alle spelers

    void initialize(const int& nPlayers, const int& nHorses);   // initialiseert het spel met n players en n horses
    void printField();  // drukt het veld af
    void race();    // start de race
    void reset();   // zet de paarden naar de startpositie
    void derby();   // speelt de derby
}

void derby::initialize(const int& nPlayers, const int& nHorses) {
    for (int p = 0; p < nPlayers; p++) {
        stringstream ss;
        ss << p + 1;
        string name = ss.str();
        Player player(name);
        derby::players.push_back(player);   // Maak een lijst van de spelers
    }

    list<Player>::iterator it = derby::players.begin();
    int index = 0;
    for (int h = 0; h < nHorses; h++) {
        stringstream ss;
        ss << h + 1;
        string name = ss.str();
        Horse horse(name);
        it->addHorse(horse);    // voegt een paard toe aan de speler
        
        if (index == derby::players.size() - 1) {
            it = derby::players.begin();
            index = 0;
            continue;
        }
        it++;
        index++;
    }

}


void derby::printField() {
    cout << "==================================================" << endl;
    list<Player>::iterator it = derby::players.begin();
    while (it != derby::players.end()) {
        it->printP();
        it++;
    }
    cout << "==================================================" << endl;
}


void derby::race() {
    list<Player>::iterator it = derby::players.begin();
    while (it != derby::players.end()) {
        it->forwardP();
        it++;
    }
    derby::printField();

    it = derby::players.begin();
    int countHorses = 0;
    while (it != derby::players.end()) {
        countHorses += it->count() - it->countFinished();
        it++;
    }

    if (countHorses > 1) {
        return derby::race();   // blijf racen totdat er 1 paard overblijft
    }

    switch (countHorses) {
    case 0:
        return derby::race();   // bij een gelijkspel spelen we overnieuw
    case 1:
        it = derby::players.begin();
        while (it != derby::players.end()) {
            int c = it->count() - it->countFinished();
            if (c == 1) {
                break;
            }
            it++;
        }
        cout << "Paard " << it->isLast().getNameH() << " valt af." << endl;
        it->removeHorse(it->isLast());   // verwijdert zijn traagste paard

        if (it->count() == 0) {
            cout << it->getNameP() << " heeft geen paarden meer en valt dus weg." << endl;
            derby::players.erase(it);  // verwijder de speler als hij geen paarden meer heeft
        }
    }
}


void derby::reset() {
    list<Player>::iterator it = derby::players.begin();
    while (it != derby::players.end()) {
        it->resetP();
        it++;
    }
}


void derby::derby() {
    while (derby::players.size() > 1) {
        derby::printField();    // laat eerst de beginstatus van het veld zien
        derby::race();  // blijf racen totdat er 1 speler overblijft
        derby::reset();
    }

    cout << "De winnaar is: " << derby::players.begin()->getNameP() << "!" << endl;
}


int main(int argc, char* argv[])
{
	int numberOfPlayers, numberOfHorses;
	bool validInput = false;

	while(!validInput) {
		// Read the number of players and horses
		cout << "Sir, how many horse owners are there?" << endl;
		cin >> numberOfPlayers;

		cout << "Sir, how many horses do you want?" << endl;
		cin >> numberOfHorses;

		if((numberOfPlayers > 0) && (numberOfHorses >= numberOfPlayers)) {
			// Everything OK, continue with game
			validInput = true;
		}
		else {
			// Invalid input
			cout << "Excuse me, Sir. I believe what you said is impossible." << endl;
		}
	}

	cout << "Thank you for your help, Sir!" << endl;

	// Start the game
		derby::initialize(numberOfPlayers, numberOfHorses);
		derby::derby();
}
Last edited on
closed account (o3hC5Di1)
As you marked this topic as solved - I'm assuming there's no problem with this code and you just wanted to share it with us?

All the best,
NwN
Correct.


*Makefile*

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
all: race

race: Horse.o Player.o main.o
	g++ Horse.o Player.o main.o -o race

Horse.o: Horse.cpp
	g++ -c --pedantic -g Horse.cpp -o Horse.o

main.o: main.cpp
	g++ -c --pedantic -g main.cpp -o main.o

Player.o: Player.cpp
	g++ -c --pedantic -g Player.cpp -o Player.o
	
clean:
	rm -f *.o *.*~ *.exe race
	
tgz:    clean
	rm -f tarball.tgz
	tar -czf tarball.tgz *
	chmod 644 tarball.tgz
Last edited on
Topic archived. No new replies allowed.