Question
***My code is below..Before jumping in, I recommend running the gui class below, you will like it*** Create a basic car racing game (no graphics
***My code is below..Before jumping in, I recommend running the gui class below, you will like it***
Create a basic car racing game (no graphics necessary). Using the UML diagram below, create a bet class, a player class (two players), and a car class (four cars). Create a demo class for the three. Then, create a GUI application (extending from JFrame) where each player can select a vehicle and place a wager (capped at $100). The game should be able to run until one player has $0 remaining or until the user wants to quit. The game should display who won (player one or player two)
Variables
Player1
Player2
Player1 Bet
Player2 Bet
Cars quarterMileTime
vehicleOne 14.7s
vehicleTwo 13.7s
vehicleThree 14.0s
vehicleFour 13.9s
UML DIAGRAM:
Racing Game
Bet ---------- player1 : Player player2 : Player wager : int ---------- +Bet( player1, player 2 : Player, wager : int ) +Race() : String
Player ---------- -name : String -cash : int -vehicle : Car ----------- +getName() : String +getCash() : int +addCash( int cash ) : int +setVehicle( Car car ) : void
+getVehicle() : Car
Car ----- -make : string -model : string ---- +getQuarterMileTime() : double - make this random < 10 seconds +getMake() : String +getModel() : String +toString() : String
GUI screen -Player 1 select a car ( radio button? ) -total cash ( starts at 100 ) -Player 2 select a car ( radio button? ) -total cash ( starts at 100 )
-Text box for wager -button for Place Bet -button for Race
display race result -If player 1 wins, cash goes up, player 2 cash goes down
Game ends when one player has <= 0 cash
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I created my gui, bet, player, and car classes. My problem is getting to input my data from car, bet and player classes into my gui so that it works...and to display results...i need them to mesh. All the help I can get will be appreciated...especially on the car class...having issues with the Math.random() method.
Below is my work that I need fixing.
Thanks!
-------------------
BetClass
public class BetClass implements BetClassInterface {
private Player player1; private Player player2; private int wager; public BetClass(Player player1, Player player2, int wager) { this.player1 = player1; this.player2 = player2; this.wager = wager; } @Override public String Race() { if(player1.getVehicle().makeRandom() > player2.getVehicle().makeRandom()) { player1.addCash(wager); player2.addCash(-1 * (wager)); return player1.getName() + " in a " + player1.getVehicle().getModel() + " beats " + player2.getName() + " in a " + player2.getVehicle().getModel(); } else { player1.addCash(wager * -1); player2.addCash(wager); return player1.getName() + " in a " + player1.getVehicle().getModel() + " loses to " + player2.getName() + " in a " + player2.getVehicle().getModel();
} }
}
---------------------------------------
BetClassInterface
public interface BetClassInterface{ public String Race(); }
---------------------------------------
PlayerClass
public class PlayerClass implements PlayerClassInterface {
String name = "default"; int money; VehicleClass car = new VehicleClass(); int numcompare; public PlayerClass() { this.money = 100; } public PlayerClass(String name, int cash , VehicleClass car) { this.name = name; this.money = cash; this.car = car; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCash() { return money; } public void addCash(int cash) { this.money += money; } public VehicleClass getVehicle() { return car; } public void setVehicle(VehicleClass car) { this.car = car; } }
----------------------------------
PlayerClassInterface
public interface PlayerClassInterface {
public String getName(); public int getCash(); public void addCash(int i); public void setVehicle(VehicleClass carinterface); VehicleClass getVehicle(); }
----------------------------------------
VehicleClass
public class VehicleClass { //declare fields private String make; private String model; private double time; //create constructor public VehicleClass ()//(String make, String model, double time) { super(); this.make = make; this.model = model; getTime();// calling getTime because user cannot enter time } //getters and setters public String getMake(){ // "1995 Mitsubishi Eclipse" , "1999 Nissan Skyline GT-R", // "2003 Honda S200","1995 Mazda RX-7" String carMakeOption1= "1995 Mitsubishi"; String carMakeOption2 = "1999 Nissan"; String carMakeOption3= "2003 Honda"; String carMakeOption4= "1995 Mazda"; return make; } public String getModel(){ String carModelOption1= "Eclipse"; String carModelOption2 = "Skyline GT-R"; String carModelOption3= "S200"; String carModelOption4= "RX-7"; return model; } public double getTime(){ double carMakeOption1.carModelOption1; double carMakeOption2.carModelOption2; double carMakeOption3.carModelOption3; double carMakeOption4.carModelOption4; time =( Math.random()*10) + 1; return time; } //tostring @Override public String toString() { return "Car [make=" + make + ", model=" + model + ", time=" + time + "]"; } }
----------------------------
CarGui
import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder;
public class CarTest extends JFrame {
private final int WIDTH = 900; private final int HEIGHT = 700; private List
public static void main(String[] args) { new CarTest(); }
public CarTest() {
this.images = setImageInformation(); // TODO test without 'this' getContentPane().setBackground(background); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Racing Game"); setSize(WIDTH, HEIGHT); setLocationRelativeTo(null); setResizable(false); setLayout(new FlowLayout());
playerTitles(); buildPanelOne(); buildPanelTwo(); buildButtonPanel();
add(playerOneImage); add(playerOne); add(playerTwoImage); add(playerTwo); add(buttonPanel);
cashOneField.setText("100"); cashTwoField.setText("100"); // pack(); setVisible(true); }
private void playerTitles() { Image image1 = null; try { URL url1 = new URL("https://s30.postimg.org/nehuqy7u9/player1.png"); image1 = ImageIO.read(url1); } catch (IOException e) { e.printStackTrace(); } playerOneImage = new JLabel(new ImageIcon(image1.getScaledInstance(200, -1, Image.SCALE_DEFAULT)));
Image image2 = null; try { URL url2 = new URL("https://s30.postimg.org/ih4a5u5v5/player2.png"); image2 = ImageIO.read(url2); } catch (IOException e) { e.printStackTrace(); } playerTwoImage = new JLabel(new ImageIcon(image2.getScaledInstance(200, -1, Image.SCALE_DEFAULT)));
}
private void buildButtonPanel() {
buttonPanel = new JPanel(); buttonPanel.setBorder(BorderFactory.createLineBorder(blue, 3)); buttonPanel.setBackground(Color.DARK_GRAY); race = new JButton("Race!"); race.addActionListener(new RaceListener()); reset = new JButton("Reset"); reset.addActionListener(new ResetListener()); buttonPanel.add(race); buttonPanel.add(reset); }
private class RaceListener implements ActionListener { ///
public void actionPerformed(ActionEvent e) { Player player1 = new Player(); Player player2 = new Player(); Bet race = new Bet(player1, player2, 100); race.Race(); VehicleClass makeChosen = new VehicleClass(); String make = makeChosen.getMake(); VehicleClass modelChosen = new VehicleClass(); String model = modelChosen.getModel(); VehicleClass speedCarChosen = new VehicleClass(); double qMileTime = speedCarChosen.getTime(); //JOptionPane.showMessageDialog(null, //String.format("(winner + " Wins! Your time was (in seconds) " + winTimeFormat + ". " + loser + " lost by " + loseTimeFormat + " seconds"); } }
private class ResetListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "The game has been reset"); cashOneField.setText("100"); cashTwoField.setText("100");
}
}
private void buildPanelOne() {
playerOne = new JPanel(); playerOne.setBorder(BorderFactory.createLineBorder(blue, 3)); playerOne.setBackground(Color.DARK_GRAY); playerOne.setLayout(new FlowLayout());
cashOneLabel = new JLabel("Total Cash: "); cashOneLabel.setForeground(yellow); cashOneField = new JTextField(5); cashOneField.setEditable(false);
carOne = new JPanel(); carOne.setBorder(BorderFactory.createLineBorder(blue, 2)); carOne.setBackground(background);
imagePanelOne = new JPanel(); imagePanelOne.setBackground(Color.DARK_GRAY); imagePanelOne.setLayout(new BorderLayout());
ImageInformation defaultImageInformation = images.get(0); imageLabelOne = new JLabel(new ImageIcon(defaultImageInformation.getImage())); descriptionLabelOne = new JLabel(defaultImageInformation.getDescription()); descriptionLabelOne.setForeground(yellow); descriptionLabelOne.setHorizontalAlignment(JLabel.CENTER);
imagePanelOne.add(imageLabelOne, BorderLayout.CENTER); imagePanelOne.add(descriptionLabelOne, BorderLayout.SOUTH);
carOne.add(createControlPanelOne(), BorderLayout.WEST); carOne.add(imagePanelOne, BorderLayout.CENTER);
betOneAmount = new JLabel("Place Bet: "); betOneAmount.setForeground(yellow); betOneField = new JTextField(5);
playerOne.add(cashOneLabel); playerOne.add(cashOneField); playerOne.add(carOne); playerOne.add(betOneAmount); playerOne.add(betOneField); }
private JPanel createControlPanelOne() {
JPanel panel = new JPanel(); panel.setBackground(Color.DARK_GRAY); panel.setForeground(yellow); TitledBorder selectCar = new TitledBorder(new EtchedBorder(), "Select Car"); selectCar.setTitleColor(yellow); panel.setBorder(selectCar); panel.setLayout(new GridLayout(0, 1));
ButtonGroup group = new ButtonGroup();
listenerOne = new ImageListener();
for (int i = 0; i < images.size(); i++) { ImageInformation imageInformation = images.get(i); JRadioButton button = new JRadioButton(imageInformation.getTitle()); if (i == 0) { button.setSelected(true); } button.setForeground(orange); button.addItemListener(listenerOne); group.add(button); panel.add(button); }
return panel; }
private void buildPanelTwo() {
playerTwo = new JPanel(); playerTwo.setBorder(BorderFactory.createLineBorder(blue, 3)); playerTwo.setBackground(Color.DARK_GRAY); playerTwo.setLayout(new FlowLayout());
cashTwoLabel = new JLabel("Total Cash: "); cashTwoLabel.setForeground(yellow); cashTwoField = new JTextField(5); cashTwoField.setEditable(false);
carTwo = new JPanel(); carTwo.setBorder(BorderFactory.createLineBorder(blue, 2)); carTwo.setBackground(background); JPanel imagePanel = new JPanel(); imagePanel.setBackground(Color.DARK_GRAY); imagePanel.setLayout(new BorderLayout());
ImageInformation defaultImageInformation = images.get(0); imageLabelTwo = new JLabel(new ImageIcon(defaultImageInformation.getImage())); descriptionLabelTwo = new JLabel(defaultImageInformation.getDescription()); descriptionLabelTwo.setForeground(yellow); descriptionLabelTwo.setHorizontalAlignment(JLabel.CENTER);
imagePanel.add(imageLabelTwo, BorderLayout.CENTER); imagePanel.add(descriptionLabelTwo, BorderLayout.SOUTH);
carTwo.add(createControlPanelTwo(), BorderLayout.WEST); carTwo.add(imagePanel, BorderLayout.CENTER);
betTwoAmount = new JLabel("Place Bet: "); betTwoAmount.setForeground(yellow); betTwoField = new JTextField(5);
playerTwo.add(cashTwoLabel); playerTwo.add(cashTwoField); playerTwo.add(carTwo); playerTwo.add(betTwoAmount); playerTwo.add(betTwoField); }
private JPanel createControlPanelTwo() {
JPanel panel = new JPanel(); panel.setBackground(Color.DARK_GRAY); panel.setForeground(yellow); TitledBorder selectCar = new TitledBorder(new EtchedBorder(), "Select Car"); selectCar.setTitleColor(yellow); panel.setBorder(selectCar); panel.setLayout(new GridLayout(0, 1));
ButtonGroup group2 = new ButtonGroup();
ImageListener2 listenerTwo = new ImageListener2();
for (int i = 0; i < images.size(); i++) { ImageInformation imageInformation = images.get(i); JRadioButton button = new JRadioButton(imageInformation.getTitle()); if (i == 0) { button.setSelected(true); } button.setForeground(purple); button.addItemListener(listenerTwo); group2.add(button); panel.add(button); }
return panel; }
public class ImageListener implements ItemListener {
@Override public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) { JRadioButton button = (JRadioButton) event.getItem(); String labelText = button.getText(); ImageInformation imageInformation = getImageInformation(labelText); if (imageInformation != null) { imageLabelOne.setIcon(new ImageIcon(imageInformation.getImage())); descriptionLabelOne.setText(imageInformation.getDescription()); } } } }
public class ImageListener2 implements ItemListener {
@Override public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
JRadioButton button = (JRadioButton) event.getItem();
String labelText = button.getText();
ImageInformation imageInformation = getImageInformation(labelText);
if (imageInformation != null) { imageLabelTwo.setIcon(new ImageIcon(imageInformation.getImage())); descriptionLabelTwo.setText(imageInformation.getDescription()); } } } }
private ImageInformation getImageInformation(String title) {
for (ImageInformation imageInformation : images) {
if (title.equals(imageInformation.getTitle())) {
return imageInformation; }
}
return null; }
public class ImageInformation {
private final Image image; private final String title; private final String description;
public ImageInformation(Image image, String title, String description) { this.image = image; this.title = title; this.description = description; }
public Image getImage() { return image; }
public String getTitle() { return title; }
public String getDescription() { return description; }
}
private List
List
Image eclipse = null, skyline = null, s2000 = null, rX7 = null;
try { URL url1 = new URL( "http://vignette2.wikia.nocookie.net/nfs/images/c/cb/Nfs_underground_mitsubishi_eclipse_gsx_petey_pablo.jpg/revision/latest?cb=20120517131849&path-prefix=en"); eclipse = ImageIO.read(url1); } catch (IOException e) { e.printStackTrace(); }
try { URL url2 = new URL( "http://vignette2.wikia.nocookie.net/nfs/images/7/7f/Nfs_underground_nissan_skyline_gt-r_eddie.jpg/revision/latest?cb=20120523092506&path-prefix=en"); skyline = ImageIO.read(url2); } catch (IOException e) { e.printStackTrace(); }
try { URL url3 = new URL( "http://vignette3.wikia.nocookie.net/nfs/images/2/20/Nfs_underground_honda_s2000.jpg/revision/latest?cb=20120529144514&path-prefix=en"); s2000 = ImageIO.read(url3); } catch (IOException e) { e.printStackTrace(); }
try { URL url4 = new URL( "http://vignette1.wikia.nocookie.net/nfs/images/d/d8/Nfs_underground_mazda_rx-7.jpg/revision/latest?cb=20120615214223&path-prefix=en"); rX7 = ImageIO.read(url4); } catch (IOException e) { e.printStackTrace(); }
int scaled = 320; images.add(new ImageInformation(eclipse.getScaledInstance(scaled, -1, Image.SCALE_FAST), "1995 Mitsubishi Eclipse", "14.7 second quarter mile")); images.add(new ImageInformation(skyline.getScaledInstance(scaled, -1, Image.SCALE_FAST), "1999 Nissan Skyline GT-R", "13.7 second quarter mile")); images.add(new ImageInformation(s2000.getScaledInstance(scaled, -1, Image.SCALE_FAST), "2003 Honda S2000", "14.0 second quarter mile")); images.add(new ImageInformation(rX7.getScaledInstance(scaled, -1, Image.SCALE_FAST), "1995 Mazda RX-7", "13.9 second quarter mile"));
return images; } }
-----------------------
Thanks again!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started