commit eb27d16d750ddd9acf76175c110b77e9a8a990c3 Author: 250420 <250420@localhost> Date: Mon Apr 27 17:14:59 2026 +0100 commit exercicio aula diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d051850 --- /dev/null +++ b/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + com.mycompany + JogoLpProjetoCompleto + 1.0-SNAPSHOT + jar + + UTF-8 + 24 + com.mycompany.jogolpprojetocompleto.JogoLpProjetoCompleto + + \ No newline at end of file diff --git a/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java b/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java new file mode 100644 index 0000000..fbccc08 --- /dev/null +++ b/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java @@ -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(); + } +} + +