-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiceRoller
307 lines (270 loc) · 6.52 KB
/
DiceRoller
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
public class Program
{
public static readonly Random roll = new Random();
private static readonly object syncLock = new object();
public static int dieSize {get; set;}
public static int diceCount {get; set;}
public static bool reroll {get; set;}
public static bool modReroll {get; set;}
public static int eachValue { get; set; }
public static int allValue { get; set; }
public static void Main()
{//Initializes Dice Rolla Program
Begin();
}
public static void Begin()
{//Sequence of events to get inputs and return results
if(!reroll)
{//If user is rerolling exactly the previously used dice, do not ask for inputs about dice.
InputDie();
InputCount();
}
if(!modReroll)
{//If user is rerolling exactly the previously used modifiers, do not ask for inputs about modifiers.
InputMods();
}
Results();
//Display results of dice rolls with applicable modifiers
Again();
//Ask if user would like to Roll again, Reroll previous dice, and if they want the same modifiers.
}
public static void InputDie()
{//Gathers input for the size of the Die/Dice
string die_size;
do
{
Console.Write("Choose a Die by sides: 2, 4, 6, 8, 10, 12, 20, 100: ");
die_size = Console.ReadLine();
}
while (!validSize(die_size));
dieSize = Convert.ToInt32(die_size);
}
public static void InputCount()
{//Gathers input for the amount of Dice
string die_count;
do
{
Console.Write("How many dice do you want to roll? ");
die_count = Console.ReadLine();
}while (!validCount(die_count));
diceCount = Convert.ToInt32(die_count);
}
//To Complete:
public static void InputMods()
{//Gathers input for modifiers to the Dice
string addMod;
string addEach;
string addAll;
eachValue = 0;
allValue = 0;
do
{
if(!reroll&&!modReroll){
Console.Write("Would you like to have a modifier on the roll? (y/n): ");
addMod = Console.ReadLine();
} else {
addMod = "y";
}
}while (!validYN(addMod));
if(addMod == "y"){
do
{
Console.Write("Is this modifier on the total of your roll? (y/n): ");
addAll = Console.ReadLine();
}while(!validYN(addAll));
if (addAll == "n"){
do
{
Console.Write("Is this modifier on each die? (y/n): ");
addEach = Console.ReadLine();
}while(!validYN(addEach));
if(addEach == "n")
{
return;
}
InputModValue(true);
} else {
InputModValue(false);
}
}
}
public static void InputModValue(bool isEach)
{
string modInput;
int modifier;
Console.Write("What value would you like for this modifier? (precede with - for negative) ");
modInput = Console.ReadLine();
if(!Int32.TryParse(modInput, out modifier))
{
BadInput();
}
if(isEach)
{
allValue = 0;
eachValue = modifier;
} else {
eachValue = 0;
allValue = modifier;
}
}//To Complete ^^
public static void Results()
{//Gathers the results of the Dice rolls into an array
int[] results = new int[diceCount];
for (int i = 0; i < diceCount; i++)
{
results[i] = rollDie(dieSize);
}
printResults(results);
}
public static bool validSize(string size)
{//Checks if input for Die size is valid
int die;
if (!int.TryParse(size, out die))
{
BadInput();
return false;
}
switch (die)
{
case 2:
case 4:
case 6:
case 8:
case 10:
case 12:
case 20:
case 100:
return true;
default:
BadInput();
return false;
}
}
public static bool validCount(string count)
{//Checks if the input for the number of dice is valid
int dice;
if (!int.TryParse(count, out dice))
{
BadInput();
return false;
}
if (dice > 0)
{
return true;
}
BadInput();
return false;
}
public static bool validYN(string check)
{//Checks if input for any (y/n) input is valid
switch(check)
{
case "y":
case "n":
return true;
default:
BadInput();
return false;
}
}
public static int rollDie(int inputDie)
{//Uses Random object to generate a die result, then returns the result to Results() for the array.
lock(syncLock)
{
int result = roll.Next(1, inputDie + 1);
return result;
}
}
public static void printResults(int[] results)
{//Outputs the amount of dice, dice size, total of rolls, and individual rolls results for the user.
int total = 0;
int modValue = 0;
string addSub = "";
string each ="";
for (int i = 0; i < results.Length; i++)
{
total += results[i];
}
if(allValue != 0)
{
modValue = allValue;
total += modValue;
}else if(eachValue != 0)
{
modValue = eachValue;
total += modValue * diceCount;
each = "(each)";
}
if(modValue > 0)
{
addSub = "+";
}
Console.WriteLine("");
if(modValue != 0){
Console.Write("You rolled {0}d{1}{5}{4} {6} for: {2} [{3}] \n", results.Length, dieSize, total, string.Join(", ", results), modValue, addSub, each);
}else{
Console.Write("You rolled {0}d{1} for: {2} [{3}] \n", results.Length, dieSize, total, string.Join(", ", results));
}
Console.WriteLine("");
}
public static void BadInput()
{//Outputs message for invalid inputs
Console.WriteLine("--That is not valid, try again.--");
Console.WriteLine("");
}
public static void Again()
{//Takes input if the user would like to use the Dice Rolla again.
Console.Write("Would you like to roll again? (y/n): ");
string rerun = Console.ReadLine();
switch(rerun)
{
case "y":
Reroll();
break;
default:
break;
}
}
public static void Reroll()
{//Takes input is the user would like to roll the same dice again.
string rollAgain;
do{
Console.Write("Would you like to reroll your previous dice? (y/n): ");
rollAgain = Console.ReadLine();
}while(!validYN(rollAgain));
switch(rollAgain)
{
case "y":
reroll = true;
ModReroll();
Begin();
break;
case "n":
reroll = false;
Begin();
break;
default:
break;
}
}
public static void ModReroll()
{//Takes input if the user would like to use the same modifiers again.
string rollAgain;
do{
Console.Write("Would you like to change your modifier? (y/n): ");
rollAgain = Console.ReadLine();
}while(!validYN(rollAgain));
switch(rollAgain)
{
case "y":
modReroll = false;
break;
case "n":
modReroll = true;
break;
default:
break;
}
}
}