|
3 | 3 |
|
4 | 4 | #include <limits.h>
|
5 | 5 | #include <stdio.h>
|
| 6 | +#include <string.h> |
6 | 7 |
|
7 | 8 | #include "../patterns/main.h"
|
8 | 9 | #include "../utilities.h"
|
@@ -74,8 +75,24 @@ void drawPatternInDashboard(TGame* pGame, TPattern* pPattern) {
|
74 | 75 | size_t pI = 0;
|
75 | 76 | size_t pJ = 0;
|
76 | 77 |
|
77 |
| - const int startRow = pGame->center[0] - pPattern->center[0]; |
78 |
| - const int startCol = pGame->center[1] - pPattern->center[1]; |
| 78 | + int startRow; |
| 79 | + int startCol; |
| 80 | + |
| 81 | + if (pPattern->rows > pGame->rows || pPattern->cols > pGame->cols) { |
| 82 | + destroy2DArray(pGame->dashboard, pGame->rows, pGame->cols); |
| 83 | + |
| 84 | + pGame->dashboard = new2DArray(pPattern->rows, pPattern->cols); |
| 85 | + pGame->rows = pPattern->rows; |
| 86 | + pGame->cols = pPattern->cols; |
| 87 | + pGame->cellsDead = (pGame->rows * pGame->cols) - pGame->cellsAlive; |
| 88 | + |
| 89 | + setDashboardCenter(pGame); |
| 90 | + |
| 91 | + fillDashboard(pGame, DEAD_CELL); |
| 92 | + } |
| 93 | + |
| 94 | + startRow = pGame->center[0] - pPattern->center[0]; |
| 95 | + startCol = pGame->center[1] - pPattern->center[1]; |
79 | 96 |
|
80 | 97 | for (i = startRow; pI < pPattern->rows; i++) {
|
81 | 98 | if (i < 0) continue;
|
@@ -203,6 +220,103 @@ void setDashboardCenter(TGame* pGame) {
|
203 | 220 | pGame->center[1] = col;
|
204 | 221 | }
|
205 | 222 |
|
| 223 | +int setDashboardFromFile(const char* filePath, TGame* pGame, const int minRows, const int minCols) { |
| 224 | + FILE* pf; |
| 225 | + TPattern pattern; |
| 226 | + |
| 227 | + char* line; |
| 228 | + const size_t lineLength = 100; |
| 229 | + |
| 230 | + char* row; |
| 231 | + char* col; |
| 232 | + char* sep; |
| 233 | + |
| 234 | + int rowInt; |
| 235 | + int colInt; |
| 236 | + |
| 237 | + int rows = minRows; |
| 238 | + int cols = minCols; |
| 239 | + |
| 240 | + int patternRows = 0; |
| 241 | + int patternCols = 0; |
| 242 | + |
| 243 | + pf = fopen(filePath, "rt"); |
| 244 | + if (pf == NULL) return 0; |
| 245 | + |
| 246 | + line = malloc(sizeof(char) * (lineLength + 1)); |
| 247 | + if (line == NULL) { |
| 248 | + fclose(pf); |
| 249 | + return 0; |
| 250 | + }; |
| 251 | + *(line + lineLength) = '\0'; |
| 252 | + |
| 253 | + fgets(line, lineLength, pf); |
| 254 | + |
| 255 | + while (fgets(line, lineLength, pf)) { |
| 256 | + row = line; |
| 257 | + sep = strrchr(line, ';'); |
| 258 | + if (sep == NULL) continue; |
| 259 | + |
| 260 | + *sep = '\0'; |
| 261 | + col = sep + 1; |
| 262 | + |
| 263 | + sscanf(row, "%d", &rowInt); |
| 264 | + sscanf(col, "%d", &colInt); |
| 265 | + |
| 266 | + patternRows = MAX(rowInt, patternRows); |
| 267 | + patternCols = MAX(colInt, patternCols); |
| 268 | + } |
| 269 | + |
| 270 | + rows = MAX(patternRows, rows); |
| 271 | + cols = MAX(patternCols, cols); |
| 272 | + |
| 273 | + pGame->dashboard = new2DArray(rows, cols); |
| 274 | + pGame->rows = rows; |
| 275 | + pGame->cols = cols; |
| 276 | + pGame->cellsAlive = 0; |
| 277 | + pGame->generation = 0; |
| 278 | + |
| 279 | + setDashboardCenter(pGame); |
| 280 | + |
| 281 | + fillDashboard(pGame, DEAD_CELL); |
| 282 | + |
| 283 | + pattern.arr = new2DArray(patternRows, patternCols); |
| 284 | + pattern.rows = patternRows; |
| 285 | + pattern.cols = patternCols; |
| 286 | + |
| 287 | + setPatternCenter(&pattern); |
| 288 | + |
| 289 | + fillPattern(&pattern, DEAD_CELL); |
| 290 | + |
| 291 | + rewind(pf); |
| 292 | + fgets(line, lineLength, pf); |
| 293 | + |
| 294 | + while (fgets(line, lineLength, pf)) { |
| 295 | + row = line; |
| 296 | + sep = strrchr(line, ';'); |
| 297 | + if (sep == NULL) continue; |
| 298 | + |
| 299 | + *sep = '\0'; |
| 300 | + col = sep + 1; |
| 301 | + |
| 302 | + sscanf(row, "%d", &rowInt); |
| 303 | + sscanf(col, "%d", &colInt); |
| 304 | + |
| 305 | + pattern.arr[rowInt - 1][colInt - 1] = ALIVE_CELL; |
| 306 | + pGame->cellsAlive++; |
| 307 | + } |
| 308 | + |
| 309 | + pGame->cellsDead = (cols * rows) - pGame->cellsAlive; |
| 310 | + |
| 311 | + drawPatternInDashboard(pGame, &pattern); |
| 312 | + destroy2DArray(pattern.arr, pattern.rows, pattern.cols); |
| 313 | + |
| 314 | + fclose(pf); |
| 315 | + free(line); |
| 316 | + |
| 317 | + return 1; |
| 318 | +} |
| 319 | + |
206 | 320 | void startGameByConsole(TGame* pGame, const int maxGeneration, const int delayBetweenGenerations) {
|
207 | 321 | size_t generation = 0;
|
208 | 322 | unsigned char isToInfinity = maxGeneration == INT_MAX;
|
|
0 commit comments