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

Creat a project named LetsMakeADeal. in the game, three ppizes of varying value

ID: 3816324 • Letter: C

Question

Creat a project named LetsMakeADeal. in the game, three ppizes of varying value are assigned randomly to be hidden behind three "doors" that you can implement as Buttons. For example, the prizes might be a new car, a big-screen TV, and a live goat. The player chooses a Button, and then one of the two other prizes is revealed; the one revealed is never the most desirable prize. The user then has the option of changing the original selection to the remaing unseen choice. For example, consider these two game scenarios:

Suppose that the most valuable prize is randomly assigned tot he first button, If the user chooses the first button, reveal either of the other two prizes, and ask the user if he wants to change his selection.

Suppose that the most valuable prize is assigned to the first button, but the user chooese the secound button. Reveal the thrid prize so that the most valuable prize's location is still hidden, and then ask the user whether he wants to change his selection.

After the user has chosen to retain his original selection or make a change, reveal what he has won.

Explanation / Answer

I hope that this code is readable and understandable because I have given meaningful names for var and func. If any problem, please comment my answer. Always happy to help. please give a like if you like this answer.

LetsMakeADeal.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class LetsMakeADeal extends JFrame implements ActionListener
{
   JButton jb1,jb2,jb3;
   String prizes[]={"New Benz Car","big-screen TV","Live Goat"};
   int randomPrizeAssignment[] = new int[3];
   boolean isFirstClick=true,isSecondClick=true;
   int whereIsGoat,whereIsTV;
   String clickedDoor;
   public LetsMakeADeal()
   {
       Random random = new Random();
       int randomValue = random.nextInt(3);
       randomPrizeAssignment[0] = randomValue;
       while((randomValue=random.nextInt(3))==randomPrizeAssignment[0]);
       randomPrizeAssignment[1]=randomValue;
      
       randomPrizeAssignment[2]=3-(randomPrizeAssignment[1]+randomPrizeAssignment[0]);
       System.out.println(randomPrizeAssignment[0]+"-"+randomPrizeAssignment[1]+"-"+randomPrizeAssignment[2]);
       //finding where is TV and Goat
       findGoat();
       findTV();
       System.out.println(whereIsGoat+"-"+whereIsTV);
      
       jb1 = new JButton("Door No.1");
       jb2 = new JButton("Door No.2");
       jb3 = new JButton("Door No.3");
      
       jb1.addActionListener(this);
       jb2.addActionListener(this);
       jb3.addActionListener(this);
      

      
       Container c = getContentPane();
       c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
      
       JPanel jp = new JPanel(new FlowLayout());
       jp.add(jb1);
       jp.add(jb2);
       jp.add(jb3);
      
       c.add(jp);
      
       setSize(500,500);
       setVisible(true);
   }
   public void findGoat()
   {
       for(int i=0;i<3;i++)
       {
           if(randomPrizeAssignment[i]==2)
           {
               whereIsGoat = i;
               break;
           }
       }
   }
   public void findTV()
   {
       for(int i=0;i<3;i++)
       {
           if(randomPrizeAssignment[i]==1)
           {
               whereIsTV = i;
               break;
           }
       }
   }
   public static void main(String a[])
   {
       LetsMakeADeal game = new LetsMakeADeal();
   }
   public void actionPerformed(ActionEvent ae)
   {
       clickedDoor = ae.getActionCommand();
       if(isFirstClick)
       {
           JOptionPane.showMessageDialog(null,"You have Chosen "+clickedDoor,"Your First Choose",JOptionPane.WARNING_MESSAGE);
           JOptionPane.showMessageDialog(null,"Now we will open another Door for you!!! Then you can have chance to change your Opinion to open another Door If you want to change your Door, then just click on your desired Door No.","Your First Choose",JOptionPane.WARNING_MESSAGE);
           int whichDoorToOpen = (randomPrizeAssignment[Integer.parseInt(clickedDoor.charAt(clickedDoor.length()-1)+"")-1]==2)?whereIsTV:whereIsGoat;
           JOptionPane.showMessageDialog(null,"Door No."+(whichDoorToOpen+1)+" has been opened for you!!! And it has "+prizes[randomPrizeAssignment[whichDoorToOpen]],"We opened This Door",JOptionPane.WARNING_MESSAGE);
           int dialogButton = JOptionPane.YES_NO_OPTION;
           dialogButton=JOptionPane.showConfirmDialog (null, "Would You Like to Change your Door No.?","Confirmation",dialogButton);

           if(dialogButton == JOptionPane.YES_OPTION)
           {
               JOptionPane.showMessageDialog(null,"Then click on your desired Door No.","Information",JOptionPane.WARNING_MESSAGE);;
               isSecondClick = true;
           }
           else
           isSecondClick=false;
       }
       if(isFirstClick==true && isSecondClick==false)
       {
           //show result
           int whichDoorOpened = Integer.parseInt(clickedDoor.charAt(clickedDoor.length()-1)+"")-1;
           JOptionPane.showMessageDialog(null,"You Won "+prizes[randomPrizeAssignment[whichDoorOpened]],"Result",JOptionPane.WARNING_MESSAGE);
       }
       else if(isFirstClick==false && isSecondClick==true)
       {
           //take input
           JOptionPane.showMessageDialog(null,"You have Chosen "+clickedDoor,"Your Second Choose",JOptionPane.WARNING_MESSAGE);
           int whichDoorOpened = Integer.parseInt(clickedDoor.charAt(clickedDoor.length()-1)+"")-1;
           JOptionPane.showMessageDialog(null,"You Won "+prizes[randomPrizeAssignment[whichDoorOpened]],"Result",JOptionPane.WARNING_MESSAGE);
       }
      
       isFirstClick = false;
      
   }
}