Posted By

binaryadder on 01/06/12


Tagged

wordnik hangman


Versions (?)

Wordnik Hangman


 / Published in: Java
 

Hangman game that uses the Java Wordnik API.

  1. import net.jeremybrooks.knicker.*;
  2. import net.jeremybrooks.knicker.dto.*;
  3. import java.util.*;
  4.  
  5. public class Hangman {
  6.  
  7. private static char[] correct = new char[26];
  8. private static char[] missed = new char[6];
  9. private static final String key = "your authentication key goes here";
  10. private static String word;
  11. private static final Scanner s = new Scanner(System.in);
  12. private static int skips;
  13.  
  14. private static void helpMenu(){
  15. System.out.println("\nCommands:\n" +
  16. "a-z, A-Z - These are called letters. Use them.\n" +
  17. "> - Use a skip; skip to the next game.\n" +
  18. "# - End current game, end the process, end everything!\n" +
  19. "? - This is how you accessed this list!\n");
  20. }
  21.  
  22. // Wipes the contents of the correct and missed arrays
  23. // to prepare for a new Hangman game
  24. private static void wipeArrays() {
  25. Arrays.fill(correct, (char)0);
  26. Arrays.fill(missed, (char)0);
  27. }
  28.  
  29. private static String getArt() {
  30. StringBuilder art = new StringBuilder();
  31. int misses = numMisses();
  32. art.append(" _____ \n");
  33. art.append("| | \n");
  34. if (misses > 0) {
  35. art.append("| o \n");
  36. art.append("| o o \n");
  37. art.append("| o o \n");
  38. art.append("| o \n");
  39. } else {
  40. art.append("| \n");
  41. art.append("| \n");
  42. art.append("| \n");
  43. art.append("| \n");
  44. }
  45. if (misses > 1) {
  46. if (misses > 2) {
  47. if (misses > 3) {
  48. art.append("| | \n");
  49. art.append("| /|\\ \n");
  50. art.append("| / | \\\n");
  51. } else {
  52. art.append("| | \n");
  53. art.append("| /| \n");
  54. art.append("| / | \n");
  55. }
  56. } else {
  57. art.append("| | \n");
  58. art.append("| | \n");
  59. art.append("| | \n");
  60. }
  61. } else {
  62. art.append("| \n");
  63. art.append("| \n");
  64. art.append("| \n");
  65. }
  66. if (misses > 4) {
  67. if (misses > 5) {
  68. art.append("| / \\ \n");
  69. art.append("| / \\\n");
  70. art.append("| \n");
  71. } else {
  72. art.append("| / \n");
  73. art.append("| / \n");
  74. art.append("| \n");
  75. }
  76. } else {
  77. art.append("| \n");
  78. art.append("| \n");
  79. art.append("| \n");
  80. }
  81. art.append("**********\n");
  82. return art.toString();
  83. }
  84.  
  85. private static String getBlanks() {
  86. StringBuilder blanks = new StringBuilder(word.length());
  87. int max = numCorrect();
  88. int wordLength = word.length();
  89. for (int i = 0; i < wordLength; i++) {
  90. blanks.append("_ ");
  91. }
  92. for (int i = 0; i < max; i++) {
  93. for (int k = 0; k < wordLength; k++) {
  94. if (word.charAt(k) == correct[i]) {
  95. blanks.replace(k * 2, (k * 2) + 1,
  96. Character.toString(correct[i]));
  97. }
  98. }
  99. }
  100. for (int i = 0; i < wordLength; i++){
  101. if (!Character.isLetter(word.charAt(i))){
  102. blanks.replace(i * 2, (i * 2) + 1,
  103. Character.toString(word.charAt(i)));
  104. }
  105. }
  106. return blanks.toString();
  107. }
  108.  
  109. // if correct, adds to the correct array
  110. // if incorrect, adds to the missed array
  111. // returns true if correct, else returns false
  112. private static boolean isCorrect(char c) {
  113. for (int i = 0; i < word.length(); i++) {
  114. if (c == word.charAt(i)) {
  115. correct[numCorrect()] = c;
  116. return true;
  117. }
  118. }
  119. missed[numMisses()] = c;
  120. return false;
  121. }
  122.  
  123. private static boolean alreadyGuessed(char c) {
  124. int numCorrect = numCorrect();
  125. int numMisses = numMisses();
  126. for (int i = 0; i < numCorrect; i++) {
  127. if (correct[i] == c) {
  128. return true;
  129. }
  130. }
  131. for (int i = 0; i < numMisses; i++) {
  132. if (missed[i] == c) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138.  
  139. private static int numCorrect() {
  140. int i = 0;
  141. while (i < correct.length && correct[i] != 0) {
  142. i++;
  143. }
  144. return i;
  145. }
  146.  
  147. private static int numMisses() {
  148. int i = 0;
  149. while (i < missed.length && missed[i] != 0) {
  150. i++;
  151. }
  152. return i;
  153. }
  154.  
  155. private static int guess() throws Exception{
  156. String in = s.nextLine();
  157. char c = 0;
  158. if (in.length() == 0) {
  159. System.out.println("\nEmpty string: You must enter a letter!");
  160. return -1;
  161. }
  162. c = Character.toLowerCase(in.charAt(0));
  163. if (c == '#') {
  164. System.out.println("\nPlayer cancelled the game while playing word " +
  165. word + ". Good-bye!");
  166. Thread.sleep(3000);
  167. System.exit(0);
  168. }
  169. if (c == '?'){
  170. helpMenu();
  171. return 2; // help menu
  172. }
  173. if (c == '>'){
  174. if (skips > 0){
  175. System.out.println("Are you sure that you want to skip? Skips left: " + skips);
  176. System.out.println("(Enter any char for YES. Enter # for NO.)");
  177. System.out.print("\n> ");
  178. if (s.nextLine().equalsIgnoreCase("#")){
  179. System.out.println("\nResuming game!");
  180. return 0;
  181. }
  182. else{
  183. skips--;
  184. wipeArrays();
  185. System.out.println("You skipped the word " + word +
  186. ". You have " + skips + " skips left!\nRegain your skips by winning games.");
  187. return 1;
  188. }
  189. }
  190. else{
  191. System.out.println("You have 0 skips left! You must win games to regain your skips!");
  192. return -1;
  193. }
  194. }
  195. if (!Character.isLetter(c)) {
  196. System.out.println("\nInvalid character: You must enter a letter!");
  197. return -1;
  198. }
  199. if (alreadyGuessed(c)) {
  200. System.out
  201. .println("\nAlready guessed: You must enter an unused letter!");
  202. return -1;
  203. }
  204. System.out.println("\nGuessed: " + c);
  205. if (isCorrect(c))
  206. System.out.println("Correct guess!");
  207. else
  208. System.out.println("Incorrect guess!");
  209. return 0;
  210. }
  211.  
  212. private static String guessedLetters() {
  213. StringBuilder list = new StringBuilder();
  214. int numMisses = numMisses();
  215. for (int i = 0; i < numMisses; i++) {
  216. if (i == numMisses - 1) {
  217. list.append(missed[i]);
  218. } else {
  219. list.append(missed[i] + ",");
  220. }
  221. }
  222. return list.toString();
  223. }
  224.  
  225. public static void main(String[] args) throws Exception {
  226. Random r = new Random();
  227. String hint = null;
  228. String blanks = null;
  229. List<Definition> defs = null;
  230. skips = 3;
  231. int guessStatus = 0;
  232.  
  233. // use your API key here
  234. System.setProperty("WORDNIK_API_KEY", key);
  235.  
  236. System.out.println("Welcome to Hangman!");
  237. System.out.println(" by Steve Franchak\n");
  238. System.out
  239. .println("Special thanks to Wordnik for providing a great API!");
  240. System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
  241. " _______\n| | Not Hangpony!", "| n##n,",
  242. "| /\" /##", "| (__/ ##_ ___",
  243. "| | ``` `\\", "| \\ / / |\\",
  244. "| || /_,-\\ / #", "| ||| >> >",
  245. "| //_( //_(\n|\n*************");
  246. System.out.println("\nEnter ? during a game for a list of commands!");
  247. Thread.sleep(3000l);
  248. System.out.println("\n\n");
  249.  
  250. while (true) {
  251. word = WordsApi.randomWord().getWord();
  252. defs = WordApi.definitions(word);
  253. hint = defs.get(r.nextInt(defs.size())).getText();
  254.  
  255. System.out.println("New game!\n");
  256. blanks = getBlanks();
  257. while (numMisses() < 6 && blanks.indexOf("_") != -1) {
  258. System.out.println(getArt());
  259. System.out.printf("Hint: %s\nWord: %s\nMisses: %s\n", hint,
  260. blanks, guessedLetters());
  261. System.out.print("\n> ");
  262. while((guessStatus = guess()) == 2){
  263. System.out.print("\n> ");
  264. }
  265. if (guessStatus == 1){
  266. break;
  267. }
  268. blanks = getBlanks();
  269. }
  270. if (guessStatus == 1){
  271. continue;
  272. }
  273. System.out.println(getArt());
  274. if (numMisses() == 6) {
  275. System.out.println("You lost!\nThe word is " + word + ".");
  276. } else {
  277. System.out.println("You won with " + numMisses()
  278. + " misses!\nThe word is " + word + ".");
  279. if (skips < 3) skips++;
  280. }
  281. Thread.sleep(1500);
  282. System.out
  283. .println("Play again?\n(Enter any char for YES. Enter # for NO.)");
  284. System.out.print("\n> ");
  285. if (s.nextLine().equalsIgnoreCase("#")){
  286. System.out.println("\nGood-bye!");
  287. Thread.sleep(3000);
  288. System.exit(0);
  289. }
  290. wipeArrays();
  291. }
  292. }
  293. }

Report this snippet  

You need to login to post a comment.