-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10ff.c
More file actions
executable file
·284 lines (252 loc) · 8.36 KB
/
10ff.c
File metadata and controls
executable file
·284 lines (252 loc) · 8.36 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
#include <ncurses.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOX_HEIGHT 4
#define CTRL_BACKSPACE 0x08
#define ENTER 0x0a
#define SPACE ' '
#define LSIZ 32
#define RSIZ 300
int getlastindex(char (*words)[LSIZ], int box_len, int first_index){
int ch_count = 0;
while(ch_count < box_len){
ch_count += strlen(words[first_index]) + 1;
first_index++;
}
return --first_index;
}
int printwords(char (*words)[LSIZ], WINDOW *words_win, int first_index, int typedwidx, int colorpair){
int col = COLS/2-4;
int last_index = getlastindex(words, col, first_index);
wmove(words_win, 1, 2);
for(;first_index < last_index; first_index++){
if(first_index == typedwidx){
wattron(words_win, COLOR_PAIR(colorpair));
wprintw(words_win,"%s", words[first_index]);
wattroff(words_win, COLOR_PAIR(colorpair));
wprintw(words_win," ");
}else{
wprintw(words_win,"%s ", words[first_index]);
}
}
int x_pos = getcurx(words_win);
for(int i = 0; i < col - x_pos; i++){
waddch(words_win, ' ');
}
int return_index = first_index;
wmove(words_win, 2, 2);
last_index = getlastindex(words, col, last_index);
for(;first_index < last_index; first_index++){
wprintw(words_win,"%s ", words[first_index]);
}
x_pos = getcurx(words_win);
for(int i = 0; i < col - x_pos; i++){
waddch(words_win, ' ');
}
wrefresh(words_win);
return return_index;
}
_Bool typedcorrect(char (*words)[LSIZ], char typedWord[], int typedwidx, int typedchidx){
for(int i = 0; i <= typedchidx; i++){
if(words[typedwidx][i] != typedWord[i]){
return 0;
}
}
return 1;
}
void showstats(){
wclear(stdscr);
WINDOW *stats_win;
char tmp[LSIZ];
int ch;
int highestwpm = 0;
int lowestwpm = 420;
FILE *fpstats = fopen("stats.txt", "r");
// count lines
int linescount = 0;
while((ch = fgetc(fpstats))!= EOF){
if(ch == '\n')
linescount++;
}
fseek(fpstats, 0, SEEK_SET);
// create array with the size of lines in stats file
int *stats = (int *)malloc(linescount*sizeof(int));
int i = 0;
// put each string line as int into stats array
while(fgets(tmp, LSIZ, fpstats)){
tmp[strlen(tmp) - 1] = '\0';
*(stats+i) = atoi(tmp);
//wprintw(stats_win,"%d ", *(stats+i));
if(*(stats+i) > highestwpm)
highestwpm = *(stats+i);
if(*(stats+i) < lowestwpm)
lowestwpm = *(stats+i);
i++;
}
//assume there is room enough
int statsheight = highestwpm - lowestwpm+2;
int statswidth = COLS-1;
int starty_stats = LINES/2-statsheight/2;
int startx_stats = 1;
int numpoints = linescount;
stats_win = newwin(statsheight, statswidth, starty_stats, startx_stats);
wborder(stats_win, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE, ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
int idx = numpoints - statswidth - 1;
for(int i = 0; i < statswidth-2; i++){
mvwprintw(stats_win, statsheight-(stats[idx+i]-lowestwpm)-2, i+1, "%c", '*');
}
//mvwprintw(stats_win, starty_stats, 0, "%d", 0);
for (int i = 0; i < highestwpm; i+=5) {
mvwprintw(stats_win, statsheight-i, 0, "%d", lowestwpm+i);
}
wrefresh(stats_win);
wgetch(stats_win);
fclose(fpstats);
free(stats);
}
int main()
{
WINDOW *words_win;
srand(time(0));
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
refresh();
init_pair(1, COLOR_BLACK, COLOR_RED);
init_pair(2, COLOR_BLACK, COLOR_GREEN);
_Bool running = 1;
char line[RSIZ][LSIZ];
int ch;
FILE *fpwords = fopen("words.txt", "r");
if(fpwords == NULL){
printf("ERROR: No \"words.txt\"\n");
return 1;
}
int i = 0;
int total = 0;
char words[RSIZ][LSIZ];
char typedWord[LSIZ];
int starty_win = (LINES - BOX_HEIGHT) / 2;
int startx_win = (COLS - COLS/2) / 2;
int starty_type = LINES/2+BOX_HEIGHT/2;
int startx_type = COLS/2;
int current_row;
int current_col;
words_win = newwin(BOX_HEIGHT, COLS/2, starty_win, startx_win);
while(fgets(line[i], LSIZ, fpwords)){
line[i][strlen(line[i]) - 1] = '\0';
i++;
}
total = i;
// restart here
while(running){
_Bool typing = 1;
_Bool restart = 0;
for (i = 0; i < RSIZ; ++i){
strncpy(words[i], line[rand() % (total)], LSIZ);
}
int correctwords[RSIZ] = {0};
int cwidx = 0;
int typedwidx = 0;
int typedchidx = 0;
int prntwidxtmp = 0;
int prntwidx = printwords(words, words_win, 0, typedwidx, 2);
_Bool firstflag = 1;
int totalch = 0;
time_t start_t;
wborder(words_win, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE, ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
wrefresh(words_win);
move(starty_type, startx_type);
while(typing){
switch ((ch = getch())) {
case KEY_BACKSPACE:
getyx(stdscr, current_row, current_col);
if(current_col == startx_type){
move(starty_type, startx_type);
break;
}
mvdelch(current_row, --current_col);
typedchidx--;
break;
case ENTER:
typing = 0;
restart = 1;
break;
case SPACE:
if(typedwidx == RSIZ-30)
return 1;
if(getcurx(stdscr) == startx_type){
move(starty_type, startx_type);
break;
}
move(starty_type, startx_type);
clrtoeol();
typedWord[typedchidx] = '\0';
if(typedcorrect(words, typedWord, typedwidx, typedchidx-1)){
correctwords[cwidx++] = typedwidx;
}
typedwidx++;
typedchidx = 0;
if(prntwidx == typedwidx){
prntwidxtmp = prntwidx;
prntwidx = printwords(words, words_win, prntwidx, typedwidx, 2);
}
break;
case CTRL_BACKSPACE:
move(starty_type, startx_type);
clrtoeol();
typedchidx = 0;
break;
default:
if(typedchidx == 31)
continue;
if(firstflag){
time(&start_t);
firstflag = 0;
}
typedWord[typedchidx] = ch;
addch(ch);
typedchidx++;
}
if(!typedcorrect(words, typedWord, typedwidx, typedchidx-1))
printwords(words, words_win, prntwidxtmp, typedwidx, 1);
else
printwords(words, words_win, prntwidxtmp, typedwidx, 2);
if(time(NULL) - start_t > 60 && !firstflag)
typing = 0;
}
for(i = 0; i < cwidx; i++){
totalch += strlen(words[correctwords[i]]) + 1;
}
if(!restart){
mvprintw(starty_type+2,startx_type-10,"Typed %d words correct", cwidx);
mvprintw(starty_type+3,startx_type-10,"Typed %d words wrong", typedwidx - cwidx);
mvprintw(starty_type+4,startx_type-10,"WPM: %d ", totalch/5);
mvprintw(starty_type+5,startx_type-10,"Enter to play again");
mvprintw(starty_type+6,startx_type-10,"F1 to show stats");
// append wpm to stats.txt
if(totalch/5 > 80){
FILE *fpstats = fopen("stats.txt", "a");
fprintf(fpstats, "%d%c", totalch/5, '\n');
fclose(fpstats);
}
while((ch = getch())){
if(ch == ENTER)
break;
if(ch == KEY_F(1)){
showstats();
break;
}
}
move(starty_type+5,0);
clrtobot();
}
}
fclose(fpwords);
endwin();
return 0;
}