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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| #include <iostream>
#include <cmath>
using namespace std;
enum Pavillon { JollyRogers, CompagnieDuSenegal, CompagnieDOstende };
enum Etat { Intact, Endommage, Coule };
int sq(int x)
{
return x*x;
}
class Coordonnees
{
public:
Coordonnees(int un_x, int un_y) : x_(un_x), y_(un_y) {}
int x() const {
return x_;
}
int y() const {
return y_;
}
void operator+=(Coordonnees const& autre); // à définir plus bas
private:
int x_;
int y_;
};
class Navire
{
/*****************************************************
* Compléter le code à partir d'ici
*****************************************************/
protected:
Coordonnees position_;
Pavillon pavillon_;
Etat etat_;
const int rayon_rencontre = 10;
public:
Navire(int i, int j, Pavillon& u) :position_(i, j), pavillon_(u) {};
Coordonnees position() { return position_; }
void avancer(int de_x, int de_y) { if (etat_ != Coule) { Coordonnees r(de_x, de_y); position_ += r; } }
void renflouer() { etat_ = Intact; }
ostream& afficher(ostream& sortie) {
// sortie << pavillon_ << "en " << position_ << "battant pavillon" << pavillon_ << "," << etat_;
return sortie;
}
int getPavillon() { return pavillon_; }
virtual void attaque(Navire& nav) = 0;
virtual void replique(Navire& nav) = 0;
virtual void est_toucher() = 0;
virtual void rencontrer(Navire& nav) {
if (sqrt(pow((position_.x() - position_.y()), 2) + pow((nav.position().x() - nav.position().y()), 2))<Navire::rayon_rencontre && pavillon_ != nav.getPavillon())
attaque(nav);
}
};
class Pirate :public virtual Navire
{
private:
public:
Pirate(int i, int j, Pavillon u) :Navire(i, j, u) { }
void attaque(Navire& i) override { cout << "A l'abordage !"<<endl; i.est_toucher(); }
void replique(Navire& i) override { cout << "Non mais, ils nous attaquent ! On riposte !!" << endl; attaque(i); }
void est_toucher() override { if (etat_ == Intact) { etat_ = Endommage; } if (etat_ == Endommage) { etat_ = Coule; } }
};
class Marchand :public virtual Navire
{
private:
public:
Marchand(int i, int j, Pavillon u):Navire(i,j,u) { }
void attaque(Navire& i) override { cout << "On vous aura !"; i.est_toucher(); }
void replique(Navire& i) override { if (etat_ == Coule) cout << "SOS je coule !" << endl; else cout <<"Même pas peur !"<< endl; }
void est_toucher() override { etat_ = Coule; }
};
class Felon :public Pirate, public Marchand
{
private:
public:
Felon(int i, int j, Pavillon u) :Navire(i,j,u),Pirate(i,j,u),Marchand(i,j,u) { }
void attaque(Navire& i) override { cout << "A l'abordage !" << endl; i.est_toucher(); }
void replique(Navire& i) override { if (etat_ == Coule) cout << "SOS je coule !" << endl; else cout << "Même pas peur !" << endl; }
void est_toucher() override { if (etat_ == Intact) { etat_ = Endommage; } if (etat_ == Endommage) { etat_ = Coule; } }
};
double distance (Navire const& in, Navire const & un)
{
double i = distance((in.position()), (un.position()));
return i;
}
ostream& operator << (ostream& sortie, Navire& na)
{
return na.afficher(sortie);
}
void Coordonnees::operator+=(Coordonnees const& autre)
{
x_ += autre.x();
y_ += autre.y();
// à définir ici
}
double distance(Coordonnees const& a, Coordonnees const& b)
{
double i = pow((a.x() - b.x()), 2) + pow((a.y() - b.y()), 2);
double u = sqrt(i);
return u;
}
ostream& operator <<(ostream& sortie, Coordonnees& en)
{
sortie << "(" << en.x() << "' " << en.y() << ")";
return sortie;
}
ostream& operator <<(ostream& sortie, Pavillon const& en)
{
if (en == JollyRogers)
{
sortie << "pirate";
}
else {
if (en == CompagnieDuSenegal) {
sortie << "français";
}
else {
if (en == CompagnieDOstende) {
sortie << "autichien";
}
else {
sortie << "pavillon inconnu";
}
}
}
return sortie;
}
ostream& operator << (ostream& sortie, Etat const& en)
{
if (en == Intact) {
sortie << "intact";
}
else {
if (en == Endommage) {
sortie << "ayant subi des dommages";
}
else {
if (en == Coule) {
sortie << "coulé";
}
else {
sortie << "état inconnu";
}
}
}
return sortie;
}
/*******************************************
* Ne rien modifier après cette ligne.
*******************************************/
void rencontre(Navire& ship1, Navire& ship2)
{
cout << "Avant la rencontre :" << endl;
cout << ship1 << endl;
cout << ship2 << endl;
cout << "Distance : " << distance(ship1, ship2) << endl;
ship1.rencontrer(ship2);
cout << "Apres la rencontre :" << endl;
cout << ship1 << endl;
cout << ship2 << endl;
}
int main()
{
// Test de la partie 1
cout << "===== Test de la partie 1 =====" << endl << endl;
// Un bateau pirate 0,0
Pirate ship1(0, 0, JollyRogers);
cout << ship1 << endl;
// Un bateau marchand en 25,0
Marchand ship2(25, 0, CompagnieDuSenegal);
cout << ship2 << endl;
cout << "Distance : " << distance(ship1, ship2) << endl;
cout << "Quelques déplacements..." << endl;
cout << " en haut à droite :" << endl;
// Se déplace de 75 unités à droite et 10 en haut
ship1.avancer(75, 10);
cout << ship1 << endl;
cout << " vers le bas :" << endl;
ship1.avancer(0, -5);
cout << ship1 << endl;
cout << endl << "===== Test de la partie 2 =====" << endl << endl;
cout << "Bateau pirate et marchand ennemis (trop loins) :" << endl;
rencontre(ship1, ship2);
cout << endl << "Bateau pirate et marchand ennemis (proches) :" << endl;
ship1.avancer(-40, -2);
ship2.avancer(10, 2);
rencontre(ship1, ship2);
cout << endl << "Deux bateaux pirates ennemis intacts (proches) :" << endl;
Pirate ship3(33, 8, CompagnieDOstende);
rencontre(ship1, ship3);
cout << endl << "Bateaux pirates avec dommages, ennemis :" << endl;
rencontre(ship1, ship3);
cout << endl << "Bateaux marchands ennemis :" << endl;
Marchand ship4(21, 7, CompagnieDuSenegal);
Marchand ship5(27, 2, CompagnieDOstende);
rencontre(ship4, ship5);
cout << endl << "Pirate vs Felon :" << endl;
ship3.renflouer();
Felon ship6(32, 10, CompagnieDuSenegal);
rencontre(ship3, ship6);
cout << endl << "Felon vs Pirate :" << endl;
rencontre(ship6, ship3);
return 0;
} |
Partager