commit exercicio aula

This commit is contained in:
250420
2026-06-08 11:47:13 +01:00
parent eb27d16d75
commit 4859ca1011
4 changed files with 159 additions and 21 deletions

4
dificil.txt Normal file
View File

@@ -0,0 +1,4 @@
programacao
desenvolvimento
algoritmo
estrutura

4
facil.txt Normal file
View File

@@ -0,0 +1,4 @@
java
jogo
bola
gato

4
medio.txt Normal file
View File

@@ -0,0 +1,4 @@
computador
programa
teclado
monitor

View File

@@ -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<String> 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 | ||
// | || ______ | ||
// | || / \ | ||
// __| || (\______/) | ||_____
// /__| ||___) |''| (____| ||____/
// /___|_|/ (________) |_|/___/
///_____________________________/