0% found this document useful (0 votes)
30 views2 pages

@author Bryan /: Import

This document contains code for a Hangman game written in Java. It defines classes and methods to: 1) Store an array of words to guess, number of lives, and characters guessed so far. 2) Get user input for a letter guess and validate it is a single character. 3) Check if the guessed letter is in the word, update the display if correct, otherwise decrement a life. 4) Continue gameplay until all letters are guessed or lives reach 0, then ask to play again or quit.

Uploaded by

Bryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

@author Bryan /: Import

This document contains code for a Hangman game written in Java. It defines classes and methods to: 1) Store an array of words to guess, number of lives, and characters guessed so far. 2) Get user input for a letter guess and validate it is a single character. 3) Check if the guessed letter is in the word, update the display if correct, otherwise decrement a life. 4) Continue gameplay until all letters are guessed or lives reach 0, then ask to play again or quit.

Uploaded by

Bryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 package menu;

2
3 import java.util.Scanner;
4
5 /**
6 *
7 * @author Bryan
8 */
9 public class ahorcado1 {
10
11 static int vidas,a,i,aciertos,intentos,opcion2;
12 static String []
palabras={"carro","casa","java","perro","gato","ingeniero","ahorcado","juego","proyec
to","pollo"};
13 static String []
corazones={"","\u2665","\u2665\u2665","\u2665\u2665\u2665","\u2665\u2665\u2665\u2665"
,"\u2665\u2665\u2665\u2665\u2665"};
14 static String letraintroducida;
15
16
17 public static String entrada() {
18 Scanner t= new Scanner(System.in);
19 boolean done=false;
20 while (!done) {
21 try {
22 letraintroducida=t.nextLine().toLowerCase();
23 done=true;
24 }
25 catch(Exception e) {
26 System.out.println("ERROR! Input not an integer!");
27 t.nextLine(); //
28 }
29 }
30 return letraintroducida;
31 }
32
33 public static int opcion() {
34 Scanner x= new Scanner(System.in);
35 boolean opcion1=false;
36 opcion2=0;
37 String opcion;
38 while(!opcion1)
39 {
40 System.out.println("¿Deseas seguir jugando? Si/No");
41 opcion=x.nextLine();
42 if(opcion.contains("Si")){
43 opcion1=true;
44 opcion2=1;
45 }
46 if(opcion.contains("No")){
47 opcion2=2;
48 opcion1=true;
49 }
50 }
51 return opcion2;
52 }
53
54
55
56
57 public ahorcado1() {
58
59 while(true)
60 {
61
62 int palabra= (int)(Math.random()*9);
63 char [] pseleccionada = palabras[palabra].toCharArray();
64 char letra;
65 char []posicion = new char[palabras[palabra].length()];
66 vidas=5;
67 aciertos=0;
68 for (i=0;i<palabras[palabra].length();i++){
69 posicion[i]='*';
70 }
71 while(vidas!=0)
72 {
73 System.out.println("Introduzca la letra: "+"Vidas: "+corazones[vidas]);
74 for (i=0;i<palabras[palabra].length();i++)
75 System.out.print(posicion[i]);
76 System.out.println("");
77 entrada();
78
79 try{
80 letra= letraintroducida.toCharArray()[0];
81 if(palabras[palabra].contains(letraintroducida))
82 {
83 for(i=0;i<pseleccionada.length;i++)
84 {
85 if(pseleccionada[i]==letra) // if(letra==pseleccionada[i])
86 {
87 posicion[i]=letra;
88 aciertos++;
89 }
90 }
91 }else{
92 vidas--;
93 }
94 if(aciertos==pseleccionada.length)
95 {
96 System.out.println("Ganaste, la palabra era: "+palabras[palabra]);
97 System.out.println("");
98 break;
99 }
100 if(vidas==0)
101 System.out.println("Perdiste, la palabra era: "+palabras[palabra]);
System.out.println("");
102 }catch(Exception e){
103 entrada();
104 }
105 }
106 opcion();
107 if(opcion2==2){
108 new menu1();
109 break;
110 }
111 }
112
113 }
114 }

You might also like