New To Java - null
How can I compare one die to another? I play the game Risk. On each roll, one player rolls up to 3 dice and the other player rolls up to 2 dice. I am going to add two more dice for the second player. I would like to display a comparison of the first roller's highest die to the second player's highest die and the first roller's 2nd highest die to the second player's lowest die.
This is what I have so far:
import java.awt.*;
import java.awt.event.*;
class DiceApp4extends Frameimplements ActionListener,
WindowListener{
Label directions; Button die1; Button die2; Button clear;
Button die3;
TextArea roll;
TextArea roll2;
TextArea roll3;
TextArea showdie;
privatestatic String d1 ="1 die";
privatestatic String d2 ="2 dice";
privatestatic String d3 ="3 dice";
privatestatic String c ="Press to clear";
d1 n1 =new d1();
DiceApp4(String s){
super(s);
setSize(1000, 600);
setLayout(new FlowLayout());
addWindowListener(this);
directions =new Label("click on button to choose # of Die to roll.");
add(directions, BorderLayout.NORTH);
die1 =new Button("1 die");
die1.setActionCommand(d1);
die1.addActionListener(this);
add(die1);
die2 =new Button("2 dice");
die2.setActionCommand(d2);
die2.addActionListener(this);
add(die2);
die3 =new Button("3 dice");
die3.setActionCommand(d3);
die3.addActionListener(this);
add(die3);
clear =new Button("Press to clear");
clear.setActionCommand(c);
clear.addActionListener(this);
add(clear);
roll =new TextArea(20,16);
add(roll, BorderLayout.CENTER);
setVisible(true);
roll2 =new TextArea(20, 16);
add(roll2, BorderLayout.SOUTH);
setVisible(true);
roll3 =new TextArea(20, 16);
add(roll3, BorderLayout.EAST);
setVisible(true);
showdie =new TextArea(8, 10);
add(showdie, BorderLayout.SOUTH);
setVisible(true);
}
publicvoid windowClosed(WindowEvent event){}
publicvoid windowDeiconified(WindowEvent event)
{}
publicvoid windowIconified(WindowEvent event){}
publicvoid windowActivated(WindowEvent event){}
publicvoid windowDeactivated(WindowEvent event)
{}
publicvoid windowOpened(WindowEvent event){}
publicvoid windowClosing(WindowEvent event)
{ System.exit(0);}
publicvoid actionPerformed(ActionEvent ae){
String asd = ae.getActionCommand();
int die = (int) (Math.random() * 6 +1);
String q ="\n -";
switch (die){
case 1:
q +="\n||";
q +="\n|*|";
q +="\n||";
q +="\n -";
q +="\n1";
break;
case 2:
q +="\n|* |";
q +="\n||";
q +="\n| *|";
q +="\n -";
q +="\n2";
break;
case 3:
q +="\n|* |";
q +="\n|*|";
q +="\n| *|";
q +="\n -";
q +="\n3";
break;
case 4:
q +="\n| ** |";
q +="\n| |";
q +="\n| ** |";
q +="\n -";
q +="\n4";
break;
case 5:
q +="\n| ** |";
q +="\n|*|";
q +="\n| ** |";
q +="\n -";
q +="\n5";
break;
case 6:
q +="\n| ** |";
q +="\n| ** |";
q +="\n| ** |";
q +="\n -";
q +="\n6";
break;
default:
q +="\n||";
q +="\n|?|";
q +="\n||";
}
String r = String.valueOf(q);
int die2 = (int) (Math.random() * 6 +1);
String m ="\n -";
switch (die2){
case 1:
m +="\n||";
m +="\n|*|";
m +="\n||";
m +="\n -";
m +="\n1";
break;
case 2:
m +="\n|* |";
m +="\n||";
m +="\n| *|";
m +="\n -";
m +="\n2";
break;
case 3:
m +="\n|* |";
m +="\n|*|";
m +="\n| *|";
m +="\n -";
m +="\n3";
break;
case 4:
m +="\n| ** |";
m +="\n||";
m +="\n| ** |";
m +="\n -";
m +="\n4";
break;
case 5:
m +="\n| ** |";
m +="\n|*|";
m +="\n| ** |";
m +="\n -";
m +="\n5";
break;
case 6:
m +="\n| ** |";
m +="\n| ** |";
m +="\n| ** |";
m +="\n -";
m +="\n6";
break;
default:
m +="\n||";
m +="\n|?|";
m +="\n||";
m +="\n -";
m +="\n?";
}
String w = String.valueOf(m);
int die3 = (int) (Math.random() * 6 +1);
String v ="\n -";
switch (die3){
case 1:
v +="\n||";
v +="\n|*|";
v +="\n||";
v +="\n -";
v +="\n1";
break;
case 2:
v +="\n|* |";
v +="\n||";
v +="\n| *|";
v +="\n -";
v +="\n2";
break;
case 3:
v +="\n|* |";
v +="\n|*|";
v +="\n| *|";
v +="\n -";
v +="\n3";
break;
case 4:
v +="\n| ** |";
v +="\n||";
v +="\n| ** |";
v +="\n -";
v +="\n4";
break;
case 5:
v +="\n| ** |";
v +="\n|*|";
v +="\n| ** |";
v +="\n -";
v +="\n5";
break;
case 6:
v +="\n| ** |";
v +="\n| ** |";
v +="\n| ** |";
v +="\n -";
v +="\n6";
break;
default:
v +="\n||";
v +="\n|?|";
v +="\n||";
v +="\n -";
v +="\n?";
}
String z = String.valueOf(v);
if (d1.equals(asd)){
roll.setText(r);
}
elseif(d2.equals(asd)){
roll2.setText(r+ w);
}
elseif(d3.equals(asd)){
roll3.setText(r + w + z);
}
elseif(c.equals(asd)){
roll.setText("");
roll2.setText("");
roll3.setText("");
}
}
publicstaticvoid main(String[] args){
DiceApp4 frame =new DiceApp4("Roll Dice");
Image onFrame = Toolkit.getDefaultToolkit().getImage("whitedie.gif");
frame.setIconImage(onFrame);
}
}
Thank you,
dw15194

