Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. package com.qtech.hangman;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6. import java.util.concurrent.ThreadLocalRandom;
  7.  
  8. public class Main {
  9. private static Scanner stdin = new Scanner(System.in);
  10. private static ArrayList<String> words = new ArrayList<String>();
  11.  
  12. private static boolean exit;
  13. private static boolean won;
  14. private static boolean lost;
  15.  
  16. private static String currentPhrase;
  17. private static StringBuilder usedLetters = new StringBuilder();
  18. private static ArrayList<Character> phraseStatus = new ArrayList<Character>();
  19. private static int attemptsLeft;
  20.  
  21.  
  22. public static void main(String[] args) {
  23. initPhraseList();
  24. while(!exit) {
  25. cls();
  26. switch(doMenu())
  27. {
  28. case 1:
  29. resetVariables();
  30. doGame();
  31. break;
  32. case 2:
  33. exit = true;
  34. return;
  35. }
  36. }
  37. }
  38.  
  39. public static void initPhraseList() {
  40. words.add("josh");
  41. words.add("faggot");
  42. words.add("bitch");
  43. words.add("jews");
  44. words.add("josh is gay");
  45. words.add("fuck bitches get money");
  46. words.add("lolwin");
  47. }
  48.  
  49. private static void resetVariables() {
  50. won = false;
  51. lost = false;
  52. attemptsLeft = 6;
  53. phraseStatus.clear();
  54. currentPhrase = words.get(ThreadLocalRandom.current().nextInt(0, words.size()));
  55. for(char c:currentPhrase.toCharArray()) { if(c==' ') phraseStatus.add(' '); else phraseStatus.add('_'); }
  56. usedLetters.delete(0, usedLetters.length());
  57. }
  58.  
  59. public static int doMenu() {
  60. System.out.println(" HANGMAN ");
  61. System.out.println("1. Play");
  62. System.out.println("2. Quit");
  63. String raw = stdin.next();
  64. int response;
  65. try{
  66. response = Integer.parseInt(raw);
  67. } catch(Exception e) {
  68. response = -1;
  69. }
  70. switch(response) {
  71. case 1:
  72. case 2:
  73. return response;
  74. default:
  75. return doMenu();
  76. }
  77. }
  78.  
  79. public static void doGame() {
  80. while(!exit) {
  81. cls();
  82.  
  83. System.out.println("Wrong letters: "+usedLetters.toString());
  84. updateMan();
  85. updateWord();
  86.  
  87. if(won) {
  88. System.out.println("You win!");
  89. waitForEnter();
  90. break;
  91. } else if(lost) {
  92. System.out.println("You lose!");
  93. waitForEnter();
  94. break;
  95. }
  96.  
  97. System.out.println("Enter letter or guess word:");
  98. String in = stdin.nextLine();
  99. if(in != "") {
  100. if(in.length() > 1)
  101. if(checkWord(in)) { won = true; } else {
  102. usedLetters.delete(0, usedLetters.length());
  103. usedLetters.append(in);
  104. lost = true;
  105. }
  106. else if(in.length() == 1)
  107. checkLetter(in.charAt(0));
  108. } else {
  109. System.out.println("Please enter a letter or guess the word!");
  110. }
  111. }
  112. }
  113.  
  114. public static void updateMan() {
  115. String head = " O ";
  116. String body = "|";
  117. String lArm = "\\";
  118. String rArm = "/";
  119. String lLeg = "/";
  120. String rLeg = "\\";
  121. StringBuilder man = new StringBuilder();
  122. if(attemptsLeft > 0) man.append(head+"\n");
  123. if(attemptsLeft > 2) man.append(lArm); else man.append(" ");
  124. if(attemptsLeft > 1) man.append(body); else man.append(" ");
  125. if(attemptsLeft > 3) man.append(rArm+"\n"); else man.append(" \n");
  126. if(attemptsLeft > 1) man.append(" "+body+" \n"); else man.append(" \n");
  127. if(attemptsLeft > 4) man.append(lLeg); else man.append(" ");
  128. if(attemptsLeft > 5) man.append(" "+rLeg); else man.append(" ");
  129. System.out.println(man.toString());
  130. }
  131.  
  132. public static void updateWord() {
  133. StringBuilder phrase = new StringBuilder();
  134. for(char c:phraseStatus) {
  135. phrase.append(c+" ");
  136. }
  137. System.out.println(phrase.toString());
  138. }
  139.  
  140. public static boolean checkWord(String in) {
  141. if(in.toLowerCase().equals(currentPhrase.toLowerCase())) {
  142. for(int i=0; i<currentPhrase.length(); i++) {
  143. phraseStatus.set(i, currentPhrase.charAt(i));
  144. }
  145. return true;
  146. }
  147. return false;
  148. }
  149.  
  150. public static void checkLetter(char letter) {
  151. boolean found = false;
  152. for(int i=0; i<currentPhrase.length(); i++) {
  153. if(currentPhrase.charAt(i) == letter) {
  154. phraseStatus.set(i, currentPhrase.charAt(i));
  155. found = true;
  156. }
  157. }
  158. if(!found){
  159. usedLetters.append(letter);
  160. attemptsLeft--;
  161. if(attemptsLeft == 0) lost = true;
  162. }
  163.  
  164. StringBuilder currentWord = new StringBuilder();
  165. for(char c:phraseStatus) { currentWord.append(c); }
  166. won = checkWord(currentWord.toString());
  167. }
  168.  
  169. private static void cls() { //Quick and dirty clearscreen
  170. for(int i=0;i<60;i++)
  171. System.out.println();
  172. }
  173.  
  174. private static void waitForEnter() {
  175. try {
  176. System.in.read();
  177. } catch (IOException e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement