Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. package task1_hangman;
  7.  
  8. import java.util.*; // Random
  9. import java.io.*; // Lectura de datos - excepción
  10.  
  11. /*
  12. * Hangman.java
  13. */
  14. /**
  15. * Class that executes the hangman game, that consist of trying to guess a word,
  16. * with a maximum number of errors.
  17. * @version 2.0
  18. * @author Eva
  19. */
  20. public class Hangman {
  21. public static Scanner uin = new Scanner(System.in);
  22.  
  23. //************************** CONSTANTS ****************************//
  24. /** Maximum number of errors allowed*/
  25. public static final int INUNMAXERR = 8;
  26. /** Possible words */
  27. public static final String[] ASWORDS = {"hello", "ugly", "monster","head"};
  28. //** I create a space to better visualize the doll
  29. public static final String AWHITESPACE = (" ");
  30.  
  31. public char[] userWord;
  32.  
  33. /**
  34. * Main method of input to our game.
  35. * @param args
  36. */
  37. public static void main(String[] args)throws IOException{
  38. //I define an object of my own class to call non-static methods.
  39. Hangman ahorcado = new Hangman();
  40. //Returns array of chars as word solution.
  41. char[] palabra = ahorcado.randomWordSearch();
  42. //Returns array of chars as user word.
  43. ahorcado.userWord = ahorcado.createUserWord(palabra);
  44. //Show: welcome to the game.
  45. ahorcado.welcomeGameShow();
  46. //Initialize the number of errors and successes to zero.
  47. int numError = 0;
  48. int success = 0;
  49. int numSuccess = 0;
  50. char car;
  51.  
  52. do{
  53. //I show the word to the user.
  54. ahorcado.wordUserDisplay(ahorcado.userWord);
  55. //I ask the letter to the user.
  56. car = ahorcado.requestLetter();
  57. //I check if the letter is in the word solution and keep the number of successes in this round.
  58. numSuccess = ahorcado.comprobarLetra(car, palabra, ahorcado.userWord);
  59.  
  60. //Increase the number of successes in this round to the general successes of the game.
  61. //If the successes in this round are zero.
  62. if(numSuccess == 0){
  63. //Increase the number of general errors.
  64. numError++;
  65. //Show the number of oportunities.
  66. System.out.println("Te quedan " + (INUNMAXERR - numError) + " oportunidades más");
  67. //Show the picture of a hangman.
  68. ahorcado.showDrawing(numError);
  69. }else{
  70. success += numSuccess;
  71. }
  72.  
  73.  
  74. //It is repeated while remaining errors and the number of successes does not match the length of the solution.
  75. }while(numError < INUNMAXERR && success < palabra.length );
  76.  
  77. //If we left it is because the number of successes matches the length of the solution.
  78. if(success == ahorcado.userWord.length){
  79. //Show the congratulatory message.
  80. ahorcado.wordUserDisplay(ahorcado.userWord);
  81. ahorcado.showCongratulatoryMessage();
  82. }else{
  83. System.out.print("La palabra era: ");
  84. ahorcado.wordUserDisplay(palabra);
  85. ahorcado.showGoodbyeMessage();
  86. }
  87. System.exit(0);
  88.  
  89. //We leave the game.
  90.  
  91. }
  92.  
  93. /**
  94. * Returns an array of chars with a random word.
  95. * @return char[] with the random word found.
  96. */
  97. public char[] randomWordSearch(){
  98. //First I select a random number from 0 to ASWORDS-1
  99. Random rnd = new Random ();
  100. int randomNum = rnd.nextInt(3 - 0 + 1) + 0;
  101. return ASWORDS[randomNum].toCharArray();
  102. }
  103.  
  104. /**
  105. * Returns a new array of chars with so many
  106. * hyphens as letters have the input array.
  107. * @param inacWord
  108. * @return acWordUsu
  109. */
  110. public char[] createUserWord(char[] inacWord){
  111. char[] a = new char[inacWord.length];
  112. for (int i = 0; i < inacWord.length; i++) {
  113. a[i] = '-';
  114. }
  115. return a;
  116. }
  117.  
  118. /**
  119. * Method that checks whether the letter entered is in the array introduced.
  120. * If there is such a letter, changes in the user word the hiphen for the letter
  121. * in the position found.
  122. * @param incLetter
  123. * @param inacWordSolution
  124. * @param inacWordUsu
  125. * @return el número de letras acertadas.
  126. */
  127. public int comprobarLetra(char incLetter,char[] inacWordSolution, char[] inacWordUsu){
  128. int iNumHits = 0;
  129. //I check that the letter does not exist in the word of the user.
  130. for (int i = 0; i < inacWordSolution.length; i++) {
  131. if(incLetter == inacWordUsu[i]){
  132. iNumHits++;
  133. }
  134. }
  135. if(iNumHits != 0){
  136. System.out.println("Ya has introducido esta letra");
  137. return 0;
  138. }
  139. //If you have not found in the user's word, then we look in the solution.
  140. for (int i = 0; i < inacWordSolution.length; i++) {
  141. if(incLetter == inacWordSolution[i]){
  142. iNumHits++;
  143. inacWordUsu[i] = incLetter;
  144. }
  145. }
  146. if(iNumHits != 0){
  147. System.out.println("Existen " + iNumHits + " letras en la palabra");
  148. return iNumHits;
  149. }else{
  150. System.out.println("No existe esta letra en la palabra");
  151. return 0;
  152. }
  153. }
  154.  
  155. /**
  156. * Method shows welcome screen.
  157. */
  158. public void welcomeGameShow(){
  159. System.out.println("Bienvenido al Ahorcado");
  160. }
  161.  
  162. /**
  163. * Method shows goodby screen.
  164. */
  165. public void showGoodbyeMessage(){
  166. System.out.println("Adios. Vuelve pronto");
  167. }
  168.  
  169. /**
  170. * Solicita una letra al usuario
  171. * @return char with the letter specified by the user.
  172. */
  173. public char requestLetter()throws IOException{
  174. System.out.println("Introduce una letra: ");
  175. return uin.next().charAt(0);
  176. }
  177.  
  178. /**
  179. * Show the picture depending on the errors that come as a parameter
  180. * @param iniNumErr
  181. */
  182. public void showDrawing(int iniNumErr){
  183. switch(iniNumErr){
  184. case 1:
  185. System.out.print(AWHITESPACE + " ___\n");
  186. break;
  187. case 2:
  188. System.out.print(AWHITESPACE + " ___\n" +
  189. AWHITESPACE + " | \n");
  190. break;
  191. case 3:
  192. System.out.print(AWHITESPACE + " ___\n" +
  193. AWHITESPACE + " | \n" +
  194. AWHITESPACE + " O \n");
  195. break;
  196. case 4:
  197. System.out.print(AWHITESPACE + " ___\n" +
  198. AWHITESPACE + " | \n" +
  199. AWHITESPACE + " O \n" +
  200. AWHITESPACE + "- \n");
  201. break;
  202. case 5:
  203. System.out.print(AWHITESPACE + " ___\n" +
  204. AWHITESPACE + " | \n" +
  205. AWHITESPACE + " O \n" +
  206. AWHITESPACE + "- -\n");
  207. break;
  208. case 6:
  209. System.out.print(AWHITESPACE + " ___\n" +
  210. AWHITESPACE + " | \n" +
  211. AWHITESPACE + " O \n" +
  212. AWHITESPACE + "- -\n" +
  213. AWHITESPACE + " | \n");
  214. break;
  215. case 7:
  216. System.out.print(AWHITESPACE + " ___\n" +
  217. AWHITESPACE + " | \n" +
  218. AWHITESPACE + " O \n" +
  219. AWHITESPACE + "- -\n" +
  220. AWHITESPACE + " | \n" +
  221. AWHITESPACE + "/ \n");
  222. break;
  223. case 8:
  224. System.out.print(AWHITESPACE + " ___\n" +
  225. AWHITESPACE + " | \n" +
  226. AWHITESPACE + " O \n" +
  227. AWHITESPACE + "- -\n" +
  228. AWHITESPACE + " | \n" +
  229. AWHITESPACE + "/ \\\n");
  230. break;
  231. default:
  232. System.out.println("Fatal Error");
  233. System.exit(0);
  234. break;
  235. }
  236. }
  237.  
  238. /**
  239. * Shows the array of char introduced as input.
  240. * @param inacharWord
  241. */
  242. public void wordUserDisplay(char[] inacharWord){
  243. System.out.println(String.valueOf(inacharWord));
  244. }
  245.  
  246. /**
  247. * Shows the congratulatory message if you have completely matched.
  248. */
  249. public void showCongratulatoryMessage(){
  250. System.out.println("Enhorabuena, has ganado. Te estaremos esperando");
  251. }
  252.  
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement