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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Copyright 2012 Blatech <http://www.blatech.co.uk>
Author: Joe Robinson <joe@lc8n.com>
Changelog:
v0.1 - Initial release
v0.2 - Added Makefile
v0.3 - Added help
v0.31 - Changed help description
v0.4 - Added coin & dice presets
v0.5 - Added 8ball preset
v0.6 - Can now use count with presets
v0.61 - Added error handling for 8ball, removed ability to count
*/
#include <iostream>
#include <sstream>
#include <climits>
#include <string>
#include <RandomLib/Random.hpp>
int main() {
std::string version = "0.61";
RandomLib::Random r; // Create r
r.Reseed(); // and give it a unique seed
char input[100];
fgets(input, 100, stdin);
std::string strInput = input;
std::string args[30] = { "" };
std::istringstream iss(strInput);
int min = INT_MIN;
int max = INT_MIN;
int count = 1;
for(int i = 0; i < 3; i++) {
iss >> args[i];
}
if (!args[0].compare("-v") || !args[0].compare("--version")) {
std::cout << "v" << version << std::endl;
return 0;
}
if (!args[0].compare("-h") || !args[0].compare("--help")) {
std::cout << "Usage: !random [minimum bound] [maximum bound] [count] | Examples: !random : returns a number from 1 to 10 inclusive | !random 20 : Returns a number from 1 to 20 inclusive | !random 10 20 : Returns a number from 10 to 20 inclusive | !random 1 10 3 : Returns 3 random numbers from 1 to 10 inclusive" << std::endl;
return 0;
}
if (args[2].empty()) {
count = 1;
} else {
count = atoi(args[2].c_str());
}
if (args[1].empty()) {
max = atoi(args[0].c_str());
min = 1;
} else {
max = atoi(args[1].c_str());
min = atoi(args[0].c_str());
}
bool coin = false;
bool letter = false;
bool eightball = false;
bool dice = false;
bool d20 = false;
if (args[0].empty()) {
min = 1;
max = 10;
} else if (!args[0].compare("dice") || !args[0].compare("die")) {
min = 1;
max = 6;
dice = true;
} else if (!args[0].compare("coin")) {
min = 0;
max = 1;
coin = true;
} else if (!args[0].compare("d20")) {
min = 1;
max = 20;
d20 = true;
} else if (!args[0].compare("letter") || !args[0].compare("letter")) {
min = 0;
max = 25;
letter = true;
} else if (!args[0].compare("8ball")) {
min = 0;
max = 17;
eightball = true;
std::string question = "";
for (int i = 1; i < 30; i++) {
question += args[i];
}
if (question.length() == 0 ||
question.substr(question.length()-1, 1).compare("?")) {
std::cout << "Ask a question!" << std::endl;
return 1;
}
}
if (coin || dice || letter || d20) {
if (args[1].empty()) {
count = 1;
} else {
count = atoi(args[1].c_str());
}
}
int intResult = 0;
std::string strResult = "";
char letters[26] = { 'A','B','C','D','E','F','G','H','I','J','K',
'L','M','N','O','P','Q','R','S','T','U','V',
'W','X','Y','Z' };
std::string eightBallStr[18] = { "Yes.",
"No.",
"Outlook so so.",
"Absolutely.",
"My sources say no.",
"Yes definitely.",
"Very doubtful.",
"Most likely.",
"Forget about it.",
"Are you kidding?",
"Go for it.",
"Not now.",
"Looking good.",
"Who knows.",
"A definite yes.",
"You will have to wait.",
"Yes, in due time.",
"I have my doubts." };
for (int i = 0; i < count; i++) {
intResult = r.IntegerC(min, max);
if (coin) {
if (intResult == 1) {
strResult = "H";
} else {
strResult = "T";
}
} else if (letter) {
strResult = letters[intResult];
} else if (eightball) {
strResult = eightBallStr[intResult];
} else {
std::stringstream ss;
ss << intResult;
strResult = ss.str();
}
std::cout << strResult << " ";
}
std::cout << std::endl;
return 0;
}
|