Note that there are some explanatory texts on larger screens.

plurals
  1. POI am making a hangman project and I wanted to know how to make the game stop once you finish all your tries?
    primarykey
    data
    text
    <p>This is my code that i have written so far. The problem is that when it asks for how many tries and I put 3 it only lets me try 3 times no matter what, even if I guess wrong or right. The tries represent how many times you get the guess wrong. So I if didn't get the word in 3 tries then it would stop the game. That's my problem.</p> <pre><code>import java.io.*; import java.util.*; public class Hangman { public static void main(String[] args) throws FileNotFoundException { String[] dictionary = loadWords(); Scanner kb = new Scanner(System.in); System.out.println("Welcome to Hangman!"); System.out.println("How many tries"); int tries = kb.nextInt(); String word = chooseWord(dictionary); System.out.println("OK, I've choseen my word. Start guessing"); System.out.println(word); boolean[] bool = new boolean[26]; for(int i = 0; i &lt; tries; i++) { String wordguess = kb.next(); char guess = wordguess.charAt(0); processGuess(guess, word, bool); printPattern(word, bool); } } // Loads a list of words from a file and returns the lsit as a String[]. public static String[] loadWords() throws FileNotFoundException { ArrayList&lt;String&gt; wordList = new ArrayList&lt;String&gt;(); Scanner wordFile = new Scanner(new File("dictionary.txt")); String line; while (wordFile.hasNextLine()) { line = wordFile.nextLine(); if(!line.equals("")) wordList.add(line); } String[] result = new String[1]; return wordList.toArray(result); } // Takes an array of strings that represents the valid words as a parameter. // Chooses one such word randomly and returns it. public static String chooseWord(String[] dict) { Random r = new Random(); int numword = r.nextInt(dict.length); String hangword = dict[numword]; return hangword; } // Checks if a player has won the game // Returns true only if all letters in the word have been guessed public static boolean hasWon(String word, boolean[] guesses) { for(int i = 0; i &lt; word.length(); i++) { if (guesses[word.charAt(i)-'a'] == false){ return false; } } return true; } // Prints out the pattern of letters in the secret word based on the word // and the letters that have been guessed. // Prints any letter that has already been guessed and a _ for a letter that // has not been guessed. public static void printPattern(String word, boolean[] guesses) { StringBuilder pattern = new StringBuilder(); for(int i = 0; i &lt; word.length(); i++) { if (guesses[word.charAt(i) - 'a']) { pattern.append(word.charAt(i)); } else { pattern.append("_"); } pattern.append(" "); } System.out.println(pattern.toString()); } // Handles a guess by marking the letter as guessed and returns the number of // tries to be charged: 0 if the guessed letter is in the word and 1 otherwise. public static int processGuess(char guess, String word, boolean[] guesses) { if(guesses[(guess - 'a')] == false){ guesses[(guess - 'a')] = true; } else { System.out.println("you already guessed that letter"); } return guess; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload