TD pokemon

This commit is contained in:
JunkJumper
2020-05-03 14:26:41 +02:00
committed by GitHub
parent e5983242c6
commit 8e7f283cac
27 changed files with 719 additions and 0 deletions

View File

@ -0,0 +1,49 @@
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import pokemons.Joueur;
import pokemons.PokemonEAU;
import pokemons.PokemonELECTRIK;
import pokemons.PokemonFEU;
import pokemons.PokemonPLANTE;
class testPokemon {
//test Pokemons
PokemonPLANTE celebi = new PokemonPLANTE("Celebi", 0.6, 5.0, 100, 50);
PokemonFEU hooh = new PokemonFEU("Ho-Oh", 3.8, 199.0, 106, 52, 2);
PokemonEAU kyogre = new PokemonEAU("Kyogre", 4.5, 352.0, 100, 30, 7);
PokemonELECTRIK electhor = new PokemonELECTRIK("Electhor", 1.6, 52.6, 90, 15, 2, 2, 90);
@Test
void testAffiche() {
assertEquals("Pokemon Celebi de type PLANTE (5.0 kg, 0.6 m, 100 pts de vie, 50 force de combat)"
, celebi.affiche());
assertEquals("Pokemon Ho-Oh de type FEU (199.0 kg, 3.8 m, 106 pts de vie, 52 force de combat, 199.0 kg, 3.8 m, 106 pts de vie, 52 force de combat, 2 pattes)",
hooh.affiche());
assertEquals("Pokemon Kyogre de type EAU (352.0 kg, 4.5 m, 100 pts de vie, 30 force de combat, 7 nageoires)",
kyogre.affiche());
assertEquals("Pokemon Electhor de type ELECTRIK (52.6 kg, 1.6 m, 90 pts de vie, 15 force de combat, 2 pattes, 2 ailes, 90.0 mA)",
electhor.affiche());
}
@Test
void testCalulerVitesse() {
assertEquals(3.3333333333333335 , celebi.calculerVitesse());
assertEquals(11.94 , hooh.calculerVitesse());
assertEquals(98.56 , kyogre.calculerVitesse());
assertEquals(18.0, electhor.calculerVitesse());
}
@Test
void testAttaque() {
celebi.attaque(hooh);
assertEquals(81, hooh.getPv());
hooh.attaque(celebi);
assertEquals(0, celebi.getPv());
kyogre.attaque(electhor);
assertEquals(60, electhor.getPv());
electhor.attaque(kyogre);
assertEquals(70, kyogre.getPv());
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,9 @@
package pokemons;
import java.util.Comparator;
public class CompareAlpha implements Comparator<Joueur>{
public int compare(Joueur j, Joueur j2) {
return j.getNom().compareTo(j2.getNom());
}
}

View File

@ -0,0 +1,10 @@
package pokemons;
import java.util.Comparator;
public class CompareNiveau implements Comparator<Joueur> {
public int compare(Joueur j1, Joueur j2) {
if (j1.getNiveau() > j2.getNiveau())
return -1;
else return 0;
}
}

View File

@ -0,0 +1,5 @@
package pokemons;
public interface IAttaque {
void attaque(Pokemon p);
}

View File

@ -0,0 +1,79 @@
package pokemons;
import java.util.*;
public class Joueur {
private String nom;
private int niveau;
private int nbPoints;
private ArrayList<Pokemon> collection;
//constructeur
public Joueur(String nom) {
this.nom = nom;
this.niveau = 1;
this.nbPoints = 0;
collection = new ArrayList<Pokemon>();
}
//Getters
public ArrayList<Pokemon> getCollection() {
return collection;
}
public String getNom() {
return nom;
}
public int getNiveau() {
return niveau;
}
public int getnbPoints() {
return nbPoints;
}
//m<>thodes
public void attraperPokemon(Pokemon p) {
this.collection.add(p);
System.out.println(this.nom + " <20> attraper " + p.getNom());
}
public void transfererPokemon(Pokemon p) {
this.collection.remove(p);
System.out.println(this.nom + " <20> transf<73>rer " + p.getNom());
}
public double vitesseMoyenne() {
double resultat = 0;
for (Pokemon p : collection)
resultat += p.calculerVitesse();
return resultat / collection.size();
}
public double vitesseMoyenneTYPE(String t) {
double resultat = 0;
int compteur = 0;
for (Pokemon p : collection) {
if (p.getType().getDescription().equals(t)) {
resultat += p.calculerVitesse();
compteur++;
}
}
return resultat / compteur;
}
public String toString() {
System.out.println("Joueur " + this.nom + ", niveau " + this.niveau +
"\rNombre de points : " + this.nbPoints +
"\rPoss<EFBFBD>de : ");
for (int i=0; i<collection.size(); i++) {
System.out.println(collection.get(i).affiche());
}
return "";
}
public void gagnerNiveau() {
this.niveau ++;
System.out.println(this.nom + " gagne un niveau, niveau actuelle : " + this.getNiveau());
}
}

View File

@ -0,0 +1,76 @@
package pokemons;
import java.io.*;
import java.util.*;
public class Partie {
//atributs
private ArrayList<Joueur> joueurs;
private ArrayList<Pokemon> pokemons;
//getters
public ArrayList<Pokemon> getPokemons() {
return pokemons;
}
//m<>thode
public void creerPokemons(String s) throws IOException, FileNotFoundException, InputMismatchException {
Scanner pokemon = new Scanner(new File (s));
ArrayList<Pokemon> pokemons= new ArrayList<Pokemon>();
while (pokemon.hasNextLine())
{
String str = pokemon.nextLine();
Scanner scn = new Scanner(str);
scn.useDelimiter("\\s* \\s*");
String nom = scn.next();
String type = scn.next();
double taille = Double.parseDouble(scn.next());
double poids = Double.parseDouble(scn.next());
int pv = Integer.parseInt(scn.next());
int pc = Integer.parseInt(scn.next());
switch (type) {
case "Eau" :
int nbNageoires = Integer.parseInt(scn.next());
PokemonEAU p1 = new PokemonEAU(nom, taille, poids, pv, pc, nbNageoires) ;
pokemons.add(p1);
break;
case "Plante" :
PokemonPLANTE p2 = new PokemonPLANTE(nom, taille, poids, pv, pc);
pokemons.add(p2);
break;
case "Feu" :
int nbPattes = Integer.parseInt(scn.next());
PokemonFEU p3 = new PokemonFEU(nom, taille, poids, pv, pc, nbPattes);
pokemons.add(p3);
break;
case "Electrik" :
int nb_Pattes = Integer.parseInt(scn.next());
int nbAiles = Integer.parseInt(scn.next());
int intensite = Integer.parseInt(scn.next());
PokemonELECTRIK p4 = new PokemonELECTRIK(nom, taille, poids, pv, pc, nb_Pattes, nbAiles, intensite);
pokemons.add(p4);
break;
}
scn.close();
}
pokemon.close();
this.pokemons = pokemons;
}
public void ajouterJoueur(Joueur j) {
ArrayList<Joueur> joueurs= new ArrayList<Joueur>();
joueurs.add(j);
this.joueurs = joueurs;
System.out.println(joueurs.toString());
}
public String toString() {
System.out.println();
System.out.println("Et voici la liste des pokemons disponibles : ");
for (int j=0; j< pokemons.size(); j++) {
System.out.println(pokemons.get(j).affiche());
}
return "";
}
}

View File

@ -0,0 +1,140 @@
package pokemons;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class PokeCentre {
public static void main(String[] args) {
try {
//creation Partie
Partie p = new Partie();
p.creerPokemons("../ListePokemon.txt");
Scanner sc = new Scanner(System.in);
System.out.println("Entrez nom Joueur 1");
String nomj1 = sc.next();
System.out.println("Entrez nom Joueur 2");
String nomj2 = sc.next();
Joueur j1 = new Joueur(nomj1);
p.ajouterJoueur(j1);
Joueur j2 = new Joueur(nomj2);
p.ajouterJoueur(j2);
System.out.println(p.toString());
//Choix des pok<6F>mons
//pour j1
for (int j=0; j<5; j++) {
System.out.println(j1.getNom() + " entrez le nom d'un pokemon que vous souhaitez attraper");
String nom = sc.next();
for (int i=0; i<p.getPokemons().size(); i++) {
if (p.getPokemons().get(i).getNom().equals(nom)) { //cherche le pokemon dans la liste
j1.attraperPokemon(p.getPokemons().get(i));
}
}
System.out.println("Voulez-vous ajouter un autre Pok<6F>mon ? (oui/non)");
String reponse = sc.next();
if (reponse.equals("non")) {
j = 6;
}
}
System.out.println(j1.getNom() + " votre collection de Pokemon est compl<70>te !");
System.out.println(j1.toString());
System.out.println();
//pour j2
for (int j=0; j<6; j++) {
System.out.println(j2.getNom() + " entrez le nom d'un pokemon que vous souhaitez attraper");
String nom = sc.next();
for (int i=0; i<p.getPokemons().size(); i++) {
if (p.getPokemons().get(i).getNom().equals(nom)) { //cherche le pokemon dans la liste
j2.attraperPokemon(p.getPokemons().get(i));
}
}
System.out.println("Voulez-vous ajouter un autre Pok<6F>mon ? (oui/non)");
String reponse = sc.next();
if (reponse.equals("non")) {
j = 6;
}
}
System.out.println(j2.getNom() + " votre collection de Pokemon est compl<70>te !");
System.out.println(j2.toString());
System.out.println();
//Combats !!!
System.out.println("Le combat peut commenc<6E> !!!");
Pokemon attaquant = null;
Pokemon deffenseur = null;
System.out.println(j1.getNom() + " quel pok<6F>mon voulez-vous mettre en jeux ? ");
String p1 = sc.next();
for (int i=0; i < j1.getCollection().size(); i++) {
if (j1.getCollection().get(i).getNom().equals(p1)) {
attaquant = j1.getCollection().get(i);
}
}
System.out.println(j2.getNom() + " quel pok<6F>mon voulez-vous mettre en jeux ?");
String p2 = sc.next();
for (int j=0; j < j2.getCollection().size(); j++) {
if (j2.getCollection().get(j).getNom().equals(p2)) {
deffenseur = j2.getCollection().get(j);
}
}
while (attaquant.getPv() > 0 && deffenseur.getPv() > 0) {
if (attaquant.calculerVitesse() > deffenseur.calculerVitesse()) {
attaquant.attaque(deffenseur);
deffenseur.attaque(attaquant);
}else {
deffenseur.attaque(attaquant);
attaquant.attaque(deffenseur);
}
if (attaquant.getPv() <= 0) {
System.out.println();
System.out.println(attaquant.getNom() + " est vaincu");
System.out.println();
j1.getCollection().remove(attaquant);
if (j1.getCollection().size() > 0) {
System.out.println(j1.getNom() + " quel pok<6F>mon voulez-vous remettre en jeux ?");
String pokemon = sc.next();
for (int i=0; i < j1.getCollection().size(); i++) {
if (j1.getCollection().get(i).getNom().equals(pokemon)) {
attaquant = j1.getCollection().get(i);
}
}
}
}
if (deffenseur.getPv() <= 0) {
System.out.println();
System.out.println(deffenseur.getNom() + " est vaincu");
System.out.println();
j2.getCollection().remove(deffenseur);
if (j2.getCollection().size() > 0) {
System.out.println(j2.getNom() + " quel pok<6F>mon voulez-vous remettre en jeux ?");
String pokemon = sc.next();
for (int i=0; i < j2.getCollection().size(); i++) {
if (j2.getCollection().get(i).getNom().equals(pokemon)) {
deffenseur = j2.getCollection().get(i);
}
}
}
}
}
System.out.println("Fin de partie !");
if (j1.getCollection().size() > 0) {
System.out.println(j1.getNom() + " est vainceur !");
}else System.out.println(j2.getNom() + " est vainceur !");
sc.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,70 @@
package pokemons;
import java.util.*;
public abstract class Pokemon implements IAttaque, Comparable<Pokemon> {
//attributs
private String nom;
private double taille; // en m
private double poids; // en kg
private int pv;
private int pc;
Type type;
//constructeurs
public Pokemon(String n, double t, double p, int v, int c) {
nom = n;
taille = t;
poids = p;
pv = v;
pc = c;
}
//getters
public String getNom() {
return nom;
}
public double getTaille() {
return taille;
}
public double getPoids() {
return poids;
}
public int getPv() {
return pv;
}
public int getPc() {
return pc;
}
public Type getType() {
return type;
}
//M<>thodes
public String toString() {
return "Pokemon "+nom
+" de type "+ type.getDescription()
+" ("+poids+" kg, "
+taille+" m, "
+pv+" pts de vie, "
+pc+" force de combat";
}
public void changePv(int modif) {
pv = Math.max(0, pv-modif);
System.out.println(this.nom + " perd " + modif + " pv");
}
public abstract void attaque(Pokemon p);
public abstract double calculerVitesse();
public abstract String affiche();
public int compareTo(Pokemon p)
{
int n = this.getType().getDescription().compareTo(p.getType().getDescription());
if (n == 0)
return this.getPv() - p.getPv();
else return n;
}
}

View File

@ -0,0 +1,39 @@
package pokemons;
public class PokemonEAU extends Pokemon {
//attribut
private int nb_nageoires;
//Constructeurs
public PokemonEAU(String n, double t, double p, int v, int c, int g) {
super (n, t, p, v, c);
nb_nageoires = g;
type = Type.EAU;
}
//Getters
public int getNb_nageoires() {
return nb_nageoires;
}
//m<>thodes
@Override
public double calculerVitesse() {
return (super.getPoids()*nb_nageoires)/25.0;
}
@Override
public String affiche() {
return this.toString() + ", " + nb_nageoires + " nageoires)";
}
@Override
public void attaque(Pokemon p) {
System.out.println(this.getNom() + " attaque " + p.getNom());
if (p.getType() == Type.FEU)
p.changePv(super.getPc()*2);
else if (p.getType() == Type.EAU|| p.getType() == Type.PLANTE)
p.changePv( (int) Math.round(super.getPc()*0.5));
else p.changePv(super.getPc());
}
}

View File

@ -0,0 +1,49 @@
package pokemons;
public class PokemonELECTRIK extends Pokemon {
//attributs
private int nb_pattes;
private int nb_ailes;
private double intensite;
//constructeur
public PokemonELECTRIK(String n, double t, double p, int v, int c, int g, int a, double i) {
super (n, t, p, v, c);
nb_pattes = g;
nb_ailes = a;
intensite = i;
type = Type.ELECTRIK;
}
//getters
public int getPattes() {
return nb_pattes;
}
public int getAiles() {
return nb_ailes;
}
public double getIntensite() {
return intensite;
}
//m<>thodes
@Override
public double calculerVitesse() {
return (nb_ailes+nb_pattes) * intensite * 0.05;
}
@Override
public String affiche() {
return this.toString()+ ", " +nb_pattes+" pattes, "+nb_ailes+" ailes, "+intensite+" mA)";
}
@Override
public void attaque(Pokemon p) {
System.out.println(this.getNom() + " attaque " + p.getNom());
if (p.getType() == Type.EAU)
p.changePv(super.getPc()*2);
else if (p.getType() == Type.ELECTRIK || p.getType() == Type.PLANTE)
p.changePv( (int) Math.round(super.getPc()*0.5));
else p.changePv(super.getPc());
}
}

View File

@ -0,0 +1,42 @@
package pokemons;
public class PokemonFEU extends Pokemon {
//attributs
private int nb_pattes;
//constructeurs
public PokemonFEU(String n, double t, double p, int v, int c, int g) {
super (n, t, p, v, c);
nb_pattes = g;
type = Type.FEU;
}
//getters
public int getPattes() {
return nb_pattes;
}
//m<>thodes
@Override
public double calculerVitesse() {
return this.getPoids()*nb_pattes* 0.03;
}
@Override
public String affiche() {
return this.toString() + ", " +super.getPoids()+" kg, "+super.getTaille()+" m, "+super.getPv()+" pts de vie, "+super.getPc()+" force de combat, "+nb_pattes+" pattes)";
}
@Override
public void attaque(Pokemon p) {
System.out.println(this.getNom() + " attaque " + p.getNom());
if (p.getType() == Type.PLANTE)
p.changePv(super.getPc()*2);
else if (p.getType() == Type.EAU || p.getType() == Type.ELECTRIK)
p.changePv( (int) Math.round(super.getPc()*0.5));
else p.changePv(super.getPc());
}
}

View File

@ -0,0 +1,32 @@
package pokemons;
public class PokemonPLANTE extends Pokemon {
//Constructeur
public PokemonPLANTE(String n, double t, double p, int v, int c) {
super (n, t, p, v, c);
type = Type.PLANTE;
}
//m<>thodes
@Override
public double calculerVitesse() {
return 10.0 / (this.getPoids()*this.getTaille());
}
@Override
public String affiche() {
return this.toString() + ")";
}
@Override
public void attaque(Pokemon p) {
System.out.println(this.getNom() + " attaque " + p.getNom());
if (p.getType() == Type.ELECTRIK)
p.changePv(super.getPc()*2);
else if (p.getType() == Type.FEU || p.getType() == Type.PLANTE)
p.changePv( (int) Math.round(super.getPc()*0.5));
else p.changePv(super.getPc());
}
}

View File

@ -0,0 +1,13 @@
package pokemons;
enum Type {
EAU ("EAU"),
ELECTRIK ("ELECTRIK"),
FEU ("FEU"),
PLANTE ("PLANTE");
Type(String s){description = s;}
private String description;
public String getDescription() {
return description;
}
}