-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
144 lines (131 loc) · 4.56 KB
/
Copy pathmain.cpp
File metadata and controls
144 lines (131 loc) · 4.56 KB
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
#include <algorithm>
#include <asm-generic/ioctls.h>
#include <bits/stdc++.h>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[]) {
// Terminal colors mapping
// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
const std::pmr::map<std::string, int> COLORS = {
{"black", 30},
{"red", 31},
{"green", 32},
{"yellow", 33},
{"blue", 34},
{"magenta", 35},
{"cyan", 36},
{"white", 37}
};
std::string vertical_color = "red";
std::string horizontal_color = "blue";
char vertical_symbol = '1';
char horizontal_symbol = '2';
int step_time = 100;
float density = 0.5;
for (int i = 1; i < argc; i++) {
printf("%s\n", argv[i]);
if (strcmp(argv[i], "--vertical-color") == 0 || strcmp(argv[i], "-vc") == 0) {
vertical_color = argv[i + 1];
std::transform(vertical_color.begin(), vertical_color.end(), vertical_color.begin(), ::tolower);
continue;
}
if (strcmp(argv[i], "--horizontal-color") == 0 || strcmp(argv[i], "-hc") == 0) {
horizontal_color = argv[i + 1];
std::transform(horizontal_color.begin(), horizontal_color.end(), horizontal_color.begin(), ::tolower);
continue;
}
if (strcmp(argv[i], "--vertical-symbol") == 0 || strcmp(argv[i], "-vs") == 0) {
vertical_symbol = argv[i + 1][0];
continue;
}
if (strcmp(argv[i], "--horizontal-symbol") == 0 || strcmp(argv[i], "-hs") == 0) {
horizontal_symbol = argv[i + 1][0];
continue;
}
if (strcmp(argv[i], "--step-time") == 0 || strcmp(argv[i], "-st") == 0) {
step_time = atoi(argv[i + 1]);
continue;
}
if (strcmp(argv[i], "--density") == 0 || strcmp(argv[i], "-d") == 0) {
density = atof(argv[i + 1]);
continue;
}
}
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
std::srand(std::time({}));
int lattice[w.ws_row][w.ws_col];
for (int i = 0; i < w.ws_row; i++) {
for (int j = 0; j < w.ws_col; j++) {
lattice[i][j] = 0;
}
}
int vehicle_count = int(density * (w.ws_row * w.ws_col));
while (vehicle_count > 0) {
int i = std::rand() % w.ws_row;
int j = std::rand() % w.ws_col;
if (lattice[i][j] == 0) {
lattice[i][j] = std::rand() % 2 + 1;
vehicle_count -= 1;
}
}
bool step = true;
while (1) {
// 1. Printing to the terminal
for (int i = 0; i < w.ws_row; i++) {
for (int j = 0; j < w.ws_col; j++) {
if (lattice[i][j] > 0) {
if (lattice[i][j] > 2) {
lattice[i][j] = lattice[i][j] - 2;
}
if (lattice[i][j] == 1) {
printf("\033[%dm%c", COLORS.at(vertical_color), vertical_symbol);
} else {
printf("\033[%dm%c", COLORS.at(horizontal_color), horizontal_symbol);
}
} else {
printf(" ");
}
}
printf("\n");
}
// 2. Updating the lattice
for (int i = 0; i < w.ws_row; i++) {
for (int j = 0; j < w.ws_col; j++) {
if (lattice[i][j] == 1 && step) {
if (i < w.ws_row - 1) {
if (lattice[i + 1][j] == 0) {
lattice[i + 1][j] = 3; lattice[i][j] = 0;
}
} else {
if (lattice[0][j] == 0) {
lattice[0][j] = 3; lattice[i][j] = 0;
}
}
} else if (lattice[i][j] == 2 && !step) {
if (i < w.ws_col - 1) {
if (lattice[i][j + 1] == 0) {
lattice[i][j + 1] = 4; lattice[i][j] = 0;
}
} else {
if (lattice[i][0] == 0) {
lattice[i][0] = 4; lattice[i][j] = 0;
}
}
}
}
}
step = !step;
std::this_thread::sleep_for(std::chrono::milliseconds(step_time));
}
printf("\033[39;49m"); // reset to default terminal colors
return 0;
}