aboutsummaryrefslogtreecommitdiff
path: root/week09/Exam1/DroneShow.cpp
blob: aaffc8f0b97c8daa46c72b3823981d3d235e4990 (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
#include "DroneShow.h"
#include <iostream>
#include <cstring>

/* Голяма петица */
void DroneShow::free() {
	delete[] name;
for (int i = 0; i < 1024; i++) {
		delete drones[i];
	}
}
void DroneShow::copyFrom(const DroneShow& other) {
	name = new char[strlen(other.name) + 1];
	strcpy(name, other.name);
	for (int i = 0; i < 1024; i++) {
		drones[i] = new Drone(*other.drones[i]);
	}
}

DroneShow::DroneShow() {
	name = nullptr;
	for (int i = 0; i < 1024; i++) {
		drones[i] = nullptr;
	}
}

DroneShow::~DroneShow() {
	free();
}

DroneShow::DroneShow(const DroneShow& other) {
	copyFrom(other);
}

DroneShow& DroneShow::operator=(const DroneShow& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

DroneShow::DroneShow(DroneShow&& other) {
	this->name = other.name;
	other.name = nullptr;

	for (int i = 0; i < 1024; i++) {
		this->drones[i] = other.drones[i];
		other.drones[i] = nullptr;
	}
}

DroneShow& DroneShow::operator=(DroneShow&& other) {
	if (this != &other) {
		free();

		this->name = other.name;
		other.name = nullptr;

		for (int i = 0; i < 1024; i++) {
			this->drones[i] = other.drones[i];
			other.drones[i] = nullptr;
		}
	}
	return *this;
}

/* Метод от 2.2 */
bool DroneShow::isEmpty(int index) {
	return drones[index] == nullptr;
}

/* Конструктор от 2.2 */
DroneShow::DroneShow(const char* name, Drone* drones[1024]) {
	this->name = new char[strlen(name) + 1];
	strcpy(this->name, name);
	for (int i = 0; i < 1024; i++) {
		this->drones[i] = new Drone(*drones[i]); // Директно извикваме копиращия конструктор
	}
}

/* Метод от 2.2 */
void DroneShow::play() {
	try {
		while(true) { // за всяка стъпка от шоуто
			for (int i = 0; i < 1024; i++) { // за всеки дрон
				if (isEmpty(i)) continue; // правим го за да не пишем код в още къдрави скоби

				// Всички дронове имат еднакъв брой въведени позиции, тоест първият дрон,
				// който се опита да отиде на следваща несъществуваща позиция ще хвърли грешка и това описва
				// края на шоуто
				Coordinate c = drones[i]->positionAndMove();
				std::cout << c.x << ", " << c.y << std::endl;
			}
		}
	}
	catch(const char* error) {
		// Няма какво да правим по условие
	}
}