Commit 405cef0f authored by Guillaume Dewisme's avatar Guillaume Dewisme

refonte du jeu

parent 85fe1786
package org.example;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
public class BeatThemAllGame {
// Je l'ai fait à la va vite donc très probablement que ca ne fonctionne pas parfaitement (j'ai pas testé)
public static class Logger {
private static Logger instance;
private FileWriter writer;
private Logger() {
try {
writer = new FileWriter("game_log.txt", true);
} catch (IOException e) {
System.out.println("Erreur lors de l'initialisation du logger : " + e.getMessage());
}
}
public static Logger getInstance() {
if (instance == null) {
instance = new Logger();
}
return instance;
}
public void log(String message) {
try {
String timestamp = "[" + LocalDateTime.now() + "] ";
writer.write(timestamp + message + "\n");
writer.flush();
} catch (IOException e) {
System.out.println("Erreur lors de l'écriture dans le fichier de log : " + e.getMessage());
}
}
}
// on pourra la modifier par la suite si des éléments ne conviennent pas
public static abstract class Character {
protected String name;
protected int maxHP;
protected int currentHP;
protected int attackPower;
protected int defense;
protected SpecialAbility specialAbility;
public Character(String name, int maxHP, int attackPower, int defense) {
this.name = name;
this.maxHP = maxHP;
this.currentHP = maxHP;
this.attackPower = attackPower;
this.defense = defense;
}
public boolean isAlive() {
return currentHP > 0;
}
public void takeDamage(int damage) {
int actualDamage = Math.max(damage - defense, 0);
currentHP = Math.max(currentHP - actualDamage, 0);
System.out.println(name + " subit " + actualDamage + " dégâts. (PV restants : " + currentHP + ")");
}
public abstract void attack(Character target);
public void useSpecialAbility(Character target) {
if (specialAbility != null) {
specialAbility.activate(this, target);
}
}
}
// Classe pour le héros
public static class Hero extends Character {
private boolean specialUsed = false;
private List<Item> inventory = new ArrayList<>();
private int experience = 0;
private int level = 1;
public Hero(String name, int maxHP, int attackPower, int defense, SpecialAbility specialAbility) {
super(name, maxHP, attackPower, defense);
this.specialAbility = specialAbility;
}
@Override
public void attack(Character target) {
System.out.println(name + " attaque " + target.name + " !");
Logger.getInstance().log(name + " attaque " + target.name);
target.takeDamage(attackPower);
}
public void useSpecialAbility(Character target) {
if (!specialUsed) {
specialAbility.activate(this, target);
specialUsed = true;
Logger.getInstance().log(name + " utilise sa capacité spéciale");
} else {
System.out.println("Capacité spéciale déjà utilisée !");
}
}
public void resetSpecialAbility() {
specialUsed = false;
}
public void addItem(Item item) {
inventory.add(item);
System.out.println(item.getName() + " a été ajouté à votre inventaire.");
Logger.getInstance().log(name + " a obtenu un objet : " + item.getName());
}
public void useItem() {
if (inventory.isEmpty()) {
System.out.println("Votre inventaire est vide !");
return;
}
System.out.println("Inventaire :");
for (int i = 0; i < inventory.size(); i++) {
System.out.println((i + 1) + ". " + inventory.get(i).getName());
}
System.out.print("Choisissez un objet à utiliser : ");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
if (choice > 0 && choice <= inventory.size()) {
Item item = inventory.get(choice - 1);
item.applyEffect(this);
inventory.remove(item);
Logger.getInstance().log(name + " utilise un objet : " + item.getName());
} else {
System.out.println("Choix invalide.");
}
}
public void gainExperience(int xp) {
experience += xp;
System.out.println(name + " gagne " + xp + " points d'expérience.");
Logger.getInstance().log(name + " gagne " + xp + " XP");
if (experience >= level * 100) {
levelUp();
}
}
private void levelUp() {
level++;
experience = 0;
maxHP += 20;
attackPower += 5;
defense += 2;
currentHP = maxHP;
System.out.println(name + " passe au niveau " + level + " !");
Logger.getInstance().log(name + " monte au niveau " + level);
}
}
// Classe pour les ennemis
public static class Enemy extends Character {
public Enemy(String name, int maxHP, int attackPower, int defense, SpecialAbility specialAbility) {
super(name, maxHP, attackPower, defense);
this.specialAbility = specialAbility;
}
@Override
public void attack(Character target) {
System.out.println(name + " attaque " + target.name + " !");
Logger.getInstance().log(name + " attaque " + target.name);
target.takeDamage(attackPower);
}
}
public static class HalveEnemyHP implements SpecialAbility {
@Override
public void activate(Character user, Character target) {
System.out.println(user.name + " réduit de moitié les PV de " + target.name + " !");
target.currentHP /= 2;
}
}
public static class StealItem implements SpecialAbility {
@Override
public void activate(Character user, Character target) {
System.out.println(user.name + " tente de voler un objet à " + target.name + " !");
// Simplification : L'ennemi n'a pas d'objets à voler
System.out.println("Mais " + target.name + " n'a rien à voler !");
}
}
// pouvoir des ennemis (on pourra en ajouter ou en enelver)
public static class WeaknessSpell implements SpecialAbility {
@Override
public void activate(Character user, Character target) {
System.out.println(user.name + " lance un sort de faiblesse sur " + target.name + " !");
target.attackPower = Math.max(target.attackPower - 5, 0);
System.out.println(target.name + " voit son attaque réduite à " + target.attackPower + " !");
}
}
// Objets
public interface Item {
void applyEffect(Hero hero);
String getName();
}
public static class HealthPotion implements Item {
@Override
public void applyEffect(Hero hero) {
int healAmount = 50;
hero.currentHP = Math.min(hero.currentHP + healAmount, hero.maxHP);
System.out.println("Vous récupérez " + healAmount + " PV. (PV actuels : " + hero.currentHP + ")");
}
@Override
public String getName() {
return "Potion de Santé";
}
}
public static class AttackBoost implements Item {
@Override
public void applyEffect(Hero hero) {
int boostAmount = 5;
hero.attackPower += boostAmount;
System.out.println("Votre attaque augmente de " + boostAmount + " points ! (Attaque actuelle : " + hero.attackPower + ")");
}
@Override
public String getName() {
return "Potion de Puissance";
}
}
// gére tout le jeu
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
displayTitle();
Hero hero = chooseHero(scanner);
System.out.println();
System.out.println("Votre aventure commence !");
Logger.getInstance().log("Début de l'aventure de " + hero.name);
boolean gameRunning = true;
while (gameRunning && hero.isAlive()) {
Enemy enemy = generateRandomEnemy();
System.out.println("\nUn " + enemy.name + " apparaît !");
Logger.getInstance().log(hero.name + " rencontre un " + enemy.name);
battle(scanner, hero, enemy);
if (!hero.isAlive()) {
displayGameOver();
Logger.getInstance().log(hero.name + " est mort");
break;
}
hero.resetSpecialAbility();
hero.gainExperience(50);
// Chance de trouver un objet après le combat
if (new Random().nextInt(100) < 50) {
Item foundItem = getRandomItem();
System.out.println("Vous trouvez un objet sur l'ennemi !");
hero.addItem(foundItem);
}
// Demander au joueur s'il veut continuer
System.out.print("\nVoulez-vous continuer ? (oui/non) : ");
String choice = scanner.next();
if (!choice.equalsIgnoreCase("oui")) {
gameRunning = false;
System.out.println("Merci d'avoir joué !");
Logger.getInstance().log(hero.name + " a quitté le jeu");
}
}
}
// choix du héro
public static Hero chooseHero(Scanner scanner) {
System.out.println("Choisissez votre héros :");
System.out.println("1. Guerrier (Double Attaque)");
System.out.println("2. Mage (Réduit les PV des ennemis de moitié)");
System.out.println("3. Voleur (Tentative de vol d'objets)");
System.out.println("4. Archer (Attaque à distance avant le combat)");
System.out.println("5. Berserker (Plus il est blessé, plus il frappe fort)");
System.out.print("Votre choix : ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consommer le retour à la ligne
System.out.print("Entrez le nom de votre héros : ");
String name = scanner.nextLine();
switch (choice) {
case 1:
return new Hero(name, 80, 20, 10, new DoubleAttack());
case 2:
return new Hero(name, 60, 15, 5, new HalveEnemyHP());
case 3:
return new Hero(name, 70, 18, 7, new StealItem());
case 4:
return new Hero(name, 75, 17, 6, new PreemptiveStrike());
case 5:
return new Hero(name, 100, 22, 5, new Rage());
default:
System.out.println("Choix invalide, Guerrier sélectionné par défaut.");
return new Hero(name, 150, 20, 10, new DoubleAttack());
}
}
// Capacités spéciales supplémentaires (a voir si on garde)
public static class PreemptiveStrike implements SpecialAbility {
@Override
public void activate(Character user, Character target) {
int damage = user.attackPower;
System.out.println(user.name + " tire une flèche sur " + target.name + " avant le combat !");
target.takeDamage(damage);
}
}
public static class Rage implements SpecialAbility {
@Override
public void activate(Character user, Character target) {
int rageBonus = (user.maxHP - user.currentHP) / 10;
user.attackPower += rageBonus;
System.out.println(user.name + " entre en rage, augmentant son attaque de " + rageBonus + " points !");
}
}
// ennemi aléatoire
public static Enemy generateRandomEnemy() {
int enemyType = new Random().nextInt(4);
switch (enemyType) {
case 0:
return new Enemy("Brigand", 80, 15, 5, null);
case 1:
return new Enemy("Sorcier", 90, 10, 5, new WeaknessSpell());
case 2:
return new Enemy("Gangster", 70, 12, 4, null);
case 3:
return new Enemy("Catcheur", 150, 10, 8, null);
default:
return new Enemy("Brigand", 80, 15, 5, null);
}
}
// pareil pour uin objet
public static Item getRandomItem() {
int itemType = new Random().nextInt(2);
switch (itemType) {
case 0:
return new HealthPotion();
case 1:
return new AttackBoost();
default:
return new HealthPotion();
}
}
// pour gérer le combat
public static void battle(Scanner scanner, Hero hero, Enemy enemy) {
if (hero.specialAbility instanceof PreemptiveStrike && !hero.specialUsed) {
hero.useSpecialAbility(enemy);
hero.specialUsed = true;
if (!enemy.isAlive()) {
System.out.println("Vous avez vaincu " + enemy.name + " avant même qu'il puisse agir !");
Logger.getInstance().log(hero.name + " a vaincu " + enemy.name + " avec une attaque préventive");
return;
}
}
while (hero.isAlive() && enemy.isAlive()) {
System.out.println("\n=== Votre tour ===");
System.out.println("1. Attaquer");
System.out.println("2. Utiliser la capacité spéciale");
System.out.println("3. Utiliser un objet");
System.out.print("Votre choix : ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
hero.attack(enemy);
break;
case 2:
hero.useSpecialAbility(enemy);
break;
case 3:
hero.useItem();
break;
default:
System.out.println("Choix invalide, vous perdez votre tour !");
break;
}
if (enemy.isAlive()) {
System.out.println("\n=== Tour de l'ennemi ===");
if (enemy.specialAbility != null && new Random().nextInt(100) < 30) {
enemy.useSpecialAbility(hero);
Logger.getInstance().log(enemy.name + " utilise sa capacité spéciale");
} else {
enemy.attack(hero);
}
} else {
System.out.println("Vous avez vaincu " + enemy.name + " !");
Logger.getInstance().log(hero.name + " a vaincu " + enemy.name);
}
}
}
// afficher le titre (on pourra changer l'ascii)
public static void displayTitle() {
System.out.println("**************************************************");
System.out.println("* BEAT THEM ALL *");
System.out.println("**************************************************");
System.out.println(" ____ _ _ _ ");
System.out.println(" | _ \\ | | | | | | ");
System.out.println(" | |_) | __ _| |_| |_| | ___ ");
System.out.println(" | _ < / _` | __| __| |/ _ \\ ");
System.out.println(" | |_) | (_| | |_| |_| | __/ ");
System.out.println(" |____/ \\__,_|\\__|\\__|_|\\___| ");
System.out.println();
}
// pareil qu'au dessus
public static void displayGameOver() {
System.out.println("\n=== VOUS ÊTES MORT ===");
System.out.println(" _____");
System.out.println(" / \\");
System.out.println(" | |");
System.out.println(" | RIP |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |_______|");
System.out.println("\nMerci d'avoir joué !");
}
// Tests unitaires intégrés dans le main (pour simplifier) pour l'instant
public static void runTests() {
System.out.println("\n--- Exécution des tests unitaires ---");
testEnemyDefeated();
testGameOver();
testSpecialAbility();
testItemUsage();
System.out.println("--- Tous les tests se sont terminés avec succès ---\n");
}
public static void testEnemyDefeated() {
Hero hero = new Hero("TestHero", 100, 50, 0, null);
Enemy enemy = new Enemy("TestEnemy", 50, 0, 0, null);
hero.attack(enemy);
assert enemy.currentHP == 0 : "L'ennemi aurait dû être vaincu.";
}
public static void testGameOver() {
Hero hero = new Hero("TestHero", 0, 0, 0, null);
assert !hero.isAlive() : "Le héros devrait être mort.";
}
public static void testSpecialAbility() {
Hero hero = new Hero("TestHero", 100, 20, 0, new DoubleAttack());
Enemy enemy = new Enemy("TestEnemy", 100, 0, 0, null);
hero.useSpecialAbility(enemy);
assert enemy.currentHP == 60 : "La capacité spéciale du héros n'a pas fonctionné correctement.";
}
public static void testItemUsage() {
Hero hero = new Hero("TestHero", 100, 20, 0, null);
hero.currentHP = 50;
Item potion = new HealthPotion();
potion.applyEffect(hero);
assert hero.currentHP == 100 : "La potion de santé n'a pas restauré correctement les PV du héros.";
}
}
\ No newline at end of file
package org.example;
// on pourra la modifier par la suite si des éléments ne conviennent pas
public abstract class Character {
protected String name;
protected int maxHP;
protected int currentHP;
protected int attackPower;
protected int defense;
protected SpecialAbility specialAbility;
public Character(String name, int maxHP, int attackPower, int defense) {
this.name = name;
this.maxHP = maxHP;
this.currentHP = maxHP;
this.attackPower = attackPower;
this.defense = defense;
}
public boolean isAlive() {
return currentHP > 0;
}
public void takeDamage(int damage) {
int actualDamage = Math.max(damage - defense, 0);
currentHP = Math.max(currentHP - actualDamage, 0);
System.out.println(name + " subit " + actualDamage + " dégâts. (PV restants : " + currentHP + ")");
}
public abstract void attack(Character target);
public void useSpecialAbility(Character target) {
if (specialAbility != null) {
specialAbility.activate(this, target);
}
}
}
package org.example;
import org.example.SpecialAbility;
// Capacités spéciales des héros
public class DoubleAttack implements SpecialAbility {
@Override
public void activate(Character user, Character target) {
int damage = user.attackPower * 2;
System.out.println(user.name + " utilise Double Attaque !");
target.takeDamage(damage);
}
}
\ No newline at end of file
package org.example;
public abstract class Entity {
private int damage;
private int pvMax;
private int pv;
public void heal(){
this.pv += this.pvMax/3;
}
public int getDamage() {
return this.damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public int getPvMax() {
return this.pvMax;
}
public void setPvMax(int pvMax) {
this.pvMax = pvMax;
}
public int getPv() {
return this.pv;
}
public void setPv(int pv) {
this.pv = pv;
}
public boolean isAlive(){
return (this.pv > 0);
}
}
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
package org.example;
public class Player extends Entity {
private String name;
private SpecialAbility specialAbility;
private int endurance;
private static final int MAX_ENDURANCE = 3;
public Player(String name, SpecialAbility specialAbility) {
super();
this.name = name;
this.specialAbility = specialAbility;
this.endurance = MAX_ENDURANCE;
this.setPvMax(100);
this.setPv(this.getPvMax());
this.setDamage(5);
}
public void attackEnnemy(){
--this.endurance;
//implement when Ennemy class is created
}
public void useSpecialAbility(){
if(this.specialAbility == SpecialAbility.DAMAGE){
this.setDamage(this.getDamage()*2);
}
if(this.specialAbility == SpecialAbility.FULLHEAL){
this.setPv(this.getPvMax());
}
if(this.specialAbility == SpecialAbility.SHIELD ){
//to-do
//add random for variation damage each round
}
}
}
package org.example;
public enum SpecialAbility {
DAMAGE,
FULLHEAL,
SHIELD,
// ... other abilities ...
}
// Interface pour les capacités spéciales
public interface SpecialAbility {
void activate(Character user, Character target);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment