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

Hangman game code

Submitted by: @import:stackexchange-codereview··
0
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

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 * 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, pnl5 and pnl6? Remove them.



  • Why are you declaring the constructor and main as throws Exception when they don't? Remove. The same goes for throws IOException on createPuz.



  • 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) becomes if (gameDone)



  • if (rightletter == false) becomes if (!rightletter)



  • if (wrong == true) becomes if (wrong)



Footnotes

1 If you download it, be sure to use the new Darcula theme.

Code Snippets

@SuppressWarnings("serial")
// Panels where everything is drawn on

Context

StackExchange Code Review Q#21534, answer score: 7

Revisions (0)

No revisions yet.