我正在制作一个刽子手项目,我想知道如果你完成所有尝试后如何让游戏停止?

时间:2011-11-27 01:17:03

标签: java

这是我到目前为止编写的代码。问题在于,当它询问尝试了多少次而我放了3次它只会让我尝试3次,无论如何,即使我猜错了也没错。尝试表示您猜错了多少次。所以如果我没有在3次尝试中得到这个词,那么它就会停止游戏。那是我的问题。

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 < 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<String> wordList = new ArrayList<String>();
        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 < 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 < 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;
    }
}

3 个答案:

答案 0 :(得分:2)

for(int i = 0; i < tries; i++) {

您正在迭代固定次数。不应该像

那样
while (!wordComplete && triesCount < tries) {
 //...

答案 1 :(得分:0)

不应使用for循环,而应使用while循环。

我假设您可能希望能够进行可变数量的尝试,而不仅仅是3.因此该示例将适应这种情况。

这样的事情:

int incorrectTries = 0;

while (incorrectTries <= tries)
{
    String wordguess = kb.next();
    char guess = wordguess.charAt(0);
    processGuess(guess, word, bool);
    printPattern(word, bool);
}

// 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 < word.length(); i++) {
if (guesses[word.charAt(i)-'a'] == false){
    incorrectTries++;
    return false;
    }
}   
return true;

}

答案 2 :(得分:0)

使用while循环检查是否尝试等于零,而不是迭代遍历尝试的for循环。当做出错误猜测时,从尝试中减去1。这应该可以满足您的需求。