-
Notifications
You must be signed in to change notification settings - Fork 0
/
lettre.cpp
121 lines (106 loc) · 2.42 KB
/
lettre.cpp
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <term.h>
#include <termios.h>
#include <iomanip>
#include <fcntl.h> // Include fcntl.h for file control options
using namespace std;
int height = 20;
int width = 60;
int cntAll = 0;
int cntCorrect = 0;
int Level = 0;
void hideCursor()
{
printf("\033[?25l");
}
void showCursor()
{
printf("\033[?25h");
}
void setColor(int foreColor, int backColor)
{
printf("\033[%d;%dm", foreColor + 30, backColor + 40);
}
void setPosition(int x, int y)
{
printf("\033[%d;%dH", y + 1, x + 1);
}
void showLevel()
{
setPosition(0, height + 2);
setColor(2, 7);
cout << "\tCurrent Score: " << cntCorrect << "\\" << cntAll << "\t";
if (cntAll != 0)
{
cout << setw(10) << right << "Level: " << Level << "\t";
}
}
int kbhit()
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if (ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
hideCursor();
system("clear");
int cursor_x, cursor_y;
char chRandom;
char chUserType;
srand(unsigned(time(NULL)));
while (true)
{
showLevel();
chRandom = rand() % 26 + 'A';
cntAll++;
cursor_x = rand() % width;
cursor_y = 0;
setPosition(cursor_x, cursor_y);
cout << chRandom << flush;
while (cursor_y < height)
{
setPosition(cursor_x, cursor_y);
setColor(rand() % 14, 7);
cout << chRandom << flush;
usleep(150000);
setPosition(cursor_x, cursor_y);
cout << " " << flush;
cursor_y++;
if (kbhit())
{
chUserType = getchar();
if (chUserType == chRandom || chUserType + 32 == chRandom)
{
cntCorrect++;
Level++;
break;
}
else
{
Level = 0;
}
}
}
}
showCursor();
return 0;
}