@@ -163,84 +163,84 @@ bool GameController::getCellState(int x, int y) const {
163
163
return grid.getCellState (x, y);
164
164
}
165
165
166
- void GameController::saveGameState (const std::string& filename) {
167
- std::ofstream file (filename);
168
- if (file.is_open ()) {
169
- for (int y = 0 ; y < grid.getHeight (); ++y) {
170
- for (int x = 0 ; x < grid.getWidth (); ++x) {
171
- file << (grid.getCellState (x, y) ? " 1" : " 0" );
172
- }
173
- file << ' \n ' ; // Íîâàÿ ñòðîêà äëÿ ñëåäóþùåãî ðÿäà
174
- }
175
- file.close ();
176
- }
177
- else {
178
- std::cerr << " Íå óäàëîñü îòêðûòü ôàéë äëÿ çàïèñè" << std::endl;
179
- }
180
- }
181
-
182
- void GameController::loadGameState (const std::string& filename) {
183
- std::ifstream file (filename);
184
- if (file.is_open ()) {
185
- std::string line;
186
- int y = 0 ;
187
- while (std::getline (file, line) && y < grid.getHeight ()) {
188
- for (int x = 0 ; x < std::min (static_cast <int >(line.length ()), grid.getWidth ()); ++x) {
189
- grid.setCellState (x, y, line[x] == ' 1' );
190
- }
191
- ++y;
192
- }
193
- file.close ();
194
- }
195
- else {
196
- std::cerr << " Íå óäàëîñü îòêðûòü ôàéë äëÿ ÷òåíèÿ" << std::endl;
197
- }
198
- }
199
-
200
- void GameController::saveGameStateCSV (const std::string& filename) {
201
- std::ofstream file (filename);
202
- if (file.is_open ()) {
203
- for (int y = 0 ; y < grid.getHeight (); ++y) {
204
- for (int x = 0 ; x < grid.getWidth (); ++x) {
205
- const Cell& cell = grid.getCell (x, y);
206
- file << (cell.getAlive () ? " 1" : " 0" ) << " ,"
207
- << cell.getType () << " ,"
208
- << cell.getColor ().X () << " ,"
209
- << cell.getColor ().Y () << " ,"
210
- << cell.getColor ().Z ();
211
- if (x < grid.getWidth () - 1 ) file << " ;" ; // Ðàçäåëèòåëü ìåæäó êëåòêàìè â ñòðîêå
212
- }
213
- file << ' \n ' ; // Íîâàÿ ñòðîêà äëÿ ñëåäóþùåãî ðÿäà
214
- }
215
- file.close ();
216
- }
217
- }
218
-
219
- void GameController::loadGameStateCSV (const std::string& filename) {
220
- std::ifstream file (filename);
221
- if (file.is_open ()) {
222
- std::string line, cellData;
223
- int y = 0 ;
224
- while (std::getline (file, line) && y < grid.getHeight ()) {
225
- std::istringstream iss (line);
226
- for (int x = 0 ; x < grid.getWidth (); ++x) {
227
- if (std::getline (iss, cellData, ' ;' )) {
228
- std::istringstream cellStream (cellData);
229
- bool isAlive; int type; float r, g, b;
230
- char comma;
231
- if (cellStream >> isAlive >> comma >> type >> comma >> r >> comma >> g >> comma >> b) {
232
- Cell& cell = grid.getCell (x, y);
233
- cell.setAlive (isAlive);
234
- cell.setType (type);
235
- cell.setColor (Vector3d (r, g, b));
236
- }
237
- }
238
- }
239
- ++y;
240
- }
241
- file.close ();
242
- }
243
- }
166
+ // void GameController::saveGameState(const std::string& filename) {
167
+ // std::ofstream file(filename);
168
+ // if (file.is_open()) {
169
+ // for (int y = 0; y < grid.getHeight(); ++y) {
170
+ // for (int x = 0; x < grid.getWidth(); ++x) {
171
+ // file << (grid.getCellState(x, y) ? "1" : "0");
172
+ // }
173
+ // file << '\n'; // Íîâàÿ ñòðîêà äëÿ ñëåäóþùåãî ðÿäà
174
+ // }
175
+ // file.close();
176
+ // }
177
+ // else {
178
+ // std::cerr << "Íå óäàëîñü îòêðûòü ôàéë äëÿ çàïèñè" << std::endl;
179
+ // }
180
+ // }
181
+
182
+ // void GameController::loadGameState(const std::string& filename) {
183
+ // std::ifstream file(filename);
184
+ // if (file.is_open()) {
185
+ // std::string line;
186
+ // int y = 0;
187
+ // while (std::getline(file, line) && y < grid.getHeight()) {
188
+ // for (int x = 0; x < std::min(static_cast<int>(line.length()), grid.getWidth()); ++x) {
189
+ // grid.setCellState(x, y, line[x] == '1');
190
+ // }
191
+ // ++y;
192
+ // }
193
+ // file.close();
194
+ // }
195
+ // else {
196
+ // std::cerr << "Íå óäàëîñü îòêðûòü ôàéë äëÿ ÷òåíèÿ" << std::endl;
197
+ // }
198
+ // }
199
+
200
+ // void GameController::saveGameStateCSV(const std::string& filename) {
201
+ // std::ofstream file(filename);
202
+ // if (file.is_open()) {
203
+ // for (int y = 0; y < grid.getHeight(); ++y) {
204
+ // for (int x = 0; x < grid.getWidth(); ++x) {
205
+ // const Cell& cell = grid.getCell(x, y);
206
+ // file << (cell.getAlive() ? "1" : "0") << ","
207
+ // << cell.getType() << ","
208
+ // << cell.getColor().X() << ","
209
+ // << cell.getColor().Y() << ","
210
+ // << cell.getColor().Z();
211
+ // if (x < grid.getWidth() - 1) file << ";"; // Ðàçäåëèòåëü ìåæäó êëåòêàìè â ñòðîêå
212
+ // }
213
+ // file << '\n'; // Íîâàÿ ñòðîêà äëÿ ñëåäóþùåãî ðÿäà
214
+ // }
215
+ // file.close();
216
+ // }
217
+ // }
218
+
219
+ // void GameController::loadGameStateCSV(const std::string& filename) {
220
+ // std::ifstream file(filename);
221
+ // if (file.is_open()) {
222
+ // std::string line, cellData;
223
+ // int y = 0;
224
+ // while (std::getline(file, line) && y < grid.getHeight()) {
225
+ // std::istringstream iss(line);
226
+ // for (int x = 0; x < grid.getWidth(); ++x) {
227
+ // if (std::getline(iss, cellData, ';')) {
228
+ // std::istringstream cellStream(cellData);
229
+ // bool isAlive; int type; float r, g, b;
230
+ // char comma;
231
+ // if (cellStream >> isAlive >> comma >> type >> comma >> r >> comma >> g >> comma >> b) {
232
+ // Cell& cell = grid.getCell(x, y);
233
+ // cell.setAlive(isAlive);
234
+ // cell.setType(type);
235
+ // cell.setColor(Vector3d(r, g, b));
236
+ // }
237
+ // }
238
+ // }
239
+ // ++y;
240
+ // }
241
+ // file.close();
242
+ // }
243
+ // }
244
244
245
245
void GameController::resizeGrid (int newWidth, int newHeight) {
246
246
if (newWidth <= 0 || newHeight <= 0 ) return ; // Ïðîâåðêà íà ïîëîæèòåëüíûé ðàçìåð
@@ -249,8 +249,10 @@ void GameController::resizeGrid(int newWidth, int newHeight) {
249
249
Grid newGrid (newWidth, newHeight);
250
250
251
251
// Êîïèðóåì ñòàðûå äàííûå â íîâóþ ñåòêó, ãäå ýòî âîçìîæíî
252
- for (int y = 0 ; y < std::min (grid.getHeight (), newHeight); ++y) {
253
- for (int x = 0 ; x < std::min (grid.getWidth (), newWidth); ++x) {
252
+ for (int y = 0 ; y < grid.getHeight (); ++y) {
253
+ if (y >= newHeight) break ; // Âûõîäèì èç öèêëà, åñëè ïðåâûøàåì íîâóþ âûñîòó
254
+ for (int x = 0 ; x < grid.getWidth (); ++x) {
255
+ if (x >= newWidth) break ; // Âûõîäèì èç öèêëà, åñëè ïðåâûøàåì íîâóþ øèðèíó
254
256
newGrid.setCellState (x, y, grid.getCellState (x, y));
255
257
}
256
258
}
0 commit comments