-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.c
69 lines (58 loc) · 1.64 KB
/
grid.c
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "grid.h"
#define MAX_LINE_LENGTH 1024
Grid* createGrid(int size) {
Grid *grid = malloc(sizeof(Grid));
grid->size = size;
grid->letters = malloc(size * size * sizeof(char));
grid->letterMultiplier = malloc(size * size * sizeof(int));
grid->wordMultiplier = malloc(size * size * sizeof(int));
for (int i = 0; i < size * size; i++) {
grid->letterMultiplier[i] = 1;
grid->wordMultiplier[i] = 1;
}
return grid;
}
void loadGrid(const char *filePath, Grid *grid) {
FILE *file = fopen(filePath, "r");
if (!file) {
perror("Error opening grid file");
exit(1);
}
char line[MAX_LINE_LENGTH];
int row = 0;
while (fgets(line, sizeof(line), file) && row < grid->size) {
int col = 0;
for (int i = 0; line[i] && col < grid->size; i++) {
if (isalpha(line[i])) {
grid->letters[row * grid->size + col] = toupper(line[i]);
// Count total '*' and '^' characters
int letterMultiplier = 0;
int wordMultiplier = 0;
while (line[i+1] == '*' || line[i+1] == '^') {
if (line[i+1] == '*') letterMultiplier++;
if (line[i+1] == '^') wordMultiplier++;
i++;
}
grid->letterMultiplier[row * grid->size + col] = letterMultiplier + 1;
grid->wordMultiplier[row * grid->size + col] = wordMultiplier + 1;
col++;
}
}
row++;
}
if (row < grid->size) {
fprintf(stderr, "Error: Not enough rows in grid file\n");
exit(1);
}
fclose(file);
}
void freeGrid(Grid *grid) {
free(grid->letters);
free(grid->letterMultiplier);
free(grid->wordMultiplier);
free(grid);
}