import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
public class WordGuessGame
{
JFrame frame;
JPanel panel;
JLabel label;
JTextArea input;
String word;
JButton checkletter;
JButton giveup;
public static void main(String[]args)
{
WordGuessGame app = new WordGuessGame();
app.start();
}
public void start()
{
frame = new JFrame();
panel = new JPanel();
frame.getContentPane().add(BorderLayout.NORTH, panel);
frame.setVisible(true);
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(new FlowLayout());
label = new JLabel(" Word Guessing Game");
panel.add(label);
checkletter = new JButton("Start");
panel.add(checkletter);
input = new JTextArea("");
panel.add(input);
giveup = new JButton("Give up!");
panel.add(giveup);
}
//create an inner listener class for the first button
class startListener implements ActionListener
{
String guess;
int wrongGuess;
Random generator = new Random();
JLabel invalidLbl;
JLabel guessedLbl;
public void actionPerformed(ActionEvent event)
{
//string array of words
String [] words = {"paprika", "savvy", "knickknack", "cyclists",
"daydreamt", "cushion", "karaoke", "parlour", "cormac",
"riverrock", "storage", "chemistry", "cockatoo", "fujitsu"};
char userGuess, again;//declare variables
//start do loop
do {
int num = words.length;//words that are available
word = selectRandomWord(words, num);//selects a random word from array
int word_length = word.length();//finds length of word
guess = initialise(word_length);//creates dashes to display depending on length of word
wrongGuess = 0;//user wrong guesses
boolean[] history = new boolean[26];//stores users guesses in array so not repeated again
System.out.println(guess);//outsputs the current users guess
//while loop to repeat the users guess until lives run out
while(wrongGuess <10 && !guess.equals(word))
{
userGuess = getGuess(guess, wrongGuess);//gets the users next guess
if (userGuess < 'a' || userGuess > 'z')//ensures that guess is a letter
invalidLbl.setText(invalidLbl.getText() +("Invalid guess " + userGuess));//display output if guess is not a letter and displays what they entered
else if (history[userGuess - 'a'])//checks history array to ensure guess has already not be used
guessedLbl.setText(guessedLbl.getText() +("You already guessed " + userGuess));//display output if guess has been entered before
else
{
history[userGuess - 'a'] = true;//enters letter as guessed to histroy array
if(inWord(userGuess, word)) //checks if guess letter is in word and enters in to display
guess = fillInGuess(userGuess, guess, word);//adds the guessed letter to variable
else//wrong guess
wrongGuess++;//chnages amount of wrong guesses (adds on, loses a life)
}
//if statement which displays answer is number of wrong guesses is reached
if(wrongGuess==10)
JOptionPane.showMessageDialog(null, "The answer was: " + word);//output to user is maximum guesses has been reached
}again=goAgain();// opens goAgain method
} while (again=='Y');//repeats if answer to restart game is "Y"
}//close playgame method
//start goAgain method
public char goAgain()
{
//display output to prompt user to go again, if yes do loop is repeated above
return JOptionPane.showInputDialog("Do you want to play again? (Y/N)").toUpperCase().charAt(0);
}
//select random word method
public String selectRandomWord(String[] words, int num)
{
//selects a random word from array
return words[generator.nextInt(num)];
}
//start initialise method
public String initialise(int word_length)
{
//sets the number of "-" to be displayed depending on length of word
String temp="";
for(int i=0;i<word_length;i++)
temp+="-";
return temp;
}
//start getGuess method
public char getGuess(String currentGuess, int numWrong)
{
//output on optionpane to prompt user to enter another guess and shows how many wrong guesses made
return JOptionPane.showInputDialog("Your current guess is " +
currentGuess + "\n\n"+ numWrong + "/10" + " wrong guesses so far\n"
+ "\nEnter your next guess").toLowerCase().charAt(0);
}
//start inWord method
public boolean inWord(char userGuess, String word)
{
//checks if users guess is a charactor in the word
boolean returnvalue=false;//if not returns as false
for(int i=0;i<word.length();i++)
if(userGuess==word.charAt(i))
returnvalue=true;//is yes returns as true
return returnvalue;
}
//start method fillInGuess
public String fillInGuess(char userGuess, String guess, String word)
{
//fills in the correctly guessed charactor to the position in the word to display in panel
String temp="";
for(int i=0;i<word.length();i++)
{
if(guess.charAt(i)!='-')
temp=temp+guess.charAt(i);
else if(word.charAt(i)!=userGuess)
temp=temp+"-";
else temp=temp+userGuess;
}
return temp;
}//ends fillInGuess method
}
//end class
}