Created
February 26, 2019 12:44
-
-
Save MatMercer/340a71553b7cd6d7f4a60e9c5cfed889 to your computer and use it in GitHub Desktop.
Jogo da Forca
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.matbm.forca; | |
import java.util.Scanner; | |
import static java.lang.System.out; | |
public class Main { | |
public static void main(String[] args) { | |
String palavra = "otorrinolaringologista"; | |
int maxErro = 5; | |
String palavraAtual = palavra.toLowerCase(); | |
while(maxErro != 0 && palavraAtual.length() != 0) { | |
Scanner s = new Scanner(System.in); | |
out.print("Descubra a palavra: "); | |
String letra = s.next().substring(0, 1).toLowerCase(); | |
if(palavraAtual.contains(letra)) { | |
String[] faltantes = palavraAtual.split(letra); | |
palavraAtual = String.join("", faltantes); | |
out.println("Faltam " + palavraAtual.length() + " letras!"); | |
out.println(forcaBonito(palavraAtual, palavra.toLowerCase())); | |
} | |
else { | |
out.println("Você errou seu idiota."); | |
maxErro -= 1; | |
} | |
} | |
if (maxErro == 0) { | |
out.println("Você perdeu! :("); | |
} | |
else { | |
out.println("Você ganhou! :D"); | |
} | |
out.println("A palavra era: " + palavra); | |
} | |
static String forcaBonito(String faltantes, String palavra) { | |
StringBuilder forca = new StringBuilder(); | |
for (String charPal : palavra.split("")) { | |
if(faltantes.contains(charPal)) { | |
forca.append("_"); | |
} | |
else { | |
forca.append(charPal); | |
} | |
} | |
return forca.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment