264 lines
4.7 KiB
C++
264 lines
4.7 KiB
C++
#include <iostream>
|
|
#include <list>
|
|
#include <vector>
|
|
#include <fstream>
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
class Boule {
|
|
char couleur;
|
|
int points;
|
|
public:
|
|
|
|
void saisie(ifstream& f);
|
|
void affichage();
|
|
|
|
char getCouleur();
|
|
int getPoints();
|
|
|
|
bool operator==(Boule b);
|
|
bool operator<(Boule b) const;
|
|
};
|
|
|
|
char Boule::getCouleur() { return couleur; }
|
|
int Boule::getPoints() { return points; }
|
|
|
|
void Boule::saisie(ifstream& f) {
|
|
f >> couleur; f.ignore();
|
|
f >> points; f.ignore();
|
|
}
|
|
|
|
void Boule::affichage() {
|
|
cout << couleur << " : " << points;
|
|
}
|
|
|
|
bool Boule::operator==(Boule b) {
|
|
return couleur == b.couleur && points == b.points;
|
|
}
|
|
|
|
bool Boule::operator<(Boule b) const {
|
|
return (couleur == b.couleur) ? points < b.points : couleur < b.couleur;
|
|
}
|
|
|
|
template <class C>
|
|
void lec_boules(C& cont, const string fname) {
|
|
ifstream f(fname);
|
|
|
|
if (!f.is_open()) {
|
|
cout << "Erreur lors de la lecture du fichier" << endl;
|
|
exit(1);
|
|
}
|
|
|
|
cout << "Lecture du fichier " << fname << endl;
|
|
|
|
int N;
|
|
|
|
f >> N; f.ignore();
|
|
|
|
while ((N--) > 0) {
|
|
Boule b;
|
|
|
|
b.saisie(f); f.ignore();
|
|
cont.push_back(b);
|
|
}
|
|
}
|
|
|
|
template <class C>
|
|
void affichage(C cont) {
|
|
for (auto a : cont) {
|
|
a.affichage(); cout << endl;
|
|
}
|
|
}
|
|
|
|
template <class C>
|
|
void affichage(multimap<string, C> cont) {
|
|
|
|
for (auto a : cont) {
|
|
a.second.affichage(); cout << endl;
|
|
}
|
|
}
|
|
|
|
|
|
template <class C>
|
|
int penalite(C cont, char c) {
|
|
int i = 0;
|
|
for (auto a : cont) {
|
|
if (a.getCouleur() != c) {
|
|
i++;
|
|
}
|
|
}
|
|
return i;
|
|
}
|
|
|
|
template <class C>
|
|
int Som(C cont, char c) {
|
|
int score = 0;
|
|
|
|
for (auto a : cont) {
|
|
if (a.getCouleur() == c) {
|
|
score += a.getPoints();
|
|
}
|
|
else {
|
|
score -= a.getPoints();
|
|
}
|
|
}
|
|
|
|
return score;
|
|
}
|
|
|
|
// Robot1 = 1, Robot2 = 2, égalité = 0
|
|
template <class A, class B>
|
|
int getWinner(A a, char ac, B b, char bc) {
|
|
int SL = Som(a, ac);
|
|
int SV = Som(b, bc);
|
|
|
|
/*cout << "Score Robot1 : " << SL << endl;
|
|
cout << "Score Robot2 : " << SV << endl;
|
|
cout << endl;*/
|
|
|
|
if (SL > SV) {
|
|
return 1;
|
|
}
|
|
else if (SL < SV) {
|
|
return 2;
|
|
}
|
|
else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
template <class C>
|
|
void fill(multiset<Boule> &set, C c) {
|
|
for (Boule b : c) {
|
|
set.insert(b);
|
|
}
|
|
}
|
|
|
|
template <class C>
|
|
void fill(multimap<string,Boule> &m, C& c) {
|
|
affichage(m);
|
|
for (auto it = c.begin(); it != c.end(); ) {
|
|
cout << endl;
|
|
string k = it->getCouleur() + to_string(it->getPoints());
|
|
cout << "MMap : " << endl;
|
|
m.insert(make_pair(k, *it));
|
|
it = c.erase(it);
|
|
affichage(m);
|
|
cout << "Boules perdant : " << endl;
|
|
affichage(c);
|
|
}
|
|
}
|
|
|
|
template <class C>
|
|
void del_half(C& c) {
|
|
for (auto it = c.begin(); it != c.end();) {
|
|
if (it != c.end()) it++;
|
|
else break;
|
|
|
|
if (it != c.end())
|
|
it = c.erase(it);
|
|
}
|
|
}
|
|
|
|
|
|
int menu() {
|
|
int c;
|
|
|
|
cout << "1- LectureSaisieL et Affichage " << endl
|
|
<< "2- LectureSaisieV et Affichage" << endl
|
|
<< "3- Compter le nombre de boules pénalisantes dans L et V" << endl
|
|
<< "4- Qui est le gagnant? " << endl
|
|
<< "5- Déposer les boules du Robot perdant dans une MMap" << endl
|
|
<< "6- Afficher L, V et MMap " << endl
|
|
<< "7- Supprimer un élément sur deux de L, V et MMap" << endl;
|
|
cout << "Choix : ";
|
|
cin >> c; cin.ignore();
|
|
return c;
|
|
}
|
|
|
|
int main() {
|
|
setlocale(LC_ALL, "");
|
|
|
|
list<Boule> L1;
|
|
vector<Boule> V1;
|
|
|
|
int winner;
|
|
|
|
// Je ne comprend absolument pas pourquoi il faut utiliser une multimap
|
|
// Au lieu d'un multiset
|
|
// Ils veulent que la clé soit couleur + score concat??!
|
|
// C'est plus malin avec juste un set un opérateur de tri bien choisis (voir Boule::operator< )
|
|
// Dcp je vais faire en version set et version multimap
|
|
multimap<string, Boule> MMap;
|
|
multiset<Boule> MSet;
|
|
|
|
while (true) {
|
|
switch (menu()) {
|
|
case 1:
|
|
lec_boules(L1, "Robot1.txt");
|
|
affichage(L1);
|
|
break;
|
|
case 2:
|
|
lec_boules(V1, "Robot2.txt");
|
|
affichage(V1);
|
|
break;
|
|
case 3:
|
|
cout << "L : nombre de boules pénalisantes = " << penalite(L1, 'R') << endl;
|
|
cout << "V : nombre de boules pénalisantes = " << penalite(V1, 'B') << endl;
|
|
|
|
break;
|
|
case 4:
|
|
winner = getWinner(L1, 'R', V1, 'B');
|
|
|
|
switch (winner) {
|
|
case 1:
|
|
case 2:
|
|
cout << "Le gagnant est : Robot"<<winner << endl;
|
|
break;
|
|
default:
|
|
cout << "Pas de gagnant!" << endl;
|
|
break;
|
|
}
|
|
|
|
break;
|
|
|
|
case 5:
|
|
winner = getWinner(L1, 'R', V1, 'B');
|
|
switch (winner) {
|
|
case 1:
|
|
fill(MSet, V1);
|
|
fill(MMap, V1);
|
|
break;
|
|
case 2:
|
|
fill(MSet, L1);
|
|
fill(MMap, V1);
|
|
break;
|
|
default:
|
|
cout << "Pas de gagnant!" << endl;
|
|
break;
|
|
}
|
|
|
|
break;
|
|
case 6:
|
|
cout << "L1 : " << endl;
|
|
affichage(L1);
|
|
cout << "V1 : " << endl;
|
|
affichage(V1);
|
|
cout << "MMap : " << endl;
|
|
affichage(MMap);
|
|
|
|
break;
|
|
case 7:
|
|
del_half(L1);
|
|
del_half(V1);
|
|
del_half(MMap);
|
|
|
|
break;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
} |