aboutsummaryrefslogtreecommitdiff
path: root/week03/Exercise8.cpp
blob: 3019efa9450548ad1637be840167df34b6bec7eb (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
struct ResidentialBuilding {
	float apartmentPrice;
	unsigned floors;
	unsigned peoplePerFloor;
	bool hasBasement;
	bool hasAttic;
	bool hasParking;
};

struct CommercialBuilding {
	float shopPrice;
	unsigned parkingFloors;
	unsigned shopFloors;
	unsigned shopsPerFloor;
};

struct City {
private:
	struct ResidentialPair {
		ResidentialBuilding* residential;
		unsigned tenants;
	};
	struct CommercialPair {
		CommercialBuilding* commercial;
		unsigned shops;
	};

	ResidentialPair* residentials;
	int lastResidential;
	CommercialPair*  commercials;
	int lastCommercial;

	// 0  - no building
	// >0 - index from residentials, starting from 1
	// <0 - index from commercials, starting from -1
	int** city;
	int size;

	bool cityFull() {
		return lastResidential == size * size || lastCommercial == size * size;
	}

public:
	City() {
		size = 5;
		lastResidential = lastCommercial = 0;

		city = new int*[size];
		for (int i = 0; i < size; i++) {
			city[i] = new int[size];
			for (int j = 0; j < size; j++) {
				city[i][j] = 0;
			}
		}

		residentials = new ResidentialPair[size * size];
		commercials  = new CommercialPair[size * size];
	}
	~City() {
		for (int i = 0; i < lastResidential; i++) {
			delete residentials[i].residential;
		}
		delete[] residentials;
		for (int i = 0; i < lastCommercial; i++) {
			delete commercials[i].commercial;
		}
		delete[] commercials;
		for (int i = 0; i < size; i++) {
			delete[] city[i];
		}
		delete[] city;
	}

	void BuildResidential(const ResidentialBuilding& rb, unsigned tenants, int coordX, int coordY) {
		if (cityFull() || city[coordX][coordY] != 0) return;

		residentials[lastResidential] = { new ResidentialBuilding(rb), tenants };
		city[coordX][coordY] = lastResidential + 1;
		lastResidential++;
	}

	void BuildCommercial(const CommercialBuilding& cb, unsigned shops, int coordX, int coordY) {
		if (cityFull() || city[coordX][coordY] != 0) return;

		commercials[lastCommercial] = { new CommercialBuilding(cb), shops };
		city[coordX][coordY] = -(lastCommercial + 1);
		lastCommercial++;
	}

	void DemolishBuilding(int coordX, int coordY) {
		int& index = city[coordX][coordY];
		if (index == 0) {
			return;
		}

		if (index > 0) {
			index -= 1;
			delete residentials[index].residential;
			for (int i = index; i < lastResidential - 1; i++) {
				residentials[i] = residentials[i+1];
			}
			lastResidential--;
			residentials[lastResidential] = { nullptr, 0 };
		}
		else {
			index = -index - 1;
			delete commercials[index].commercial;
			for (int i = index; i < lastResidential - 1; i++) {
				commercials[i] = commercials[i+1];
			}
			lastCommercial--;
			commercials[lastCommercial] = { nullptr, 0 };
		}
		index = 0;
	}
};

int main() {
	City city;
	city.BuildResidential({ 10000.0, 6, 7, true, true, false }, 21, 0, 0);
	city.BuildResidential({ 100000.0, 5, 5, false, true, false }, 10, 2, 2);
	city.BuildCommercial({ 12000.0, 0, 2, 1 }, 1, 2, 3);
	city.DemolishBuilding(2, 3);
	city.BuildCommercial({ 130800.0, 2, 5, 4 }, 8, 2, 3);
}