ClearCutFan
ClearCutFan

Reputation: 47

Java Dice Program Always goes to Else statement despite the outcome of the users choice

The program asks the user how many players he wishes to have, once prompted the program runs, player 1 receives 3 rolls of two 6 sided dices (3 times) and so on for the next players. The players can choose which rolls to keep or potentially keeping both.

The problem occurs however, the rolls are always the same for every player, it's like the Math.random has no effect in my player class program. Another problem occurs here : else { JOptionPane.showMessageDialog(Test,"Tie "+ data[0] + " And " + data[1]); where the program always goes to the else statement of that class when 2 players are selected. It goes to the else statement on every single player count as in, anywhere from 2-4 players, always results in proceeding to the specified else statement.

I have tried running a for loop in the PairOFDice class around the two Die's, but to no avail it did not change the rolls of the dice. I have also tried to reset the Die values after every time the program has gone through one loop, however that simply caused their values to be stuck at zero. Any input would be much appreciated.

import javax.swing.*;//MAIN LOGIC PROGRAM
public class Logic {


public static void main (String [] args) {


PairOfDice p1 = new PairOfDice("Player 1");
PairOfDice p2 = new PairOfDice("Player 2");
Player PClass = new Player();

JFrame IntroPane = new JFrame ();
JFrame Test = new JFrame ();

int Crolls = 0;
int data[]  = new int[4] ;

JOptionPane.showMessageDialog(Test,"Hello and welcome to the program! In the Following program, you will be playing a game of dice against another player\nEach of you will roll two six sided dice's three times, choosing to hold the first second or both die's\nThe highest total wins! Good luck!");
String c = JOptionPane.showInputDialog(Test,"To begin, how many players are playing?\n2 Players enter '2'" + "\n3 Players enter '3'" + "\n4 Players enter '4'");
int x = Integer.parseInt(c);

for (int i = 0; i < x; i++) {
  for(int s = 0; s < 3; s++) {
p1.play();
p2.play();

    JOptionPane.showMessageDialog(IntroPane,"Player " + (i+1));
    JOptionPane.showMessageDialog(IntroPane,"Dice 1 rolled : " + p1.getDice1() + "\nDice 2 rolled : " + p1.getDice2());
    Object[] options = {"Hold Dice 1",
      "Hold Dice 2",
      "Hold Both"};
    int n = JOptionPane.showOptionDialog(Test,
                                         "Which Roll would you like to keep?\nKeep Dice 1 or Dice 2\nOr keep both!\n\nYour Total so far is :" +data[i]
                                           + "",
                                         "",
                                         JOptionPane.YES_OPTION,
                                         JOptionPane.QUESTION_MESSAGE,
                                         null,
                                         options,
                                         options[2]);
    if(n == JOptionPane.YES_OPTION) 
    {
      PClass.HoldFirstDie(p1.getDice1());
      JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice1() ); 
      data[i] += PClass.getFirstDie();
      Crolls++;
    }

    else if(n == JOptionPane.NO_OPTION) {
      PClass.HoldSecondDie(p1.getDice2());
      JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice2() ); 
      data[i] += PClass.getSecondDie();
      Crolls++;
    }

    else if(n== JOptionPane.CANCEL_OPTION) {
      PClass.HoldFirstDie(p1.getDice1());
      PClass.HoldSecondDie(p1.getDice2());
      JOptionPane.showMessageDialog(Test,"You choose to hold :" +p1.getDice1() + " and :" + p1.getDice2() ); 
      data[i] += ( PClass.getFirstDie() + PClass.getSecondDie() ) ;
      Crolls++;
    }
  }
}

if( x == 2) {
  if(Crolls == 3 && data[0] > data[1]) {
    JOptionPane.showMessageDialog(Test,"Player 1 wins");
  }

  else if (Crolls == 3 && data[1] > data[0]) {
    JOptionPane.showMessageDialog(Test,"Player 2 wins");
  }

  else {
    JOptionPane.showMessageDialog(Test,"Tie "+ data[0] + " And " + data[1]);
  }
}

if(x == 3) {
  if(Crolls == 3 && data[2] > data[0] && data[2] > data[1]) {
    JOptionPane.showMessageDialog(Test,"Player 3 wins");

  }

  else if(Crolls == 3 && data[0] > data[1] && data[0] > data[2]) {

    JOptionPane.showMessageDialog(Test,"Player 1 wins");

  }
  else if(Crolls == 3 && data[1] > data[0] && data[1] > data[2]) {
    JOptionPane.showMessageDialog(Test,"Player 2 wins");

  }

  else {
    JOptionPane.showMessageDialog(Test,"Tie");
  }
}



if(x ==4) {
  if(Crolls == 3 && data[0] > data[1] && data[0] > data[2] && data[0] > data[3]) {
    JOptionPane.showMessageDialog(Test,"Player 1 wins");

  }

  else if(Crolls == 3 && data[1] > data[0] && data[1] > data[2] && data[1] > data[3]) {
    JOptionPane.showMessageDialog(Test,"Player 2 wins");

  }
  else if(Crolls == 3 && data[2] > data[0] && data[2] > data[1] && data[2] > data[3]) {
    JOptionPane.showMessageDialog(Test,"Player 3 wins");

  }
  else if(Crolls == 3 && data[3] > data[0] && data[3] > data[1] && data[3] > data[2]) {
    JOptionPane.showMessageDialog(Test,"Player 4 wins");

  }
  else {
    JOptionPane.showMessageDialog(Test,"Tie");
  }
}
}}

PairOFDice CLass

public class PairOfDice {



private int Dice1 = 0, Dice2 = 0;

public String name;

PairOfDice(String name){
    this.name = name;
}

public void PairOfDice2() {

 play(); 
}

public void play () {
  //for(int i = 0; i < 3; i++) {
  Dice1 = (int)(Math.random () * 6) + 1;
  Dice2 = (int)(Math.random () * 6) + 1;
  }
//}

public int getDice1() {
  return Dice1;
}

public int getDice2() {
 return Dice2; 
}

public int getTotal () {
 return Dice1 + Dice2; 
}
}

Player Class

public class Player {
private int holdDice1 = 0;
private int holdDice2 = 0;

    public void HoldFirstDie (int FirstDie) {
     holdDice1 = FirstDie;   
    }

    public void HoldSecondDie(int SecondDie) {
     holdDice2 = SecondDie;
 }

    public int getFirstDie() {
 return holdDice1; 
 }
 public int getSecondDie() {

  return holdDice2; 
 }
}

Upvotes: 1

Views: 976

Answers (2)

Paul Wagland
Paul Wagland

Reputation: 29116

In your loop, you say "player " + (i+1), however you only ever reference the results from p1. This sounds like the issue that you are seeing.

Upvotes: 0

TwoThe
TwoThe

Reputation: 14269

Random in Java needs to be initialized fist, otherwise it will always return the same sequence of numbers.

Math.random() isn't the best way of generating random numbers, you should use Random.nextInt() instead.

Upvotes: 1

Related Questions