-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroll.c
More file actions
executable file
·223 lines (204 loc) · 9.83 KB
/
roll.c
File metadata and controls
executable file
·223 lines (204 loc) · 9.83 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int rrange(int, int);
int main(int argc, char** argv)
{
srand(time(NULL));
if(argc <= 1 || argc > 3)
{
printf("Roll rolls a specified numer of dice, and tells you the result.\n");
printf("To use roll, type the command roll, followed by a string in this format:\n");
printf("[<number of dice to roll>]d<maximum value of die>\n");
printf("[<+, -, * or /><number to add, subtract, multiply or divide from value>]\n");
printf("[<'<' or '>'><the current throw will succeed it the throw is 'larger than or\n");
printf(" equal to', or 'less than or equal to', this number>]\n");
printf("You may need to surround your command with inverted commas.\n");
printf("\n");
printf("Examples:\n");
printf("roll \"2d6\" This rolls 2 six-sided dice.\n");
printf("roll \"d12+4\" This rolls a twelve-sided die, and adds 4 to the result.\n");
printf("roll \"3d12>7\" This rolls 3 12-sided dice, and for each throw, tells you that");
printf(" throw succeeded if the result was larger or equal to 7.\n");
printf("roll \"7d24*2>40\" This rolls 7 24-sided dice, and multiplieseach result by");
printf(" 2. Then roll tells you whether each result was larger than 40 or not.\n");
printf("\n");
printf("If you put a \"-n\" on the end of the command, then roll will make sure that");
printf("you won't miss any of the dice rolls.\n");
return 0;
}
int howMany = 0;
int diceValue = 0;
int whichSign = 0;
int changeAmount = 0;
char switchBy = 0;
int throwSuccess = 0;
float temp = 0;
if(argv[1][0] == 'd')
{
howMany = 1;
int tempCount = sscanf(argv[1], "d%i%c%i%c%i", &diceValue, &whichSign, &changeAmount, &switchBy, &throwSuccess);
if(tempCount == EOF || tempCount == 0 || tempCount == 2 || tempCount == 4 || tempCount > 5)
{
printf("Roll rolls a specified numer of dice, and tells you the result.\n");
printf("To use roll, type the command roll, followed by a string in this format:\n");
printf("[<number of dice to roll>]d<maximum value of die>\n");
printf("[<+, -, * or /><number to add, subtract, multiply or divide from value>]\n");
printf("[<'<' or '>'><the current throw will succeed it the throw is 'larger than or\n");
printf(" equal to', or 'less than or equal to', this number>]\n");
printf("You may need to surround your command with inverted commas.\n");
printf("\n");
printf("Examples:\n");
printf("roll \"2d6\" This rolls 2 six-sided dice.\n");
printf("roll \"d12+4\" This rolls a twelve-sided die, and adds 4 to the result.\n");
printf("roll \"3d12>7\" This rolls 3 12-sided dice, and for each throw, tells you that");
printf(" throw succeeded if the result was larger or equal to 7.\n");
printf("roll \"7d24*2>40\" This rolls 7 24-sided dice, and multiplieseach result by");
printf(" 2. Then roll tells you whether each result was larger than 40 or not.\n");
printf("\n");
printf("If you put a \"-n\" on the end of the command, then roll will make sure that");
printf("you won't miss any of the dice rolls.\n");
return 0;
}
else if(tempCount == 1)
{
printf("Die rolled a %i.\n", rrange(1, diceValue));
}
else if(tempCount == 3)
{
temp = rrange(1, diceValue);
switch(whichSign)
{
case '+': printf("Die rolled a %.0f+%i = %.0f.\n", temp, changeAmount, temp + changeAmount);
temp += changeAmount; break;
case '-': printf("Die rolled a %.0f-%i = %.0f.\n", temp, changeAmount, temp - changeAmount);
temp -= changeAmount; break;
case '*': printf("Die rolled a %.0f*%i = %.0f.\n", temp, changeAmount, temp * changeAmount);
temp *= changeAmount; break;
case '/': printf("Die rolled a %.0f/%i = %.2f.\n", temp, changeAmount, temp / (float)changeAmount);
temp /= changeAmount; break;
case '>': printf("Die rolled a %.0f: throw %s.\n",
temp, (temp >= changeAmount) ? "succeeded" : "failed"); break;
case '<': printf("Die rolled a %.0f: throw %s.\n",
temp, (temp <= changeAmount) ? "succeeded" : "failed"); break;
}
}
else if(tempCount == 5)
{
temp = rrange(1, diceValue);
switch(whichSign)
{
case '+': printf("Die rolled a %.0f+%i = %.0f:", temp, changeAmount, temp + changeAmount);
temp += changeAmount; break;
case '-': printf("Die rolled a %.0f-%i = %.0f:", temp, changeAmount, temp - changeAmount);
temp -= changeAmount; break;
case '*': printf("Die rolled a %.0f*%i = %.0f:", temp, changeAmount, temp * changeAmount);
temp *= changeAmount; break;
case '/': printf("Die rolled a %.0f/%i = %.02:", temp, changeAmount, temp / (float)changeAmount);
temp /= changeAmount; break;
}
switch(switchBy)
{
case '>': printf(" throw %s.\n", (temp >= throwSuccess) ? "succeeded" : "failed"); break;
case '<': printf(" throw %s.\n", (temp <= throwSuccess) ? "succeeded" : "failed"); break;
}
}
}
else
{
int tempCount = sscanf(argv[1], "%id%i%c%i%c%i", &howMany, &diceValue, &whichSign, &changeAmount, &switchBy, &throwSuccess);
if(tempCount == EOF || tempCount <= 1 || tempCount == 3 || tempCount == 5 || tempCount > 6)
{
printf("Roll rolls a specified numer of dice, and tells you the result.\n");
printf("To use roll, type the command roll, followed by a string in this format:\n");
printf("[<number of dice to roll>]d<maximum value of die>\n");
printf("[<+, -, * or /><number to add, subtract, multiply or divide from value>]\n");
printf("[<'<' or '>'><the current throw will succeed it the throw is 'larger than or\n");
printf(" equal to', or 'less than or equal to', this number>]\n");
printf("You may need to surround your command with inverted commas.\n");
printf("\n");
printf("Examples:\n");
printf("roll \"2d6\" This rolls 2 six-sided dice.\n");
printf("roll \"d12+4\" This rolls a twelve-sided die, and adds 4 to the result.\n");
printf("roll \"3d12>7\" This rolls 3 12-sided dice, and for each throw, tells you that");
printf(" throw succeeded if the result was larger or equal to 7.\n");
printf("roll \"7d24*2>40\" This rolls 7 24-sided dice, and multiplies each result by");
printf(" 2. Then roll tells you whether each result was larger than 40 or not.\n");
printf("\n");
printf("If you put a \"-n\" on the end of the command, then roll will make sure that");
printf("you won't miss any of the dice rolls.\n");
return 0;
}
else if(tempCount == 2)
{
for(int i = 0; i < howMany; i++)
{
printf("Die %i rolled a %i.\n", (i + 1), rrange(1, diceValue));
}
}
else if(tempCount == 4)
{
for(int i = 0; i < howMany; i++)
{
temp = rrange(1, diceValue);
switch(whichSign)
{
case '+': printf("Die rolled a %.0f+%i = %.0f.\n", temp, changeAmount, temp + changeAmount);
temp += changeAmount; break;
case '-': printf("Die rolled a %.0f-%i = %.0f.\n", temp, changeAmount, temp - changeAmount);
temp -= changeAmount; break;
case '*': printf("Die rolled a %.0f*%i = %.0f.\n", temp, changeAmount, temp * changeAmount);
temp *= changeAmount; break;
case '/': printf("Die rolled a %.0f/%i = %.02.\n", temp, changeAmount, temp / (float)changeAmount);
temp /= changeAmount; break;
case '>': printf("Die rolled a %.0f: throw %s.\n",
temp, (temp >= changeAmount) ? "succeeded" : "failed"); break;
case '<': printf("Die rolled a %.0f: throw %s.\n",
temp, (temp <= changeAmount) ? "succeeded" : "failed"); break;
}
}
}
else if(tempCount == 6)
{
for(int i = 0; i < howMany; i++)
{
temp = rrange(1, diceValue);
switch(whichSign)
{
case '+': printf("Die rolled a %.0f+%i = %.0f:", temp, changeAmount, temp + changeAmount);
temp += changeAmount; break;
case '-': printf("Die rolled a %.0f-%i = %.0f:", temp, changeAmount, temp - changeAmount);
temp -= changeAmount; break;
case '*': printf("Die rolled a %.0f*%i = %.0f:", temp, changeAmount, temp * changeAmount);
temp *= changeAmount; break;
case '/': printf("Die rolled a %.0f/%i = %.02:", temp, changeAmount, temp / (float)changeAmount);
temp /= changeAmount; break;
}
switch(switchBy)
{
case '>': printf(" throw %s.\n", (temp >= throwSuccess) ? "succeeded" : "failed"); break;
case '<': printf(" throw %s.\n", (temp <= throwSuccess) ? "succeeded" : "failed"); break;
}
}
}
}
if(argc > 2)
{
if(strlen(argv[2]) == 2 && argv[2][0] == '-' && argv[2][1] == 'n')
{
printf("Press any key to quit... ");
getchar();
return 0;
}
}
}
int rrange(int a, int b)
{
int temp = rand();
while(temp < a || temp > b)
{
temp = rand();
}
return temp;
}