// type.c
// by spin
#include <curses.h>
#include <time.h>
#include <stdlib.h>
#define ESC '\x1b'
char *word[] = {
"auto","break","case","char","const","continue","default","do",
"double","else","enum","extern","float","for","goto","if","int",
"long","register","return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void","volatile","while"
};
int num = sizeof(word)/sizeof(char*);
void print_guide(void);
void init_word(void);
int continue_game(void);
void clear_line(int y);
void print_result(clock_t start, clock_t end, int miss);
int typing_game(void);
int main(void)
{
initscr();
cbreak();
noecho();
while (typing_game())
;
endwin();
return 0;
}
void print_guide(void)
{
mvaddstr(0, 0, "*---*---*---*---*---*---*---*---*");
mvaddstr(1, 0, " Cタイピング");
mvaddstr(2, 0, " 32個の単語のタイプ速度を競おう");
mvaddstr(3, 0, " (Escで中断出来ます)");
mvaddstr(4, 0, "*---*---*---*---*---*---*---*---*");
mvaddstr(5, 0, "タイピングすると同時に計測を開始します");
}
void init_word(void)
{
int i, a, b;
char *tmp;
srand(time(NULL));
for (i = 0;i < 100;i++) {
a = rand()%num;
b = rand()%num;
tmp = word[a];
word[a] = word[b];
word[b] = tmp;
}
}
int continue_game(void)
{
int ch;
while ((ch = getch()) != 'n')
if (ch == 'y')
return 1;
return 0;
}
void clear_line(int y)
{
mvaddstr(y, 0, " ");
}
void print_result(clock_t start, clock_t end, int miss)
{
char buf[50];
mvaddstr(6, 0, "-*-*-*-Finish!!!-*-*-*-");
sprintf(buf, "結果 : %.3fsec ミス : %d打"
,(double)(end-start)/CLOCKS_PER_SEC, miss);
mvaddstr(8, 0, buf);
}
int typing_game(void)
{
char buf[50];
int i, k, ch, miss_count, isStart;
clock_t start, end;
clear();
print_guide();
init_word();
i = k = miss_count = 0;
isStart = 1;
sprintf(buf, "残り%2d個 : %s%*s", 32-i,word[i]+k, k, "");
mvaddstr(6, 0, buf);
while ((ch=getch()) != ESC) {
if (isStart) {
clear_line(5);
start = clock();
isStart = 0;
}
(ch == word[i][k]) ? k++ : miss_count++;
if (k == strlen(word[i])) {
k = 0;
i++;
if (i == num) {
end = clock();
print_result(start, end, miss_count);
break;
}
}
sprintf(buf, "残り%2d個 : %s%*s", 32-i,word[i]+k, k, "");
mvaddstr(6, 0, buf);
}
mvaddstr(10, 0, "もう一度やりますか[y,n]?");
if (!continue_game())
return 0;
return 1;
}