So I've made a game called HangMan, but it still needs the finishing touches. Anyways, a bit about the game... A JFrame shows 26 buttons for the letters of the alphabet and a whole set of other buttons to make up empty slots (they eventually will be filled in everytime the user picks the correct letter.)
Now, the finishing touches are.. I need to implement methods hasWon() and gameOver(). I've wrote them up, but their messages like "YOU WON!!" and "GAME OVER!!" do not show up. Can anyone help me try to call these messages to set the JLabel error?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class HangMan extends JFrame implements ActionListener{
private ArrayList <Vocab> satWords = new ArrayList <Vocab>();
private boolean gameOver;
private boolean empty;
private JLabel error = new JLabel ("Start Game!");
private JButton slots [] = new JButton [0]; //initial state has 0 slots.
private JButton ltrs [] = new JButton [26]; //array of buttons to create a button for each leter of the alphabet
private JPanel buttonPanel = new JPanel();
private JPanel slotPanel = new JPanel();
private JPanel cluePanel = new JPanel();
private String winner;
private String clueDef;
private int numLetters;
private int wrong; //once wrong equals 10, the game is finished.
public HangMan(){
setLayout(new BorderLayout());
setSize(750,200);
buttonPanel.setLayout(new GridLayout(2,2));
add(buttonPanel, BorderLayout.SOUTH);
char[] aChar = new char[1]; // use for String constructor
int indexCount = 0; /* needed to designate an index in Array ltrs [] so that
each new JButton will be added in that spot.*/
for(char ltr = 'a'; ltr <= 'z'; ltr++) {
aChar[0] = ltr; // setup for below
ltrs[indexCount] = new JButton(new String(aChar));
ltrs[indexCount].addActionListener(this);
buttonPanel.add(ltrs[indexCount]);
indexCount++;
}
try{
Scanner inFile = new Scanner(new File("SAT.txt"));
int numWords = inFile.nextInt();
inFile.nextLine(); //needed to flush EOL
while(inFile.hasNext()){
String randomWord = inFile.nextLine();
String def = inFile.nextLine();
satWords.add(new Vocab(randomWord, def));
}
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
Random chooser = new Random();
int pick = chooser.nextInt(satWords.size());
winner = satWords.get(pick).getWord();
//THE WINNING WORD! must be kept hidden!
clueDef = satWords.get(pick).getDefinition();
//Definition used as a clue.
numLetters = satWords.get(pick).getNumLetters();
getSlots();
add(error, BorderLayout.WEST);
wrong = 0;
}
private void getSlots(){
add(cluePanel, BorderLayout.NORTH);
add(slotPanel, BorderLayout.CENTER);
JLabel visibleDefinition = new JLabel(clueDef);
cluePanel.add(visibleDefinition);
//adds the selected vocab definition as a Label.
//definition given as a clue to help player figure out what the word is.
slots = new JButton[numLetters];
for(int i = 0; i < numLetters; i++){
slots[i] = new JButton("__");
slots[i].addActionListener(this);
slotPanel.add(slots[i], BorderLayout.WEST);
} //number of slots made are based on the number of letters
}
public void actionPerformed(ActionEvent e){
for(int k = 0; k < ltrs.length; k++){
if(e.getSource() == ltrs[k]){
isWrong(ltrs[k]);
//fillIn(ltrs[k]);
//will check to see if the Letter selected is used in the hidden word
}
}
for (int i = 0; i <slots.length; i++){
if(e.getSource() == slots[i]){
error.setText("You need to pick a letter!");
//makes sure that the user does not hit the slot buttons!
}
}
}
private void fillIn(boolean correct, int index){
//checks to see if the game is still going on!
//for(int m = 0; m < ltrs.length; m++){
if(!hasWon(numLetters)){
if(correct){ //if the letter chosen is correct, it will appear on the slot where it belongs
String addWord = "" + winner.charAt(index); //converts the correct char into String
slots[index].setText(addWord); //makes String become visible in empty slot (now no longer empty)
numLetters--;
}//else{ //if letter chosen is incorrect, variable wrong will increment until it reaches 10
//wrong++;
//error.setText("Wrong guess! You have " + (10 - wrong) + " guesses left!");
// }
//else if (!correct && !gameFinished(wrong)){
}
// }
}
private void isWrong(JButton choice){
boolean isFound = false;
char ltrPicked = choice.getText().charAt(0);//gets the letter from the Letter button chosen.
//char [] correctLetters = new char[winner.length()]; //max size for an array (the amount of letters in the word)
//int indexCount = 0; //serves as index marker for array, beginning @ 0 to store first matched letter found in beginning of array
for(int i = 0; i < winner.length(); i++){
if(winner.charAt(i) == ltrPicked){
// {
fillIn(true, i); //ensures that the picked letter matches one or more of the letters in the word
isFound = true;
// }
//correctLetters[indexCount] = winner.charAt(i);
//indexCount++;
if (isFound == false){
fillIn(false, -1);
wrong++;
gameOver(wrong);
}
}
}
}
private boolean gameOver(int numWrong){
if (numWrong == 10){
error.setText("GAME OVER!!");
return true;
}else if (numWrong > 0 && numWrong < 10){
error.setText("Wrong guess! You have " + (10 - numWrong) + " guesses left!");
}
return false;
}
private boolean hasWon(int remainLtrs){
//for(int x = 0; x < slots.length; x++){
//if (slots[x].getText().equals("")){
if(remainLtrs == 0){
error.setText("YOU WON!"); //makes sure none of the slots are empty
return true; //if all the slots are filled, the user as won.
}
// }
// }
return false;
//if any of the slots are empty, the user still hasn't won yet.
}
public static void main(String[] args) {
JFrame frame = new HangMan();
//frame.setVisible(true);
frame.show();
}
}