El penjat
Submit solution
Points:
10
Time limit:
5.0s
Memory limit:
256M
Author:
Problem types
Allowed languages
C#, Go, Java, Python
A partir del següent codi, crea la classe Penjat
:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
// escriu el codi aqui
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.ENGLISH);
Ahorcado ahorcado = new Ahorcado(scanner.next());
while(ahorcado.fallos < 6 && !ahorcado.completado){
for (int i = 0; i < ahorcado.descubierta.length; i++) {
System.out.print(" " + ahorcado.descubierta[i] + " ");
}
System.out.println();
char letra = scanner.next().charAt(0);
ahorcado.completado = true;
boolean estaLaLetra = false;
for (int i = 0; i < ahorcado.palabra.length(); i++) {
if(ahorcado.palabra.charAt(i) == letra){
ahorcado.descubierta[i] = letra;
estaLaLetra = true;
}
if(ahorcado.descubierta[i] == '_'){
ahorcado.completado = false;
}
}
if(!estaLaLetra){
ahorcado.fallos++;
}
if(ahorcado.fallos == 5) System.out.println("\n __ \n | | \n | 0 \n | /|\\ \n | / \n |____ \n");
else if(ahorcado.fallos == 4) System.out.println("\n __ \n | | \n | 0 \n | /|\\ \n | \n |____ \n");
else if(ahorcado.fallos == 3) System.out.println("\n __ \n | | \n | 0 \n | /| \n | \n |____ \n");
else if(ahorcado.fallos == 2) System.out.println("\n __ \n | | \n | 0 \n | / \n | \n |____ \n");
else if(ahorcado.fallos == 1) System.out.println("\n __ \n | | \n | 0 \n | \n | \n |____ \n");
else if(ahorcado.fallos == 0) System.out.println("\n __ \n | | \n | \n | \n | \n |____ \n");
}
if(ahorcado.fallos == 6) System.out.println("\n __ \n | | \n | 0 \n | /|\\ \n | / \\ \n |____ \n");
else System.out.println("YOU WIN");
}
}
Input Format
-
Constraints
-
Output Format
-
Sample Input 0
hola
h
o
l
a
Sample Output 0
_ _ _ _
__
| |
|
|
|
|____
h _ _ _
__
| |
|
|
|
|____
h o _ _
__
| |
|
|
|
|____
h o l _
__
| |
|
|
|
|____
YOU WIN
Sample Input 1
java
j
a
v
Sample Output 1
_ _ _ _
__
| |
|
|
|
|____
j _ _ _
__
| |
|
|
|
|____
j a _ a
__
| |
|
|
|
|____
YOU WIN
Sample Input 2
constructor
c o n s t r u
Sample Output 2
_ _ _ _ _ _ _ _ _ _ _
__
| |
|
|
|
|____
c _ _ _ _ _ _ c _ _ _
__
| |
|
|
|
|____
c o _ _ _ _ _ c _ o _
__
| |
|
|
|
|____
c o n _ _ _ _ c _ o _
__
| |
|
|
|
|____
c o n s _ _ _ c _ o _
__
| |
|
|
|
|____
c o n s t _ _ c t o _
__
| |
|
|
|
|____
c o n s t r _ c t o r
__
| |
|
|
|
|____
YOU WIN
Sample Input 3
classe
c l a e z x v b n m
Sample Output 3
_ _ _ _ _ _
__
| |
|
|
|
|____
c _ _ _ _ _
__
| |
|
|
|
|____
c l _ _ _ _
__
| |
|
|
|
|____
c l a _ _ _
__
| |
|
|
|
|____
c l a _ _ e
__
| |
| 0
|
|
|____
c l a _ _ e
__
| |
| 0
| /
|
|____
c l a _ _ e
__
| |
| 0
| /|
|
|____
c l a _ _ e
__
| |
| 0
| /|\
|
|____
c l a _ _ e
__
| |
| 0
| /|\
| /
|____
c l a _ _ e
__
| |
| 0
| /|\
| / \
|____
Autoria: Gerard Falcó
CC BY-NC-SA 4.0