include iostream include cstdlib include sys time include string inclu

  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
#include <iostream>
#include <cstdlib>
#include <sys/time.h>
#include <string>
#include <map>
#include <sys/ioctl.h>
#include <cstdarg>
using namespace std;
int points = 0, hearts = 5, start_time;
map<wstring, int> commands = {
{L"EXIT", 0},
{L"START", 1},
{L"ABOUT", 2}
};
int rand(unsigned int min, unsigned int max);
wstring getStyleString(wstring str, unsigned int n, ...);
short int getStyleCode(string key);
void processGame();
wstring printCenter(wstring str);
int getTerminalSize();
wstring hr();
void showStartScreen();
int main() {
setlocale(LC_ALL, "ru_RU.UTF-8");
system("clear");
processGame();
wcout << endl;
return 0;
}
void processGame() {
wstring command = showStartScreen();
switch(::commands[command]) {
case 0:
wcout << hr() << endl << getStyleString(printCenter(L"Заходите ещё! ;-)"), 2, "bold", "green");
break;
case 2:
showAboutForm();
default:
wcout << L"pizda";
}
}
string showStartScreen() {
system("clear");
wstring command;
wcout << getStyleString(printCenter(L"--- ИГРА \"УГАДАЙ ЧИСЛО\" ---"), 1, "bold") << endl << hr();
wcout << getStyleString(L"START: ", 2, "bold", "green") << L"начать игру" << endl;
wcout << getStyleString(L"ABOUT: ", 2, "bold", "green") << L"о авторе" << endl;
wcout << getStyleString(L"EXIT: ", 2, "bold", "green") << L"выход" << endl << hr();
wcout << getStyleString(L"Команда: ", 2, "bold", "blue");
wcin >> command;
return command;
}
wstring hr() {
wstring hr = L"";
for(int i = 0; i < getTerminalSize(); i++, hr += L"-");
return hr;
}
int getTerminalSize() {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
return w.ws_col;
}
wstring printCenter(wstring str) {
int position = getTerminalSize() / 2 - str.size() / 2;
wstring n = L"";
for(int i = 0; i < position; i++, n += L" ");
n += str;
return n;
}
int rand(unsigned int min, unsigned int max) {
timeval time;
gettimeofday(&time, NULL);
srand(((unsigned long long) time.tv_sec * 1000000) + time.tv_usec);
return min > max ? 0 : min + rand() % (max - min);
}
wstring getStyleString(wstring str, unsigned int n, ...) {
wstring s = L"\x1b[";
va_list arguments;
va_start(arguments, n);
for(int i = 0; i < n; i++)
s += to_wstring(getStyleCode(va_arg(arguments, char*))) + (i != n - 1 ? L";" : L"");
va_end(arguments);
s += L"m" + str + L"\x1b[0m";
return s;
}
short int getStyleCode(string key) {
map <string, short int> styles;
//атрибуты
styles["default"] = 0;
styles["bold"] = 1;
styles["underline"] = 4;
styles["blink"] = 5;
styles["inversion"] = 7;
styles["invisible"] = 8;
//цвет текста
styles["black"] = 30;
styles["red"] = 31;
styles["green"] = 32;
styles["yellow"] = 33;
styles["blue"] = 34; //голубой
styles["purple"] = 35;
styles["blue1"] = 36; //синий
styles["white"] = 37;
//цвет фона
styles["bg_black"] = 40;
styles["bg_red"] = 41;
styles["bg_green"] = 42;
styles["bg_yellow"] = 43;
styles["bg_blue"] = 44; //голубой
styles["bg_purple"] = 45;
styles["bg_blue1"] = 46; //синий
styles["bg_white"] = 47;
return styles.count(key) ? styles[key] : 0;
}