commit exercicio aula
This commit is contained in:
13
pom.xml
Normal file
13
pom.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.mycompany</groupId>
|
||||
<artifactId>JogoLpProjetoCompleto</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.release>24</maven.compiler.release>
|
||||
<exec.mainClass>com.mycompany.jogolpprojetocompleto.JogoLpProjetoCompleto</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.mycompany.jogolpprojetocompleto;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 250420
|
||||
*/
|
||||
public class JogoLpProjetoCompleto {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
char jogarNovamente;
|
||||
|
||||
do {
|
||||
System.out.println("=== JOGO DA FORCA ===");
|
||||
|
||||
// escolher dificuldade
|
||||
System.out.println("Escolhe a dificuldade:");
|
||||
System.out.println("1 - Fácil");
|
||||
System.out.println("2 - Médio");
|
||||
System.out.println("3 - Difícil");
|
||||
int opcao = scanner.nextInt();
|
||||
|
||||
String[] palavrasFaceis = {"java", "jogo", "bola", "gato"};
|
||||
String[] palavrasMedias = {"computador", "programa", "teclado", "monitor"};
|
||||
String[] palavrasDificeis = {"programacao", "desenvolvimento", "algoritmo", "estrutura"};
|
||||
|
||||
String palavra;
|
||||
|
||||
if (opcao == 1) {
|
||||
palavra = palavrasFaceis[(int)(Math.random() * palavrasFaceis.length)];
|
||||
} else if (opcao == 2) {
|
||||
palavra = palavrasMedias[(int)(Math.random() * palavrasMedias.length)];
|
||||
} else {
|
||||
palavra = palavrasDificeis[(int)(Math.random() * palavrasDificeis.length)];
|
||||
}
|
||||
|
||||
char[] descoberta = new char[palavra.length()];
|
||||
for (int i = 0; i < descoberta.length; i++) {
|
||||
descoberta[i] = '_';
|
||||
}
|
||||
|
||||
int tentativas = 6;
|
||||
boolean ganhou = false;
|
||||
String usadas = "";
|
||||
|
||||
while (tentativas > 0 && !ganhou) {
|
||||
|
||||
System.out.println("\n------------------");
|
||||
System.out.println("Tentativas restantes: " + tentativas);
|
||||
|
||||
System.out.print("Palavra: ");
|
||||
for (char c : descoberta) {
|
||||
System.out.print(c + " ");
|
||||
}
|
||||
|
||||
System.out.println("\nLetras usadas: " + usadas);
|
||||
|
||||
System.out.print("Letra: ");
|
||||
char letra = scanner.next().charAt(0);
|
||||
|
||||
// verificar repetição
|
||||
if (usadas.contains(String.valueOf(letra))) {
|
||||
System.out.println("Já usaste essa letra!");
|
||||
continue;
|
||||
}
|
||||
|
||||
usadas += letra;
|
||||
|
||||
boolean acertou = false;
|
||||
|
||||
for (int i = 0; i < palavra.length(); i++) {
|
||||
if (palavra.charAt(i) == letra) {
|
||||
descoberta[i] = letra;
|
||||
acertou = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!acertou) {
|
||||
tentativas--;
|
||||
System.out.println("Erraste!");
|
||||
}
|
||||
|
||||
// verificar vitória
|
||||
ganhou = true;
|
||||
for (char c : descoberta) {
|
||||
if (c == '_') {
|
||||
ganhou = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ganhou) {
|
||||
System.out.println("\nParabéns! Descobriste a palavra!");
|
||||
} else {
|
||||
System.out.println("\nGame Over! A palavra era: " + palavra);
|
||||
}
|
||||
|
||||
// jogar novamente
|
||||
System.out.print("\nQueres jogar outra vez? (s/n): ");
|
||||
jogarNovamente = scanner.next().charAt(0);
|
||||
|
||||
} while (jogarNovamente == 's' || jogarNovamente == 'S');
|
||||
|
||||
System.out.println("Obrigado por jogar!");
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user