/ Published in: Java
Hangman game that uses the Java Wordnik API.
Expand |
Embed | Plain Text
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 int skips; private static void helpMenu(){ "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() { } 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(); } 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, } } } for (int i = 0; i < wordLength; i++){ blanks.replace(i * 2, (i * 2) + 1, } } 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; } char c = 0; if (in.length() == 0) { return -1; } if (c == '#') { word + ". Good-bye!"); } if (c == '?'){ helpMenu(); return 2; // help menu } if (c == '>'){ if (skips > 0){ if (s.nextLine().equalsIgnoreCase("#")){ return 0; } else{ skips--; wipeArrays(); ". You have " + skips + " skips left!\nRegain your skips by winning games."); return 1; } } else{ return -1; } } return -1; } if (alreadyGuessed(c)) { System.out .println("\nAlready guessed: You must enter an unused letter!"); return -1; } if (isCorrect(c)) else return 0; } 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(); } List<Definition> defs = null; skips = 3; int guessStatus = 0; // use your API key here System.out .println("Special thanks to Wordnik for providing a great API!"); " _______\n| | Not Hangpony!", "| n##n,", "| /\" /##", "| (__/ ##_ ___", "| | ``` `\\", "| \\ / / |\\", "| || /_,-\\ / #", "| ||| >> >", "| //_( //_(\n|\n*************"); while (true) { word = WordsApi.randomWord().getWord(); defs = WordApi.definitions(word); hint = defs.get(r.nextInt(defs.size())).getText(); blanks = getBlanks(); while (numMisses() < 6 && blanks.indexOf("_") != -1) { blanks, guessedLetters()); while((guessStatus = guess()) == 2){ } if (guessStatus == 1){ break; } blanks = getBlanks(); } if (guessStatus == 1){ continue; } if (numMisses() == 6) { } else { + " misses!\nThe word is " + word + "."); if (skips < 3) skips++; } System.out .println("Play again?\n(Enter any char for YES. Enter # for NO.)"); if (s.nextLine().equalsIgnoreCase("#")){ } wipeArrays(); } } }
You need to login to post a comment.
