patternjavaMinor
Hangman game code refactoring
Viewed 0 times
codegamehangmanrefactoring
Problem
I would like some help refactoring this code. This is to improve some of the nonfunctional attributes of the software to
```
package Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class GameStructure {
private String []wordList = {"computer","java","activity","alaska","appearance","article",
"automobile","basket","birthday","canada","central","character","chicken","chosen",
"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
"london","massage","minerals","outer","paint","particles","personal","physical","progress",
"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
private JTextField tf;
private JLabel jlLetsUsed;
static String letter;
static int []wordLength = new int[64];
public void window() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Developer", KeyEvent.VK_T);
menu.add(menuItem);
JMenuItem menuItem2 = new JMenuItem("Instructions", KeyEvent.VK_T);
menu.add(menuItem2);
JMenuItem menuItem3 = new JMenuItem("Levels", KeyEvent.VK_T);
menu.add(menuItem3);
JMenuItem menuItem4 = new JMenuItem("Restart", KeyEvent.VK_T);
menu.add(menuItem4);
JMenuItem menuItem5 = new JMenuItem("Exit", KeyEvent.VK_T);
menu.add(menuItem5);
ImageIcon ic = new Ima
- improve code readability
- reduce complexity to improve the maintainability of the source code.
```
package Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
class GameStructure {
private String []wordList = {"computer","java","activity","alaska","appearance","article",
"automobile","basket","birthday","canada","central","character","chicken","chosen",
"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
"london","massage","minerals","outer","paint","particles","personal","physical","progress",
"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday"};
private JTextField tf;
private JLabel jlLetsUsed;
static String letter;
static int []wordLength = new int[64];
public void window() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_A);
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("Developer", KeyEvent.VK_T);
menu.add(menuItem);
JMenuItem menuItem2 = new JMenuItem("Instructions", KeyEvent.VK_T);
menu.add(menuItem2);
JMenuItem menuItem3 = new JMenuItem("Levels", KeyEvent.VK_T);
menu.add(menuItem3);
JMenuItem menuItem4 = new JMenuItem("Restart", KeyEvent.VK_T);
menu.add(menuItem4);
JMenuItem menuItem5 = new JMenuItem("Exit", KeyEvent.VK_T);
menu.add(menuItem5);
ImageIcon ic = new Ima
Solution
Some comments:
-
Make sure you have a consistent indentation style. It makes it difficult to follow the code flow, if there are multiple lines with different indentation.
-
You should separate the view and the game logic. Having both the Swing design setup and the actual game logic mixed up—in a single method—makes the code very hard to navigate. Ideally, both should be in separate classes.
-
Consider making a subclass of
-
-
Make sure you have a consistent indentation style. It makes it difficult to follow the code flow, if there are multiple lines with different indentation.
-
You should separate the view and the game logic. Having both the Swing design setup and the actual game logic mixed up—in a single method—makes the code very hard to navigate. Ideally, both should be in separate classes.
-
Consider making a subclass of
JFrame for your game view. It will be the only thing responsible for setting up the visuals. You can pass it a GameStructure object (which would be only the game logic), so it can combine everything.-
for(j = 0; j
-
tf.addActionListener(new ActionListener() { … — Instead of having an anonymous listener, consider making an actual type to clean everything up. As you only need to have a single listener, you can also make the class you’re in implement ActionListener, so you can pass this to addActionListener`.Context
StackExchange Code Review Q#38048, answer score: 7
Revisions (0)
No revisions yet.