forked from mayerui/sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.inl
82 lines (73 loc) · 1.97 KB
/
utility.inl
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
#ifndef _SUDOKU_UTILITY_INL_
#define _SUDOKU_UTILITY_INL_
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iostream>
//not real random,return number between [begin,end]
inline unsigned int random(int begin, int end)
{
assert(end >= begin && begin >= 0);
srand(time(NULL));
return (unsigned int)rand() % (end - begin + 1) + begin;
}
//网上找的均匀化随机数算法,不含max,非随机,弃用
inline int AverageRandom(int min, int max)
{
int minInteger = min * 10000;
int maxInteger = max * 10000;
srand(time(NULL));
int randInteger = rand() * rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return (resultInteger / 10000);
}
inline void message(const char* msg = "", bool lf = true)
{
std::cout << msg;
if (lf) std::cout << std::endl;
}
inline void message(const std::string& msg, bool lf = true) {
message(msg.c_str(), lf);
}
#ifdef _WIN32
#include <conio.h>
#else
#ifdef __linux__
#include <termio.h>
#include <cstdio>
#elif __APPLE__
#include <termios.h>
#endif
inline char getch(void)
{
struct termios tmtemp, tm;
char c;
int fd = 0;
if (tcgetattr(fd, &tm) != 0)
{ /*获取当前的终端属性设置,并保存到tm结构体中*/
return -1;
}
tmtemp = tm;
cfmakeraw(&tmtemp); /*将tetemp初始化为终端原始模式的属性设置*/
if (tcsetattr(fd, TCSANOW, &tmtemp) != 0)
{ /*将终端设置为原始模式的设置*/
return -1;
}
c = getchar();
if (tcsetattr(fd, TCSANOW, &tm) != 0)
{ /*接收字符完毕后将终端设置回原来的属性*/
return 0;
}
return c;
}
#endif
inline void cls(void)
{
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
#endif