forked from R3DHULK/cpp-for-gamers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar-racing.cpp
70 lines (60 loc) · 1.8 KB
/
car-racing.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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to generate a random number between min and max (inclusive)
int random(int min, int max)
{
return rand() % (max - min + 1) + min;
}
int main()
{
srand(time(NULL)); // Seed the random number generator with the current time
// Set up the game
const int trackLength = 20;
const int numCars = 3;
int carPositions[numCars] = {0};
char track[trackLength];
for (int i = 0; i < trackLength; i++)
{
track[i] = '-';
}
// Play the game
while (true)
{
// Update the positions of the cars
for (int i = 0; i < numCars; i++)
{
int move = random(1, 3); // Randomly move the car 1-3 spaces
carPositions[i] += move;
if (carPositions[i] >= trackLength)
{
carPositions[i] = trackLength - 1; // Ensure the car doesn't go past the end of the track
}
}
// Update the track based on the new positions of the cars
for (int i = 0; i < trackLength; i++)
{
track[i] = '-';
}
for (int i = 0; i < numCars; i++)
{
track[carPositions[i]] = '0' + i; // Use the character representing the car number to show its position on the track
}
// Print the updated track
cout << track << endl;
// Check if any of the cars have won
for (int i = 0; i < numCars; i++)
{
if (carPositions[i] == trackLength - 1)
{
cout << "Car " << i << " wins!" << endl;
return 0;
}
}
// Wait for a moment before updating the game again
system("sleep 0.5"); // Sleep for half a second
}
return 0;
}