-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcells.c
127 lines (117 loc) · 2.52 KB
/
cells.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
#include "cells.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
// -------------
// --- CELLS ---
// -------------
void printCell(cell *c)
// Prints cell using terminal background color
{
switch (c -> type)
{
case EMPTY: printf("\033[107m \033[49m"); break;
case LAND:
switch (c -> hint)
{
case 0:
printf("\033[42m \033[49m");
break;
default:
printf("\033[42m%3d\033[49m", c -> hint);
break;
};
break;
case WATER: printf("\033[104m \033[49m"); break;
}
}
void cellToFile(cell *c, FILE *fp, bool solved)
// Write hidden or solved cell to file
{
if (solved)
{
if(c -> type == LAND)
{
if (c -> hint == 0)
{
fprintf(fp, " · ");
}
else
{
fprintf(fp, "%3d", c -> hint);
}
} else {
fprintf(fp, " W ");
}
} else {
if (c -> hint == 0)
{
fprintf(fp, " ");
}
else
{
fprintf(fp, "%3d", c -> hint);
}
}
}
void setCellStatus(cell *c, cellType type)
// Sets cell type
{
c -> type = type;
}
// ------------------
// --- CELL LISTS ---
// ------------------
cellList *initCellList()
// Creates an empty cell list
{
cellList *list;
list = (cellList*)malloc(sizeof(cellList));
list -> head = NULL;
return list;
}
void appendToCellList(cellList *list, cell *c)
// Append cell node *c to cell list
{
cellNode *new_node, *current;
new_node = (cellNode*)malloc(sizeof(cellNode));
new_node -> cell = c;
new_node -> next = NULL;
current = list -> head;
if ((list -> head) == NULL)
{
list -> head = new_node;
} else {
while (current -> next != NULL) {
current = current -> next;
}
current -> next = new_node;
}
}
cell *popCellList(cellList *list)
// Pops last cell from list
{
cell *pcell;
cellNode *temp;
if ((list -> head) == NULL)
{
return NULL;
}
temp = list -> head;
list -> head = temp -> next;
pcell = temp -> cell;
return pcell;
}
void printCellList(cellList *list)
// Prints cellList
{
cell *c;
cellNode *current = list -> head;
while (current != NULL) {
c = current -> cell;
printf("[%d, %d]", c -> row, c -> col);
current = current -> next;
}
printf("\n");
}