From 4859ca101108f9179525a43bb67ccf14e2bcf939 Mon Sep 17 00:00:00 2001 From: 250420 <250420@localhost> Date: Mon, 8 Jun 2026 11:47:13 +0100 Subject: [PATCH] commit exercicio aula --- dificil.txt | 4 + facil.txt | 4 + medio.txt | 4 + .../JogoLpProjetoCompleto.java | 168 +++++++++++++++--- 4 files changed, 159 insertions(+), 21 deletions(-) create mode 100644 dificil.txt create mode 100644 facil.txt create mode 100644 medio.txt diff --git a/dificil.txt b/dificil.txt new file mode 100644 index 0000000..2bdae9d --- /dev/null +++ b/dificil.txt @@ -0,0 +1,4 @@ +programacao +desenvolvimento +algoritmo +estrutura diff --git a/facil.txt b/facil.txt new file mode 100644 index 0000000..f08a590 --- /dev/null +++ b/facil.txt @@ -0,0 +1,4 @@ +java +jogo +bola +gato diff --git a/medio.txt b/medio.txt new file mode 100644 index 0000000..636fac7 --- /dev/null +++ b/medio.txt @@ -0,0 +1,4 @@ +computador +programa +teclado +monitor diff --git a/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java b/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java index fbccc08..7f8a066 100644 --- a/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java +++ b/src/main/java/com/mycompany/jogolpprojetocompleto/JogoLpProjetoCompleto.java @@ -4,6 +4,11 @@ package com.mycompany.jogolpprojetocompleto; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; import java.util.Scanner; /** @@ -12,35 +17,95 @@ import java.util.Scanner; */ public class JogoLpProjetoCompleto { + + public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); + + // Criar os ficheiros automaticamente se não existirem + try { + + File facil = new File("facil.txt"); + if (!facil.exists()) { + PrintWriter pw = new PrintWriter(facil); + pw.println("java"); + pw.println("jogo"); + pw.println("bola"); + pw.println("gato"); + pw.close(); + } + + File medio = new File("medio.txt"); + if (!medio.exists()) { + PrintWriter pw = new PrintWriter(medio); + pw.println("computador"); + pw.println("programa"); + pw.println("teclado"); + pw.println("monitor"); + pw.close(); + } + + File dificil = new File("dificil.txt"); + if (!dificil.exists()) { + PrintWriter pw = new PrintWriter(dificil); + pw.println("programacao"); + pw.println("desenvolvimento"); + pw.println("algoritmo"); + pw.println("estrutura"); + pw.close(); + } + + } catch (IOException e) { + System.out.println("Erro ao criar os ficheiros."); + return; + } + + Scanner scanner = new Scanner(System.in); char jogarNovamente; do { - System.out.println("=== JOGO DA FORCA ==="); - // escolher dificuldade + System.out.println("\n=== JOGO DA FORCA ==="); 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; + String nomeFicheiro; if (opcao == 1) { - palavra = palavrasFaceis[(int)(Math.random() * palavrasFaceis.length)]; + nomeFicheiro = "facil.txt"; } else if (opcao == 2) { - palavra = palavrasMedias[(int)(Math.random() * palavrasMedias.length)]; + nomeFicheiro = "medio.txt"; } else { - palavra = palavrasDificeis[(int)(Math.random() * palavrasDificeis.length)]; + nomeFicheiro = "dificil.txt"; } + ArrayList palavras = new ArrayList<>(); + + try { + File ficheiro = new File(nomeFicheiro); + Scanner leitor = new Scanner(ficheiro); + + while (leitor.hasNextLine()) { + palavras.add(leitor.nextLine()); + } + + leitor.close(); + + } catch (FileNotFoundException e) { + System.out.println("Erro: ficheiro não encontrado."); + scanner.close(); + return; + } + + String palavra = palavras.get( + (int) (Math.random() * palavras.size()) + ); + char[] descoberta = new char[palavra.length()]; + for (int i = 0; i < descoberta.length; i++) { descoberta[i] = '_'; } @@ -62,15 +127,14 @@ public class JogoLpProjetoCompleto { System.out.println("\nLetras usadas: " + usadas); System.out.print("Letra: "); - char letra = scanner.next().charAt(0); + char letra = scanner.next().toLowerCase().charAt(0); - // verificar repetição if (usadas.contains(String.valueOf(letra))) { System.out.println("Já usaste essa letra!"); continue; } - usadas += letra; + usadas += letra + " "; boolean acertou = false; @@ -86,31 +150,93 @@ public class JogoLpProjetoCompleto { System.out.println("Erraste!"); } - // verificar vitória ganhou = true; + for (char c : descoberta) { if (c == '_') { ganhou = false; + break; } } } if (ganhou) { System.out.println("\nParabéns! Descobriste a palavra!"); + System.out.println("A palavra era: " + palavra); } else { - System.out.println("\nGame Over! A palavra era: " + palavra); + System.out.println("\nGame Over!"); + System.out.println("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!"); - + System.out.println("\nObrigado por jogar!"); scanner.close(); } -} - +} + +// ____________ +// |____________|_ +// ||--------|| | _________ +// ||- _ || |(HA ha ha!) +// || - _ || | --------- +// || -|| | // +// || || O\ __ +// || || \\ (..) +// || || \\_| |_ +// || || \ \/ ) +// || || : :| +// || || : :| +// || || :====:O +// || || ( ) +// ||__@@@@__|| | `' | +// || @|..|@ || | || | +// ||O@`=='@O|| | || | +// ||_@\/\/@_|| |_||_| +// ---------------- '_'`_` +///________________\----------\ +//| GUILLOTINE |-----------| +//| OF CASTLE DE SADE | +//|____________________________| +//Just trying to get a head!!! + +// __.--.____________.--.__ +// ( _.------._ ). +// '._.--' || '--._.' ) +// | |================| || ( +// | || | || ) +// | || _| || ( +// | || _.-' | || ) +// | || _.-' | || ( +// | || _.-' | || ) +// | |.-' | || ( +// | || | || ) +// | || | || ( +// | || | || ) +// | || | || ( +// | || | || ) +// | || | || ( +// | .---. | || ) +// | | | | || ( +// | | | | || ) +// | | .' | || ( +// | | ' | || ) +// | | '. | || ( +// | | | | || ) +// | |O__| .))). | || ( +// | || ( O O ) | || +// | ||===._ (_) _.===| || +// | || '-.-' | || +// | || JOGO DA FORCA | || +// | || better days | || +// | || ______ | || +// | || / \ | || +// __| || (\______/) | ||_____ +// /__| ||___) |''| (____| ||____/ +// /___|_|/ (________) |_|/___/ +///_____________________________/ +