commit exercicio aula
This commit is contained in:
4
dificil.txt
Normal file
4
dificil.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
programacao
|
||||||
|
desenvolvimento
|
||||||
|
algoritmo
|
||||||
|
estrutura
|
||||||
@@ -4,6 +4,11 @@
|
|||||||
|
|
||||||
package com.mycompany.jogolpprojetocompleto;
|
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;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,35 +17,95 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
public class JogoLpProjetoCompleto {
|
public class JogoLpProjetoCompleto {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// 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);
|
Scanner scanner = new Scanner(System.in);
|
||||||
char jogarNovamente;
|
char jogarNovamente;
|
||||||
|
|
||||||
do {
|
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("Escolhe a dificuldade:");
|
||||||
System.out.println("1 - Fácil");
|
System.out.println("1 - Fácil");
|
||||||
System.out.println("2 - Médio");
|
System.out.println("2 - Médio");
|
||||||
System.out.println("3 - Difícil");
|
System.out.println("3 - Difícil");
|
||||||
|
|
||||||
int opcao = scanner.nextInt();
|
int opcao = scanner.nextInt();
|
||||||
|
|
||||||
String[] palavrasFaceis = {"java", "jogo", "bola", "gato"};
|
String nomeFicheiro;
|
||||||
String[] palavrasMedias = {"computador", "programa", "teclado", "monitor"};
|
|
||||||
String[] palavrasDificeis = {"programacao", "desenvolvimento", "algoritmo", "estrutura"};
|
|
||||||
|
|
||||||
String palavra;
|
|
||||||
|
|
||||||
if (opcao == 1) {
|
if (opcao == 1) {
|
||||||
palavra = palavrasFaceis[(int)(Math.random() * palavrasFaceis.length)];
|
nomeFicheiro = "facil.txt";
|
||||||
} else if (opcao == 2) {
|
} else if (opcao == 2) {
|
||||||
palavra = palavrasMedias[(int)(Math.random() * palavrasMedias.length)];
|
nomeFicheiro = "medio.txt";
|
||||||
} else {
|
} 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()];
|
char[] descoberta = new char[palavra.length()];
|
||||||
|
|
||||||
for (int i = 0; i < descoberta.length; i++) {
|
for (int i = 0; i < descoberta.length; i++) {
|
||||||
descoberta[i] = '_';
|
descoberta[i] = '_';
|
||||||
}
|
}
|
||||||
@@ -62,15 +127,14 @@ public class JogoLpProjetoCompleto {
|
|||||||
System.out.println("\nLetras usadas: " + usadas);
|
System.out.println("\nLetras usadas: " + usadas);
|
||||||
|
|
||||||
System.out.print("Letra: ");
|
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))) {
|
if (usadas.contains(String.valueOf(letra))) {
|
||||||
System.out.println("Já usaste essa letra!");
|
System.out.println("Já usaste essa letra!");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
usadas += letra;
|
usadas += letra + " ";
|
||||||
|
|
||||||
boolean acertou = false;
|
boolean acertou = false;
|
||||||
|
|
||||||
@@ -86,31 +150,93 @@ public class JogoLpProjetoCompleto {
|
|||||||
System.out.println("Erraste!");
|
System.out.println("Erraste!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// verificar vitória
|
|
||||||
ganhou = true;
|
ganhou = true;
|
||||||
|
|
||||||
for (char c : descoberta) {
|
for (char c : descoberta) {
|
||||||
if (c == '_') {
|
if (c == '_') {
|
||||||
ganhou = false;
|
ganhou = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ganhou) {
|
if (ganhou) {
|
||||||
System.out.println("\nParabéns! Descobriste a palavra!");
|
System.out.println("\nParabéns! Descobriste a palavra!");
|
||||||
|
System.out.println("A palavra era: " + palavra);
|
||||||
} else {
|
} 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): ");
|
System.out.print("\nQueres jogar outra vez? (s/n): ");
|
||||||
jogarNovamente = scanner.next().charAt(0);
|
jogarNovamente = scanner.next().charAt(0);
|
||||||
|
|
||||||
} while (jogarNovamente == 's' || jogarNovamente == 'S');
|
} while (jogarNovamente == 's' || jogarNovamente == 'S');
|
||||||
|
|
||||||
System.out.println("Obrigado por jogar!");
|
System.out.println("\nObrigado por jogar!");
|
||||||
|
|
||||||
scanner.close();
|
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 | ||
|
||||||
|
// | || ______ | ||
|
||||||
|
// | || / \ | ||
|
||||||
|
// __| || (\______/) | ||_____
|
||||||
|
// /__| ||___) |''| (____| ||____/
|
||||||
|
// /___|_|/ (________) |_|/___/
|
||||||
|
///_____________________________/
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user