Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 9cf897e6 rédigé par Florian Dargère's avatar Florian Dargère
Parcourir les fichiers

Ajout d'itérateurs

parent 7317fda4
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -41,35 +41,35 @@ CMap::~CMap()
void CMap::FindConnexComponent(int idPlayer)
{
int* MarkedCells = new int[nbCells];
for (unsigned int i = 0; i < nbCells; i++) MarkedCells[i] = 0;
std::vector<int> MarkedCells;
//int* MarkedCells = new int[nbCells];
//for (unsigned int i = 0; i < nbCells; i++) MarkedCells[i] = 0;
std::vector<Region*> vec_regions; //Vecteur de composantes connexes
for (unsigned int i = 0; i < nbCells; i++) {
if (cells[i]->owner == idPlayer && MarkedCells[i] == 0) {
for (auto& cell: cells) {
if (cell->owner == idPlayer && std::find(begin(MarkedCells), end(MarkedCells), cell->id) == end(MarkedCells)) {
//On a trouv une cellule appartenant idPlayer et on calcule sa composante connexe associe
std::vector<Cell*> vec_region;
std::function<void(Cell*)> CalculRegion;
CalculRegion = [this, &MarkedCells, &vec_region, &CalculRegion, idPlayer](Cell* cell) {
vec_region.push_back(cells[cell->id]);
MarkedCells[cell->id] = 1;
MarkedCells.push_back(cell->id);
for (unsigned int j = 0; j < cell->nbNeighbors; j++) {
if (cell->neighbors[j]->owner == idPlayer && MarkedCells[cell->neighbors[j]->id] == 0) {
CalculRegion(cell->neighbors[j]);
for (auto& cell_neighbor: cell->neighbors) {
if (cell_neighbor->owner == idPlayer && std::find(begin(MarkedCells), end(MarkedCells), cell_neighbor->id) == end(MarkedCells)) {
CalculRegion(cell_neighbor);
}
}
};
CalculRegion(cells[i]);
CalculRegion(cell);
vec_regions.push_back(new Region(vec_region));
std::sort(begin(vec_regions), end(vec_regions), [](Region* reg1, Region* reg2) {return (reg1->r_cells.size() > reg2->r_cells.size()); });
}
}
regions[idPlayer] = vec_regions;
delete[](MarkedCells);
}
std::pair<std::vector<Cell*>, int> CMap::DijkstraAlgo(Cell* cell_depart, Cell* cell_arrivee)
......@@ -131,13 +131,13 @@ std::pair<std::vector<Cell*>, int> CMap::DijkstraAlgo(Cell* cell_depart, Cell* c
}
int distance;
for (unsigned int i = 0; i < current_cell.first->nbNeighbors; i++) {
if (current_cell.first->neighbors[i]->owner != id_player || current_cell.first->neighbors[i] == cell_arrivee) {
for (auto &current_cell_neighbor: current_cell.first->neighbors) {
if (current_cell_neighbor->owner != id_player || current_cell_neighbor == cell_arrivee) {
//On push chaque voisin qui ne nous appartient pas dans la queue
std::vector<Cell*> path(current_cell.second.first);
std::pair<std::vector<Cell*>, int> cell_pair = std::make_pair(path, 9 - nbDices_depart);
distance = current_cell.second.second + current_cell.first->neighbors[i]->nbDices;
queue.push(std::make_pair(current_cell.first->neighbors[i], cell_pair));
distance = current_cell.second.second + current_cell_neighbor->nbDices;
queue.push(std::make_pair(current_cell_neighbor, cell_pair));
}
}
}
......@@ -148,10 +148,10 @@ std::pair<std::vector<Cell*>, int> CMap::DijkstraAlgo(Region* region_depart, Reg
{
unsigned int Id = region_depart->r_cells[0]->owner;
std::pair<std::vector<Cell*>, int> best_path = DijkstraAlgo(regions[Id][0]->r_cells[0], regions[Id][1]->r_cells[0]);
for (unsigned int i = 0; i < regions[Id][0]->r_cells.size(); i++) {
for (unsigned int j = 0; j < regions[Id][1]->r_cells.size(); j++) {
std::pair<std::vector<Cell*>, int> new_path1 = DijkstraAlgo(regions[Id][0]->r_cells[i], regions[Id][1]->r_cells[j]);
std::pair<std::vector<Cell*>, int> new_path2 = DijkstraAlgo(regions[Id][1]->r_cells[j], regions[Id][0]->r_cells[i]);
for (auto& cell_region1: regions[Id][0]->r_cells) {
for (auto& cell_region2: regions[Id][1]->r_cells) {
std::pair<std::vector<Cell*>, int> new_path1 = DijkstraAlgo(cell_region1, cell_region2);
std::pair<std::vector<Cell*>, int> new_path2 = DijkstraAlgo(cell_region2, cell_region1);
if (new_path1.second < best_path.second) {
best_path = new_path1;
}
......@@ -160,7 +160,6 @@ std::pair<std::vector<Cell*>, int> CMap::DijkstraAlgo(Region* region_depart, Reg
}
}
}
return(best_path);
}
......
......@@ -12,28 +12,22 @@ StrategyBase::StrategyBase(unsigned int id, unsigned int nbPlayer, const SMap* m
{
}
StrategyBase::~StrategyBase()
{
// détruire proprement la structure Map
}
void StrategyBase::updateOwnedCells() {
// mise à jour du vecteur des cellules alliées triées par nombre de voisins alliés
ownedCells.clear();
for (unsigned int i = 0; i < Map.nbCells; i++) {
if (Map.cells[i]->owner == Id) {
ownedCells.push_back(Map.cells[i]);
for (auto& cell: Map.cells) {
if (cell->owner == Id) {
ownedCells.push_back(cell);
}
}
std::sort(ownedCells.begin(), ownedCells.end(), [this](Cell* c1, Cell* c2) {
int n1 = 0, n2 = 0;
for (unsigned int i = 0; i < c1->nbNeighbors; i++) if (c1->neighbors[i]->owner == Id) n1++;
for (unsigned int j = 0; j < c2->nbNeighbors; j++) if (c2->neighbors[j]->owner == Id) n2++;
for (auto& neighbor: c1->neighbors) if (neighbor->owner == Id) n1++;
for (auto& neighbor: c2->neighbors) if (neighbor->owner == Id) n2++;
return n1 > n2;
});
}
......@@ -61,23 +55,26 @@ bool StrategyBase::PlayTurn(unsigned int gameTurn, const SGameState* state, STur
}
}
//Stratégie de renforcement des frontières (attaques depuis l'intérieur)
//updateOwnedCells();
//for (SCell* cell : ownedCells) {
// for (unsigned int numNeighbor = 0; numNeighbor < cell->nbNeighbors; numNeighbor++) {
// if (cell->infos.nbDices > cell->neighbors[numNeighbor]->infos.nbDices and cell->neighbors[numNeighbor]->infos.owner != Id) {
// turn->cellFrom = cell->infos.id;
// turn->cellTo = cell->neighbors[numNeighbor]->infos.id;
//for (auto& cell : ownedCells) {
// for (auto& neighbor: cell->neighbors) {
// if (cell->nbDices > neighbor->nbDices and neighbor->owner != Id) {
// turn->cellFrom = cell->id;
// turn->cellTo = neighbor->id;
// return(true);
// }
// }
//}
for (unsigned int numCell = 0; numCell < Map.nbCells; numCell++) {
if (Map.cells[numCell]->owner == Id) {
for (unsigned int numNeighbor = 0; numNeighbor < Map.cells[numCell]->nbNeighbors; numNeighbor++) {
if (probabilites[Map.cells[numCell]->nbDices][Map.cells[numCell]->neighbors[numNeighbor]->nbDices] > 0.7 and Map.cells[numCell]->neighbors[numNeighbor]->owner != Id) {
turn->cellFrom = numCell;
turn->cellTo = Map.cells[numCell]->neighbors[numNeighbor]->id;
//Stratégie consistant à jouer aléatoirement les coups dont la probabilité de victoire est supérieure à 0.7
for (auto& cell: Map.cells) {
if (cell->owner == Id) {
for (auto& neighbor: cell->neighbors) {
if (probabilites[cell->nbDices][neighbor->nbDices] > 0.7 and neighbor->owner != Id) {
turn->cellFrom = cell->id;
turn->cellTo = neighbor->id;
return(true);
}
}
......
......@@ -12,7 +12,7 @@ public:
StrategyBase(unsigned int id, unsigned int nbPlayer, const SMap* map);
StrategyBase(const StrategyBase &obj) =delete;
StrategyBase(StrategyBase &&obj) =delete;
~StrategyBase();
~StrategyBase()=default;
StrategyBase& operator=(const StrategyBase &obj) =delete;
StrategyBase& operator=(StrategyBase &&obj) =delete;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter