diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..138cfca775d3339b660d1e842ce467215b01df02 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,13 @@ +sonarqube-check: + image: gradle:jre11-slim + variables: + SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache + GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task + cache: + key: "${CI_JOB_NAME}" + paths: + - .sonar/cache + script: gradle sonar + allow_failure: true + rules: + - if: $CI_COMMIT_BRANCH == 'main' diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000000000000000000000000000000000000..b86273d9424b73937ca6ad7933f6b7eeb60f4a3d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="CompilerConfiguration"> + <bytecodeTargetLevel target="21" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000000000000000000000000000000000000..fdc392fe877c32ab51d532fa67f65ff2e75e9061 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="RemoteRepositoriesConfiguration"> + <remote-repository> + <option name="id" value="central" /> + <option name="name" value="Maven Central repository" /> + <option name="url" value="https://repo1.maven.org/maven2" /> + </remote-repository> + <remote-repository> + <option name="id" value="jboss.community" /> + <option name="name" value="JBoss Community repository" /> + <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> + </remote-repository> + <remote-repository> + <option name="id" value="MavenRepo" /> + <option name="name" value="MavenRepo" /> + <option name="url" value="https://repo.maven.apache.org/maven2/" /> + </remote-repository> + </component> +</project> \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 6dec2713cae7f8cde6573290e335aff9a8dc295c..7d042f2e3a26e0ed4fd800a4061463e04fde7770 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,10 +1,13 @@ plugins { id("java") + id ("org.sonarqube") version "4.2.1.3168" } + group = "org.example" version = "1.0-SNAPSHOT" + repositories { mavenCentral() } @@ -14,6 +17,14 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter") } +sonar { + properties { + property("sonar.projectKey", "cayraca_cayrac_tamagoshis_AZO68mE2p6w51ilm7U6-") + property("sonar.projectName", "Cayrac_Tamagoshis") + property("sonar.qualitygate.wait", true) + } +} + tasks.test { useJUnitPlatform() } \ No newline at end of file diff --git a/src/main/java/BigEater.java b/src/main/java/BigEater.java new file mode 100644 index 0000000000000000000000000000000000000000..1717a0d40e84c3411ebfd545fbb42e32e383423c --- /dev/null +++ b/src/main/java/BigEater.java @@ -0,0 +1,11 @@ +public class BigEater extends Tamagoshi { + public BigEater(String name) { + super(name); + } + + @Override + protected void decrementStates() { + energy -= 2; // Consomme plus d'énergie + fun--; + } +} diff --git a/src/main/java/BigPlayer.java b/src/main/java/BigPlayer.java new file mode 100644 index 0000000000000000000000000000000000000000..35362bb4e8d93fb7c883ad0a400621d6d0b65452 --- /dev/null +++ b/src/main/java/BigPlayer.java @@ -0,0 +1,11 @@ +public class BigPlayer extends Tamagoshi { + public BigPlayer(String name) { + super(name); + } + + @Override + protected void decrementStates() { + fun -= 2; // Perte de fun plus rapide + energy--; + } +} \ No newline at end of file diff --git a/src/main/java/Game.java b/src/main/java/Game.java new file mode 100644 index 0000000000000000000000000000000000000000..81db1a857dd5fbd43119fca720338fc29b8400dc --- /dev/null +++ b/src/main/java/Game.java @@ -0,0 +1,77 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Game { + private final List<Tamagoshi> tamagoshis = new ArrayList<>(); + private final Scanner scanner = new Scanner(System.in); + + public void start() { + System.out.println("Entrez le nombre de tamagoshis désiré !"); + int count = Integer.parseInt(scanner.nextLine()); + + for (int i = 1; i <= count; i++) { + System.out.println("Entrez le nom du tamagoshi numéro " + i + " :"); + String name = scanner.nextLine(); + tamagoshis.add(createRandomTamagoshi(name)); + } + + play(); + } + + private void play() { + int turn = 0; + while (tamagoshis.stream().anyMatch(Tamagoshi::isAlive)) { + turn++; + System.out.println("\n------------ Tour n°" + turn + " -------------"); + tamagoshis.forEach(t -> System.out.println(t.getState())); + + System.out.println("\nNourrir quel tamagoshi ?"); + interact("feed"); + + System.out.println("\nJouer avec quel tamagoshi ?"); + interact("play"); + + tamagoshis.forEach(Tamagoshi::ageOneTurn); + } + + endGame(); + } + + private void interact(String action) { + for (int i = 0; i < tamagoshis.size(); i++) { + System.out.println("(" + i + ") " + tamagoshis.get(i).getName()); + } + int choice = Integer.parseInt(scanner.nextLine()); + Tamagoshi selected = tamagoshis.get(choice); + if ("feed".equals(action)) { + selected.feed(); + System.out.println(selected.getName() + ": Merci !"); + } else if ("play".equals(action)) { + selected.play(); + System.out.println(selected.getName() + ": On se marre !"); + } + } + + private void endGame() { + System.out.println("\n--------- fin de partie !! ----------------"); + int totalMaxAge = tamagoshis.size() * Tamagoshi.MAX_AGE; + int totalAge = tamagoshis.stream().mapToInt(Tamagoshi::getAge).sum(); + double score = (double) totalAge / totalMaxAge * 100; + + tamagoshis.forEach(t -> { + String status = t.isAlive() ? "a survécu et vous remercie :)" : "n'est pas arrivé au bout et ne vous félicite pas :("; + System.out.println(t.getName() + " qui était un " + t.getClass().getSimpleName() + " " + status); + }); + + System.out.println("Niveau de difficulté : " + tamagoshis.size() + ", score obtenu : " + score + "%"); + } + + private Tamagoshi createRandomTamagoshi(String name) { + return Tamagoshi.random.nextBoolean() ? new BigEater(name) : new BigPlayer(name); + } + + public static void main(String[] args) { + new Game().start(); + } +} diff --git a/src/main/java/Tamagoshi.java b/src/main/java/Tamagoshi.java new file mode 100644 index 0000000000000000000000000000000000000000..0e8c6803592b393a856d232f14bde5d01207c9fb --- /dev/null +++ b/src/main/java/Tamagoshi.java @@ -0,0 +1,64 @@ +import java.util.Random; + +public abstract class Tamagoshi { + protected String name; + protected int age = 0; + protected int energy; + protected int maxEnergy; + protected int energyThreshold; + protected int fun; + protected int maxFun; + protected int funThreshold; + + protected static final int MAX_AGE = 10; + protected static final Random random = new Random(); + + public Tamagoshi(String name) { + this.name = name; + this.energy = random.nextInt(3) + 3; + this.maxEnergy = random.nextInt(5) + 5; + this.energyThreshold = random.nextInt(3) + 3; + this.fun = random.nextInt(3) + 3; + this.maxFun = random.nextInt(5) + 5; + this.funThreshold = random.nextInt(3) + 3; + } + + public boolean isAlive() { + return age < MAX_AGE && (energy > 0 || fun > 0); + } + + public void ageOneTurn() { + age++; + decrementStates(); + } + + public String getState() { + if (energy <= energyThreshold && fun <= funThreshold) { + return name + ": je suis affamé et je m'ennuie à mourir !"; + } else if (energy <= energyThreshold) { + return name + ": je suis affamé !"; + } else if (fun <= funThreshold) { + return name + ": je m'ennuie à mourir !"; + } else { + return name + ": Tout va bien !"; + } + } + + public void feed() { + energy = Math.min(maxEnergy, energy + random.nextInt(3) + 1); + } + + public void play() { + fun = Math.min(maxFun, fun + random.nextInt(3) + 1); + } + + protected abstract void decrementStates(); + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +}