I'm a beginner in Java, and this is the first time I have written a program from scratch without a guide explaining how and where to create classes etc. Please review my code to see whether the program has been written in a logical way.
The program is a very basic FruitMachine. The user clicks on "spin", and three cards are drawn. Depending on what these 3 cards are the user either wins or loses points. The game continues until a certain threshold is met.
FruitMachine Class - Main class, creates a UserInterFaceFrame
public class FruitMachine {
public static void main(String[] args) {
UserInterfaceFrame userInterface = new UserInterfaceFrame("Fruitmachine");
}
}
GameLogic class - Controls the functioning of the game
import java.beans.PropertyChangeSupport;
import java.util.Random;
public class GameLogic {
private String[] cards;
private String[] drawnCards;
private int balance;
public GameLogic() {
cards = new String[]{"Ace", "King", "Queen", "Jack", "Joker"};
drawnCards = new String[3];
this.balance = 100;
}
public String[] getCards() {
return cards;
}
public int getBalance() {
return balance;
}
public String getDrawnCardAtX(int x) {
return drawnCards[x];
}
public void setBalance(int newBalance) {
balance = newBalance;
}
public String spin() {
Random random = new Random();
int count = 0;
for (int i = 0; i < 3; i++) {
drawnCards[i] = cards[random.nextInt(cards.length)];
System.out.println(drawnCards[i]);
if (drawnCards[i].equals("Joker")) {
balance -= 25;
count++;
}
}
if (count > 1) {
return count + " jokers: you lose " + (25 * count) + " points";
}
if (count != 0) {
return count + " joker: you lose " + (25 * count) + " points";
}
if (drawnCards[0].equals(drawnCards[1]) && drawnCards[1].equals(drawnCards[2])) {
balance += 50;
return "Three of a kind - you win 50 points";
} else if (drawnCards[0].equals(drawnCards[1]) || drawnCards[0].equals(drawnCards[2])
|| drawnCards[1].equals(drawnCards[2])) {
balance += 20;
return "Two of a kind - you win 20 points";
}
return null;
}
}
UserInterFace class - Creates a JFrame and fills it with JPanels for the user interface.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class UserInterfaceFrame extends JFrame implements ActionListener {
private GameLogic gamelogic;
;
public UserInterfaceFrame(String title) {
super(title);
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new GridLayout(2, 0));
this.gamelogic = new GameLogic();
JPanel labelsBorder = new JPanel();
labelsBorder.setBorder(BorderFactory.createLoweredBevelBorder());
JPanel labels = new JPanel();
labels.setLayout(new BoxLayout(labels, BoxLayout.Y_AXIS));
DataLabel balanceLabel = new DataLabel("Balance is " + gamelogic.getBalance());
labels.add(balanceLabel);
DataLabel cards = new DataLabel("Welcome");
labels.add(cards);
DataLabel winLose = new DataLabel("");
labels.add(winLose);
labelsBorder.add(labels);
JPanel lowerFrame = new JPanel();
lowerFrame.setLayout(new GridLayout(0, 2));
JPanel lowerRightFrame = new JPanel();
lowerRightFrame.setLayout(new GridLayout(2, 0));
lowerRightFrame.setBorder(BorderFactory.createEmptyBorder(60, 60, 60, 60));
JPanel lowerLeftFrame = new JPanel();
lowerLeftFrame.setLayout(new GridLayout(0, 3, 30, 0));
lowerLeftFrame.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
JPanel card1 = new CardPanel();
JLabel card1Label = new JLabel("", SwingConstants.CENTER);
card1.add(card1Label);
JPanel card2 = new CardPanel();
JLabel card2Label = new JLabel("", SwingConstants.CENTER);
card2.add(card2Label);
JPanel card3 = new CardPanel();
JLabel card3Label = new JLabel("", SwingConstants.CENTER);
card3.add(card3Label);
lowerLeftFrame.add(card1);
lowerLeftFrame.add(card2);
lowerLeftFrame.add(card3);
JButton spin = new JButton("Spin");
spin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cards.setText(gamelogic.spin());
balanceLabel.setText("Balance is " + gamelogic.getBalance());
card1Label.setText(gamelogic.getDrawnCardAtX(0));
card2Label.setText(gamelogic.getDrawnCardAtX(1));
card3Label.setText(gamelogic.getDrawnCardAtX(2));
}
});
JButton reset = new JButton("New game");
reset.setEnabled(false);
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gamelogic.setBalance(100);
reset.setEnabled(false);
spin.setEnabled(true);
winLose.setText("");
balanceLabel.setText("Balance is: " + gamelogic.getBalance());
}
});
balanceLabel.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
if (gamelogic.getBalance() < 0) {
winLose.setText("You lose!");
reset.setEnabled(true);
spin.setEnabled(false);
}
if (gamelogic.getBalance() > 150) {
winLose.setText("You win!");
reset.setEnabled(true);
spin.setEnabled(false);
}
}
});
lowerRightFrame.add(spin);
lowerRightFrame.add(reset);
lowerFrame.add(lowerLeftFrame);
lowerFrame.add(lowerRightFrame);
add(labelsBorder);
add(lowerFrame);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
DataLabel Class - Extends JPanel to overwrite some of the default behaviour
import javax.swing.BorderFactory;
import javax.swing.JLabel;
public class DataLabel extends JLabel {
public DataLabel(String setString) {
super(setString);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
}
Card Panel Class
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class CardPanel extends JPanel {
public CardPanel() {
setBorder(BorderFactory.createRaisedBevelBorder()); // Creates a raised border around the edge of the cards to make them "pop".
setBackground(Color.yellow); // Sets the colour of the cards to yellow
setLayout(new GridBagLayout()); // GridBagLayout with out any constraints will always centre a JLabel.
}
}