-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHFileIO.c
322 lines (282 loc) · 7.39 KB
/
HFileIO.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "HCard.h"
#include "HDeck.h"
#include "HPlayer.h"
#include "HGame.h"
#include "HFileIO.h"
#include "Debug_Center.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
/*
File Format Description
i) Every new Structure Type start with '>', end with '<'
ii) To make parsing easily, every tokens are seperated by ' '
iii) Every last of the line is end with '\n'.
iv) Every comments start with '[', end with ']'
*/
/*
SAVE FUNCTION
*/
void HFileIO_tab(FILE *oFile, int depth)
{
/*
Styling!!!
*/
for(int i=0; i<depth; ++i)
{
fprintf(oFile, " ");
}
}
void HFileIO_exportDeck(FILE *oFile, HDeck *deck, HCard const *CARD_SET, char const *label, int depth)
{
/*
About exportDeck
Iterate input deck and get the card number.
Save these card number at the oFile.
Card number is calculated by referencing CARD_SET
*/
HFileIO_tab(oFile, depth);
fprintf(oFile, "> [ HDeck : %s ]\n", label);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ size ] %d\n", deck->size);
// Print Every Card Number. This Order is start from first.
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ card ]");
for(int c=0; c<deck->size; ++c)
{
HCard const *this_card = HDeck_get(deck, c)->data;
fprintf(oFile, " %2d", this_card-CARD_SET);
}
fprintf(oFile, "\n");
// End Sequence
HFileIO_tab(oFile, depth);
fprintf(oFile, "<\n");
}
void HFileIO_exportPlayer(FILE *oFile, HPlayer *player, HCard const *CARD_SET, char const *label, int depth)
{
/*
About exportPlayer
Export player's data & name.
exportDeck will be exported internally.
*/
HFileIO_tab(oFile, depth);
fprintf(oFile, "> [ HPlayer : %s ]\n", label);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ name ] %s\n", player->name);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ money ] %d\n", player->money);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ gonum ] %d\n", player->how_many_go);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ score ] %d\n", player->score);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ goscr ] %d\n", player->score_lastgo);
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ APchn ]");
if(player->hasChangeAP)
{
fprintf(oFile, " 1\n");
}
else
{
fprintf(oFile, " 0\n");
}
HFileIO_tab(oFile, depth);
fprintf(oFile, " [ shake ]");
if(player->hasShake)
{
fprintf(oFile, " 1");
}
else
{
fprintf(oFile, " 0");
}
fprintf(oFile, "\n");
// Deck
HFileIO_exportDeck(oFile, player->myDeck , CARD_SET, "myDeck" , depth+1);
HFileIO_exportDeck(oFile, player->normDeck, CARD_SET, "normDeck", depth+1);
HFileIO_exportDeck(oFile, player->animDeck, CARD_SET, "animDeck", depth+1);
HFileIO_exportDeck(oFile, player->lineDeck, CARD_SET, "lineDeck", depth+1);
HFileIO_exportDeck(oFile, player->gwanDeck, CARD_SET, "gwanDeck", depth+1);
HFileIO_tab(oFile, depth);
fprintf(oFile, "<\n");
}
bool HFileIO_saveGame(HGame *game, HCard const *CARD_SET)
{
/*
Open the File as Writing
*/
FILE *oFile = fopen("savedata.txt", "w");
// Check for File Open Failure
if(oFile == NULL)
{
#ifdef DEBUG
printError("HFileIO", "Error", "saveGame", "Fail to open the savedata.txt");
#endif
return false;
}
//
fprintf(oFile, "> [ HGame : Main ]\n");
fprintf(oFile, " [ current_player_num ] %d\n", game->current_player_num);
if(game->was_nagari)
{
fprintf(oFile, " [ was_nagari ] 1\n");
}
else
{
fprintf(oFile, " [ was_nagari ] 0\n");
}
// Player Export
HFileIO_exportPlayer(oFile, game->player[0], CARD_SET, "Player_1", 1);
HFileIO_exportPlayer(oFile, game->player[1], CARD_SET, "Player_2", 1);
HFileIO_exportPlayer(oFile, game->player[2], CARD_SET, "Player_3", 1);
// Deck
HFileIO_exportDeck(oFile, game->unknown_cards, CARD_SET, "Unknown_cards", 1);
for(int m=0; m<12; ++m)
{
char contents[32];
sprintf(contents, "Visible_cards(%d)", m);
HFileIO_exportDeck(oFile, game->visible_cards[m], CARD_SET, contents, 1);
}
HFileIO_exportDeck(oFile, game->display_cards, CARD_SET, "Display_cards", 1);
fprintf(oFile, "<\n");
fclose(oFile);
return true;
}
/*
LOAD FUNCTION
*/
void HFileIO_throwLine(FILE *iFile)
{
/*
This throw one line entirely.
*/
int c;
while((c = fgetc(iFile)) != '\n' && c != EOF);
}
void HFileIO_goNextToken(FILE *iFile)
{
/*
This will move file pointer untill search ']' or EOF
*/
int c;
while((c = fgetc(iFile)) != ']' && c != EOF);
}
void HFileIO_endThisBlock(FILE *iFile)
{
/*
This will move file pointer until search '<'
*/
int c;
while((c = fgetc(iFile)) != '<' && c != EOF);
HFileIO_throwLine(iFile);
}
void HFileIO_loadDeck(FILE *iFile, HDeck *deck, HCard const *CARD_SET)
{
int size;
HDeck_clear(deck);
#ifdef DEBUG
printError("HFileIO", "Note", "loadDeck", "Try to load HDeck");
#endif
// Start from >
HFileIO_throwLine(iFile); // Throw [ HDeck : ... ]
HFileIO_goNextToken(iFile); // Go [ Size ]
fscanf(iFile, "%d", &size);
HFileIO_goNextToken(iFile); // Go [ Card ]
for(int c=0; c<size; ++c)
{
int card_num;
fscanf(iFile, "%d", &card_num);
HDeck_insert(deck, &CARD_SET[card_num], 0);
}
HFileIO_endThisBlock(iFile); // Go <
#ifdef DEBUG
printError("HFileIO", "Note", "loadDeck", "Success loading HDeck");
#endif
}
void HFileIO_loadPlayer(FILE *iFile, HPlayer *player, HCard const *CARD_SET)
{
int temp;
HFileIO_throwLine(iFile); // Throw [ HPlayer : ... ]
HFileIO_goNextToken(iFile);
fscanf(iFile, "%s", player->name);
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &(player->money));
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &(player->how_many_go));
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &(player->score));
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &(player->score_lastgo));
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &temp); // APChange
if(temp)
{
player->hasChangeAP = true;
}
else
{
player->hasChangeAP = false;
}
HFileIO_goNextToken(iFile); // Shaked
fscanf(iFile, "%d", &temp); // APChange
if(temp)
{
player->hasShake = true;
}
else
{
player->hasShake = false;
}
fgetc(iFile); // Flush '\n'
HFileIO_loadDeck(iFile, player->myDeck , CARD_SET);
HFileIO_loadDeck(iFile, player->normDeck, CARD_SET);
HFileIO_loadDeck(iFile, player->animDeck, CARD_SET);
HFileIO_loadDeck(iFile, player->lineDeck, CARD_SET);
HFileIO_loadDeck(iFile, player->gwanDeck, CARD_SET);
HFileIO_endThisBlock(iFile);
}
bool HFileIO_loadGame(HGame *game, HCard const *CARD_SET)
{
/*
Loading is little tricky.
Simple Data (Non-Dynamic) loading is pretty easy.
But, pointer-based data is pretty hard.
As game instance loaded, you don't have to re-allocate datas.
But reset is very important, so don't forget.
*/
FILE *iFile = fopen("savedata.txt", "r");
if(iFile == NULL)
{
#ifdef DEBUG
printError("HFileIO", "Error", "loadGame", "Fail Loading savedata.txt");
#endif
return false;
}
int temp;
HFileIO_throwLine(iFile); // Throw [HGame : Main ]
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &(game->current_player_num));
HFileIO_goNextToken(iFile);
fscanf(iFile, "%d", &temp); // Was Nagari?
if(temp)
{
game->was_nagari = true;
}
else
{
game->was_nagari = false;
}
HFileIO_throwLine(iFile);
HFileIO_loadPlayer(iFile, game->player[0], CARD_SET);
HFileIO_loadPlayer(iFile, game->player[1], CARD_SET);
HFileIO_loadPlayer(iFile, game->player[2], CARD_SET);
HFileIO_loadDeck(iFile, game->unknown_cards, CARD_SET);
for(int m=0; m<12; ++m)
{
HFileIO_loadDeck(iFile, game->visible_cards[m], CARD_SET);
}
HFileIO_loadDeck(iFile, game->display_cards, CARD_SET);
HFileIO_endThisBlock(iFile);
return true;
}