Hangman game in Java - Complete tutorial

preview_player
Показать описание
Hangman game execution using Java - complete tutorial with code

tags-
hangman game in java
hangman in java
java hangman project
java hangman
hangman project in java
hangman java
Рекомендации по теме
Комментарии
Автор

Wonderful tutorial. I like this video and I enjoyed viewing it. Thanks.

carvelbell
Автор

i am writing the same program on netbeans.. and it is causing pronlem when i write array list(same as that of video).. i also do not know if i have to attach any other file to add alphabets... please help

gagandeepsingh
Автор

For some reason my code read only two letters ("e", "c").

SKVLL
Автор

Seems like this person plays RuneScape

Snatcher_Catcher
Автор

Nice, but my code doesn't read the letters "w", "c" and "j", I don't know why, but my code is right, right?


package com.jogoforca.main;

import java.util.ArrayList;
import java.util.Scanner;

public class Hangman {

private static Scanner s;

static public void guess (String palavra, int vidas) {

char[] campo = new char [palavra.length()];
int i = 0;
while (i<palavra.length()) {
campo[i] = '-';
if(palavra.charAt(i) == ' ') {
campo[i] = ' ';
}
i++;
}
System.out.print(campo);
System.out.println(" Vidas restantes=" +vidas);

s = new Scanner (System.in);

ArrayList<Character> l= new ArrayList<Character>();

while(vidas>0) {
char x=s.next().charAt(0); //carácteres digitados pelo usuário

if(l.contains(x)) {
System.out.println("Letra já digitada");
continue; //continua o laço while
}
l.add(x);

if(palavra.contains(x+" ")) {
for (int y=0; y<palavra.length();y++) { //este loop verificará todos os índices para o caractere
if(palavra.charAt(y)==x) { //e substituirá o caractere por "-"
campo[y]=x;
}
}
}
else {
vidas--; //diminui a vida se o caractere digitado for incorreto
}

{ //checa se o campo é igual a palavra
System.out.println(campo);
System.out.println("Você ganhou");
break;
}

System.out.println(campo);
System.out.println("Vidas restantes=" +vidas);
}

if(vidas == 0) {
System.out.println("Perdeu otário!");
}
}

public static void main(String [] args) {
String palavra = "welcome to java"; //Palavra a ser advinhada
int vidas = 5; //quantidade de tentativas
guess(palavra, vidas);
}
}


obs* I changed some words cause I speak portuguese.

SKVLL
join shbcf.ru