-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrank.c
381 lines (337 loc) · 8.96 KB
/
rank.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <menu.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <termios.h>
#include <err.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
#include "save.h"
#include "login.h"
#include "maze.h"
#include "sudoku.h"
#include "rank.h"
//menu select
static char *choices[] = {
"1. SUDOKU",
"2. MAZE",
(char *)NULL,
};
static char *ranking[] = {
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
"Choice 6", "Choice 7", "Choice 8", "Choice 9", "Choice 10",
"Choice 11", "Choice 12", "Choice 13", "Choice 14", "Choice 15",
"Choice 16", "Choice 17", "Choice 18", "Choice 19", "Choice 20",
"Exit",
(char *)NULL,
};
//print ddok ddok logo
static void print_logo(WINDOW *my_menu_win);
char* intToTime(int time)
{
int sec, min;
char *format, buf[2];
format = malloc(sizeof(char) * 5);
memset(format, '\0', 5);
min = time / 60;
sec = time % 60;
buf[0] = (min / 10) + '0';
buf[1] = (min % 10) + '0';
strcat(format, buf);
format[2] = ':';
buf[0] = (sec / 10) + '0';
buf[1] = (sec % 10) + '0';
strcat(format, buf);
return format;
}
void select_game_show()
{
//menu seletions in MENU
ITEM **my_items;
int c;
//MENU WINDOW
MENU *my_menu;
//MAIN WINDOW
WINDOW *my_menu_win;
int n_choices, i;
//Init curses
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
//create menu seletions
n_choices = ARRAY_SIZE(choices);
my_items = (ITEM **)malloc(n_choices* sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(choices[i],"");
//create menu window
my_menu = new_menu((ITEM **)my_items);
//create main menu window
my_menu_win = newwin(30, 70, 4, 4);
keypad(my_menu_win, TRUE);
/* Set main window and sub window */
//?? should to googling
set_menu_win(my_menu, my_menu_win);
set_menu_sub(my_menu, derwin(my_menu_win, 6, 17, 3, 1));
//point user selection
set_menu_mark(my_menu, "--> ");
//print title and ddok ddok
box(my_menu_win, 0, 0);
mvwprintw(my_menu_win, 1, 30, "%s", "Game Menu");
mvwhline(my_menu_win, 2, 1, ACS_HLINE, 68);
mvprintw(LINES - 2, 34, "ddok ddok");
print_logo(my_menu_win);
box(my_menu_win, 0, 0);
refresh();
//post menu in window
post_menu(my_menu);
wrefresh(my_menu_win);
int flag=0,level=1;
enum{SUDOKU=1,MAZE};
while(1)
{
c = wgetch(my_menu_win);
switch(c)
{ case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case 10: //enter
{
char selection=item_name(current_item(my_menu))[0];
flag=1;
clear();
if(atoi(&selection)==1)
show_rank(1);
else
show_rank(2);
break;
}
}
if(flag) break;
box(my_menu_win, 0, 0);
refresh();
post_menu(my_menu);
wrefresh(my_menu_win);
}
//free all
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
clear();
}
void printScore(int ChooseLank){
Score output[500]={0,}, temp={0, }; //1000명의 사람을 수용
Score miro_out[500] = {0, }; //미로(1-10)
Score sudo_out[500] = {0, }; //스도쿠(1-10)
int miro = 0;
int sudo = 0;
int N = 0, go; //갖고오는 사람 수
FILE *fp = fopen(".score","rb+");
if(fp==NULL){
printf("파일 오류입니다.\n");
return;
}
printf("\n\n");
rewind(fp);
while(fread(&output[N],sizeof(Score),1,fp)){
N+=1;
}
fclose(fp);
for(int i=0; i<N-1; i++){ //버블정렬로 파일에서 입력받은 정보들을 소트
for(int j=0; j<N-i-1; j++){
if(output[j].score>output[j+1].score){
strcpy(temp.name,output[j].name);
temp.score = output[j].score;
temp.level = output[j].level;
strcpy(output[j].name, output[j+1].name);
output[j].score = output[j+1].score;
output[j].level = output[j+1].level;
strcpy(output[j+1].name, temp.name);
output[j+1].score = temp.score;
output[j+1].level = temp.level;
}
}
}
for(int i=0; i<N; i++){
if(output[i].miro_su == 1){ //미로
miro_out[miro] = output[i];
miro +=1;
}
else if(output[i].miro_su == 2){ //스도쿠
sudo_out[sudo] = output[i];
sudo +=1;
}
}
go = ChooseLank;
int choice_go = 0;
char lank[20], strscore[20];
if(go == 2){ //상위 30명만
for(int i = 0; i < 30; i++){ //30명의
if(strcmp(miro_out[i].name, "") == 0){
ranking[choice_go] = (char *)NULL;
break;
}
for(int j = 0; j < 3; j++){ //rank, name, score
if(choice_go % 3 == 0){ //rank
sprintf(lank, "%d Rank", i+1);
ranking[choice_go] = malloc(sizeof(char)*10);
strcpy(ranking[choice_go], lank);
}
else if(choice_go % 3 == 1){ //name
ranking[choice_go] = malloc(sizeof(char)*30);
strcpy(ranking[choice_go], miro_out[i].name);
}
else if(choice_go % 3 == 2){ //score
sprintf(strscore, "%s", intToTime(miro_out[i].score));
ranking[choice_go] = malloc(sizeof(char)*20);
strcpy(ranking[choice_go], strscore);
}
choice_go += 1;
}
}
}
else if(go == 1){ //상위 30명만
for(int i = 0; i < 30; i++){ //30명의
if(strcmp(sudo_out[i].name, "") == 0){
ranking[choice_go] = (char *)NULL;
break;
}
for(int j = 0; j < 3; j++){ //rank, name, score
if(choice_go % 3 == 0){ //rank
sprintf(lank, "%d Rank", i+1);
ranking[choice_go] = malloc(sizeof(char)*10);
strcpy(ranking[choice_go], lank);
}
else if(choice_go % 3 == 1){ //name
ranking[choice_go] = malloc(sizeof(char)*30);
strcpy(ranking[choice_go], sudo_out[i].name);
}
else if(choice_go % 3 == 2){ //score
sprintf(strscore, "%s", intToTime(sudo_out[i].score));
ranking[choice_go] = malloc(sizeof(char)*20);
strcpy(ranking[choice_go], strscore);
}
choice_go += 1;
}
}
}
else{
printf("잘못 입력하셨습니다.\n");
}
}
void show_rank(int game_selection)
{
printScore(game_selection);
ITEM **my_items;
int c;
MENU *my_menu;
WINDOW *win;
int n_choices, i;
//init ncurses window
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(1, COLOR_RED,COLOR_YELLOW);
//create rank list
n_choices = ARRAY_SIZE(ranking);
my_items = (ITEM **)malloc(n_choices* sizeof(ITEM *));
for(i = 0; i < n_choices; ++i)
my_items[i] = new_item(ranking[i], ranking[i]);
//create menu window
my_menu = new_menu((ITEM **)my_items);
menu_opts_off(my_menu, O_SHOWDESC);//only show 1 list per line
//create main window
win = newwin(30, 70, 4, 4);
keypad(win, TRUE);
//###############should googling derwin(), set_menu_sub()###################
set_menu_win(my_menu, win);
set_menu_sub(my_menu, derwin(win, 25, 68, 3, 1));
set_menu_format(my_menu, 25, 3);
set_menu_mark(my_menu, "--> ");
//print border line and title
box(win, 0, 0);
mvwprintw(win,0,8,"RANK");
mvwprintw(win,0,20,"NICKNAME");
mvwprintw(win,0,32,"SCORE");
//print explation at the bottom
attron(COLOR_PAIR(1));
mvprintw(LINES - 3, 30, "<--- prev next--->");
mvprintw(LINES - 2, 34, "(F2 to Exit)");
attroff(COLOR_PAIR(1));
//refresh all
refresh();
post_menu(my_menu);
wrefresh(win);
while((c = wgetch(win)) != KEY_F(2))
{
switch(c) {
case KEY_DOWN:
menu_driver(my_menu, REQ_DOWN_ITEM);
break;
case KEY_UP:
menu_driver(my_menu, REQ_UP_ITEM);
break;
case KEY_LEFT:
menu_driver(my_menu, REQ_SCR_UPAGE);
break;
case KEY_RIGHT:
menu_driver(my_menu, REQ_SCR_DPAGE);
break;
}
wrefresh(win);
}
//free all
unpost_menu(my_menu);
free_menu(my_menu);
for(i = 0; i < n_choices; ++i)
free_item(my_items[i]);
endwin();
}
void inputScore(char *nickname, double score, int level, int miro_su){
Score input = {"",0};
FILE *fp = fopen(".score","ab+");
if(fp==NULL){
printf("파일 오류입니다.\n");
fclose(fp);
return;
}
strcpy(input.name, nickname); //이름
input.score = score; //점수(걸린 시간)
input.level = level; //난이도
input.miro_su = miro_su; //미로 스도쿠 (1 2)
fseek(fp,0,SEEK_END); //맨 끝으로 이동
fwrite(&input,sizeof(Score),1,fp); //이진 파일로 저장
fclose(fp);
printf("\n입력이 완료되었습니다.\n\n");
}
static void print_logo(WINDOW *my_menu_win)
{
char line[255];
FILE *fp;
fp=fopen(".logo","r");
if(fp==NULL) err(EXIT_FAILURE,"NO logo file");
int i=12;
while(fgets(line,sizeof(line),fp)!=NULL)
mvwprintw(my_menu_win, i++, 4, "%s", line);
}