forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswimming.cpp
84 lines (68 loc) · 2.15 KB
/
swimming.cpp
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
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
// Define the player's swimmer class
class Swimmer {
public:
Swimmer(string name, int speed) {
this->name = name;
this->speed = speed;
this->distance = 0;
}
string getName() {
return name;
}
int getSpeed() {
return speed;
}
int getDistance() {
return distance;
}
void swim() {
int distance_swimmed = rand() % (speed + 1);
distance += distance_swimmed;
cout << name << " swam " << distance_swimmed << " meters." << endl;
}
private:
string name;
int speed;
int distance;
};
// Define a function to generate a random swimmer
Swimmer generateSwimmer() {
string names[] = {"Alice", "Bob", "Charlie", "David", "Emily", "Frank", "Grace", "Henry", "Isabelle", "Jack"};
int speeds[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int random_index = rand() % 10;
return Swimmer(names[random_index], speeds[random_index]);
}
int main() {
srand(time(0));
cout << "Welcome to the swimming pool game!" << endl;
// Generate three swimmers
Swimmer swimmer1 = generateSwimmer();
Swimmer swimmer2 = generateSwimmer();
Swimmer swimmer3 = generateSwimmer();
// Display the swimmers' names and speeds
cout << "Swimmer 1: " << swimmer1.getName() << " (speed: " << swimmer1.getSpeed() << ")" << endl;
cout << "Swimmer 2: " << swimmer2.getName() << " (speed: " << swimmer2.getSpeed() << ")" << endl;
cout << "Swimmer 3: " << swimmer3.getName() << " (speed: " << swimmer3.getSpeed() << ")" << endl;
// Loop until one swimmer has swum 50 meters
while (swimmer1.getDistance() < 50 && swimmer2.getDistance() < 50 && swimmer3.getDistance() < 50) {
swimmer1.swim();
swimmer2.swim();
swimmer3.swim();
}
// Determine the winner
Swimmer winner;
if (swimmer1.getDistance() >= 50) {
winner = swimmer1;
} else if (swimmer2.getDistance() >= 50) {
winner = swimmer2;
} else {
winner = swimmer3;
}
// Display the winner
cout << winner.getName() << " wins!" << endl;
return 0;
}