Using Java, create a GUI based two player dice game call Chicago that uses two d
ID: 3810978 • Letter: U
Question
Using Java, create a GUI based two player dice game call Chicago that uses two dice to play. The user will be one player and the computer will be the second player.
To Play: There are eleven rounds numbered 2 -12. In each round the player tries to roll and score the number of the round, the numbers being the combinations possible with 2 dice.
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.
- Complete rules are displayed in an easy to read manner at the beginning of the game.
- Game has an appealing layout and game play is easy to follow in GUI.
- Game follows rules as described at beginning of the game.
- Game uses an object oriented approach and game uses structured code in methods and main().
-Game is playable in 10 minutes or less and game provides complete interaction between player and computer.
Explanation / Answer
/*
* hope this code fulfills your requirement,feel free to comment for any queries,All The Best
*
*/
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
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");
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.