-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speedway.cpp
184 lines (164 loc) · 4.39 KB
/
Speedway.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Speedway.h"
#include "colors.h"
#include <stdlib.h>
#include <stdio.h>
#include <iterator>
#include <fstream>
#include <unistd.h>
#include <vector>
#include <algorithm>
vector <string> Rider::names;
vector <string> Rider::exclusions;
void Rider::init()
{
ifstream file("names.dat");
copy(istream_iterator<string>(file),
istream_iterator<string>(),
back_inserter(names));
file.close();
}
void Rider::initArbiter()
{
ifstream file("exclusions.dat");
copy(istream_iterator<string>(file),
istream_iterator<string>(),
back_inserter(exclusions));
file.close();
}
int randomValue()
{
return 10 + rand() % 90;
}
Rider::Rider()
{
static int amountOfNames = (init(), names.size());
_name = names[rand() % amountOfNames];
_reaction = randomValue();
_acceleration = randomValue();
_stress = randomValue();
_luck = randomValue();
}
string Rider::description()
{
return _name + "\n\tReaction:" + to_string(_reaction) +
" Acceleration:" + to_string(_acceleration) + " Stress:" + to_string(_stress)+ " Luck:" + to_string(_luck)+ "\n";
}
string Rider::arbiter()
{
static int amountOfExclusions = (initArbiter(), exclusions.size());
return exclusions[rand()%amountOfExclusions].c_str();
}
double Rider::way(int value)
{
return _reaction+(_acceleration*value*value/2);
}
void start()
{
for (int i = 3; i >0; i--)
{
textcolor(BRIGHT, CYAN);
usleep(TIME_SEC);
printf("\n%14d\n", i);
}
usleep(TIME_SEC);
printf("\n%18s\n", "START!!");
}
void textcolor(int attr, int fg)
{
char command[13];
sprintf(command, "%c[%d;%dm", 0x1B, attr, fg + 30);
printf("%s", command);
}
void timeSurvey(std::vector < double > &tab, Rider riders[])
{
double times[N];
double timesLap[16];
double difference[16];
int k=0;
for (int i = 0; i < N; i++)
{
textcolor(BRIGHT, WHITE);
printf("\n%s%9d %s\n",":::::::::::::::::::", i+1, "Lap:");
for (int j = 0; j< N; j++)
{
bool ifFinish=true;
int wayStart=0;
double wayLenght=0;
clock_t begin, end;
time( &begin );
textcolor(BRIGHT, j+1);
printf("%s\n", riders[j].name().c_str());
while(ifFinish)
{
usleep(TIME_INTERVAL);
wayStart++;
wayLenght += ((riders[j].stress())/100+riders[j].acceleration()*(i+1)/10+randomValue()/100)*wayStart*wayStart/2;
printf("%20s\n", "*");
if(wayLenght<4000)
ifFinish=true;
else
ifFinish=false;
}
time( &end);
difference[k] = 10 + difftime( end, begin ) + riders[j].reaction()/1000/(i+1);
printf("%s %f\n\n", "Time: ", difference[k]);
k++;
}
}
for (int i = 0; i < N; i++)
{
times[i]+=difference[i]+difference[i+4]+difference[i+8]+difference[i+12];
tab.push_back( times[i] );
}
textcolor(BRIGHT, WHITE);
printf("%s\n%19s\n", "::::::::::::::::::::::::::::::::::", "FINISH!");
}
void classification(std::vector < double > &tab, Rider riders[])
{
std::vector < double > tabCopy;
tabCopy=tab;
int whoWin[N];
sort( tabCopy.begin(), tabCopy.end() );
for( int i = 0; i < N; i++ )
{
if (tabCopy[0]==tab[i])
{
whoWin[0]=i;
}
if (tabCopy[1]==tab[i])
{
whoWin[1]=i;
}
if (tabCopy[2]==tab[i])
{
whoWin[2]=i;
}
if (tabCopy[3]==tab[i])
{
whoWin[3]=i;
}
}
for (int i = 0; i < N; i++)
{
if(riders[whoWin[i]].luck()>EX_RATIO)
{
printf("\n\n %d %s %s %s %.3f\n", i+1, "|", riders[whoWin[i]].name().c_str(), "Time:", tabCopy[i]);
}
else
{
printf("\n\n %d %s %s %s %.3f\n", i+1, "|", riders[whoWin[i]].name().c_str(), "Time:", tabCopy[i]);
textcolor(BRIGHT,RED);
printf("\t%s %s", "EXCLUDED!!:", riders[whoWin[i]].arbiter().c_str());
textcolor(BRIGHT,WHITE);
}
}
textcolor(BRIGHT, GREEN);
if(riders[whoWin[0]].luck()<EX_RATIO && riders[whoWin[1]].luck()<EX_RATIO && riders[whoWin[2]].luck()<EX_RATIO)
printf("\n\n%s %s %s %.3f\n", "WINNER:", riders[whoWin[3]].name().c_str(), "Time:", tabCopy[3]);
else if(riders[whoWin[0]].luck()<EX_RATIO && riders[whoWin[1]].luck()<EX_RATIO)
printf("\n\n%s %s %s %.3f\n", "WINNER:", riders[whoWin[2]].name().c_str(), "Time:", tabCopy[2]);
else if(riders[whoWin[0]].luck()<EX_RATIO)
printf("\n\n%s %s %s %.3f\n", "WINNER:", riders[whoWin[1]].name().c_str(), "Time:", tabCopy[1]);
else
printf("\n\n%s %s %s %.3f\n", "WINNER:", riders[whoWin[0]].name().c_str(), "Time:", tabCopy[0]);
}