I have been assignedfrom my university teacher to write a java source code for a hangman game.I have been stuck on the guess method where the player guess a character(especially when he's right).Here is what I have done so far!
Please help me.Feel free to change anything you want but remember this is for a beginner so try to kee it simple.The player has to use the methods by himself.I'm using bluej .Finally the class Words was given by the teacher and is that :<public class Hangman { private static int ALLOWDED_MISTAKES=7; private String secretWord; private int noOfMistakes; private int chances=7-noOfMistakes; public Hangman() { newGame(); }//Hangman constructor public void newGame() { noOfMistakes=0; secretWord=Words.randomWord(); System.out.println(); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println("* * * * * * * * * * N E W G A M E * * * * * * * * * *"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println(); System.out.print("The word is:"); for (int i=1;i<=secretWord.length();i++) System.out.print("_"); System.out.println(); System.out.println(); System.out.println("You have 7 chances to find the word"); System.out.println("use method \"guess(char c)\" to guess a character"); System.out.println("from: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"); System.out.println("or use method \"giveUp()\" to stop playing"); }//newGame public void giveUp() { System.out.println(); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println("* * * * * * * * * * * *G I V E U P * * * * * * * * * * *"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println(); System.out.println("The hidden word is "+secretWord); System.out.println(); System.out.println("Use method \"newGame()\"to start a new game."); }//giveUp public void guess(char c) if (!Character.isLetter(c)){ System.out.println("Only enter letters!"); } if (secretWord.indexOf(c) == -1){ noOfMistakes++; System.out.println("wrong guess,try again"); System.out.println("your remaining chances are "+chances); if (noOfMistakes==ALLOWDED_MISTAKES){ System.out.println(); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println("* * * * * * * * * * * *YOU LOST * * * * * * * * * * *"); System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *"); System.out.println(); System.out.println("The hidden word is "+secretWord); System.out.println(); System.out.println("Use method \"newGame()\"to start a new game."); } } } }//class }>
< import java.util.ArrayList; import java.util.Random; /** * Provides static method for choosing randomly a word from a dictionary * * @author (CR) * @version (11/2012) */ public class Words { //class variables private static ArrayList<String> dictionary=new ArrayList<String>(); private static Random random=new Random(); /* * Generates a list of words */ private static void generateWords() { String[] words={"ALCOHOL","AMBULANCE","ANSWER", "AUDIENCE", "AUTOMN", "BLANKET", "BRIDGE", "CARPENTER", "CHICKEN", "COMFORTABLE", "COMPUTER", "CONFIDENCE", "COUNTRY", "DANGEROUS", "DAUGHTER", "DESCRIBE", "DIRECTLY", "DISTANCE", "EQUIPMENT", "EXAMPLE", "EXPERIMENT", "FAIRYTALE", "FISHERMAN", "GOVERNMENT", "IMAGINATION", "IMPORTANCE", "INDUSTRY", "INFLUENCE", "INSTRUMENT", "KEYHOLE", "KNOWLEDGE", "LANGUAGE", "MACHINE", "NATURAL", "ORDINARY", "PICTURE", "PLEASURE", "POPULATION", "POWERFUL", "QUESTION", "STRANGE", "SUBSTANCE", "SURFACE", "SURPRISE", "SYSTEM", "TEMPERATURE", "TRIANGLE", "UNKNOWN", "WORKER", "YOUTHFUL"}; for(int i=0;i<words.length;i++) dictionary.add(words[i]); }//generateWords /** * Creates a list of words and picks one in random * * @return the chosen word */ public static String randomWord() { generateWords(); return (String) dictionary.get(random.nextInt(dictionary.size())); }//randomWord }//class>