patternjavaMinor
Hangman game code
Viewed 0 times
codegamehangman
Problem
I've developed a code for a Hangman game and what I need help in is basically simplifying, correcting and removing parts of the code that are not needed (or there is an easier way to do it). I think there are parts where I could have done it easier but I do not know where they are. I would appreciate any/ all help and will welcome all constructive criticism.
```
package Hangman;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.net.URL;
@SuppressWarnings("serial")
public class HangManGUI extends JFrame implements ActionListener, KeyListener {
// Panels where everything is drawn on
DrwPnl dPnl1 = new DrwPnl(), dPnl2 = new DrwPnl(), pnlBoard = new DrwPnl();
JPanel align = new JPanel(), pnl2 = new JPanel(), pnl3 = new JPanel(),
pnl4 = new JPanel(), pnl5 = new JPanel(), pnl6 = new JPanel(),
pnl7 = new JPanel();
String p1 = "Dixon", p2 = "Computer", puz = "", selected = "Easy";
String[] categories = { "Easy", "Food", "Standard", "Geography", "Hard",
"Holidays", "Animals", "Sports" }, allPuz, puzzle;
JTextField cusPuz = new JTextField("Custom Puzzle"),
txtName = new JTextField("Player"), txtOpponent = new JTextField(
"Opponent");
JLabel lblName = new JLabel(), lblOpponent = new JLabel(),
wordlist = new JLabel();
JButton[] btnLetters = new JButton[26], close = new JButton[3],
lblWordList = new JButton[8];
JButton player1 = new JButton(), player2 = new JButton(),
btnBack = new JButton("Back"), btnStart = new JButton("Start"),
resetBtn = new JButton("Reset Scores"), newGameBtn = new JButton(
"New Game"), btnMain = new JButton("Menu");
int length, count = 0, chan
```
package Hangman;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.net.URL;
@SuppressWarnings("serial")
public class HangManGUI extends JFrame implements ActionListener, KeyListener {
// Panels where everything is drawn on
DrwPnl dPnl1 = new DrwPnl(), dPnl2 = new DrwPnl(), pnlBoard = new DrwPnl();
JPanel align = new JPanel(), pnl2 = new JPanel(), pnl3 = new JPanel(),
pnl4 = new JPanel(), pnl5 = new JPanel(), pnl6 = new JPanel(),
pnl7 = new JPanel();
String p1 = "Dixon", p2 = "Computer", puz = "", selected = "Easy";
String[] categories = { "Easy", "Food", "Standard", "Geography", "Hard",
"Holidays", "Animals", "Sports" }, allPuz, puzzle;
JTextField cusPuz = new JTextField("Custom Puzzle"),
txtName = new JTextField("Player"), txtOpponent = new JTextField(
"Opponent");
JLabel lblName = new JLabel(), lblOpponent = new JLabel(),
wordlist = new JLabel();
JButton[] btnLetters = new JButton[26], close = new JButton[3],
lblWordList = new JButton[8];
JButton player1 = new JButton(), player2 = new JButton(),
btnBack = new JButton("Back"), btnStart = new JButton("Start"),
resetBtn = new JButton("Reset Scores"), newGameBtn = new JButton(
"New Game"), btnMain = new JButton("Menu");
int length, count = 0, chan
Solution
Thank you for sharing your code. Quite frankly, it's a bit of a mess, so let's try to clean it up.
General Code Issues
Here are a few examples of issues with your code that can easily be corrected. All these issues can be automatically detected by a good IDE such as the free, open-source and excellent IntelliJ IDEA Community Edition1 (Analyze->Inspect Code).
-
You import many classes that you never use. Remove all unnecessary imports, keeping only the ones your code requires. You should also be consistent in your use of
-
Why are you suppressing a warning that is not affecting your code? Remove it.
so you should change
-
Remove redundant comments that say nothing, such as this one:
-
There's no need to add compare Boolean values with
Footnotes
1 If you download it, be sure to use the new Darcula theme.
General Code Issues
Here are a few examples of issues with your code that can easily be corrected. All these issues can be automatically detected by a good IDE such as the free, open-source and excellent IntelliJ IDEA Community Edition1 (Analyze->Inspect Code).
-
You import many classes that you never use. Remove all unnecessary imports, keeping only the ones your code requires. You should also be consistent in your use of
* wildcards; either use them always or never. -
Why are you suppressing a warning that is not affecting your code? Remove it.
@SuppressWarnings("serial")- Why are you declaring but never using
pnl3,pnl4,pnl5andpnl6? Remove them.
- Why are you declaring the constructor and
mainasthrows Exceptionwhen they don't? Remove. The same goes forthrows IOExceptiononcreatePuz.
- Package names in Java are expected to be lower-case,
so you should change
package Hangman; to package hangman;- Version logs should be kept in source control, not in the source files (I noticed this in your upload)
-
Remove redundant comments that say nothing, such as this one:
// Panels where everything is drawn on-
There's no need to add compare Boolean values with
true or false literals, just use them directly:if (gameDone == false)becomesif (gameDone)
if (rightletter == false)becomesif (!rightletter)
if (wrong == true)becomesif (wrong)
Footnotes
1 If you download it, be sure to use the new Darcula theme.
Code Snippets
@SuppressWarnings("serial")// Panels where everything is drawn onContext
StackExchange Code Review Q#21534, answer score: 7
Revisions (0)
No revisions yet.