Hangman game - Java

2

I have a problem with a program I'm doing, which is about creating a Hangman Game. I used a for so that the entered character is equal to one of the characters of the word (which is chosen randomly) and a if to verify if it is correct; up there all right.

But I noticed that when it comes to using a else , when removing points, the program continues to take lives until the user loses the game without the opportunity to enter another character.

My doubt is that if it is possible to stop that for e if once you have found all the characters (that is, if a word has two "A" that stops when you find them and does not fall in%) else ).

Thanks in advance

PS: I am working with multiple classes, with one being abstract . From abstract is the method that quitaVidas , which is true while the user still has lives available.

The code in question:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Usuario
 */
public class JuegoAhorcado extends Juego {

    private String palabraR;
    private int posicionR;
    private ArrayList<String> cadenasAdivinar = new ArrayList();
    protected Random seleccionaCadenaAdivinar = new Random();

    public JuegoAhorcado(int vidas) {
        super(vidas);

        cadenasAdivinar.addAll(Arrays.asList("Tiburon", "Emblema", "Emperador",
                "Ahorcado", "Vidriera", "Elefante", "Paralelepipedo", "Romboide", "Washington",
                "Bebestible", "Kiwi", "Experiencia", "Dorado", "Tarot", "Ataraxia", "Descenso",
                "Mascara", "Caballo", "Excalibur", "Quijote"));

        int menor = 1;
        int mayor = 20;

        int resultado = seleccionaCadenaAdivinar.nextInt(mayor - menor) + menor;

        posicionR = resultado;

        palabraR = cadenasAdivinar.get(posicionR);

    }

    @Override
    public void Juega() {
        try {

            reiniciarPartida();

            char[] letras = palabraR.toCharArray();

            System.out.println("");

            System.out.println(palabraR);

            System.out.println("Adiviné la palabra: \n");

            //oculto = oculto.replaceAll("(?s).", "-");
            char[] oculto = new char[letras.length];

            for (int i = 0; i < letras.length; i++) {
                oculto[i] = '-';
            }

            Lectura Teclado = new Lectura();

            while (vidasRestantes() != 0) {

                System.out.println("");

                System.out.println(oculto);

                System.out.println("Ingrese un caracter: ");

                char letra = Teclado.leerCaracter();

                if (letras == oculto) {

                    System.out.println("\nFelicidades, has completado la palabra " + palabraR);

                    System.out.println(actualizarRecord());
                    break;

                }

                for (int i = 0; i <= letras.length - 1; i++) {
                    if (letras[i] == letra) {

                        oculto[i] = letra;

                        System.out.println("");



                    }
                    else {

                        System.out.println("\nCaracter incorrecto, has perdido una vida");

                        if (quitaVidas() == true) {

                            System.out.println("Le quedan " + vidasRestantes() + " vidas");

                        } else {
                            System.out.println("Has perdido el juego, la palabra era: " + palabraR);

                            break;
                        }

                    }
                }

            }
    
asked by blue-blaze 19.12.2018 в 02:27
source

1 answer

4

Only ... is to think a bit about your logic ..

By wanting to do everything together, is where you are complicating the game.

The idea would be:

go letter by letter
show what corresponds (as you do now)
and .. here the difference .. Not having an else .. if not having a flag that says if you found the letter or not ...

And when leaving ... check that flag .. and put the contents of the else there ...

Then .. let's see just that part of your code:

bool bandera = false;
for (int i = 0; i <= letras.length - 1; i++) {
    if (letras[i] == letra) {
        oculto[i] = letra;
        System.out.println("");
        bandera = true;
    }
}
if (!bandera) {
    System.out.println("\nCaracter incorrecto, has perdido una vida");
    if (quitaVidas() == true) {
        System.out.println("Le quedan " + vidasRestantes() + " vidas");
    } else {
        System.out.println("Has perdido el juego, la palabra era: " + palabraR);                           
}
    
answered by 19.12.2018 / 02:34
source