aboutsummaryrefslogtreecommitdiff
path: root/week02/Exercise3.cpp
blob: 4ea53329f92e25bd6aa6c98fbbeded2c26583a39 (plain) (blame)
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
#include <iostream>
#include <random>

/* Решението изглежда странно, идеята на тази задача беше да се упражни употреба на указатели */

int randBetween(int a, int b) {
	return a + rand() % (b + 1);
}

int min(int a, int b) {
	return (a > b) ? a : b;
}

struct Car {
	int row;
	int col;
	int speed;

	Car(int rows, int cols) {
		row = randBetween(0, rows - 1);
		col = randBetween(0, cols - 1);
		speed = randBetween(4, 6);
	}

	int ForwardCol() {
		return row + speed;
	}

	void MoveForward() {
		col += speed;
		if (speed == 0) {
			speed = 1;
		}
		else if (speed < 8) {
			speed = min(8, randBetween(0, 3));
		}
	}

	bool TryCrash(Car& crashWith) {
		if (ForwardCol() < crashWith.col) return false;

		speed /= 2;
		if (ForwardCol() < crashWith.col) return false;

		crashWith.speed = speed = 0;
		col = crashWith.col - 1;
		return true;
	}
};

struct Highway {
private:
	Car** cars; // Динамично-заделен масив от динамично-заделени коли
	int carsSize;
	int carsCount;

	int rows;
	int cols;

	void initCars() {
		for (int i = 0; i < carsSize; i++) {
			cars[i] = new Car(rows, cols);
		}
		carsCount = carsSize;
	}

	Car** getCar(int row, int col) {
		for (int i = 0; i < carsSize; i++) {
			if (cars[i] != nullptr && cars[i]->row == row && cars[i]->col == col) {
				return &cars[i];
			}
		}
		return nullptr;
	}

	bool moveForward(Car& current, int row, int startCol, int endCol) {
		for (int col = startCol + 1; col < endCol; col++) {
			Car** collidesWith = getCar(row, col);
			if (collidesWith != nullptr && current.TryCrash(**collidesWith)) {
				return false;
			}
		}
		current.MoveForward();
		return true;
	}

public:
	Highway(int rows, int cols, int size) {
		this->rows = rows;
		this->cols = cols;
		this->carsSize = this->carsCount = size;
		cars = new Car*[carsSize];
	}
	~Highway() {
		for (int i = 0; i < carsSize; i++) {
			delete cars[i];
		}
		delete[] cars;
	}

	int SimulateStep() {
		int crashes = 0;
		for (int j = cols - 1; j >= 0; j--) {
			for (int i = 0; i < rows; i++) {
				Car** current = getCar(i, j);
				if (current == nullptr) continue;
				crashes += !moveForward(**current, i, j, (*current)->ForwardCol());

				if ((*current)->col >= cols) {
					delete *current;
					*current = nullptr;
					carsCount--;
				}
			}
		}
		return crashes;
	}

	void Simulate() {
		initCars();

		int iterations = 1, crashes = 0;
		while (carsCount > 0) {
			crashes += SimulateStep();
			iterations++;
		}

		std::cout << "Iters: " << iterations << " Crashes: " << crashes << std::endl;
	}
};

int main() {
	Highway h1(1, 10, 10);
	h1.Simulate();
}