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

[15071 byte] By [dw15194a] at [2007-11-26 23:16:47]
# 1

Large chunks of repeated code is sure sign that you need to refactor. A Die is a perfect candidate to make into a separate class. It should be responsible for choosing random numbers, and creating a String representation of itself.

public class Die {

private Random r = new Random();

public static final String[] stringReps= {

"\n||\n|*|\n||\n -\n1",// face of a 1

"" // etc

};

public int roll() {

return r.nextInt(6) + 1;

}

public String getStringRep(int i) {

return stringReps[i+1];

}

}

Then you only have to create one instance of a Die class and keep calling the roll method to simulate rolling a die, and then the getStringRep method with that result to get the String representation of that result.

You shouldn't compare the dice, you should compare the results from rolling the dice, which are ints.

hunter9000a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 2

I did as you suggested and made another class for the die, but I still can't figure out how to compare rolls. I tried a couple things, but my ideas did not work. I can't figure out how to compare the highest random int from the attacker to the highest random int from the defender's roll... and so on from the second highest.

import java.util.Random;

public class Die6 {

private Random r = new Random();

public static final String[] stringReps= {"3", "5",

"\n -\n||\n|*|\n||\n -\n1",// face of a 1

"\n -\n|* |\n||\n|*|\n -\n2", // face of a 2

"\n -\n|* |\n|*|\n| *|\n -\n3", //face of a 3

"\n -\n| ** |\n||\n| ** |\n -\n4", //face of a 4

"\n -\n| ** |\n|*|\n| ** |\n -\n5",//face of a 5

"\n -\n| ** |\n| ** |\n| ** |\n -\n6"//face of a 6

};

public int roll() {

return r.nextInt(6) + 1;

}

public String getStringRep(int i) {

return stringReps[i+1];

}

}

import java.awt.*;

import java.awt.event.*;

class DiceApp5 extends Frame implements ActionListener,

WindowListener{

Label directions;

Label directions2; Button die1; Button die2; Button clear;

Button die3;

Button die4;

Button die5;

TextArea roll;

TextArea roll2;

TextArea roll3;

TextArea roll4;

TextArea roll5;

TextArea showdie;

private static String d1 = "1 die";

private static String d2 = "2 dice";

private static String d3 = "3 dice";

private static String d4 = "1 dies";

private static String d5 = "2 dices";

private static String c = "Press to clear";

d1 n1 = new d1();

Die6 diecube = new Die6();

DiceApp5(String s) {

super(s);

setSize(1000, 750);

setLayout(new FlowLayout());

addWindowListener(this);

directions = new Label("Attacker click button to choose # of Die to roll.");

add(directions);

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 all");

clear.setActionCommand(c);

clear.addActionListener(this);

add(clear);

roll = new TextArea(10,16);

add(roll);

setVisible(true);

roll2 = new TextArea(15, 16);

add(roll2);

setVisible(true);

roll3 = new TextArea(20, 16);

add(roll3);

setVisible(true);

directions2 = new Label("Defender click button to choose # of Die to roll.");

add(directions2);

die4 = new Button("1 die");

die4.setActionCommand(d4);

die4.addActionListener(this);

add(die4);

die5 = new Button("2 dice");

die5.setActionCommand(d5);

die5.addActionListener(this);

add(die5);

roll4 = new TextArea(10,16);

add(roll4);

setVisible(true);

roll5 = new TextArea(15,16);

add(roll5);

setVisible(true);

showdie = new TextArea(6, 70);

add(showdie);

setVisible(true);

}

public void windowClosed(WindowEvent event){}

public void windowDeiconified(WindowEvent event)

{}

public void windowIconified(WindowEvent event){}

public void windowActivated(WindowEvent event){}

public void windowDeactivated(WindowEvent event)

{}

public void windowOpened(WindowEvent event){}

public void windowClosing(WindowEvent event)

{ System.exit(0); }

public void actionPerformed(ActionEvent ae) {

String asd = ae.getActionCommand();

if (d1.equals(asd)) {

roll.setText(diecube.getStringRep(diecube.roll()));

}

else if(d2.equals(asd)) {

roll2.setText(diecube.getStringRep(diecube.roll()) + diecube.getStringRep(diecube.roll()));

}

else if(d3.equals(asd)) {

roll3.setText(diecube.getStringRep(diecube.roll()) + diecube.getStringRep(diecube.roll()) + diecube.getStringRep(diecube.roll()));

}

else if(d4.equals(asd)) {

roll4.setText(diecube.getStringRep(diecube.roll()));

}

else if(d5.equals(asd)) {

roll5.setText(diecube.getStringRep(diecube.roll()) + diecube.getStringRep(diecube.roll()));

}

else if(c.equals(asd)) {

roll.setText("");

roll2.setText("");

roll3.setText("");

roll4.setText("");

roll5.setText("");

}

}

public static void main(String[] args) {

DiceApp5 frame = new DiceApp5("Roll Dice");

Image onFrame = Toolkit.getDefaultToolkit().getImage("whitedie.gif");

frame.setIconImage(onFrame);

}

}

Thank you,

dw15194

dw15194a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 3

I did as you suggested and made another class for the

die, but I still can't figure out how to compare

rolls. I tried a couple things, but my ideas did not

work. I can't figure out how to compare the highest

random int from the attacker to the highest random

int from the defender's roll... and so on from the

second highest.

There are several discrete steps in what you described. It's a good idea to take a large set of steps and break it down, it makes it easier to work with.

1) Make a series of rolls for the attacker and the defender. Store them in some kind of list.

2) Find the highest roll of the attacker, and of the defender.

3) Compare them and make some decision based on the comparison

4) Repeat step 2 with the second highest roll, etc.

hunter9000a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 4

There are several discrete steps in what you

described. It's a good idea to take a large set of

steps and break it down, it makes it easier to work

with.

1) Make a series of rolls for the attacker and the

defender. Store them in some kind of list.

Doesn't my work above show a series of rolls? If so, how can I store them in some kind of list? If not, could you please elaborate on how to make a series of rolls.

Thank you!

dw15194a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 5

Doesn't my work above show a series of rolls? If so,

how can I store them in some kind of list? If not,

could you please elaborate on how to make a series of

rolls.

I only see code that makes one roll, and sets a textarea's text to the String representation of that roll.

Here's a mock method that you could use:

private CompareRolls(int numAttackerDice, int numDefenderDice) {// number of rolls each person will make

// allocate array to hold results of the rolls

int[] attackerRolls = new int[numAttackerDice];

int[] defenderRolls = new int[numDefenderDice];

// make the rolls and save the results in the array

for (int i=0; i < attackerRolls.length; i++) {

attackerRolls<i> = theDie.roll();

}

for (int i=0; i < defenderRolls.length; i++) {

defenderRolls<i> = theDie.roll();

}

// sort the arrays so that the highest roll is first!!

// compare each pair of rolls and do whatever it is you want to do

for (int i=0; i < attackerRolls.length; i++) {// tweak this if attacker and defender can have different number of dice

if (attackerRolls<i> < defenderRolls<i>) {

// attacker rolled lower than defender

}

else if ( ... ) {

}

// etc...

}

}

Even though this is off-topic, it's still relevant, your variable names are not very descriptive. If you give them better names that describe what they are and what they're used for, it's easier to follow what your code's doing.

hunter9000a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 6

Instead of using this code:

(Math.random() * 6 +1);

you should be using

import java.util.Random;

Random random = new Random();

random.nextInt(6)

which gives a better distribution.

Whats is d1 n1 = new d1() in your code?

TuringPesta at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 7

"Whats is d1 n1 = new d1() in your code?"

I accidently left that in from previous attempts at making this program. d1 was something like the class that hunter9000 wrote in his first response to my question. I did not use d1 because I didn't know how to create a "return type" as the compiler said I needed. I got rid of it in my code now, thanks. I am trying to teach myself JAVA because the only programming my cyber high school offers is an intro course that uses ALICE. I have read the tutorial and most of Learn Java in a Weekend, which obviously doesn't cover all I need to know, but I was trying to make this to use with my family when we play Risk.

Instead of using this code:

(Math.random() * 6 +1);

you should be using

import java.util.Random;

Random random = new Random();

random.nextInt(6)

I thought I was using java.util.Random. In the class Die6 on the second reply I think I wrote the code like you did except like this:

import java.util.Random;

Random r = new Random();

r.nextInt(6) +1;

I won't use +1.

Thanks

dw15194a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...
# 8

I thought I was using java.util.Random. In the class

Die6 on the second reply I think I wrote the code

like you did except like this:

import java.util.Random;

Random r = new Random();

r.nextInt(6) +1;

won't use +1.

hanks

nextInt(6) return a random number between 0 and 5 inclusive. Since you want one between 1 and 6 inclusive you have to add 1 to it. That's the way you get random numbers in a certain range. If you wanted a random number between 45 and 60, you'd use nextInt(15) + 45;

hunter9000a at 2007-7-10 14:17:44 > top of Java-index,Java Essentials,New To Java...