'GUI Hangman issues
I'm working on a hangman in GUI, and I'm totally lost for what the issues are with my code. My Reset class won't run and I don't know what else to move on to so I can have a fully functioning game. Any help would be appreciated as I cannot figure this out for the life of me.
package karce_jaidan_swinghangman;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class swinghangman2 {
private static int numGuesses;
private static ArrayList<Character> wrongGuesses = new ArrayList<>();
private static char[] wordArr;
private static String userinput;
private static Dictionary dictionary;
private static JTextField text = new JTextField();
private static JTextArea console = new JTextArea();
private static JTextArea repo = new JTextArea();
private static String getuserinput, secretWord;
private static final int MAX_GUESSES = 8;
private static int key = 0;
private static JPanel hangman = new JPanel();
private static JPanel win = new JPanel();
private static JPanel lost = new JPanel();
//--------------------------------------------------------------------------------------------------------------------------------------
public swinghangman2() throws FileNotFoundException {
// FRAME
JFrame f = new JFrame();
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1920, 1080);
wrongGuesses.add('a');
//--------------------------------------------------------------------------------------------------------------------------------------
// WELCOME PANEL
JPanel instructionscreen = new JPanel();
instructionscreen.setLayout(null);
instructionscreen.setSize(1920, 1080);
instructionscreen.setVisible(false);
// actual instructions on WELCOME PANEL
JTextField helptext3 = new JTextField();
helptext3.setText("How To Play: There will be a secret word that you need to guess. Every wrong letter is a body part on a stick figure. Complete the figure and you lose. Complete the figure and you lose. Press GUESSWORD if you want to guess the word, and HELP for these instructions at anytime.");
helptext3.setBounds(100, 100, 1550, 100);
helptext3.setEditable(false);
// INSTRUCTIONS ON MIDGAME HELP SCREEN
JTextField helptext4 = new JTextField();
helptext4.setText("How To Play: There will be a secret word that you need to guess. Every wrong letter is a body part on a stick figure. Complete the figure and you lose. Complete the figure and you lose. Press GUESSWORD if you want to guess the word, and HELP for these instructions at anytime.");
helptext4.setBounds(100, 100, 1550, 100);
helptext4.setEditable(false);
//--------------------------------------------------------------------------------------------------------------------------------------
// INSTRUCTION PANEL2 FOR MID GAME HELP
JPanel instructionscreen2 = new JPanel();
instructionscreen2.setLayout(null);
instructionscreen2.setSize(1920, 1080);
instructionscreen2.setVisible(false);
// STARTGAME BUTTON ON WELCOME SCREEN
JButton startGame = new JButton("Start Game");
startGame.setBounds(900, 200, 200, 100);
//--------------------------------------------------------------------------------------------------------------------------------------
// BUTTONS TO GET OUT OF INSTRUCTIONS
JButton cont = new JButton("Continue");
cont.setBounds(900, 600, 200, 100);
JButton helpcont2 = new JButton("Continue");
helpcont2.setBounds(900, 600, 200, 100);
//-------------------------------------------------------------------------------------------------------------------------------------
// HANGMAN SWING
hangman.setSize(1920, 1080);
hangman.setLayout(null);
hangman.setVisible(false);
JButton exit = new JButton("Exit");
exit.setBounds(950, 500, 100, 100);
hangman.add(exit);
JButton exitgame = new JButton("Exit");
exitgame.setBounds(0, 0, 100, 100);
JButton GuessLetter = new JButton("Guess Letter");
GuessLetter.setBounds(1500, 700, 200, 60);
hangman.add(GuessLetter);
JButton GuessWord = new JButton("Guess Word");
GuessWord.setBounds(1300, 700, 200, 60);
hangman.add(GuessWord);
JButton help = new JButton("Help");
help.setBounds(1100, 700, 200, 60);
hangman.add(help);
JTextField console = new JTextField();
console.setBounds(350, 10, 800, 650);
console.setEditable(true);
JScrollPane scrollableTextArea = new JScrollPane(console);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setBounds(350, 10, 800, 650);
hangman.add(scrollableTextArea);
repo.setText(secretWord);
repo.setBounds(100, 500, 200, 100);
repo.setEditable(false);
// KEYBOARD---------------------------------------
String[] qwertyRow1 = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};
char[] qwertyRow1char = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
String[] qwertyRow2 = {"a", "s", "d", "f", "g", "h", "j", "k", "l",};
char[] qwertyRow2char = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l',};
String[] qwertyRow3 = {"z", "x", "c", "v", "b", "n", "m",};
char[] qwertyRow3char = {'z', 'x', 'c', 'v', 'b', 'n', 'm',};
JTextField tf1 = new JTextField();
tf1.setBounds(50, 675, 800, 50);
tf1.setEditable(false);
hangman.setLayout(null);
hangman.add(tf1);
hangman.setSize(3000, 3000);
for (int i = 0; i < qwertyRow1.length; i++) {
int x = i;
int xaxis = i * 100;
JButton b = new JButton(qwertyRow1[i]);
b.setBounds(xaxis, 750, 50, 50); // sets bounds, !!!
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf1.getText() + qwertyRow1[x]);
}
});
hangman.add(b);
}
for (int i = 0; i < qwertyRow2.length; i++) {
int x = i;
int xaxis = i * 100;
JButton b = new JButton(qwertyRow2[i]);
b.setBounds(xaxis, 825, 50, 50); // sets bounds, !!!
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf1.getText() + qwertyRow2[x]);
}
});
hangman.add(b);
}
for (int i = 0; i < qwertyRow3.length; i++) {
int x = i;
int xaxis = i * 100;
JButton b = new JButton(qwertyRow3[i]);
b.setBounds(xaxis, 900, 50, 50); // sets bounds, !!!
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tf1.setText(tf1.getText() + qwertyRow3[x]);
}
});
hangman.add(b);
}
JButton delete = new JButton("BACKSPACE");
delete.setBounds(700, 900, 150, 50);
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tf1.getText().length() > 0) {
tf1.setText(tf1.getText().substring(0, tf1.getText().length() - 1));
}
}
});
// -------------------------------------------------------------------------------------------------------------------------------------
// ENDGAME
lost.setSize(1920, 1040);
lost.setLayout(null);
lost.setVisible(false);
win.setSize(1920, 1040);
win.setLayout(null);
win.setVisible(false);
// -------------------------------------------------------------------------------------------------------------------------------------
// ACTIONLISTENERS
helpcont2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
instructionscreen2.setVisible(false);
hangman.setVisible(true);
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
cont.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("aa");
hangman.setVisible(true);
instructionscreen.setVisible(false);
main();
}
});
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
instructionscreen2.setVisible(true);
hangman.setVisible(false);
}
});
exitgame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
GuessWord.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (validWordGuess() == true) {
checkWordGuess();
displayData();
text.setText("");
complete();
}
}
});
GuessLetter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (validLetteruserinput() == true) {
checkLetterGuess();
displayData();
text.setText("");
complete();
}
}
});
// ADDITIONS
instructionscreen2.add(helptext4);
instructionscreen.add(helptext3);
instructionscreen.add(exit);
instructionscreen.add(cont);
instructionscreen2.add(helpcont2);
hangman.add(exitgame);
hangman.add(repo);
f.add(instructionscreen);
f.add(instructionscreen2);
f.add(hangman);
f.add(lost);
f.add(win);
// VISIBILITY
instructionscreen.setVisible(true);
f.setVisible(true);
}
//-------------------------------------------------------------------------------------------------------------------------------------
// HANGMAN METHODS
private static void reset() {
numGuesses = 0;
wrongGuesses.clear();
secretWord = Dictionary.getRandomWord();
int Length = secretWord.length();
wordArr = new char[Length];
for (int i = 0; i < secretWord.length(); i++) {
wordArr[i] = '_';
}
console.setText("aaa");
}
private static void main() {
reset();
}
private static boolean validLetteruserinput() {
userinput = (text.getText());
if (userinput.length() == 1 && userinput.toLowerCase() == userinput) // could be issue later
// if no work
{
return true;
}
else {
return false;
}
}
private static boolean validWordGuess() {
userinput = (text.getText());
boolean check = false;
if (userinput.length() == secretWord.length()) {
check = true;
}
else {
repo.setText(repo.getText() + "\n" + "Invalid Input.");
}
text.setText("");
return check;
}
private static void checkLetterGuess() {
int temp = 0;
char letter = userinput.charAt(0);
for (int i = 0; i < secretWord.length(); i++) {
if (letter == (secretWord.charAt(i))) {
wordArr[i] = letter;
}
else if (wordArr[i] != '_') {
repo.setText(repo.getText() + "\n" + "You've already guessed this");
}
else {
temp++;
}
}
if (temp == secretWord.length()) {
wrongGuesses.add(letter);
numGuesses++;
}
}
private static void checkWordGuess() {
boolean won = true;
while (won) {
if (userinput.equals(secretWord)) {
win.setVisible(true);
hangman.setVisible(false);
won = false;
}
else if (!userinput.equals("cancel") && !userinput.equals(secretWord)) {
repo.setText(repo.getText() + " Wrong.");
numGuesses++;
won = false;
}
}
}
private static void displayData() {
drawPerson(numGuesses);
repo.setText(secretWord);
console.setText(console.getText() + ("\n"));
for (int i = 0; i < secretWord.length(); i++) {
console.setText(console.getText() + (wordArr[i] + " "));
}
console.setText(console.getText() + "\n" + ("Wrong Guesses:" + wrongGuesses));
console.setText(console.getText() + "\n"
+ ("You have " + (MAX_GUESSES - numGuesses) + " guesses left."));
}
private static boolean wordCompleted() {
// TODO: Replace with your own code
boolean correct = false;
int counter = 0;
for (int i = 0; i < secretWord.length(); i++) {
if (wordArr[i] == secretWord.charAt(i)) {
counter++;
}
else {
}
}
if (counter == secretWord.length()) {
correct = true;
}
return correct;
}
private static void complete() {
if (MAX_GUESSES - numGuesses == 0) {
lost.setVisible(true);
hangman.setVisible(false);
}
if (wordCompleted() == true) {
win.setVisible(true);
hangman.setVisible(false);
}
}
private static void drawPerson(int numGuesses) {
Scanner draw = null;
try {
draw = new Scanner(new File("man" + numGuesses));
}
catch (FileNotFoundException e) {
System.out.println("Could not find file to draw display.");
}
while (draw.hasNextLine()) {
console.setText(console.getText() + "\n" + (draw.nextLine()));
}
}
public static void main(String[] args) throws FileNotFoundException {
new swinghangman2();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
