-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
216 lines (191 loc) · 6.19 KB
/
Program.cs
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
using System.CommandLine;
namespace rpdice;
class RPDice
{
static async Task<int> Main(string[] args)
{
var rootCommand = new RootCommand("Calculator for Role-Playing dice");
var diceArgument = new Argument<string>(
name: "dice input",
description: "input any value and print it out");
diceArgument.SetDefaultValue("empty");
rootCommand.AddArgument(diceArgument);
var armorOption = new Option<int>(
name: "--ac",
description: "The armor class of the target",
getDefaultValue: () => 10);
armorOption.AddAlias("-ac");
var advantageOption = new Option<int>(
name: "--advantage",
description: "If the roll has advantage [values -1,0,1]",
getDefaultValue: () => 0);
advantageOption.AddAlias("-adv");
var hitCommand = new Command("hit", "Calculate chance of hitting enemy");
hitCommand.AddOption(armorOption);
hitCommand.AddOption(advantageOption);
hitCommand.AddArgument(diceArgument);
rootCommand.AddCommand(hitCommand);
var avgCommand = new Command("avg", "Calculate the average roll");
avgCommand.AddArgument(diceArgument);
rootCommand.AddCommand(avgCommand);
rootCommand.SetHandler((diceArgument) =>
{
if (diceArgument == "empty") return;
rollDice(diceArgument);
}, diceArgument);
hitCommand.SetHandler((diceArgument, armorOption, advantageOption) =>
{
if (diceArgument == "empty") return;
CalculateHit(diceArgument, armorOption, advantageOption);
}, diceArgument, armorOption, advantageOption);
avgCommand.SetHandler((diceArgument) =>
{
if (diceArgument == "empty") return;
CalculateDice(diceArgument);
}, diceArgument);
return await rootCommand.InvokeAsync(args);
}
static void CalculateDice(string input)
{
try
{
DiceSet dice = parseDice(input);
double averageRoll = 0;
foreach (var Die in dice.diceList)
{
averageRoll += Die.amount * (Die.value / 2 + 0.5);
}
averageRoll += dice.modifier;
Console.WriteLine(averageRoll);
}
catch (Exception)
{
Console.WriteLine("Invalid Input");
}
}
static void CalculateHit(string input, int ac, int adv)
{
if (!input.Contains('d')) input = "1d0" + input;
try
{
DiceSet dice = parseDice(input);
double averageRoll = 0;
foreach (var Die in dice.diceList)
{
averageRoll += Die.amount * (Die.value / 2 + 0.5);
}
averageRoll += dice.modifier;
int clampedChance = 0;
double chanceToHit = 0;
//Take advantage into account. There is no 0% or 100% because of nat 20's and 1's.
if (adv == 1)
{
//Formula for advantage-rolls only works if the bonus(+1) is not equal to or greater than target AC.
if (averageRoll + 1 < ac)
{
chanceToHit = 1 - (Math.Pow((ac - averageRoll - 1), 2) / 400);
}
else
{
chanceToHit = 100;
}
clampedChance = Clamp((int)Math.Round(chanceToHit * 100), 10, 99);
}
else if (adv == -1)
{
//Formula for disadvantage-rolls breaks down if there is a large enough negative bonus.
if (21 + averageRoll - ac > 0)
{
chanceToHit = Math.Pow((21 + averageRoll - ac), 2) / 400;
}
else
{
chanceToHit = 0;
}
clampedChance = Clamp((int)Math.Round(chanceToHit * 100), 1, 90);
}
else
{
chanceToHit = (21 + averageRoll - ac) / 20;
clampedChance = Clamp((int)Math.Round(chanceToHit * 100), 5, 95);
}
Console.WriteLine(clampedChance + "%");
}
catch (Exception)
{
Console.WriteLine("Invalid Input");
}
}
static void rollDice(string input)
{
Random rng = new Random();
try
{
DiceSet dice = parseDice(input);
int result = 0;
foreach (var die in dice.diceList)
{
for (int i = 0; i < die.amount; i++)
{
int rValue = rng.Next(1, die.value + 1);
result += rValue;
}
}
result += dice.modifier;
Console.WriteLine(result);
}
catch (Exception)
{
Console.WriteLine("Invalid Input");
}
}
static DiceSet parseDice(string input)
{
DiceSet dice = new();
var diceInput = input.ToLower();
string[] subs = diceInput.Split('-');
diceInput = String.Join("+-", subs);
if (!diceInput.Contains('d'))
{
throw new ArgumentException("Invalid input.");
}
string[] splits = diceInput.Split('+');
foreach (string item in splits)
{
if (item.Contains('d'))
{
var die = item.Split('d');
dice.AddDie(Int32.Parse(die[0]), Int32.Parse(die[1]));
}
else
{
dice.modifier += Int32.Parse(item);
}
}
return dice;
}
static int Clamp(int value, int min, int max)
{
if (value > max) return max;
if (value < min) return min;
return value;
}
}
class DiceSet
{
public List<Die> diceList = new List<Die>();
public int modifier;
public DiceSet()
{
modifier = 0;
}
public void AddDie(int amount, int value)
{
diceList.Add(new Die(amount, value));
}
}
class Die(int dieAmount, int dieValue)
{
public int amount = dieAmount;
public int value = dieValue;
}