Create a java GUI hangman game. Your solutions will have as a minimum the follow
ID: 3601615 • Letter: C
Question
Create a java GUI hangman game. Your solutions will have as a minimum the following four classes:
GameEngine (GE) – contains all the logic for the game
Player – information about the player, keep tracks of 6 limited tries of the player, also keep track of the letter that player has already been entered before.
Words – supplies the words for the game engine
HangmanBody – draws the body
The GE will display a secret word and the player has to guess the word. The GE will keep track of the letters selected. The player will not be penalized for choosing the same letter multiple times. Your program should check user input and warn if a letter has already been used. After each missed letter a portion of the hangman body will be displayed. After six tries the complete body will be shown and this session of the game is over. The player can choose to try another word or quit the game.
Each class has a specific function and communicates with the other class to complete the overall goal of the application…to play the hangman game. The Player, Words and HangmanBody classes and be developed and tested independent of the GE, once they have been developed and tested they can be added to the GE to produce the game. Think about the methods or actions that each class must perform and how these classes are used by the GE to play the game.
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class Hangman extends JFrame {
public static void main(String[] args) {
new Hangman();
}
private JLabel commandTitle = new JLabel("Type in the word to guess"), knownWordLabel, wrongGuesses = new JLabel();
private JTextField wordTextField = new JTextField(20);
private BufferedImage hangmanImage = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
private JPanel hangmanPanel, inputPanel;
private String wordToGuess = null;
private int numOfWrongGuesses = 0;
private String wrongGuessesString = "Wrong Guesses: ", wordKnown = "";
public Hangman() {
setLayout(new BorderLayout());
drawHangmanStand(hangmanImage);
wordTextField.setToolTipText("Net Connection: Leave Blank for Random Word");
inputPanel = new JPanel(new GridBagLayout());
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.ipadx = 1;
gridBagConstraints.gridy = 1;
inputPanel.add(commandTitle, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
inputPanel.add(wordTextField, gridBagConstraints);
add(inputPanel);
wordTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (wordToGuess == null) {
wordToGuess = wordTextField.getText().toLowerCase();
if (wordToGuess.length() <= 0) {
try {
URL url = new URL("enter the url");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
List<String> dictionary = new ArrayList();
while ((str = in.readLine()) != null) {
dictionary.add(str);
}
in.close();
wordToGuess = dictionary.get( (int) (dictionary.size()*Math.random()) );
} catch (Exception e1) {
e1.printStackTrace();
}
}
wordTextField.setText("");
commandTitle.setText("Guess a letter");
String displayWord = "";
for (int i = 0; i < wordToGuess.length(); i++) {
wordKnown += "_";
displayWord += " _ ";
}
knownWordLabel = new JLabel(displayWord);
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 1;
gridBagConstraints1.gridy = 2;
gridBagConstraints1.gridwidth = 2;
inputPanel.add(knownWordLabel, gridBagConstraints1);
gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 1;
gridBagConstraints1.gridy = 3;
gridBagConstraints1.gridwidth = 2;
inputPanel.add(wrongGuesses, gridBagConstraints1);
return;
}
if (wordToGuess.indexOf(wordTextField.getText()) >= 0) {
guessRight();
} else {
guessWrong();
wordTextField.setText("");
}
}
});
setSize(1000, 600);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
showChangedHangman();
revalidate();
}
private void guessRight() {
String guess = wordTextField.getText().toLowerCase();
addGuessToKnownWord(guess);
String displayString = "";
for (int i = 0; i < wordKnown.length(); i++) {
displayString += wordKnown.substring(i, i + 1) + " ";
}
knownWordLabel.setText(displayString);
if (wordKnown.indexOf("_") < 0) {
JOptionPane.showMessageDialog(this, "You Win!");
}
wordTextField.setText("");
}
private void addGuessToKnownWord(String guess) {
ArrayList<Integer> indexes = new ArrayList<>();
for (int index = wordToGuess.indexOf(guess);
index >= 0;
index = wordToGuess.indexOf(guess, index + 1)) {
indexes.add(index);
}
for (int i = 0; i < indexes.size(); i++) {
int index = indexes.get(i);
StringBuilder stringBuilder = new StringBuilder(wordKnown);
stringBuilder.replace(index, index + guess.length(), guess);
wordKnown = stringBuilder.toString();
}
}
private void guessWrong() {
numOfWrongGuesses++;
wrongGuessesString += wordTextField.getText() + ", ";
wrongGuesses.setText(wrongGuessesString);
Graphics2D g = (Graphics2D) hangmanImage.getGraphics();
int x = 250, y = 200;
g.setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
switch (numOfWrongGuesses) {
case 1: // Head
g.drawOval(-20 + x, y, 40, 40);
break;
case 2: // Body
g.drawLine(x, y + 40, x, y + 40 + 80);
break;
case 3: // R Arm
g.drawLine(x, y + 40 + 20, x + 20, y + 40 + 60);
break;
case 4: // L Arm
g.drawLine(x, y + 40 + 20, x - 20, y + 40 + 60);
break;
case 5: // R Leg
g.drawLine(x, y + 40 + 80, x + 20, y + 40 + 80 + 40);
break;
case 6: // L Leg
g.drawLine(x, y + 40 + 80, x - 20, y + 40 + 80 + 40);
break;
default:
JOptionPane.showMessageDialog(this, "You Lose!");
break;
}
g.dispose();
showChangedHangman();
revalidate();
}
private void showChangedHangman() {
if (hangmanPanel != null)
remove(hangmanPanel);
hangmanPanel = new JPanel();
hangmanPanel.add(new JLabel(new ImageIcon(hangmanImage)));
add(hangmanPanel, BorderLayout.WEST);
revalidate();
}
private static void drawHangmanStand(BufferedImage image) {
Graphics2D g = (Graphics2D) image.getGraphics();
g.setStroke(new BasicStroke(2));
g.setColor(Color.BLACK);
g.drawLine(10, 475, 250, 475);
g.drawLine(100, 475, 100, 100);
g.drawLine(100, 100, 250, 100);
g.drawLine(250, 100, 250, 200);
g.dispose();
}
}
Please Find this code also
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HangmanGUI {
static JFrame frame;
static JTextField textField;
static JLabel guessesRemainingLabel;
static JLabel lettersGuessedLabel;
static JLabel secretWordLabel;
static void buildGUI(){
SwingUtilities.invokeLater(
new Runnable(){
@Override
public void run(){
frame = new JFrame("Hangman");
guessesRemainingLabel = new JLabel("Guesses remaining: " + String.valueOf(Hangman.guessesRemaining));
lettersGuessedLabel = new JLabel("Already guessed: ");
secretWordLabel = new JLabel();
textField = new JTextField();
JButton checkButton = new JButton("Guess");
GuessListener guessListener = new GuessListener();
checkButton.addActionListener(guessListener);
textField.addActionListener(guessListener);
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));
labelPanel.add(guessesRemainingLabel);
labelPanel.add(lettersGuessedLabel);
labelPanel.add(secretWordLabel);
// User panel
JPanel userPanel = new JPanel(new BorderLayout());
userPanel.add(BorderLayout.CENTER, textField);
userPanel.add(BorderLayout.EAST, checkButton);
labelPanel.add(userPanel);
// Add everything to this frame
frame.add(BorderLayout.CENTER, labelPanel);
frame.setSize(250, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
);
}
static void drawGuessesRemaining(){
final String guessesMessage = "Guesses remaining: " + String.valueOf(Hangman.guessesRemaining);
SwingUtilities.invokeLater(
new Runnable(){
@Override
public void run(){
guessesRemainingLabel.setText(guessesMessage);
guessesRemainingLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
}
}
);
}
static void drawLettersGuessed(){
StringBuilder lettersBuilder = new StringBuilder();
for (Character el : Hangman.lettersGuessed) {
String s = el + " ";
lettersBuilder.append(s);
}
final String letters = lettersBuilder.toString();
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
lettersGuessedLabel.setText("Already guessed: " + letters);
}
}
);
}
static void drawSecretWord(){
StringBuilder word = new StringBuilder();
for(int i=0; i<Hangman.lettersRevealed.length; i++){
if (Hangman.lettersRevealed[i]) {
String s = Hangman.secretWord.charAt(i) + " ";
word.append(s);
} else {
word.append("_ ");
}
}
final String w = word.toString();
SwingUtilities.invokeLater(
new Runnable(){
@Override
public void run() {
secretWordLabel.setText(w);
}
}
);
}
static int playAgainDialog(String m){
return JOptionPane.showConfirmDialog(frame, m, "Play again?", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
}
private static String getText(){
return textField.getText();
}
static void setText(final String t){
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
textField.setText(t);
}
}
);
}
private static class GuessListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent ev){
String guess = getText();
if(Hangman.checkUserGuess(guess)) {
Hangman.updateSecretWord(guess);
drawSecretWord();
if(Hangman.lettersGuessed.size() != 0) drawLettersGuessed();
if (Hangman.checkIfWon())
Hangman.winSequence();
else if (Hangman.guessesRemaining == 0)
Hangman.loseSequence();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.