Looking for some help on an assignment. I managed to get some of the GUI done fo
ID: 3600831 • Letter: L
Question
Looking for some help on an assignment. I managed to get some of the GUI done for the slot machine but have no clue on where to start to get most of the game running. any help would be great.
import javax.swing.*;
import java.awt.*;
import java.util.Random;
class InfoPanel extends JPanel {
public InfoPanel() {
setPreferredSize(new Dimension(500,100));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Balance",50,50);
}
}
class ColorPanel extends JPanel {
private Color col;
public ColorPanel() {
Random rnd = new Random();
int colorCode = rnd.nextInt(3);
if (colorCode == 0) {
col = Color.RED;
} else if (colorCode == 1) {
col = Color.GREEN;
} else {
col = Color.BLUE;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(col);
g.fillOval(20,20,100,100);
g.fillRect(20,20,100,100);
}
}
class MyFrame extends JFrame {
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100,100,500,500);
setTitle("Java Slot Machine");
Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel panSouth = new JPanel();
JButton btnMAX = new JButton("Bet Max");
panSouth.add(btnMAX);
JButton btnMIN = new JButton("Bet Min");
panSouth.add(btnMIN);
JButton btnSPIN = new JButton("Spin");
panSouth.add(btnSPIN);
c.add(panSouth,BorderLayout.SOUTH);
InfoPanel panInfo = new InfoPanel();
c.add(panInfo,BorderLayout.NORTH);
JPanel panCenter = new JPanel();
panCenter.setLayout(new GridLayout(1,3));
ColorPanel cpLeft = new ColorPanel();
ColorPanel cpMid = new ColorPanel();
ColorPanel cpRight = new ColorPanel();
panCenter.add(cpLeft);
panCenter.add(cpMid);
panCenter.add(cpRight);
c.add(panCenter,BorderLayout.CENTER);
}
}
public class ClarkslotMachine {
public static void main(String[] args) {
MyFrame frm = new MyFrame();
frm.setVisible(true);
}
}
In this assignment, you will create a slot machine app. The user interface will look like this: JSlotMachine Version 1.0 Welcome to JSlotMachine 1.0 Bet Max Bet Min Spin The player starts with a balance of $1.00. If they press "Spin", they bet $0.25. If they press "Bet Max", they wager $0.50. If they press Bet Min", they wager $0.10. When they press one of these buttons, the arrangement of tiles will randomly change. Each tile will display a red, green, or blue circle or rectangle. When all three tiles show the same shape and color, the player wins what they bet. If the three tiles do not show the same shape and color, the player loses what they bet. The program ends when the player runs out of money For example, let's suppose that the player first presses "Spin". This is how the display might Look after that first play JSlotMachine Version 1.0 Current balance: $0.75 Bet Max et Min SpiExplanation / Answer
Hello, I have made a few changes in your code. Let’s start from the Infopanel. We have to access it throughout the game, so Instead of using drawstring() ,I have created a JLabel object, and initialized it as “welcome to JSlotGame”. Then comes the ColorPanel class. I have included one more variable shapeCode which tells if the shape is Circle or Rectangle. Instead of filling it on the constructor, I have created a method randomShapeAndColor() and copied all the statements in the constructor. Plus one more statement is written, to give a random value to the shapeCode (1-circle , 0 for rectangle). getColor() and getShapeCode() methods are defined to access the color and shape outside the class to calculate the score. paintComponent() is also modified , you can easily understand that. Finally the MyFrame class. I have made the declaration of Infopanel, colorpanels and buttons outside the constructor, so that it can be accessed from anywhere. A float variable available_balance is defined to store the balance. Then I registered ActionListeners for the three buttons. And inside the actionPerformed() callback method, the actions to be performed are defined separately. Several other methods are defined to help in the calculation process like checkResult() , isBalanceEmpty(), gameOver(), randomSpin(), round() etc. Details about these functions are included in the comments. Check the code and comment below if you need any help. Thanks J
(A note: I haven’t changed the style of GUI much, so that it’ll be easier for you to understand)
//ClarkSlotMachine.java file
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Random;
class InfoPanel extends JPanel {
JLabel label;
public InfoPanel() {
label=new JLabel("Welcome to JSlotMachine ");
add(label);
setPreferredSize(new Dimension(500,100));
}
}
class ColorPanel extends JPanel {
private Color col; /*for storing the color*/
private int shapeCode; /*for storing the shape info (circle or rectangle)*/
public ColorPanel() {
randomShapeAndColor();
}
public void randomShapeAndColor(){
Random rnd = new Random();
int colorCode = rnd.nextInt(3);
if (colorCode == 0) {
col = Color.RED;
} else if (colorCode == 1) {
col = Color.GREEN;
} else {
col = Color.BLUE;
}
shapeCode=rnd.nextInt(2);
}
/**
* method to get the color for calculation of score
*/
public Color getCol() {
return col;
}
/**
* method to get the shape code for calculation of score
*/
public int getShapeCode() {
return shapeCode;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(col);
if(shapeCode==1){
g.fillOval(20,20,100,100);
}else{
g.fillRect(20,20,100,100);
}
}
}
class MyFrame extends JFrame implements ActionListener{
float available_balance=1.00f;
InfoPanel panInfo;
ColorPanel cpLeft,cpMid,cpRight;
JButton btnMAX,btnMIN,btnSPIN;
public MyFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100,100,500,500);
setTitle("Java Slot Machine");
Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel panSouth = new JPanel();
btnMAX = new JButton("Bet Max");
panSouth.add(btnMAX);
btnMIN = new JButton("Bet Min");
panSouth.add(btnMIN);
btnSPIN = new JButton("Spin");
panSouth.add(btnSPIN);
c.add(panSouth,BorderLayout.SOUTH);
panInfo = new InfoPanel();
c.add(panInfo,BorderLayout.NORTH);
JPanel panCenter = new JPanel();
panCenter.setLayout(new GridLayout(1,3));
cpLeft = new ColorPanel();
cpMid = new ColorPanel();
cpRight = new ColorPanel();
panCenter.add(cpLeft);
panCenter.add(cpMid);
panCenter.add(cpRight);
c.add(panCenter,BorderLayout.CENTER);
btnMAX.addActionListener(this);
btnMIN.addActionListener(this);
btnSPIN.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String command=e.getActionCommand();
if(command.equalsIgnoreCase("Bet Max")){
/**
* Bet Max button is clicked
*/
if((available_balance-0.50f)<0){
panInfo.label.setText("You dont have the required balance, current balance: "+available_balance);
}else{
randomSpin();
if(checkResult()){
available_balance+=0.50f;
available_balance=round(available_balance);
panInfo.label.setText("You won the bet, current balance: "+available_balance);
}else{
available_balance-=0.50f;
available_balance=round(available_balance);
panInfo.label.setText(" current balance: "+available_balance);
}
}
}else if(command.equalsIgnoreCase("Bet Min")){
/**
* Bet Min button is clicked
*/
if((available_balance-0.10f)<0){
panInfo.label.setText("You dont have the required balance, current balance: "+available_balance);
}else{
randomSpin();
if(checkResult()){
available_balance+=0.10f;
available_balance=round(available_balance);
panInfo.label.setText("You won the bet, current balance: "+available_balance);
}else{
available_balance-=0.10f;
available_balance=round(available_balance);
panInfo.label.setText(" current balance: "+available_balance);
}
}
} else if(command.equalsIgnoreCase("Spin")){
/**
* Spin button is clicked
*/
if((available_balance-0.25f)<0){
panInfo.label.setText("You dont have the required balance, current balance: "+available_balance);
}else{
randomSpin();
if(checkResult()){
available_balance+=0.25f;
available_balance=round(available_balance);
panInfo.label.setText("You won the bet, current balance: "+available_balance);
}else{
available_balance-=0.25f;
available_balance=round(available_balance);
panInfo.label.setText(" current balance: "+available_balance);
}
}
}
if(isBalanceEmpty()){
gameOver();
}
}
/**
* This method will return true if the player won the bet; else false
*/
public boolean checkResult() {
if (cpLeft.getCol() == cpMid.getCol()
&& cpLeft.getCol() == cpRight.getCol()) {
/**
* all three color panel's colors are matching
*/
if (cpLeft.getShapeCode() == cpMid.getShapeCode()
&& cpLeft.getShapeCode() == cpRight.getShapeCode()) {
/**
* and All three shapes are matching, player wins the bet
*/
return true;
}
}
return false;
}
/**
* method to check if the balance is empty
* @return true if the balance is empty
*/
public boolean isBalanceEmpty(){
if(available_balance==0){
return true;
}else
return false;
}
/**
* Method to perform actions when the player is out of money
*/
public void gameOver(){
/**
* Game over, printing the message and disabling the buttons, so it'll not be clicked again
*/
panInfo.label.setText("You have no money left, Game Over");
btnMAX.setEnabled(false);
btnMIN.setEnabled(false);
btnSPIN.setEnabled(false);
}
/**
* method to shuffle all the color panel objects
*/
public void randomSpin(){
cpLeft.randomShapeAndColor();
cpMid.randomShapeAndColor();
cpRight.randomShapeAndColor();
repaint();
}
public static float round(float d) {
/**
* This method is used to round the float values to maximum of 2 points,
* this is important; or else Java will not preserve accurate values to
* the float variables. (1-0.10 will give 0.8999999 instead of .90);
*
*/
int decimalPlace=2;
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}
}
public class ClarkslotMachine {
public static void main(String[] args) {
MyFrame frm = new MyFrame();
frm.setVisible(true);
}
}
//Output Screenshots
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.