forked from matijalukic/MummyMaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
high_score.c
242 lines (188 loc) · 5.19 KB
/
high_score.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "curses.h"
#define MAX_NAME 30
#define WIDTH 30
#define HEIGHT 11
#define KEY_ENTER 10
#define KEY_ESC 27
typedef struct high_score {
char player_name[30];
int score;
}high_score;
void make_high_score_table(FILE* high_scores) {
long int end_point;
high_score player;
int i;
char check = 0;
for (i = 0; i < 10; i++) {
fseek(high_scores, i*sizeof(player), SEEK_SET);
strcpy(player.player_name, "Anonymous");
player.score = 0;
fwrite(&player, sizeof(player), 1, high_scores);
rewind(high_scores);
}
fseek(high_scores, 0L, SEEK_END);
end_point = ftell(high_scores);
rewind(high_scores);
for (i = 0; i < end_point; i++) {
char help;
fread(&help, sizeof(help), 1, high_scores);
check ^= help;
}
fwrite(&check, sizeof(check), 1, high_scores);
}
int check_for_cheaters(FILE* high_scores) {
char check = 0;
long int end_point;
long int i;
fseek(high_scores, 0L, SEEK_END);
end_point = ftell(high_scores);
rewind(high_scores);
for (i = 0; i < end_point; i++) {
char help;
fread(&help, sizeof(help), 1, high_scores);
check ^= help;
}
return check;
}
int update_high_scores(int score) {
int check_cheats;
int i, j;
FILE* high_scores;
high_score player;
char flag = 0;
long int end_point;
char check = 0;
char ulaz[] = { "HIGH SCORE" };
if ((fopen_s(&high_scores, ulaz, "r+b"))) {
fopen_s(&high_scores, ulaz, "w+b");
make_high_score_table(high_scores);
check_cheats = 0;
}
else check_cheats = check_for_cheaters(high_scores);
if (check_cheats) {
fclose(high_scores);
fopen_s(&high_scores, ulaz, "w+b");
make_high_score_table(high_scores);
check_cheats = 0;
}
fclose(high_scores);
fopen_s(&high_scores, ulaz, "r+b");
fseek(high_scores, 0L, SEEK_SET);
for (i = 0; i < 10; i++) { //proveravamo da li uposte ima potrebe da menjamo list high score-eva
fread(&player, sizeof(player), 1, high_scores);
if (score > player.score) {
flag = 1;
break;
}
}
rewind(high_scores);
if (flag) {
for (j = 9; j > i; j--) {
fseek(high_scores, (j - 1) * sizeof(player), SEEK_SET);
fread(&player, sizeof(player), 1, high_scores);
fseek(high_scores, (j) * sizeof(player), SEEK_SET);
fwrite(&player, sizeof(player), 1, high_scores);
rewind(high_scores);
}
/*initscr();
start_color();
noecho();
curs_set(0);*/
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_WHITE, COLOR_BLACK);
wattron(stdscr, COLOR_PAIR(2));
mvwprintw(stdscr, 7, 2, "Enter your name : \n");
wrefresh(stdscr);
wattroff(stdscr, COLOR_PAIR(2));
char c;
j = 0;
while ((c = getch()) != KEY_ENTER) {
if (c != 8) {
player.player_name[j++] = c;
mvwprintw(stdscr, 7, 19 + j, "%c_", c);
}
else if (c == 8 && j >= 1) {
mvwprintw(stdscr, 7, 19 + j + 1, " ");
mvwprintw(stdscr, 7, 19 + j--, "_");
player.player_name[j] = ' ';
}
}
player.player_name[j] = '\0';
wrefresh(stdscr);
clrtoeol();
endwin();
//ovde mora lepse ovo da se odradi
//scanf(" %s", player.player_name);
player.score = score;
fseek(high_scores, (i) * sizeof(player), SEEK_SET);
fwrite(&player, sizeof(player), 1, high_scores);
fseek(high_scores, 0L, SEEK_END);
end_point = ftell(high_scores);
rewind(high_scores);
for (long i = 0; i < end_point; i++) {
char help;
fread(&help, sizeof(help), 1, high_scores);
check ^= help;
}
fclose(high_scores);
fopen_s(&high_scores, ulaz, "a+b");
fwrite(&check, sizeof(check), 1, high_scores);
}
fclose(high_scores);
return flag;
}
void write_high_scores() {
int i = 0;
FILE *high_scores;
high_score player;
char c;
int check_cheats;
initscr();
resize_term(27, 35);
start_color();
noecho();
curs_set(0);
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_WHITE, COLOR_BLACK);
fopen_s(&high_scores, "HIGH SCORE", "r+b");
check_cheats = check_for_cheaters(high_scores);
if (check_cheats) {
fclose(high_scores);
fopen_s(&high_scores, "HIGH SCORE", "w+b");
make_high_score_table(high_scores);
check_cheats = 0;
}
rewind(high_scores);
fread(&player, sizeof(high_score), 1, high_scores);
for (i = 0; i < 10; i++) {
if (strcmp(player.player_name, "Anonymous") != 0) {
mvwprintw(stdscr, i + 1, 4, "%s %d\n", player.player_name, player.score);
fread(&player, sizeof(high_score), 1, high_scores);
}
mvwprintw(stdscr, i + 1, 1, "%d.", i + 1);
}
mvwprintw(stdscr, i + 2, 1, "R-Reset Esc-Back \n");
while ((c = getch()) != KEY_ESC ){
if (c == 'R' || c == 'r') {
make_high_score_table(high_scores);
wattron(stdscr, COLOR_PAIR(1));
mvwprintw(stdscr, i + 2, 1, "R-Reset");
wattroff(stdscr, COLOR_PAIR(1));
for (i = 0; i < 10; i++) {
mvwprintw(stdscr, i + 1, 1, "%d. ", i + 1);
}
}
else {
wattron(stdscr, COLOR_PAIR(2));
mvwprintw(stdscr, i + 2, 1, "R-Reset");
wattroff(stdscr, COLOR_PAIR(2));
}
}
fclose(high_scores);
clrtoeol();
endwin();
//treba staviti da ostane ekran
}