-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboard.c
194 lines (169 loc) · 5.09 KB
/
board.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#pragma once
//Board-Core: Create board, read&update field, draw board
#include <string.h>
struct board myBoard;
/**
* Creates new clean and usable board (including memory allocation)
* Warning: To resize an existing board call freeBoard() before to prevent memory leak
* @param target Pointer to a fresh board structure.
* @param width Board width (>=4).
* @param height Board height (>=4).
* @return 1 on success, otherwise 0.
*/
int newBoard(struct board* target, unsigned int width, unsigned int height)
{
//sanity check
if (width<4 || height<4) {
return 0;
}
//Initialize board
target->width = width;
target->height = height;
target->numberOfFields = width*height;
target->content = malloc(target->numberOfFields * sizeof(char) + 1);
//abort if memory allocation failed
if (target->content == NULL) {
exit(EXITCODE_OUTOFMEMORY);
}
//board is ready, now clear it
clearBoard(target);
return 1;
}
/**
* Removes all chips from the board
* @param target Already existing board.
*/
void clearBoard(struct board* target) {
//fills the whole board with space chars
int i;
for (i = 0; i < target->numberOfFields; i++) {
*((target->content) + i) = FIELD_EMPTY;
}
//add null terminator which allows direct board content debug output
*(target->content + i) = '\0';
}
/**
* Internal helper method to calculate the data offset of a given field on the board
* @param target Board to base calculations on.
* @param x X-coordinate.
* @param y Y-coordinate.
* @return Pointer to requested field.
*/
char* calcFieldAddress(struct board* target, int x, int y) {
int offset = y*(target->width) + x;
return (target->content + offset);
}
/**
* Gets what is on a given field of the board (returns FIELD_XYZ constant)
* @param target Board to read.
* @param x X-coordinate.
* @param y Y-coordinate.
* @return Value of requested field.
*/
char getField(struct board* target, int x, int y) {
if (x<0 || y<0 || x>=target->width || y>=target->height)
{
return FIELD_OUTOFBOUNDS;
}
return *calcFieldAddress(target, x, y);
}
/**
* Sets what is on a given field of the board (see FIELD_XYZ constants)
* @param target Board to change.
* @param x X-coordinate.
* @param y Y-coordinate.
* @param value Field value (FIELD_EMPTY/FIELD_PLAYER1/FIELD_PLAYER2).
*/
void setField(struct board* target, int x, int y, char value) {
*calcFieldAddress(target, x, y) = value;
}
/**
* Frees memory taken by a board (warning: makes it unusable until newBoard() has been called again)
* @param target Board to free memory.
*/
void freeBoard(struct board* target) {
free(target->content);
}
/**
* Outputs a graphical representation of the given board to the console
* @param target Board to draw.
*/
void drawBoard(struct board* target) {
//calculate dimensions of output and reserve memory
char* canvas;
//4 chars wide per field plus ending bar and newline
int charsWidth = target->width*4 + 2;
//4 chars wide per field plus stand
int charsHeight = target->height*2 + 3;
//multiply by 6 for full UTF-8 eventualities plus 1 for string terminator \0
int totalSize = charsWidth*charsHeight*6 + 1;
canvas = malloc(totalSize * sizeof(char));
if (canvas==NULL) {
exit(EXITCODE_OUTOFMEMORY);
}
//writing-to-canvas setup
canvas[0] = '\0';
int fieldX;
int fieldY;
//loop over rows and build 2 console lines (grid + values) per loop
for(fieldY = target->height - 1; fieldY >= 0; fieldY--) {
//grid row
for(fieldX = 0; fieldX < target->width; fieldX++) {
if (fieldY == target->height - 1 && fieldX == 0) {
strcat(canvas,FONT_DPIPE_TOP_LEFT);
} else if (fieldY == target->height - 1) {
strcat(canvas,FONT_DPIPE_TBAR_DOWN);
} else if (fieldX == 0) {
strcat(canvas,FONT_DPIPE_TBAR_RIGHT);
} else {
strcat(canvas,FONT_DPIPE_CROSSING);
}
strcatRepeat(canvas,FONT_DPIPE_HORI_BAR,3);
}
//end of grid row
if (fieldY == target->height - 1) {
strcat(canvas,FONT_DPIPE_TOP_RIGHT);
} else {
strcat(canvas,FONT_DPIPE_TBAR_LEFT);
}
strcat(canvas,"\n");
//value row
for(fieldX = 0; fieldX < target->width; fieldX++) {
strcat(canvas,FONT_DPIPE_VERT_BAR);
strcat(canvas," ");
char field = getField(target,fieldX,fieldY);
if (field==FIELD_EMPTY) strcat(canvas,FONT_COIN_EMPTY);
if (field==FIELD_PLAYER1) strcat(canvas,FONT_COIN_PLAYER1);
if (field==FIELD_PLAYER2) strcat(canvas,FONT_COIN_PLAYER2);
strcat(canvas," ");
}
//finish value row
strcat(canvas,FONT_DPIPE_VERT_BAR);
strcat(canvas,"\n");
}
//build stand
for(fieldX = 0; fieldX < target->width; fieldX++) {
if (fieldX == 0) {
strcat(canvas,FONT_DPIPE_TBAR_RIGHT);
strcatRepeat(canvas,FONT_DPIPE_HORI_BAR,3);
} else {
strcat(canvas,FONT_DPIPE_TBAR_UP);
strcatRepeat(canvas,FONT_DPIPE_HORI_BAR,3);
}
}
strcat(canvas,FONT_DPIPE_TBAR_LEFT);
strcat(canvas,"\n");
for(fieldX = 0; fieldX < target->width; fieldX++) {
if (fieldX == 0) {
strcat(canvas,FONT_DPIPE_BOTTOM_LEFT);
strcatRepeat(canvas,FONT_DPIPE_HORI_BAR,3);
} else {
strcatRepeat(canvas,FONT_DPIPE_HORI_BAR,4);
}
}
strcat(canvas,FONT_DPIPE_BOTTOM_RIGHT);
strcat(canvas,"\n");
//we're done, output the whole thing
output("%s",canvas);
free(canvas);
}