-
Notifications
You must be signed in to change notification settings - Fork 0
/
dices.cpp
158 lines (149 loc) · 3.17 KB
/
dices.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <string>
#include <random>
/*
* Black 30
* Red 31
* Green 32
* Brown 33
* Blue 34
* Purple 35
* Cyan 36
* Light Gray 37
*/
using namespace std;
void Help(){
cout << "Usage:" << endl
<< "dices [<number of dicesw(default = 2)>] [<arg>]"
<< "--help Display usage" << endl
<< "--color <value> Change dices color" << endl;
cout << " value:" <<endl
<<" * black"<< endl
<<" * red"<< endl
<<" * green"<<endl
<<" * brown"<<endl
<<" * blue"<<endl
<<" * purple"<<endl
<<" * cyan"<<endl
<<" * lightgray" <<endl;
}
string GetColor(string color){
if(color == "black") return "30";
else if(color == "black") return "30";
else if(color == "red") return "31";
else if(color == "green") return "32";
else if(color == "brown") return "33";
else if(color == "blue") return "34";
else if(color == "purple") return "35";
else if(color == "cyan") return "36";
else if(color == "lightgray") return "37";
return "-1";
}
int TenPow(int n){
int result = 1;
for(int i = 0; i < n;++i){
result*=10;
}
return result;
}
int GetNumberOfDices(string num){
int Num = 0;
int pow = 0;
for(int i = num.length() - 1; i >= 0;--i){
if(num[i] >= '0' && num[i] <= '9'){
Num+=(num[i]-'0')*TenPow(pow);
++pow;
}
else return -1;
}
return Num;
}
string dices(int i, string color){
//⚀ ⚁ ⚂ ⚃ ⚄ ⚅
switch(i){
case 0: {
return "\e[" + color + "m⚀ " + "\e[0m";
}
case 1: {
return "\e[" + color + "m⚁ " + "\e[0m";
}
case 2: {
return "\e[" + color + "m⚂ " + "\e[0m";
}
case 3: {
return "\e[" + color + "m⚃ " + "\e[0m";
}
case 4: {
return "\e[" + color + "m⚄ " + "\e[0m";
}
default: {
return "\e[" + color + "m⚅ " + "\e[0m";
}
}
}
int main(int argc, char **argv){
// cout << endl;
string color = "";
int numberOfDices = 1;
/* for(int i = 0; i < 3; ++i){
cout << argv[i] << " ";
}
*/
if(argc == 2){
string arg(argv[1]);
if(arg == "--help"){
Help();
return 0;
}
if(arg == "--color"){
cout << "No color found" << endl;
return 0;
}
numberOfDices = GetNumberOfDices(string(argv[1]));
//cout << numberOfDices;
if(numberOfDices == -1){
cout << "Please input correct number of dices." << endl;
return 0;
}
}
if(argc > 1){
numberOfDices = GetNumberOfDices(string(argv[1]));
//cout << numberOfDices;
if(numberOfDices == -1){
cout << "Please input correct number of dices." << endl;
return 0;
}
for(int i = 2; i < argc;++i){
string arg(argv[i]);
if(arg == "--help"){
Help();
return 0;
}
if(arg == "--color"){
if(argc == i) {
cout << "Please input color after --color." << endl;
cout << "Example: dices --color blue" << endl;
return 0;
}
++i;
color = GetColor(string(argv[i]));
//cout << color;
if(color == "-1") {
cout << "Color you've inputed is not match." << endl;
cout << "Type 'dices --help' to view list of values." << endl;
return 0;
}
}
}
}
int ran;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0, 5);
for(int i = 0 ; i < numberOfDices;++i){
ran = dis(gen);
cout << dices(ran, color);
}
cout << endl;
return 0;
}