Update to Roll and Created EightBall

Added more use to the Roll command, can now do basic math in addition to rolling dice.
Created Eightball, a command to output a standard eightball saying.
This commit is contained in:
Alex Stewart
2019-03-10 20:20:11 -07:00
parent 0d0e1e09ab
commit 36b210e0f5
3 changed files with 164 additions and 50 deletions
+138 -50
View File
@@ -1,5 +1,7 @@
package commands;
import java.util.Stack;
import core.*;
import dataStructures.KittyChannel;
import dataStructures.KittyGuild;
@@ -19,71 +21,157 @@ public class CommandRoll extends Command
@Override
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
{
int dice;
int sides;
String dicenum [] = input.args.split("d");
try
{
dice = Integer.parseInt(dicenum[0]);
sides = Integer.parseInt(dicenum[1]);
res.Call(rollDice(input.args));
}
catch (NumberFormatException e)
catch(Exception e)
{
res.Call("Rude.");
return;
res.Call("Didn't work ;3;");
}
res.Call(rollDice(dice,sides));
}
private String rollDice(int dice, int sides)
private String rollDice(String thing)
{
Stack<Integer> values = new Stack<Integer>();
Stack<Character> operators = new Stack<Character>();
for(int i = 0; i < thing.length(); i ++)
{
char current = thing.charAt(i);
switch(current)
{
case '+':
case '-':
case '*':
case '/':
if(operators.isEmpty() || !hasPrecendence(operators.peek(), current) || operators.peek() == '(')
operators.add(current);
else
{
calculate(values, operators);
operators.add(current);
}
continue;
case '(':
operators.add(current);
continue;
case ')':
if(operators.peek() == '(')
operators.pop();
else
{
i --;
calculate(values, operators);
}
continue;
case 'd':
if(operators.isEmpty() || operators.peek() == '(' || !hasPrecendence(operators.peek(), current))
operators.add(current);
else
calculate(values, operators);
continue;
case ' ':
continue;
default:
try
{
// Accumulate valid characters
String accumulated = "";
for(int j = i; j < thing.length(); ++j)
{
char parse = thing.charAt(j);
if(Character.isDigit(parse))
{
accumulated += parse; // Keep tabs on our character
++i; // Offset overall loop
continue;
}
// Default leave
break;
}
if(accumulated.length() <= 0)
throw new Exception("Error! Invalid character!");
values.add(Integer.parseInt(accumulated));
i--;
}
catch (Exception e)
{
System.out.print("Something went wrong");
i = thing.length();
}
}
}
while(operators.size() > 0)
{
calculate(values, operators);
}
return "" + values.pop();
}
private void calculate(Stack<Integer> nums, Stack<Character> operators)
{
int second = nums.pop();
int first = nums.pop();
int value = 0;
switch(operators.pop())
{
case '+':
value = first + second;
break;
case '-':
value = first - second;
break;
case '*':
value = first * second;
break;
case '/':
value = first / second;
break;
case 'd':
value = roll(first, second);
}
nums.add(value);
}
private int roll(int first, int second)
{
int total = 0;
int roll = 0;
String nums = "";
//checks for lower than 1 dice or dice size
if(dice > 100 || sides > 100)
{
return ("*drowns in dice*");
}
if(dice < 1 || sides < 1)
{
return("I can't do that!");
}
int dice = first;
int sides = second;
if(dice < 1)
return 0;
for(int i = 0; dice > i; i ++)
{
roll = (int)(Math.random() * sides) + 1;
nums += roll + " ";
total += roll;
}
//makes output look nicer
String prettyNums = "";
String[] splitNums = nums.split(" ");
if(splitNums.length > 10)
{
final int len = 5;
for(int i = 0; i < splitNums.length; i += len)
{
prettyNums += "\n";
for(int j = 0; j < len; ++j)
{
if(j != 0)
prettyNums += " ";
prettyNums += splitNums[i + j];
}
}
}
else
{
prettyNums = nums;
}
return ("Rolls are\n" + prettyNums + "\n" + "Total is: " + total);
return total;
}
private boolean hasPrecendence(char op1, char op2)
{
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
if ((op2 == 'd') && (op1 == '+' || op1 == '-' || op1 == '*' || op1 == '/'))
return false;
else
return true;
}
}