Wordnik Hangman游戏,wordnikhangman游戏,import net.j
分享于 点击 9891 次 点评:2
Wordnik Hangman游戏,wordnikhangman游戏,import net.j
import net.jeremybrooks.knicker.*;import net.jeremybrooks.knicker.dto.*;import java.util.*;public class Hangman { private static char[] correct = new char[26]; private static char[] missed = new char[6]; private static final String key = "your authentication key goes here"; private static String word; private static final Scanner s = new Scanner(System.in); private static int skips; private static void helpMenu(){ System.out.println("\nCommands:\n" + "a-z, A-Z - These are called letters. Use them.\n" + "> - Use a skip; skip to the next game.\n" + "# - End current game, end the process, end everything!\n" + "? - This is how you accessed this list!\n"); } // Wipes the contents of the correct and missed arrays // to prepare for a new Hangman game private static void wipeArrays() { Arrays.fill(correct, (char)0); Arrays.fill(missed, (char)0); } private static String getArt() { StringBuilder art = new StringBuilder(); int misses = numMisses(); art.append(" _____ \n"); art.append("| | \n"); if (misses > 0) { art.append("| o \n"); art.append("| o o \n"); art.append("| o o \n"); art.append("| o \n"); } else { art.append("| \n"); art.append("| \n"); art.append("| \n"); art.append("| \n"); } if (misses > 1) { if (misses > 2) { if (misses > 3) { art.append("| | \n"); art.append("| /|\\ \n"); art.append("| / | \\\n"); } else { art.append("| | \n"); art.append("| /| \n"); art.append("| / | \n"); } } else { art.append("| | \n"); art.append("| | \n"); art.append("| | \n"); } } else { art.append("| \n"); art.append("| \n"); art.append("| \n"); } if (misses > 4) { if (misses > 5) { art.append("| / \\ \n"); art.append("| / \\\n"); art.append("| \n"); } else { art.append("| / \n"); art.append("| / \n"); art.append("| \n"); } } else { art.append("| \n"); art.append("| \n"); art.append("| \n"); } art.append("**********\n"); return art.toString(); } private static String getBlanks() { StringBuilder blanks = new StringBuilder(word.length()); int max = numCorrect(); int wordLength = word.length(); for (int i = 0; i < wordLength; i++) { blanks.append("_ "); } for (int i = 0; i < max; i++) { for (int k = 0; k < wordLength; k++) { if (word.charAt(k) == correct[i]) { blanks.replace(k * 2, (k * 2) + 1, Character.toString(correct[i])); } } } for (int i = 0; i < wordLength; i++){ if (!Character.isLetter(word.charAt(i))){ blanks.replace(i * 2, (i * 2) + 1, Character.toString(word.charAt(i))); } } return blanks.toString(); } // if correct, adds to the correct array // if incorrect, adds to the missed array // returns true if correct, else returns false private static boolean isCorrect(char c) { for (int i = 0; i < word.length(); i++) { if (c == word.charAt(i)) { correct[numCorrect()] = c; return true; } } missed[numMisses()] = c; return false; } private static boolean alreadyGuessed(char c) { int numCorrect = numCorrect(); int numMisses = numMisses(); for (int i = 0; i < numCorrect; i++) { if (correct[i] == c) { return true; } } for (int i = 0; i < numMisses; i++) { if (missed[i] == c) { return true; } } return false; } private static int numCorrect() { int i = 0; while (i < correct.length && correct[i] != 0) { i++; } return i; } private static int numMisses() { int i = 0; while (i < missed.length && missed[i] != 0) { i++; } return i; } private static int guess() throws Exception{ String in = s.nextLine(); char c = 0; if (in.length() == 0) { System.out.println("\nEmpty string: You must enter a letter!"); return -1; } c = Character.toLowerCase(in.charAt(0)); if (c == '#') { System.out.println("\nPlayer cancelled the game while playing word " + word + ". Good-bye!"); Thread.sleep(3000); System.exit(0); } if (c == '?'){ helpMenu(); return 2; // help menu } if (c == '>'){ if (skips > 0){ System.out.println("Are you sure that you want to skip? Skips left: " + skips); System.out.println("(Enter any char for YES. Enter # for NO.)"); System.out.print("\n> "); if (s.nextLine().equalsIgnoreCase("#")){ System.out.println("\nResuming game!"); return 0; } else{ skips--; wipeArrays(); System.out.println("You skipped the word " + word + ". You have " + skips + " skips left!\nRegain your skips by winning games."); return 1; } } else{ System.out.println("You have 0 skips left! You must win games to regain your skips!"); return -1; } } if (!Character.isLetter(c)) { System.out.println("\nInvalid character: You must enter a letter!"); return -1; } if (alreadyGuessed(c)) { System.out .println("\nAlready guessed: You must enter an unused letter!"); return -1; } System.out.println("\nGuessed: " + c); if (isCorrect(c)) System.out.println("Correct guess!"); else System.out.println("Incorrect guess!"); return 0; } private static String guessedLetters() { StringBuilder list = new StringBuilder(); int numMisses = numMisses(); for (int i = 0; i < numMisses; i++) { if (i == numMisses - 1) { list.append(missed[i]); } else { list.append(missed[i] + ","); } } return list.toString(); } public static void main(String[] args) throws Exception { Random r = new Random(); String hint = null; String blanks = null; List<Definition> defs = null; skips = 3; int guessStatus = 0; // use your API key here System.setProperty("WORDNIK_API_KEY", key); System.out.println("Welcome to Hangman!"); System.out.println(" by Steve Franchak\n"); System.out .println("Special thanks to Wordnik for providing a great API!"); System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", " _______\n| | Not Hangpony!", "| n##n,", "| /\" /##", "| (__/ ##_ ___", "| | ``` `\\", "| \\ / / |\\", "| || /_,-\\ / #", "| ||| >> >", "| //_( //_(\n|\n*************"); System.out.println("\nEnter ? during a game for a list of commands!"); Thread.sleep(3000l); System.out.println("\n\n"); while (true) { word = WordsApi.randomWord().getWord(); defs = WordApi.definitions(word); hint = defs.get(r.nextInt(defs.size())).getText(); System.out.println("New game!\n"); blanks = getBlanks(); while (numMisses() < 6 && blanks.indexOf("_") != -1) { System.out.println(getArt()); System.out.printf("Hint: %s\nWord: %s\nMisses: %s\n", hint, blanks, guessedLetters()); System.out.print("\n> "); while((guessStatus = guess()) == 2){ System.out.print("\n> "); } if (guessStatus == 1){ break; } blanks = getBlanks(); } if (guessStatus == 1){ continue; } System.out.println(getArt()); if (numMisses() == 6) { System.out.println("You lost!\nThe word is " + word + "."); } else { System.out.println("You won with " + numMisses() + " misses!\nThe word is " + word + "."); if (skips < 3) skips++; } Thread.sleep(1500); System.out .println("Play again?\n(Enter any char for YES. Enter # for NO.)"); System.out.print("\n> "); if (s.nextLine().equalsIgnoreCase("#")){ System.out.println("\nGood-bye!"); Thread.sleep(3000); System.exit(0); } wipeArrays(); } }}
用户点评