Advertisement
andreystar

Untitled

Feb 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. package ru.ioffe.andreystar.hangman;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class HangmanConsoleView {
  7. HangmanModel model;
  8. static Scanner in = new Scanner(System.in);
  9. static String lang;
  10.  
  11. public HangmanConsoleView(HangmanModel model) {
  12. this.model = model;
  13. }
  14.  
  15. public static char getLetter() {
  16. if (lang.equals("ru")) {
  17. System.out.println("Введите одну букву: ");
  18. } else {
  19. System.out.println("Type in one letter: ");
  20. }
  21. return in.next().charAt(0);
  22. }
  23.  
  24. public static void userView() {
  25. lang = HangmanController.lang;
  26. if (lang.equals("ru")) {
  27. System.out.println("Ваше слово: " + HangmanModel.getUserView());
  28. } else {
  29. System.out.println("Your word: " + HangmanModel.getUserView());
  30. }
  31. System.out.println();
  32. }
  33.  
  34. public static String lang() {
  35. System.out.println("Choose language: ");
  36. return in.next();
  37. }
  38.  
  39. public static void info() {
  40. System.out.println();
  41. userView();
  42. if (lang.equals("ru")) {
  43. System.out.println("Ошибки: " + HangmanModel.getMistakes());
  44. System.out.print("Неподходящие буквы: ");
  45. } else {
  46. System.out.println("Mistakes: " + HangmanModel.getMistakes());
  47. System.out.print("Incorrect guesses: ");
  48. }
  49. if (HangmanModel.getMistakes() == 0) {
  50. if (lang.equals("ru")) {
  51. System.out.println("Нет");
  52. } else {
  53. System.out.println("none");
  54. }
  55. } else {
  56. String mistakes = HangmanModel.getMissedLetters();
  57. char[] mistakesArray = new char[mistakes.length()];
  58. for (int i = 0; i < mistakesArray.length; i++) {
  59. mistakesArray[i] = mistakes.charAt(i);
  60. }
  61. for (int i = 0; i < mistakesArray.length; i++) {
  62. Arrays.sort(mistakesArray);
  63. if (i + 1 < mistakesArray.length) {
  64. System.out.print(mistakesArray[i] + ", ");
  65. } else {
  66. System.out.print(mistakesArray[i]);
  67. }
  68. }
  69. System.out.println();
  70. System.out.println();
  71. }
  72. }
  73.  
  74. public static void victory() {
  75. if (lang.equals("ru")) {
  76. System.out.println("Победа!!!");
  77. } else {
  78. System.out.println("You Won!!!");
  79. }
  80. System.out.println(HangmanModel.getUserView());
  81. }
  82.  
  83. public static void usedLetter() {
  84. if (lang.equals("ru")) {
  85. System.out.println("Вы уже пробовали эту букву! Введите другую. ");
  86. } else {
  87. System.out.println("You already tried this letter! Try another one. ");
  88. }
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. package ru.ioffe.andreystar.hangman;
  101.  
  102. public class HangmanController {
  103.  
  104. HangmanModel model;
  105. HangmanConsoleView consoleView;
  106. static int go = 0;
  107. public static String lang;
  108.  
  109. public HangmanController(HangmanModel model, HangmanConsoleView consoleView) {
  110. this.model = model;
  111. this.consoleView = consoleView;
  112. }
  113.  
  114. public void run() {
  115. while (!model.isVictorius()) {
  116. go++;
  117. if (go == 1) {
  118. HangmanConsoleView.userView();
  119. //System.out.println(HangmanModel.word);
  120. } else {
  121. HangmanConsoleView.info();
  122. }
  123. char letter = HangmanConsoleView.getLetter();
  124. while(HangmanModel.wasLetterUsed(letter)) {
  125. HangmanConsoleView.usedLetter();
  126. letter = HangmanConsoleView.getLetter();
  127. }
  128. HangmanModel.makeMove(letter);
  129. }
  130. HangmanConsoleView.info();
  131. HangmanConsoleView.victory();
  132. }
  133.  
  134. public static void main(String[] args) throws Exception {
  135. lang = HangmanConsoleView.lang();
  136. System.out.println(lang);
  137. HangmanModel model = new HangmanModel();
  138. HangmanConsoleView consoleView = new HangmanConsoleView(model);
  139. HangmanController controller = new HangmanController(model, consoleView);
  140. controller.run();
  141. }
  142.  
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. package ru.ioffe.andreystar.hangman;
  160.  
  161. import java.util.ArrayList;
  162.  
  163. public class HangmanModel {
  164. public static String word;
  165. static ArrayList<Character> guessesList;
  166.  
  167. public HangmanModel() throws Exception {
  168. if (HangmanController.lang.equals("ru")) {
  169. word = WordBase.generateRuWord().toUpperCase();
  170. } else {
  171. word = WordBase.generateEnWord().toUpperCase();
  172. }
  173. guessesList = new ArrayList<>();
  174. }
  175.  
  176. public static void makeMove(char letter) {
  177. letter = ("" + letter).toUpperCase().charAt(0);
  178. guessesList.add(letter);
  179. }
  180.  
  181. public static String getUserView() {
  182. String userViewStr = "";
  183.  
  184. for (int i = 0; i < word.length(); i++) {
  185. if (guessesList.contains(word.charAt(i))) {
  186. userViewStr += word.charAt(i) + " ";
  187. } else {
  188. userViewStr += "_ ";
  189. }
  190. }
  191.  
  192. return userViewStr;
  193. }
  194.  
  195. public static String getMissedLetters() {
  196.  
  197. String missed = "";
  198. mark: for (int i = 0; i < guessesList.size(); i++) {
  199. char curLetter = guessesList.get(i);
  200. for (int j = 0; j < word.length(); j++) {
  201. if (word.charAt(j) == curLetter) {
  202. continue mark;
  203. } else {
  204. continue;
  205. }
  206. }
  207. missed += curLetter;
  208. }
  209. return missed;
  210. }
  211.  
  212. public static int getMistakes() {
  213. return getMissedLetters().length();
  214. }
  215.  
  216. public static boolean wasLetterUsed(char letter) {
  217. letter = ("" + letter).toUpperCase().charAt(0);
  218. if (!guessesList.contains(letter)) {
  219. return false;
  220. } else {
  221. return true;
  222. }
  223. }
  224.  
  225. public boolean isVictorius() {
  226. for (int i = 0; i < word.length(); i++) {
  227. if (!guessesList.contains(word.charAt(i))) {
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. package ru.ioffe.andreystar.hangman;
  248.  
  249. import java.io.File;
  250. import java.util.Random;
  251. import java.util.Scanner;
  252.  
  253. public class WordBase {
  254. public static String generateEnWord () throws Exception {
  255. Random random = new Random();
  256. Scanner in = new Scanner(new File("enwordbase.txt"));
  257. int n = random.nextInt(4308) + 1;
  258. String word = "world";
  259. for (int i = 0; i < n; i++) {
  260. word = in.next();
  261. }
  262. in.close();
  263. return word;
  264. }
  265. public static String generateRuWord () throws Exception {
  266. Random random = new Random();
  267. Scanner in = new Scanner(new File("enwordbase.txt"));
  268. int n = random.nextInt(4308) + 1;
  269. String word = "world";
  270. for (int i = 0; i < n; i++) {
  271. word = in.next();
  272. }
  273. in.close();
  274. return word;
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement