HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Russian Roulette in Java using JFrame

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javajframeusingrouletterussian

Problem

A while ago I got bored and being the beginner programmer I am I made a Russian Roulette game in Java using JFrame. The first few windows are JOptionPanes that are a welcoming screen, an update log and a how to play. The object of the game is you get 5 lives and when you lose all of your lives, you die. At the very end there is a JOptionPane that says the amount of trigger clicks you survived. If you have any suggestions please feel free to reply! Here is the source code:

Main Class

```
@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException, FontFormatException {

RussianRouletteGame newGame = new RussianRouletteGame();
newGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newGame.setSize(220, 200);
newGame.setResizable(false);

JFrame messageFrame = new JFrame();
JOptionPane openingPane = new JOptionPane();

@SuppressWarnings("unused")
int openingMessage = openingPane.showOptionDialog(null,
"Welcome To Russian Roulette Alpha 1.1!\nFor this one and all upcoming windows (besides the game) click the \"OK\", \"Cancel\", or \"X\" button to continue.",
"Welcome!", openingPane.OK_CANCEL_OPTION, openingPane.INFORMATION_MESSAGE, null, null, null);
messageFrame.add(openingPane);

JFrame uLogFrame = new JFrame();
JOptionPane uLogPane = new JOptionPane();
@SuppressWarnings("unused")
int uLogMessage = uLogPane.showOptionDialog(null,
"Alpha 1.1 (July 12, 2015) \n-Added welcome window \n-Added update log (duh)\n-Added how to play window \n-Removed \"Thanks for playing\" at end of game \n\nAlpha 1.0 (July 5, 2015)\n-Released",
"Update Log", uLogPane.OK_CANCEL_OPTION, uLogPane.INFORMATION_MESSAGE, null, null, null);
uLogFrame.add(uLogPane);

JFrame howToFrame = new JFrame();
JOptionPane howToPane = new JOptionPane();
int howToMessage = howToPane.showOptionDialog(null,
"1. Click the button with the gun on it to

Solution

In the ActionListener code you are adding to gunButton, you have this switch statement:

Random r = new Radom();
int rand = r.nextInt(6);
...
switch(rand) {
    case 0:
        [lost a life text]
        break;
    case 1:
        [alive text]
        break;
    case 2:
        [alive text]
        break;
    case 3:
        [alive text]
        break;
    case 4:
        [alive text]
        break;
    case 5:
        [alive text]
        break;
    case 6:
        [alive text]
        break;
}


See something similar about the cases 1 through 6? They do the same exact thing. The only different one is case 0.

To make this much more efficient and clean, just simple replace this switch statement with an if statement checking that the random number is 0.

That would look like this:

if(rand == 0) {
    ...
} else {

}


In the else section is where you would put the whatever you did for cases 1 through 6.

In Java (and most every other language that I know), it is bad practice/a bad idea to reseed the random number generator every single time you go to use it.

That is what you are doing here:

public void actionPerformed(ActionEvent event) {
    Random r = new Random();
    int rand = r.nextInt(6);


By re-instantiating the Random class every time this method is called, you are re-seeding the random number generator.

Rather than doing this, you should set a Random instance to a field of this RussianRoulette class, instantiate it once, and then simple call nextInt on that field.

That way, you aren't constantly re-seeding the random number generator.

This seems a little fishy to me:

display.setText("      You're alive!");


And

display = new JTextField("  Click Gun To Play!", 15);


Why are you putting all those spaces before to message? I'm no detective, but it seems to me that you are using these spaces as a quick and dirty way to position text.

This makes your code quite ugly in these sections, and it would be a lot more cleaner and structured if you correctly positioned the text boxes in the right place in the first part.

These fields of RussianRouletteGame

int lives = 5;
JTextField livesDisplay;
JButton gunButton;
ImageIcon gunCold;
ImageIcon gunActive;
JOptionPane endPane = new JOptionPane();
int endProgram;
int clicked = 0;
JTextField display;


Should all have scope modifiers. By that I mean either public, private, protected etc.

Read here for more information on these modifiers.

Everything inside your RussianRouletteGame should have a single indent, then everything inside this class's methods should have another indent.

I do not think that it is very next to remove the indentation from the code inside the class, because it makes it less clear what code belongs to what.

Code Snippets

Random r = new Radom();
int rand = r.nextInt(6);
...
switch(rand) {
    case 0:
        [lost a life text]
        break;
    case 1:
        [alive text]
        break;
    case 2:
        [alive text]
        break;
    case 3:
        [alive text]
        break;
    case 4:
        [alive text]
        break;
    case 5:
        [alive text]
        break;
    case 6:
        [alive text]
        break;
}
if(rand == 0) {
    ...
} else {

}
public void actionPerformed(ActionEvent event) {
    Random r = new Random();
    int rand = r.nextInt(6);
display.setText("      You're alive!");
display = new JTextField("  Click Gun To Play!", 15);

Context

StackExchange Code Review Q#97624, answer score: 7

Revisions (0)

No revisions yet.