-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArene.java
More file actions
132 lines (126 loc) · 5.15 KB
/
Arene.java
File metadata and controls
132 lines (126 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.ArrayList;
import java.util.List;
public class Arene{
private static Arene self;
private List<Observateur> observateurs;
private MonstreFactory factory;
private Monstre monstre1=null;
private Monstre monstre2=null;
private Arene(){
this.observateurs=new ArrayList<>();
this.factory=new MonstreFactory();
}
public static Arene get_instance(){
if (self==null){
self=new Arene();
}
return self;
}
public void add_observateur(Observateur obs){
this.observateurs.add(obs);
}
protected void notifier(String str){
for (Observateur obs:observateurs){
obs.notifier(str);
}
}
protected void notifier(char ch){
for (Observateur obs:observateurs){
obs.notifier(ch);
}
}
protected void notifier(int nb){
for (Observateur obs:observateurs){
obs.notifier(nb);
}
}
public void ajoute_monstre(Monstre monstre){
if (this.monstre1!=null){
this.monstre1=monstre;
}
else if (this.monstre2!=null){
this.monstre2=monstre;
}
else{
this.notifier("Des clandestins ont essayé d'introduire le "+monstre.appellation()+" dans l'arène alors qu'elle était déjà complète.");
}
}
public int tournoi(Monstre[] monstres){
int[] resultats=new int[monstres.length];
for (int i=0;i<monstres.length-1;i++){
for (int j=i+1;j<monstres.length;j++){
self.notifier("\nOn fait combattre "+monstres[i].appellation()+" et "+monstres[j].appellation()+"\n----------------------------------\n");
int gagnant=self.combat(monstres[i],monstres[j]);
monstres[i].revive();
monstres[j].revive();
if(gagnant==0){
resultats[i]++;
}
else{
resultats[j]++;
}
}
}
this.notifier("\n Combats terminés. Voici les résultats par compétiteur : \n\n");
int maxi=0;
for (int i=0;i<monstres.length;i++){
this.notifier(monstres[i].appellation()+" a gagné "+resultats[i]+"/"+(monstres.length-1)+" matchs.\n");
if (resultats[maxi]<resultats[i]){
maxi=i;
}
}
this.notifier("\n");
return maxi;
}
public int combat(Monstre monstre1,Monstre monstre2){
boolean mort;
while (true){
if (monstre1.speed()>=monstre2.speed()){
try{
mort=monstre2.takes_damage(monstre1.attacks(monstre2.speed()));
this.notifier(monstre1.appellation()+" attaque "+monstre2.appellation()+". Il lui reste "+monstre2.hp()+" points de vie.\n");
if (mort){
this.notifier(monstre2.appellation()+" n'est plus en état de se battre. "+monstre1.appellation()+" est déclaré vainqueur du duel.\n");
return (0);
}
}
catch(MortException e){ //Si on a fait un oopsie, ça déclenche mortexception.
return (1);
}
}
else{
try{
mort=monstre1.takes_damage(monstre2.attacks(monstre1.speed()));
this.notifier(monstre2.appellation()+" attaque "+monstre1.appellation()+". Il lui reste "+monstre1.hp()+" points de vie.\n");
if (mort){
this.notifier(monstre1.appellation()+" n'est plus en état de se battre. "+monstre2.appellation()+" est déclaré vainqueur du duel.\n");
return (1);
}
}
catch(MortException e){
return (0);
}
}
}
}
public static void main(String[] args){
Arene self=Arene.get_instance();
Observateur sysout=new Terminal(System.out); //L'héritage fait que ce sont des observateurs même s'ils n'ont pas exactement le même type
Observateur logs=new File("./logs.txt");
self.add_observateur(sysout);
self.add_observateur(logs);
self.notifier("On a ajouté un fichier de log et le terminal en tant qu'observateurs\n");
String [][] datanames={{"Dragon","Dracolosse"},{"Professeur","Hugo Guillaume"},{"Professeur","Golden Yann"}};
int[][]datavalues={{100,50,20},{5,3,30},{20,10,2}}; //hp,attack,speed
Monstre[] competiteurs=new Monstre[datanames.length];
for (int i=0;i<datanames.length;i++) {
Monstre monstre = self.factory.create_monster(datanames[i][0],datanames[i][1],datavalues[i][0],datavalues[i][1],datavalues[i][2]);
self.notifier("On crée " + monstre.appellation()+"\n");
competiteurs[i]=monstre;
}
competiteurs[0]=new AvecArme(competiteurs[0],1.5,"feu");
self.notifier("On rend "+competiteurs[0].appellation()+"\n");
int gagnant=self.tournoi(competiteurs);
self.notifier(competiteurs[gagnant].appellation()+" a gagné le tournoi.\n");
}
}