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:
@@ -0,0 +1,25 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
import core.Command;
|
||||||
|
import dataStructures.*;
|
||||||
|
|
||||||
|
public class CommandEightBall extends Command
|
||||||
|
{
|
||||||
|
String [] answers = {"It is certain.", "It is decidedly so.", "Without a doubt.", "Yes - definitely.", "You may rely on it.", "As I see it, yes.", "Most likely.",
|
||||||
|
"Outlook good.", "Yes.", "Signs point to yes.", "Reply hazy, try again.", "Ask again later.", "Better not tell you now.", "Cannot predict now.",
|
||||||
|
"Concentrate and ask again.", "Don't count on it.", "My reply is no.", "My sources say no.", "Outlook not so good.", "Very doubtful."};
|
||||||
|
public CommandEightBall(KittyRole level, KittyRating rating)
|
||||||
|
{
|
||||||
|
super(level, rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String HelpText() { return "Answers a yes or no question! Warning: Kitty can not actually tell the future,"
|
||||||
|
+ " she claims no responsibility for any lion mauling, lack of lottery wins, or felony charges."; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
|
||||||
|
{
|
||||||
|
res.Call(answers[(int) (Math.random()*answers.length)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
+138
-50
@@ -1,5 +1,7 @@
|
|||||||
package commands;
|
package commands;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
import core.*;
|
import core.*;
|
||||||
import dataStructures.KittyChannel;
|
import dataStructures.KittyChannel;
|
||||||
import dataStructures.KittyGuild;
|
import dataStructures.KittyGuild;
|
||||||
@@ -19,71 +21,157 @@ public class CommandRoll extends Command
|
|||||||
@Override
|
@Override
|
||||||
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
|
public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res)
|
||||||
{
|
{
|
||||||
int dice;
|
|
||||||
int sides;
|
|
||||||
String dicenum [] = input.args.split("d");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
dice = Integer.parseInt(dicenum[0]);
|
res.Call(rollDice(input.args));
|
||||||
sides = Integer.parseInt(dicenum[1]);
|
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e)
|
catch(Exception e)
|
||||||
{
|
{
|
||||||
res.Call("Rude.");
|
res.Call("Didn't work ;3;");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 total = 0;
|
||||||
int roll = 0;
|
int roll = 0;
|
||||||
String nums = "";
|
int dice = first;
|
||||||
|
int sides = second;
|
||||||
//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!");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if(dice < 1)
|
||||||
|
return 0;
|
||||||
for(int i = 0; dice > i; i ++)
|
for(int i = 0; dice > i; i ++)
|
||||||
{
|
{
|
||||||
roll = (int)(Math.random() * sides) + 1;
|
roll = (int)(Math.random() * sides) + 1;
|
||||||
nums += roll + " ";
|
|
||||||
total += roll;
|
total += roll;
|
||||||
}
|
}
|
||||||
|
return total;
|
||||||
//makes output look nicer
|
}
|
||||||
String prettyNums = "";
|
|
||||||
String[] splitNums = nums.split(" ");
|
private boolean hasPrecendence(char op1, char op2)
|
||||||
if(splitNums.length > 10)
|
{
|
||||||
{
|
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
|
||||||
final int len = 5;
|
return false;
|
||||||
for(int i = 0; i < splitNums.length; i += len)
|
else
|
||||||
{
|
if ((op2 == 'd') && (op1 == '+' || op1 == '-' || op1 == '*' || op1 == '/'))
|
||||||
prettyNums += "\n";
|
return false;
|
||||||
for(int j = 0; j < len; ++j)
|
else
|
||||||
{
|
return true;
|
||||||
if(j != 0)
|
|
||||||
prettyNums += " ";
|
|
||||||
|
|
||||||
prettyNums += splitNums[i + j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
prettyNums = nums;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ("Rolls are\n" + prettyNums + "\n" + "Total is: " + total);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -325,6 +325,7 @@ public class ObjectBuilderFactory
|
|||||||
manager.Register("rpend", new CommandRPEnd(KittyRole.General, KittyRating.Safe));
|
manager.Register("rpend", new CommandRPEnd(KittyRole.General, KittyRating.Safe));
|
||||||
manager.Register(new String[] {"tony", "stark", "dontfeelgood", "dontfeelsogood"}, new CommandStark(KittyRole.General, KittyRating.Safe));
|
manager.Register(new String[] {"tony", "stark", "dontfeelgood", "dontfeelsogood"}, new CommandStark(KittyRole.General, KittyRating.Safe));
|
||||||
manager.Register("blur", new CommandBlurry(KittyRole.General, KittyRating.Safe));
|
manager.Register("blur", new CommandBlurry(KittyRole.General, KittyRating.Safe));
|
||||||
|
manager.Register(new String [] {"eightball", "8ball"}, new CommandEightBall(KittyRole.General, KittyRating.Safe));
|
||||||
|
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user