-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_state.c
134 lines (111 loc) · 2.09 KB
/
init_state.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
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
#include "init_state.h"
BOOL initialise_state(
char *input,
struct Board *board,
struct Num_Coordinates *numbers)
{
int row, col, number;
char *p;
BOOL processing[VALID_INPUTS] = { FALSE };
BOOL seen[MAX_NUMS] = { FALSE };
BOOL row_has_data = FALSE;
numbers->next_fixed = numbers->coordinates;
numbers->count = 0;
for (row = 0; row < MAX_ROWS; row++)
{
for (col = 0; col < MAX_COLS; col++)
{
board->grid[row][col] = BLOCKED;
}
}
for (number = 0; number < MAX_NUMS; number++)
{
numbers->coordinates[number].row = UNKNOWN;
numbers->coordinates[number].col = UNKNOWN;
}
row = col = number = 0;
for (p = input; p < input + strlen(input); p++)
{
if (isdigit(*p))
{
number = (number * 10) + (*p - '0');
processing[NUMBER] = TRUE;
row_has_data = TRUE;
}
else if (*p == '?')
{
processing[QUESTION_MARK] = TRUE;
row_has_data = TRUE;
}
else if (toupper(*p) == 'X')
{
processing[X] = TRUE;
row_has_data = TRUE;
}
else
{
if (processing[NUMBER])
{
if (seen[number - 1])
{
printf("Invalid input: Number %d is present multiple times.", number);
return FALSE;
}
seen[number - 1] = TRUE;
board->grid[row][col] = FIXED;
numbers->coordinates[number - 1].row = row;
numbers->coordinates[number - 1].col = col;
(numbers->count)++;
number = 0;
if (*p != '\n')
{
col++;
}
processing[NUMBER] = FALSE;
}
else if (processing[QUESTION_MARK])
{
board->grid[row][col] = FREE;
(numbers->count)++;
if (*p != '\n')
{
col++;
}
processing[QUESTION_MARK] = FALSE;
}
else if (processing[X])
{
board->grid[row][col] = BLOCKED;
if (*p != '\n')
{
col++;
}
processing[X] = FALSE;
}
if (*p == '\n' && row_has_data)
{
if (col > board->cols)
{
board->cols = col;
}
col = 0;
row++;
row_has_data = FALSE;
}
}
}
if (col == 0)
{
board->rows = row;
}
else
{
board->rows = row + 1;
}
if (!seen[ONE])
{
printf("Invalid input: Number 1 must be present.");
return FALSE;
}
return TRUE;
}