我基本上可以完成制作基本的刽子手游戏所需的所有其他事情,除非当用户用完所有尝试并且当用户猜出单词中的所有字母时停止游戏。还有一个问题是,当我输入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];
while (tries >= 0){
String wordguess = kb.next();
char guess = wordguess.charAt(0);
processGuess(guess, word, bool);
printPattern(word, bool);
}
// FINISH IMPLEMENTING THIS!
}
// Loads a list of words from a file and returns the lsit as a String[].
// DO NOT ALTER THIS IMPLEMENTATION!!
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) {
int incorrect_tries = 0;
for(int i = 0; i < word.length(); i++) {
if (guesses[word.charAt(i)-'a'] == false){
incorrect_tries++;
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;
}
}
答案 0 :(得分:0)
您需要对while
中的main
循环进行两处更改:每次循环时减少tries
并检查游戏是否已获胜。
while (tries >= 0 && !hasWon()) {
String wordguess = kb.next();
char guess = wordguess.charAt(0);
processGuess(guess, word, bool);
printPattern(word, bool);
--tries;
}
修改processGuess
以返回布尔值而不是int可能很有用,如果猜测是新的,返回值将是true
,如果用户已经是false
则返回值while (tries >= 0 && !hasWon()) {
String wordguess = kb.next();
char guess = wordguess.charAt(0);
if (processGuess(guess, word, bool)} {
--tries;
}
printPattern(word, bool);
}
猜到了。然后,只有当它是一个新猜测时,你才可以将其标记为尝试:
{{1}}