aboutsummaryrefslogtreecommitdiff
path: root/week09/Exam1/Drone.cpp
blob: 6895a74255613fcfc836ff4d87147bfbe93a74cb (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
136
137
138
139
140
#include "Drone.h"
#include <cstring>
#include <fstream>
#include <iostream>

/* Голяма четворка */

void Drone::free() {
	delete[] id;
}
void Drone::copyFrom(const Drone& other) {
	id = new char[strlen(other.id) + 1];
	strcpy(id, other.id);
	strcpy(generatedPath, other.generatedPath);
	position = other.position;
}

Drone::Drone() {
	id = nullptr;
	generatedPath[0] = '\0';
	position = 0;
}
Drone::~Drone() {
	free();
}
Drone::Drone(const Drone& other) {
	copyFrom(other);
}
Drone& Drone::operator=(const Drone& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

/* Move семантики */

Drone::Drone(Drone&& other) {
	id = other.id;
	other.id = nullptr;

	strcpy(generatedPath, other.generatedPath);
	position = other.position;
}

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

		id = other.id;
		other.id = nullptr;

		strcpy(generatedPath, other.generatedPath);
		position = other.position;
	}
	return *this;
}

/* Метод от 1.2 */
void Drone::printGeneratedPath() {
	std::ifstream inFile(generatedPath, std::ios::binary);
	if (!inFile.is_open()) {
		throw "Could not open generatedPath!"; // ок е и вместо това да се направи "return;"
	}

	while(!inFile.eof()) {
		double x, y;
		// Не е казано формата на този файл какъв е, затова допускаме най-простия възможен:
		// една голяма последователност от double-и, първия е х1, втория е y1, третия е x2, четвъртия е y2 и так. нат.
		inFile.read((char*)&x, sizeof(x));
		inFile.read((char*)&y, sizeof(y));

		std::cout << x << ", " << y << std::endl;
	}

	inFile.close();
}

/* Метод от 1.2 */
Coordinate Drone::moveWithOneStep() {
	std::ifstream inFile(generatedPath, std::ios::binary);
	if (!inFile.is_open()) {
		throw "Could not open generatedPath!"; // ок е вместо това да бъде "return;"
	}

	double x, y;
	for (int i = 0; i <= position + 1 && !inFile.eof(); i++) {
		inFile.read((char*)&x, sizeof(x));
		inFile.read((char*)&y, sizeof(y));
	}
	// Ако във файла не съществува координат на position+1
	if (inFile.eof()) {
		inFile.close();
		throw "No next position";
	}

	std::cout << x << ", " << y << std::endl;

	position++;
	inFile.close();
	return { x, y }; // Еквивалентно на "return Coordinate({ x, y });"
}

/* Помощна за 2.2, метод play */
// Кодът е почти еднакъв с moveWithOneStep, обаче при moveWithOneStep първо местим и връщаме новата позиция,
// докато тук връщаме сегашната и след това местим
// Хубаво е логиката да се изкара в някаква обща член-функция, но като за контролно така е коректно
Coordinate Drone::positionAndMove() {
	std::ifstream inFile(generatedPath, std::ios::binary);
	if (!inFile.is_open()) {
		throw "Could not open generatedPath!"; // ок е вместо това да бъде "return;"
	}

	double x, y;
	//                   vvvvvvvvvvv ето тук е единствената разлика с moveWithOneStep
	for (int i = 0; i <= position && !inFile.eof(); i++) {
		inFile.read((char*)&x, sizeof(x));
		inFile.read((char*)&y, sizeof(y));
	}
	// Ако във файла не съществува координат на position+1
	if (inFile.eof()) {
		inFile.close();
		throw "No next position";
	}

	std::cout << x << ", " << y << std::endl;

	position++;
	inFile.close();
	return { x, y }; // Еквивалентно на "return Coordinate({ x, y });"
}

/* Помощна за 3.1 */
Drone::Drone(const char* id, char generatedPath[64], int position) {
	this->id = new char[strlen(id)+1];
	strcpy(this->id, id);
	strcpy(this->generatedPath, generatedPath);
	this->position = position;
}