Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to display two dice images that will roll a possible 2-12 and explainning

ID: 3814748 • Letter: T

Question

Trying to display two dice images that will roll a possible 2-12 and explainning the rules to player as well. Need help.

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Random;

import javax.imageio.*;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

public class SwingControlDemo

{

   private JFrame mainFrame;

   private JLabel headerLabel;

   private JLabel diceLabel;

   String diceString="";

   private JLabel playerLabel;

   List<String> dice;

   private JLabel computerLabel;

   private JLabel playerStatusLabel;

   private JLabel computerStatusLabel;

   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<String>();

   for(int i=1;i<=6;i++)

   for(int j=1;j<=6;j++)

   {

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

   }

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

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

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

   }

}

   public class DisplaySwingControlDemo extends JFrame

   {

      BufferedImage dieImage;//This will display image

  

      DisplaySwingControlDemo() //Constructor = a repeat

      {

          try

          {

              //create a string of "DieFace" + random digit + ".gif"

              File imageIn = new File("DieFace1.gif");

              dieImage = ImageIO.read(imageIn);// read a pictures to see this is the right file

         }

          catch (Exception e)

          {

              System.out.println("Image of die not found to read");

          }

      JLabel dieLabel = new JLabel();//Icon Label Only

      ImageIcon dieIcon = new ImageIcon(dieImage);//Icon Label Only

      this. add(dieLabel);

      setVisible(true);

  

   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,800);

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

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

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

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

   playerStatusLabel.setSize(600,100);

   controlPanel = new JPanel();

   controlPanel.setLayout(new FlowLayout());

   mainFrame.add(headerLabel);

   mainFrame.add(diceLabel);

   mainFrame.add(playerLabel);

   mainFrame.add(computerLabel);

  

   mainFrame.add(controlPanel);

   mainFrame.add(playerStatusLabel);

   mainFrame.add(computerStatusLabel);

   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<=12)

   counter++;

   else{

   if(cPoints>pPoints)

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

   if(cPoints<pPoints)

   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++;

   }

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

   computerStatusLabel.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<pPoints)

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

   System.out.println("There are eleven rounds numbered 2-12. In each round the player tries "

             + "to roll and score the number that is display x,x."

             + "number of the round, the numbers "

             + "being the combinations possible with 2 dice.");

   System.out.println("If a player throws the correct number for that round they score 1 point. "

             + "If they throw any other number they don’t score. "

             + "The highest total after 11 rounds wins the game."

             + "You will keep rolling that same number throught the whole match.");

   }

}

Explanation / Answer

class D
{
Random r = new Random();
public int R(string s)
{
int t = 0;
var a = s.Split('+');
if (a.Count() > 1)
foreach (var b in a)
t += R(b);
else
{
var m = a[0].Split('*');
if (m.Count() > 1)
{
t = 1;

foreach (var n in m)
t *= R(n);
}
else
{
var d = m[0].Split('d');
if (!int.TryParse(d[0].Trim(), out t))
t = 0;
int f;
for (int i = 1; i < d.Count(); i++)
{
if (!int.TryParse(d[i].Trim(), out f))
f = 6;

int u = 0;
for (int j = 0; j < (t == 0 ? 1 : t); j++)
u += r.Next(1, f);

t += u;
}
}
}
return t;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote