-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.cpp
140 lines (122 loc) · 3.56 KB
/
utility.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#define SLEEP
#include <iostream> // istream
#include <string> // getline
#ifdef _WIN32
#include <windows.h> // Sleep
#include <conio.h> // _getch, _getche
#else
#include <unistd.h> // sleep (UNIX)
#include <termios.h> // termios
#endif
using namespace std; // getline
#ifdef __linux__
/* Reference: https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux */
static struct termios current, original, game;
/* Initialize new terminal i/o settings */
void change_termios(int echo, termios *before, termios &after) {
tcgetattr(0, before); /* grab old terminal i/o settings */
after = *before; /* make new settings same as old settings */
after.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo)
after.c_lflag |= ECHO; /* set echo mode */
else
after.c_lflag &= ~ECHO; /* set no echo mode */
tcsetattr(0, TCSANOW, &after); /* use these new terminal i/o settings now */
}
void init_termios(int echo) {
change_termios(echo, &original, game);
}
void set_termios(int echo) {
change_termios(echo, &game, current);
}
/* Restore old terminal i/o settings */
void reset_termios(termios *termios) {
tcsetattr(0, TCSANOW, termios);
}
void reset_original_termios() {
tcsetattr(0, TCSANOW, &original);
}
#endif
string int_to_char(int n) {
if (0 <= n&&n <= 9)
return string(1, '0' + n);
else if (10 <= n&&n <= 35)
return string(1, 'A' + n - 10);
else if (36 <= n&&n <= 61)
return "A" + string(1, 'A' + n - 36);
else
return "";
}
int char_to_int(char ch) {
if ('0' <= ch&&ch <= '9')
return ch - '0';
else if ('a' <= ch&&ch <= 'z')
return ch - 'a' + 10;
else if ('A' <= ch&&ch <= 'Z')
return ch - 'A' + 10;
else
return -1;
}
void _sleep(int ms) {
#ifdef SLEEP
#ifdef _WIN32
Sleep(ms);
#else
usleep(1000 * ms);
#endif
#endif
}
void clear_screen() {
#ifdef _WIN32
system("cls");
#else
cout << "\033[1J\033[1;1H";
#endif
}
void clear_input() {
#ifdef _WIN32
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
#else
tcflush(STDIN_FILENO, TCIFLUSH);
#endif
}
int Getch() { // a wrapper to prevent players from inputing characters at an inappropriate time
clear_input();
#ifdef _WIN32
return _getch();
#else
set_termios(0);
int ch = getchar();
reset_termios(&game);
return ch;
#endif
}
int Getche() { // a wrapper to prevent players from inputing characters at an inappropriate time
clear_input();
#ifdef _WIN32
return _getche();
#else
set_termios(1);
int ch = getchar();
reset_termios(&game);
return ch;
#endif
}
void Getline(istream& _Istr, string& _Str) { // a wrapper to prevent players from inputing characters at an inappropriate time
clear_input();
#ifdef _WIN32
getline(_Istr, _Str);
#else
reset_original_termios();
getline(_Istr, _Str);
reset_termios(&game);
#endif
}
int Getintfromch() { // convert input char into the proper integer.
char key = Getch();
return char_to_int(key);
}
int Getintfromche() { // convert input char into the proper integer.
char key = Getche();
return char_to_int(key);
}