-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscore_manager.c
More file actions
316 lines (256 loc) · 7.8 KB
/
score_manager.c
File metadata and controls
316 lines (256 loc) · 7.8 KB
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
#include "score_manager.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <Windows.h>
#pragma warning(disable:4996)
void setCPos(int x, int y) { // set current cursor position
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void checkFileExist() {
FILE* fp;
fp = fopen("SOFTWAREARCADE_SCORE.txt", "r+");
if (fp == NULL) {
fp = fopen("SOFTWAREARCADE_SCORE.txt", "w");
for (int i = 0; i < 10; i++) { // 스네이크 게임 점수 초기화
fputs("SNAKE 0\n", fp);
}
for (int i = 0; i < 10; i++) { // 테트리스 게임 점수 초기화
fputs("TETRIS 0\n", fp);
}
fputs("TICTACTOE 0 0 0\n", fp); // 틱택토 게임 승리/패배/무승부 초기화
fclose(fp);
}
else
fclose(fp);
}
void checkScore(char *gameName, int curGameScore) {
char savedGameName[10];
int savedGameScore;
FILE* file = fopen("SOFTWAREARCADE_SCORE.txt", "r");
if (file == NULL) {
printf("score file does not exist\n");
exit(-1);
}
while (fscanf(file, "%s %d", savedGameName, &savedGameScore) != EOF) {
if (strcmp(gameName, savedGameName) == 0 && curGameScore > savedGameScore) {
fclose(file);
updateFile(gameName, curGameScore);
return;
}
}
fclose(file);
}
void updateFile(char *gameName, int newScore) { // 기존 점수보다 높은 점수 파일에 추가
FILE* fp = fopen("SOFTWAREARCADE_SCORE.txt", "r");
FILE* tempFile = fopen("temp.txt", "w");
if (fp == NULL) {
printf("Could not open file");
exit(-1);
}
char buffer[256];
char name[10];
int score;
int tempScore = newScore;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
sscanf(buffer, "%s %d", name, &score);
if (strcmp(name, gameName) == 0) {
if (tempScore > score) {
fprintf(tempFile, "%s %d\n", gameName, tempScore);
tempScore = score;
}
else {
fprintf(tempFile, "%s %d\n", name, score);
}
}
else {
fprintf(tempFile, "%s", buffer);
}
}
fclose(fp);
fclose(tempFile);
remove("SOFTWAREARCADE_SCORE.txt");
rename("temp.txt", "SOFTWAREARCADE_SCORE.txt");
}
int getHighestScore(char* gameName) { // 입력받은 게임의 최고 점수 반환
char savedGameName[10];
int savedGameScore;
FILE* file = fopen("SOFTWAREARCADE_SCORE.txt", "r");
if (file == NULL) {
printf("score file does not exist\n");
exit(-1);
}
while (fscanf(file, "%s %d", savedGameName, &savedGameScore) != EOF) {
if (strcmp(gameName, savedGameName) == 0) {
fclose(file);
return savedGameScore;
}
}
fclose(file);
return -1;
}
void updateTicTacToeScore(char *gameName, int winner) {
char savedGameName[10];
int computerS, playerS, drawnS;
bool found = false;
long pos;
FILE* file = fopen("SOFTWAREARCADE_SCORE.txt", "r+");
if (file == NULL) {
printf("score file does not exist\n");
exit(-1);
}
while (true) {
pos = ftell(file);
if (fscanf(file, "%s", savedGameName) != EOF) {
if (strcmp(gameName, savedGameName) == 0) {
found = true;
fseek(file, pos, SEEK_SET); // 이전에 읽은 줄의 시작 위치로 이동
fscanf(file, "%s %d %d %d", savedGameName, &playerS, &computerS, &drawnS);
switch (winner) {
case 0:
playerS++;
break;
case 1:
computerS++;
break;
case 2:
drawnS++;
break;
default:
break;
}
fseek(file, pos, SEEK_SET); // 파일 포인터 이전에 읽은 줄의 시작 위치로 설정
fprintf(file, "\n%s %d %d %d\n", savedGameName, playerS, computerS, drawnS);
fflush(file);
break;
}
}
else
break;
}
fclose(file);
}
int ShowScoreBoard()
{
int currentgameNum = 0;
printDiscription(currentgameNum);
printTotalScore(currentgameNum);
int key = 0;
while (key != 13) { // 스코어보드 내에서 좌우로 움직이며 게임 점수 확인
if (_kbhit() != 0)
{
key = _getch();
switch (key)
{
case LEFT:
currentgameNum--;
if (currentgameNum < 0)
currentgameNum += 3;
printDiscription(currentgameNum);
printTotalScore(currentgameNum);
break;
case RIGHT:
currentgameNum++;
if (currentgameNum > 2)
currentgameNum -= 3;
printDiscription(currentgameNum);
printTotalScore(currentgameNum);
break;
case ENTER:
return -1;
break;
}
}
}
return -1;
}
int printTotalScore(int curGameNum) //순위, 점수, 이름 출력
{
FILE* fp;
fp = fopen("SOFTWAREARCADE_SCORE.txt", "r");
if (fp == NULL) {
printf("score file does not exist\n");
return -1;
}
char gameName[50];
int score1, score2, score3;
char targetGame[50];
switch (curGameNum) { // curGameNum 값에 따라 출력할 게임 이름 설정
case 0:
strcpy(targetGame, "SNAKE");
break;
case 1:
strcpy(targetGame, "TETRIS");
break;
case 2:
strcpy(targetGame, "TICTACTOE");
break;
default:
printf("Invalid game number\n");
fclose(fp);
return -1;
}
int x = 52, y = 8;
int lineNum = 0;
while (fscanf(fp, "%s", gameName) != EOF) {
if (strcmp(gameName, targetGame) == 0) {
if (curGameNum == 2) { // TICTACTOE의 경우 점수가 3개
fscanf(fp, "%d %d %d", &score1, &score2, &score3);
setCPos(x + 1, y + 2 * lineNum);
printf("%d : %d", score1, score2);
setCPos(x - 12, y + 2 * lineNum);
printf("WIN");
setCPos(x + 15, y + 2 * lineNum);
printf("LOSE");
setCPos(x + 1, y + 2 * lineNum + 4);
printf("DRAWN");
setCPos(x + 3, y + 2 * lineNum + 6);
printf("%d", score3);
}
else { // SNAKE, TETRIS의 경우 점수 1개
fscanf(fp, "%d", &score1);
setCPos(x, y + 2 * lineNum);
printf("%08d\n", score1);
}
lineNum++;
}
else {
if (curGameNum == 2) { // 다른 게임 건너뛰기 위해 읽기
fscanf(fp, "%d %d %d", &score1, &score2, &score3);
}
else {
fscanf(fp, "%d", &score1);
}
}
}
fclose(fp);
}
void printDiscription(int curGameNum) { // 점수를 제외한 스코어보드 출력
system("cls");
int Score_posX = 48;
int Score_posY = 3;
setCPos(Score_posX , Score_posY );
printf("<< SCORE BOARD >>");;
switch (curGameNum) { // 게임 이름에 따라 다르게 출력
case 0 :
setCPos(Score_posX+3, Score_posY + 2);
printf("SNAKE GAME");
break;
case 1:
setCPos(Score_posX + 3, Score_posY + 2);
printf("TETIRS GAME");
break;
case 2:
setCPos(Score_posX + 3, Score_posY + 2);
printf("TICTACTOE");
break;
}
setCPos(Score_posX - 7, Score_posY + 26);
printf("Press ENTER to go to Main Menu");
setCPos(10, Score_posY + 26);
printf("<-");
setCPos(98, Score_posY + 26);
printf("->");
return;
}