Enter risk!
This program will roll dice for the game of Risk. The initial input is two numbers separated by a space and if the attackers wish to do a blitz they can add one more space and an "!" to get a positive attack modifier but are committed for an all or nothing fight.
package riskdieroll;
import java.awt.Component;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.JOptionPane;
public class RiskDieRoll
{
private static Component frame;
public static void main(String[] args)
{
int spcCnt, atck1, dfnc1, rollCnt, atck2 = 0, dfnc2 = 0, valHold, atckMod;
String intInpt, Lsr;
while(true)
{
boolean k = false;
Lsr = "";
intInpt = JOptionPane.showInputDialog("Please enter the a"
+ "mount of attackers then the number of\ndefenders separa"
+ "ted by a space.", atck2 + " " + dfnc2);
spcCnt = rollCnt = 0;
valHold = intInpt.length();
atckMod = 6;
if("0 0".equals(intInpt))
{
break;
}
for(int i = 0; intInpt.charAt(i) != ' '; i++)
{
spcCnt++;
}
atck2 = atck1 = (int)Double.parseDouble(intInpt.substring(0 , spcCnt));
if(intInpt.charAt(valHold - 1) == '!')
{
k = true;
atckMod = 7;
valHold -= 2;
}
dfnc2 = dfnc1 = (int)Double.parseDouble(intInpt.substring(spcCnt + 1 , valHold));
Integer[] z = new Integer[3], y = new Integer[2];
if(atck1 < 2)
{
JOptionPane.showMessageDialog(frame, "INVALID ATTACK! You need"
+ " at least two armys to make an attack!");
atck2 = dfnc2 = 0;
}
else if(dfnc1 < 1)
{
JOptionPane.showMessageDialog(frame, "INVALID INPUT! There must"
+ " be at least one defending army!");
atck2 = dfnc2 = 0;
}
else
{
do
{
rollCnt = 0;
for(int i = 0; i < 3; i++)
{
z[i] = (int)Math.ceil(Math.random() * atckMod);
}
for(int i = 0; i < 2; i++)
{
y[i] = (int)Math.ceil(Math.random() * 6);
}
Arrays.sort(z, Collections.reverseOrder());
Arrays.sort(y, Collections.reverseOrder());
while(dfnc2 > 0 && rollCnt < 2 && atck2 > 1)
{
if(y[rollCnt] >= z[rollCnt])
{
atck2--;
}
else
{
dfnc2--;
}
rollCnt++;
}
}while(k && (dfnc2 > 0 && atck2 > 1));
if(dfnc2 < 1)
{
Lsr = "\n\nDefenders have lost!!";
}
else if(atck2 < 2)
{
Lsr = "\n\nAttackers have been repelled!!!";
}
JOptionPane.showMessageDialog(frame, "Attacking force now at "
+ atck2 + " (Lost " + (atck1 - atck2) + ")" + "\nDefence force now "
+ "at " + dfnc2 + " (Lost " + (dfnc1 - dfnc2) + ")" + Lsr);
if(!"".equals(Lsr))
{
atck2 = dfnc2 = 0;
}
}
}
}
}
"Please enter the a" + "mount [...] separa" + "ted by a space."
?! I thought splitting the string on multiple lines was supposed to improve readability! \$\endgroup\$