Question: Need help getting a dice to roll the correct number for the player and computer to score a point however, that doesn't seem to work

Need help getting a dice to roll the correct number for the player and computer to score a point however, that doesn't seem to work because everytime you press the roll it does the wrong number. I need help fixing this problem.

Need help getting a dice to roll the correct number for theplayer and computer to score a point however, that doesn't seem towork because everytime you press the roll it does the wrong number.I need help fixing this problem. The computer dice rolled gave thecomputer a point which it shouldn't because its not 2,1 computer didn'troll a 2 ,1. Computer should of score because it rolled a

5,1 and it still doesnt' count. Here is the code import java.awt.Dimension;

The computer dice rolled gave the computer a point which it shouldn't because its not 2,1 computer didn't roll a 2 ,1.

import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.*; import javax.swing.ImageIcon;

Computer should of score because it rolled a 5,1 and it still doesnt' count.

Here is the code

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.*;

import java.text.SimpleDateFormat;

import java.util.*;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class SwingControlDemo

{

private JFrame mainFrame;

private JLabel headerLabel;

private JLabel diceLabel;

String diceString="";

private JLabel playerLabel;

List dice;

private JLabel computerLabel;

private JLabel playerStatusLabel1,playerStatusLabel2; /eed two label to display 2 player dice

private JLabel computerStatusLabel1,computerStatusLabel2;///eed two label to display 2 computer dice

//add an image icon to display dice image on the labels

private ImageIcon imageIcon;

private int pPoints=0;

private int cPoints=0;

int counter=2;

private JPanel controlPanel;

public boolean timeOver=false;

Random rand;

public SwingControlDemo()

{

prepareGUI();

rand=new Random();

dice =new ArrayList();

for(int i=1;i

for(int j=1;j

{

dice.add(i+","+j);

}

diceString = dice.get(rand.nextInt(36));

diceLabel.setText("DICE :"+diceString);

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

new Thread(new Runnable() {

@Override

public void run() {

Date date = new Date();

date.setHours(0);

date.setMinutes(10);

date.setSeconds(0);

while(!timeOver)

{

// TODO Auto-generated method stub

headerLabel.setText(sdf.format(date));

date.setSeconds(date.getSeconds()-1);

try{

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(date.getMinutes()==0&&date.getSeconds()==0)

timeOver=true;

}

}

}).start();

}

public static void main(String[] args){

SwingControlDemo swingControlDemo = new SwingControlDemo();

swingControlDemo.showButtonDemo();

}

private void prepareGUI()

{

mainFrame = new JFrame("Chicago Dice Game");

mainFrame.setSize(800,600);

mainFrame.setLayout(new GridLayout(10, 1));

mainFrame.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent windowEvent)

{

System.exit(0);

}

});

playerLabel =new JLabel(" PLAYER ",JLabel.LEFT);

computerLabel =new JLabel(" COMPUTER ",JLabel.LEFT);

headerLabel = new JLabel("", JLabel.CENTER);

playerStatusLabel1 = new JLabel("",JLabel.CENTER);

playerStatusLabel2 = new JLabel("",JLabel.CENTER);

diceLabel=new JLabel("",JLabel.CENTER);

computerStatusLabel1=new JLabel("",JLabel.CENTER);

computerStatusLabel2=new JLabel("",JLabel.CENTER);

playerStatusLabel1.setSize(119,116);

playerStatusLabel2.setSize(119,116);

computerStatusLabel1.setSize(119,116);

computerStatusLabel2.setSize(119,116);

controlPanel = new JPanel();

controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);

mainFrame.add(diceLabel);

mainFrame.add(playerLabel);

mainFrame.add(computerLabel);

mainFrame.add(controlPanel);

mainFrame.add(playerStatusLabel1);mainFrame.add(playerStatusLabel2);

mainFrame.add(computerStatusLabel1); mainFrame.add(computerStatusLabel2);

mainFrame.setVisible(true);

}

private void showButtonDemo(){

//resources folder should be inside SWING folder.

JButton rollDice = new JButton("Roll Dice");

rollDice.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if(!timeOver)

{

if(counter

counter++;

else{

if(cPoints>pPoints)

JOptionPane.showMessageDialog(mainFrame, "Computer Wins");

if(cPoints

JOptionPane.showMessageDialog(mainFrame, "Player Wins");

if(cPoints==pPoints)

JOptionPane.showMessageDialog(mainFrame, "Its a TIE");

JOptionPane.showMessageDialog(mainFrame, "Restrating the game");

counter=2;

diceLabel.setText("DICE: "+dice.get(rand.nextInt(36)));

cPoints=0;

pPoints=0;

}

String playerDice= dice.get(rand.nextInt(36));

String computerDice=dice.get(rand.nextInt(36));

if(playerDice.equalsIgnoreCase(diceString)){

pPoints++;

}

if(computerDice.equalsIgnoreCase(diceString))

{

cPoints++;

}

//Set the image icon based on playerDice values

//split the values to get single values to determine image icon

String[] PlayerDiceIcons = playerDice.split(",");

String[] ComputerDiceIcons = computerDice.split(",");

String player_dice1 = getIconFileName(PlayerDiceIcons[0]);

String player_dice2 = getIconFileName(PlayerDiceIcons[1]);

String computer_dice1 = getIconFileName(ComputerDiceIcons[0]);

String computer_dice2 = getIconFileName(ComputerDiceIcons[1]);

//display player dice icons

imageIcon = new ImageIcon(player_dice1);

playerStatusLabel1.setIcon(imageIcon);

imageIcon = new ImageIcon(player_dice2);

playerStatusLabel2.setIcon(imageIcon);

playerStatusLabel1.setText("Player Dice Rolled:"+playerDice+" points "+pPoints);

//display computer dice icons

imageIcon = new ImageIcon(computer_dice1);

computerStatusLabel1.setIcon(imageIcon);

imageIcon = new ImageIcon(computer_dice2);

computerStatusLabel2.setIcon(imageIcon);

computerStatusLabel1.setText("Computer Dice Rolled:"+computerDice+" points "+cPoints);

playerLabel.setText(" PLAYER : "+playerDice+" "+pPoints);

computerLabel.setText(" COMPUTER: "+computerDice+" "+cPoints);

}

else

{

JOptionPane.showMessageDialog(mainFrame, "TimeUp");

if(cPoints>pPoints)

JOptionPane.showMessageDialog(mainFrame, "Computer Wins");

if(cPoints

JOptionPane.showMessageDialog(mainFrame, "Player Wins");

if(cPoints==pPoints)

JOptionPane.showMessageDialog(mainFrame, "Its a TIE");

JOptionPane.showMessageDialog(mainFrame, "Restrating the game");

counter=2;

diceString=dice.get(rand.nextInt(36));

cPoints=0;

pPoints=0;

System.exit(0);

}

}

});

controlPanel.add(rollDice);

mainFrame.setVisible(true);

}

public String getIconFileName(String DiceNumber)

{

String IconfileName = "";

if(DiceNumber.equals("1")){IconfileName = "DieFace1.gif";}

if(DiceNumber.equals("2")){IconfileName = "DieFace2.gif";}

if(DiceNumber.equals("3")){IconfileName = "DieFace3.gif";}

if(DiceNumber.equals("4")){IconfileName = "DieFace4.gif";}

if(DiceNumber.equals("5")){IconfileName = "DieFace5.gif";}

if(DiceNumber.equals("6")){IconfileName = "DieFace6.gif";}

return IconfileName;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!