commit 9d57dddefa60c90ab67fb6314205d25e388f50c4 Author: 250406 <250406@mac.epvc2.local> Date: Mon May 11 11:52:13 2026 +0100 exercicio 15 diff --git a/src/main/java/com/mycompany/experientia/Experientia.java b/src/main/java/com/mycompany/experientia/Experientia.java new file mode 100644 index 0000000..304604c --- /dev/null +++ b/src/main/java/com/mycompany/experientia/Experientia.java @@ -0,0 +1,975 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + */ +package com.mycompany.experientia; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Scanner; +import java.util.Random; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author 250406 + */ +public class Experientia { + + Scanner scanner = new Scanner(System.in); + Random rand = new Random(); + + String nomeJogador; + String classe; + + int nivelJogador = 1; + int xpJogador = 0; + int ouro = 0; + + int vidaMax = 100; + int vidaAtual = 100; + + int ataqueBase = 10; + int defesaBase = 5; + + int bonusAtaque = 0; + int bonusDefesa = 0; + + int pontosStatus = 0; + + int refreshLojaRestantes = 3; // limite de refresh por luta + + Item[] inventario = new Item[20]; + int itensCount = 0; + + Item[] loja = new Item[3]; + String password; + boolean loggedIn = false; + boolean bossFinalDerrotado = false; + boolean admin = false; + + public static void main(String[] args) throws IOException { + new Experientia().iniciar(); + } + + public void iniciar() throws IOException { + + System.out.println("===== LOGIN ====="); + System.out.println("1-Novo jogo"); + System.out.println("2-Carregar jogo"); + System.out.println("3-Login Admin"); + + int op = scanner.nextInt(); + + if (op == 1) { + criarPersonagem(); + } else if (op == 2) { + carregarJogo(); + } else if (op == 3) { + loginAdmin(); + } + + menu(); + } + + public void criarPersonagem() { + + System.out.print("Nome: "); + nomeJogador = scanner.next(); + + if (nomeJogador.equalsIgnoreCase("admin")) { + + admin = true; + + System.out.println("MODO ADMIN ATIVADO"); + + nivelJogador = 100; + xpJogador = 0; + ouro = 999999; + + vidaMax = 9999; + vidaAtual = vidaMax; + + ataqueBase = 999; + defesaBase = 999; + + bonusAtaque = 999; + bonusDefesa = 999; + + pontosStatus = 999; + + itensCount = 0; + + inventario = new Item[50]; + + return; + } + + System.out.println("Classe:"); + System.out.println("1-Guerreiro 2-Mago 3-Ladrão 4-Paladino 5-Assassino 6-Arqueiro"); + + int c = scanner.nextInt(); + + switch (c) { + case 1 -> { + classe = "Guerreiro"; + ataqueBase += 2; + defesaBase += 3; + } + case 2 -> { + classe = "Mago"; + ataqueBase += 5; + } + case 3 -> { + classe = "Ladrão"; + } + case 4 -> { + classe = "Paladino"; + defesaBase += 4; + vidaMax += 20; + vidaAtual = vidaMax; + } + case 5 -> { + classe = "Assassino"; + ataqueBase += 3; + } + case 6 -> { + classe = "Arqueiro"; + ataqueBase += 2; + } + } + } + + public void menu() throws IOException { + + while (true) { + + System.out.println("\n--------------------"); + System.out.println(nomeJogador + " Nv " + nivelJogador); + System.out.println("HP: " + vidaAtual + "/" + vidaMax); + System.out.println("Ouro: " + ouro); + System.out.println("--------------------"); + + System.out.println("1-Procurar inimigos"); + System.out.println("2-Loja"); + System.out.println("3-Inventário"); + System.out.println("4-Status"); + System.out.println("5-Sair"); + System.out.println("6-Salvar"); + System.out.println("7-Carregar"); + int op = scanner.nextInt(); + + switch (op) { + case 1 -> + ProcurarInimigos(); + case 2 -> + Loja(); + case 3 -> + Inventario(); + case 4 -> + Status(); + case 5 -> + System.exit(0); + case 6 -> + salvarJogo(); + case 7 -> + carregarJogo(); + + } + } + } + + public void ProcurarInimigos() { + + refreshLojaRestantes = 3; + + int nivelInimigo = rand.nextInt(5) + nivelJogador; + + int vidaInimigo = 60 + nivelInimigo * 20; + int ataqueInimigo = 8 + nivelInimigo * 4; + int defesaInimigo = nivelInimigo * 2; + + boolean boss = rand.nextInt(100) < 15; + + if (boss) { + + System.out.println("BOSS APARECEU"); + + vidaInimigo *= 3; + ataqueInimigo *= 2; + } + + if (nivelInimigo > nivelJogador) { + System.out.println("PERIGO: inimigo mais forte que tu"); + } + + while (vidaAtual > 0 && vidaInimigo > 0) { + + System.out.println("\nHP: " + vidaAtual + "/" + vidaMax); + System.out.println("Inimigo HP: " + vidaInimigo); + System.out.println("1-Atacar 2-Fugir"); + + int op = scanner.nextInt(); + + if (op == 1) { + + int dano = Math.max( + 1, + ataqueBase + bonusAtaque + rand.nextInt(5) - defesaInimigo + ); + + boolean critico = rand.nextInt(100) < 20; + + if (critico) { + dano *= 2; + System.out.println("CRÍTICO!"); + } + + vidaInimigo -= dano; + + int danoRecebido = Math.max( + 1, + ataqueInimigo - (defesaBase + bonusDefesa) + ); + + vidaAtual -= danoRecebido; + } else { + + if (rand.nextInt(100) < 60) { + + System.out.println("Fugiste"); + return; + + } else { + + System.out.println("Falhaste fuga"); + + vidaAtual -= 5; + } + } + } + + if (vidaAtual <= 0) { + GameOver(); + return; + } + + System.out.println("Vitória!"); + + xpJogador += 20 + nivelInimigo * 5; + ouro += 15 + nivelInimigo * 5; + + if (boss) { + xpJogador += 50; + ouro += 50; + } + + pontosStatus += 2; + + if (rand.nextInt(100) < 45) { + + Item drop = gerarItem(); + + System.out.println("\nDROP:"); + System.out.println(drop); + + adicionarItem(drop); + } + + if (rand.nextInt(1000) == 0) { + + Item unico = gerarItemUnico(); + + System.out.println("\nITEM ÚNICO ENCONTRADO!"); + System.out.println(unico); + + adicionarItem(unico); + } + + levelUp(); + } + + public void Loja() { + + gerarLoja(); + + while (true) { + + System.out.println("\nLOJA"); + System.out.println("Ouro: " + ouro); + System.out.println("Refresh restantes: " + refreshLojaRestantes); + + for (int i = 0; i < loja.length; i++) { + System.out.println(i + "-" + loja[i]); + } + + System.out.println("n-refresh -1-sair"); + + String input = scanner.next(); + + if (input.equals("-1")) { + return; + } + + if (input.equals("n")) { + + if (refreshLojaRestantes > 0) { + + refreshLojaRestantes--; + gerarLoja(); + + System.out.println("Loja atualizada"); + + } else { + System.out.println("Sem refreshes"); + } + + continue; + } + + int op = Integer.parseInt(input); + + if (op >= 0 && op < loja.length) { + + Item item = loja[op]; + + if (ouro >= item.preco || admin) { + + if (!admin) { + ouro -= item.preco; + } + + adicionarItem(item); + + System.out.println("Compraste " + item.nome); + } else { + System.out.println("Sem ouro"); + } + } + } + } + + public void Inventario() { + + while (true) { + + System.out.println("\n===== INVENTÁRIO ====="); + + if (itensCount == 0) { + System.out.println("Inventário vazio"); + } + + for (int i = 0; i < itensCount; i++) { + + System.out.println(i + " - " + inventario[i]); + } + + System.out.println("\n1-Usar"); + System.out.println("2-Equipar"); + System.out.println("3-Remover"); + System.out.println("-1-Sair"); + + int op = scanner.nextInt(); + + if (op == -1) { + return; + } + + System.out.print("ID Item: "); + int id = scanner.nextInt(); + + if (id < 0 || id >= itensCount) { + + System.out.println("Item inválido"); + continue; + } + + Item item = inventario[id]; + + if (op == 1) { + + if (item.tipo.equals("consumivel")) { + + vidaAtual = Math.min( + vidaMax, + vidaAtual + item.valor + ); + + System.out.println( + "Curaste " + item.valor + " HP" + ); + + removerItem(id); + + } else { + + System.out.println("Não podes usar isso"); + } + } else if (op == 2) { + + if (item.tipo.equals("arma")) { + + bonusAtaque = item.valor; + + System.out.println( + item.nome + " equipada" + ); + } else if (item.tipo.equals("armadura")) { + + bonusDefesa = item.valor; + + System.out.println( + item.nome + " equipada" + ); + } else { + + System.out.println("Não equipável"); + } + } else if (op == 3) { + + System.out.println( + item.nome + " removido" + ); + + removerItem(id); + } + } + } + + public void levelUp() { + + int need = nivelJogador * 50; + + while (xpJogador >= need) { + + xpJogador -= need; + nivelJogador++; + + vidaMax += 20; + ataqueBase += 2; + defesaBase += 1; + pontosStatus += 3; + + System.out.println("Subiste nível " + nivelJogador); + + need = nivelJogador * 50; + } + } + + public void Status() { + + while (true) { + + System.out.println("\nSTATUS"); + System.out.println("1-Aumentar Ataque"); + System.out.println("2-Aumentar Defesa"); + System.out.println("3-Aumentar Vida Máx"); + System.out.println("-1-Sair"); + + System.out.println("\nAtaque: " + (ataqueBase + bonusAtaque)); + System.out.println("Defesa: " + (defesaBase + bonusDefesa)); + System.out.println("Vida Max: " + vidaMax); + System.out.println("Pontos disponíveis: " + pontosStatus); + + int op = scanner.nextInt(); + + if (op == -1) { + return; + } + + if (pontosStatus <= 0) { + System.out.println("Sem pontos disponíveis"); + continue; + } + + switch (op) { + + case 1 -> { + ataqueBase++; + pontosStatus--; + System.out.println("Ataque aumentado"); + } + + case 2 -> { + defesaBase++; + pontosStatus--; + System.out.println("Defesa aumentada"); + } + + case 3 -> { + vidaMax += 10; + vidaAtual += 10; + pontosStatus--; + System.out.println("Vida aumentada"); + } + + default -> + System.out.println("Opção inválida"); + } + } + } + + public void GameOver() { + + System.out.println("\nGAME OVER"); + + while (true) { + + System.out.println("\nQueres recomeçar?"); + System.out.println("1-Sim"); + System.out.println("2-Não"); + + int op = scanner.nextInt(); + + if (op == 1) { + resetJogo(); + System.out.println("Recomeçaste com o personagem atual!"); + return; + } + + if (op == 2) { + System.out.println("A sair do jogo..."); + System.exit(0); + } + + System.out.println("Opção inválida"); + } + } + + public void adicionarItem(Item i) { + + if (admin) { + + if (itensCount >= inventario.length) { + inventario = java.util.Arrays.copyOf(inventario, inventario.length + 20); + } + + inventario[itensCount++] = i; + return; + } + + if (itensCount < inventario.length) { + inventario[itensCount++] = i; + } else { + System.out.println("Inventário cheio"); + } + } + + public void removerItem(int i) { + + if (i < 0 || i >= itensCount) { + return; + } + + for (int j = i; j < itensCount - 1; j++) { + inventario[j] = inventario[j + 1]; + } + + inventario[itensCount - 1] = null; + + itensCount--; + + if (itensCount < 0) { + itensCount = 0; + } + } + + public void gerarLoja() { + + for (int i = 0; i < loja.length; i++) { + loja[i] = gerarItem(); + } + } + + public Item gerarItem() { + + int t = rand.nextInt(3); + int r = rand.nextInt(100); + + String raridade; + + int multiplicadorValor; + int multiplicadorPreco; + + if (r < 60) { + + raridade = "Comum"; + + multiplicadorValor = 1; + multiplicadorPreco = 1; + } else if (r < 85) { + + raridade = "Raro"; + + multiplicadorValor = 3; + multiplicadorPreco = 2; + } else if (r < 97) { + + raridade = "Épico"; + + multiplicadorValor = 5; + multiplicadorPreco = 3; + } else { + + raridade = "Lendário"; + + multiplicadorValor = 8; + multiplicadorPreco = 5; + } + + if (t == 0) { + + int cura = 30 * multiplicadorValor; + int preco = 10 * multiplicadorPreco; + + return new Item( + "Poção", + "consumivel", + cura, + preco, + raridade + ); + } + + if (t == 1) { + + int dano = (5 + nivelJogador) * multiplicadorValor; + int preco = 40 * multiplicadorPreco; + + return new Item( + "Espada", + "arma", + dano, + preco, + raridade + ); + } + + int defesa = (5 + nivelJogador) * multiplicadorValor; + int preco = 40 * multiplicadorPreco; + + return new Item( + "Armadura", + "armadura", + defesa, + preco, + raridade + ); + } + + class Item { + + String nome; + String tipo; + int valor; + int preco; + String raridade; + + public Item(String n, String t, int v, int p, String r) { + nome = n; + tipo = t; + valor = v; + preco = p; + raridade = r; + } + + public String toString() { + return nome + " [" + raridade + "] +" + valor + " (" + tipo + ") - " + preco + " ouro"; + } + } + + public void resetJogo() { + + nivelJogador = 1; + xpJogador = 0; + ouro = 0; + + bonusAtaque = 0; + bonusDefesa = 0; + + pontosStatus = 0; + itensCount = 0; + + refreshLojaRestantes = 3; + + vidaMax = 100; + ataqueBase = 10; + defesaBase = 5; + + switch (classe) { + + case "Guerreiro" -> { + ataqueBase += 2; + defesaBase += 3; + } + + case "Mago" -> { + ataqueBase += 5; + } + + case "Paladino" -> { + defesaBase += 4; + vidaMax += 20; + } + + case "Assassino" -> { + ataqueBase += 3; + } + + case "Arqueiro" -> { + ataqueBase += 2; + } + } + + vidaAtual = vidaMax; + } + + public Item gerarItemUnico() { + + int t = rand.nextInt(2); + + if (t == 0) { + + return new Item( + "Excalibur", + "arma", + 100 + nivelJogador * 10, + 0, + "Único" + ); + } + + return new Item( + "Armadura Divina", + "armadura", + 80 + nivelJogador * 8, + 0, + "Único" + ); + } + + public void salvarJogo() throws IOException { + + try (PrintWriter writer = new PrintWriter(new FileWriter(nomeJogador + ".txt"))) { + + writer.println(nomeJogador); + writer.println(classe); + + writer.println(nivelJogador); + writer.println(xpJogador); + writer.println(ouro); + + writer.println(vidaMax); + writer.println(vidaAtual); + + writer.println(ataqueBase); + writer.println(defesaBase); + + writer.println(bonusAtaque); + writer.println(bonusDefesa); + + writer.println(pontosStatus); + + writer.println(itensCount); + + for (int i = 0; i < itensCount; i++) { + + Item it = inventario[i]; + + writer.println( + it.nome + ";" + + it.tipo + ";" + + it.valor + ";" + + it.preco + ";" + + it.raridade + ); + } + + System.out.println("Jogo guardado!"); + } + } + + public void carregarJogo() { + + System.out.print("Nome do jogador: "); + String nome = scanner.next(); + + try (BufferedReader br = new BufferedReader(new FileReader(nome + ".txt"))) { + + nomeJogador = br.readLine(); + classe = br.readLine(); + + nivelJogador = Integer.parseInt(br.readLine()); + xpJogador = Integer.parseInt(br.readLine()); + ouro = Integer.parseInt(br.readLine()); + + vidaMax = Integer.parseInt(br.readLine()); + vidaAtual = Integer.parseInt(br.readLine()); + + ataqueBase = Integer.parseInt(br.readLine()); + defesaBase = Integer.parseInt(br.readLine()); + + bonusAtaque = Integer.parseInt(br.readLine()); + bonusDefesa = Integer.parseInt(br.readLine()); + + pontosStatus = Integer.parseInt(br.readLine()); + + itensCount = Integer.parseInt(br.readLine()); + + inventario = new Item[50]; + + for (int i = 0; i < itensCount; i++) { + + String[] parts = br.readLine().split(";"); + + inventario[i] = new Item( + parts[0], + parts[1], + Integer.parseInt(parts[2]), + Integer.parseInt(parts[3]), + parts[4] + ); + } + + System.out.println("Jogo carregado com sucesso!"); + + } catch (IOException e) { + + System.out.println("Save não encontrado!"); + + try { + iniciar(); + } catch (IOException ex) { + System.out.println("Erro ao reiniciar menu"); + } + } + } + + public void carregarInventarioAdmin() { + + itensCount = 0; + + for (int i = 0; i < 10; i++) { + + adicionarItem(new Item( + "Espada Admin " + i, + "arma", + 999, + 0, + "ADMIN" + )); + + adicionarItem(new Item( + "Armadura Admin " + i, + "armadura", + 999, + 0, + "ADMIN" + )); + + adicionarItem(new Item( + "Poção Admin " + i, + "consumivel", + 999, + 0, + "ADMIN" + )); + } + + adicionarItem(new Item("Excalibur", "arma", 500, 0, "ÚNICO")); + adicionarItem(new Item("Armadura Divina", "armadura", 500, 0, "ÚNICO")); + } + + public void login() { + + System.out.println("===== LOGIN ====="); + System.out.println("1-Novo jogador"); + System.out.println("2-Carregar jogo"); + System.out.println("3-Login Admin"); + + int op = scanner.nextInt(); + + if (op == 1) { + + criarPersonagem(); + loggedIn = true; + } else if (op == 2) { + + try { + carregarJogo(); + loggedIn = true; + } catch (Exception e) { + System.out.println("Sem save encontrado"); + criarPersonagem(); + loggedIn = true; + } + } else if (op == 3) { + + System.out.print("Nome admin: "); + String n = scanner.next(); + + System.out.print("Password: "); + String p = scanner.next(); + + if (n.equals("adminPro") && p.equals("adimn.epvc")) { + + admin = true; + loggedIn = true; + + System.out.println("ADMIN LOGIN OK"); + + nivelJogador = 100; + ouro = 999999; + vidaMax = 9999; + vidaAtual = vidaMax; + ataqueBase = 999; + defesaBase = 999; + pontosStatus = 999; + + } else { + System.out.println("Login inválido"); + } + } + } + + public void loginAdmin() { + + System.out.print("Nome admin: "); + String nome = scanner.next(); + + System.out.print("Password: "); + String pass = scanner.next(); + + if (nome.equals("admin.epvc") && pass.equals("1234")) { + + admin = true; + + System.out.println("ADMIN LOGIN OK"); + + nivelJogador = 100; + xpJogador = 0; + ouro = 999999; + + vidaMax = 9999; + vidaAtual = vidaMax; + + ataqueBase = 999; + defesaBase = 999; + + bonusAtaque = 999; + bonusDefesa = 999; + + pontosStatus = 999; + + inventario = new Item[200]; + + carregarInventarioAdmin(); + + } else { + System.out.println("Login inválido"); + } + } +}