Skip to content
Snippets Groups Projects
Commit 5ec13c0d authored by drfailer's avatar drfailer
Browse files

capture weight

parent 92d3f5f7
Branches
Tags V1.1
No related merge requests found
Pipeline #10315 failed
......@@ -5,5 +5,5 @@ project(civilisation)
add_compile_options(-Wall -Wextra -Wuninitialized -g)
include_directories(${PROJECT_SOURCE_DIR})
add_executable(civlisation main.cxx Citizen.cxx Point.cxx Policy.cxx Cell.cxx
add_executable(civilisation main.cxx Citizen.cxx Point.cxx Policy.cxx Cell.cxx
View.cxx Map.cxx Agent.cxx AgentState.cxx Civilisation.cxx Simulator.cxx Test.cxx)
......@@ -8,24 +8,54 @@
std::mt19937 g_gen32;
Citizen::Citizen(COLORS color, int x, int y):
Agent(color), position(x, y), busy(false)
{
}
Citizen::Citizen(COLORS color, int x, int y)
: Agent(color), position(x, y), busy(false) {}
// TODO: faire dans la view
/**
* @brief Withdraw citizens of `lst` that are next to (x, y).
*
* @param x X coordinate.
* @param y Y coordinate.
* @param lst List of viewed citizens.
*
* @return List of reachagle citizens.
*/
std::list<Citizen *> getClose(int x, int y, std::list<Citizen *> lst) {
std::list<Citizen *> output;
for (Citizen *c : lst) {
if ((c->getPosition().x() >= x - 1 && c->getPosition().x() <= x + 1)
&& (c->getPosition().y() >= y - 1 && c->getPosition().y() <= y + 1)
&& !c->isBusy()) {
if ((c->getPosition().x() >= x - 1 && c->getPosition().x() <= x + 1) &&
(c->getPosition().y() >= y - 1 && c->getPosition().y() <= y + 1) &&
!c->isBusy()) {
output.push_back(c);
}
}
return output;
}
/**
* @brief Count the number of cell that are captured by the `color`
* civilisation.
*
* @param view View of the citizen.
* @param color Color to search for.
* @param x X coordinate.
* @param y Y coordinate.
*
* @return The number of cells in `color` next to (x, y).
*/
int countCapturedCell(View *view, COLORS color, int x, int y) {
int output = 0;
for (int i = std::max(y - 1, 0); i < std::min(view->getH(), y + 1); ++i) {
for (int j = std::max(x - 1, 0); j < std::min(view->getW(), x + 1); ++j) {
if (view->get(i, j).getColor() == color)
output++;
}
}
return output;
}
/******************************************************************************/
/* methodes */
/******************************************************************************/
......@@ -50,11 +80,12 @@ std::list<Citizen*> getClose(int x, int y, std::list<Citizen*> lst) {
*
* TODO: faire des fonction inline pour les tests.
*/
AgentState* Citizen::makeAction(View* view, Policy policy)
{
AgentState *Citizen::makeAction(View *view, Policy policy) {
AgentState *outputState;
std::list<Citizen *> closeAllies;
std::list<Citizen *> closeEnemies;
int capturedCellNumber =
countCapturedCell(view, color, position.x(), position.y());
// récupération des allier proches
closeAllies = getClose(position.x(), position.y(), view->getAllies());
......@@ -63,27 +94,26 @@ AgentState* Citizen::makeAction(View* view, Policy policy)
if (!closeEnemies.empty() && g_gen32() % 100 < policy.fight()) {
/* std::cout << "fight" << std::endl; */
outputState = fight(closeEnemies);
}
else if (!closeAllies.empty() && g_gen32()%100 < policy.interact()) {
} else if (!closeAllies.empty() && g_gen32() % 100 < policy.interact()) {
/* std::cout << "interact" << std::endl; */
outputState = interact(closeAllies);
}
else if (view->get(position.y(), position.x()).getColor() != color
&& g_gen32()%100 < policy.capture()) {
} else if (view->get(position.y(), position.x()).getColor() != color &&
g_gen32() % 100 <
policy.capture() +
capturedCellNumber * policy.captureIncrement()) {
/* std::cout << "capture" << std::endl; */
// TODO: la view doit donner les cases alentours
outputState = capture(); // TODO: la proba de capture doit augmenter en fonction
outputState =
capture(); // TODO: la proba de capture doit augmenter en fonction
// du nombre de cases capturées autour du cityen
}
else {
} else {
/* std::cout << "move" << std::endl; */
outputState = move(view, policy);
}
return outputState;
}
static inline bool isAlly(View* view, int x, int y, COLORS c)
{
bool isAlly(View *view, int x, int y, COLORS c) {
return view->get(y, x).getCitizen()->getColor() == c;
}
......@@ -97,8 +127,7 @@ static inline bool isAlly(View* view, int x, int y, COLORS c)
*
* @return true if `other` is nearer.
*/
static inline int nearer(Point citizen, Point currentNearest, Point other)
{
int nearer(Point citizen, Point currentNearest, Point other) {
return citizen.distance2(currentNearest) > citizen.distance2(other);
}
......@@ -110,8 +139,7 @@ static inline int nearer(Point citizen, Point currentNearest, Point other)
* Dans l'idéal, il faudrait que le citoyen ait un objectif sur un certain
* nombre de tours.
*/
AgentState* Citizen::move(View* view, Policy policy)
{
AgentState *Citizen::move(View *view, Policy policy) {
// TODO: utiliser la policy pour se diriger vers ses allié ou vers un ennemi
// Point nearestAllie(-1, -1);
// Point nearestEnemie(-1, -1);
......@@ -121,10 +149,13 @@ AgentState* Citizen::move(View* view, Policy policy)
int chosen;
AgentState *state;
for (int i = std::max(position.y() - 1, 0); i <= std::min(19, position.y() + 1); ++i) {
for (int j = std::max(position.x() - 1, 0); j <= std::min(19, position.x() + 1); ++j) {
for (int i = std::max(position.y() - 1, 0);
i <= std::min(19, position.y() + 1); ++i) {
for (int j = std::max(position.x() - 1, 0);
j <= std::min(19, position.x() + 1); ++j) {
if (view->get(i, j).getCitizen() == nullptr)
availablePosition.push_back(Point(j, i)); // NOTE: c'est la bazar avec les points et les indices
availablePosition.push_back(
Point(j, i)); // NOTE: c'est la bazar avec les points et les indices
}
}
if (availablePosition.size() > 0) {
......@@ -135,8 +166,7 @@ AgentState* Citizen::move(View* view, Policy policy)
}
}
state = new AgentState(availablePosition.front(), ACTIONS::MOVE);
}
else {
} else {
state = new AgentState(position, ACTIONS::MOVE);
}
return state;
......@@ -147,8 +177,7 @@ AgentState* Citizen::move(View* view, Policy policy)
*
* @return Un état qui permet de signifier que la case courante est capturée.
*/
AgentState* Citizen::capture()
{
AgentState *Citizen::capture() {
return new AgentState(position, ACTIONS::CAPTURE);
}
......@@ -160,8 +189,7 @@ AgentState* Citizen::capture()
* @return Un état qui permet de signifier qu'il y a eu une intéraction avec un
* allié positionné à un certain endroit.
*/
AgentState* Citizen::interact(std::list<Citizen*> allies)
{
AgentState *Citizen::interact(std::list<Citizen *> allies) {
int random = g_gen32() % allies.size();
while (random > 0) {
......@@ -179,10 +207,10 @@ AgentState* Citizen::interact(std::list<Citizen*> allies)
* @return Un état qui permet de signifier qu'un combat à lieux avec un ennemi à
* une certaine position.
*/
// TODO: Prendre en compte la couleur des ennemis dans le cas où il y a plus de 2
// TODO: Prendre en compte la couleur des ennemis dans le cas où il y a plus de
// 2
// civilisations.
AgentState* Citizen::fight(std::list<Citizen*> enemies)
{
AgentState *Citizen::fight(std::list<Citizen *> enemies) {
int random = g_gen32() % enemies.size();
while (random > 0) {
......@@ -201,8 +229,7 @@ AgentState* Citizen::fight(std::list<Citizen*> enemies)
/* to string */
/******************************************************************************/
std::string Citizen::toString()
{
std::string Citizen::toString() {
std::ostringstream oss;
oss << "Citizen: " << Agent::toString() << ", " << position.toString();
......@@ -213,27 +240,17 @@ std::string Citizen::toString()
/* accessors */
/******************************************************************************/
void Citizen::setPosition(Point position)
{
this->position = position;
}
void Citizen::setPosition(Point position) { this->position = position; }
Point Citizen::getPosition()
{
return position;
}
Point Citizen::getPosition() { return position; }
bool& Citizen::isBusy()
{
return busy;
}
bool &Citizen::isBusy() { return busy; }
/******************************************************************************/
/* print */
/******************************************************************************/
void Citizen::print()
{
void Citizen::print() {
switch (color) {
case ROUGE:
std::cout << "RR";
......@@ -248,4 +265,3 @@ void Citizen::print()
break;
}
}
......@@ -2,7 +2,7 @@
#include "Agent.hxx"
Civilisation::Civilisation(): Civilisation(BLANC, std::list<Citizen*>(),
Policy(30, 30, 30))
Policy(30, 30, 30, 5))
{
}
......
#include "Policy.hxx"
Policy::Policy(int fightWeight, int interactWeight, int captureWeight):
fightWeight(fightWeight), interactWeight(interactWeight),
captureWeight(captureWeight)
{
}
unsigned long int Policy::fight()
{
return fightWeight;
......
#ifndef __POLICY__
#define __POLICY__
class Policy
{
class Policy {
// NOTE: les poids sont compris entre 0 et 100;
private:
unsigned long int fightWeight;
unsigned long int interactWeight;
unsigned long int captureWeight;
unsigned long int captureWeightIncrement;
public:
unsigned long int fight();
unsigned long int interact();
unsigned long int capture();
unsigned long int captureIncrement() {
return captureWeightIncrement;
}
void setFight(int);
void setInteract(int);
void setCapture(int);
Policy(int, int, int);
Policy(int fightWeight, int interactWeight, int captureWeight,
int captureWeightIncrement)
: fightWeight(fightWeight), interactWeight(interactWeight),
captureWeight(captureWeight), captureWeightIncrement(captureWeight) {}
};
#endif
......@@ -33,7 +33,7 @@ Simulator::~Simulator() {
void Simulator::handleFights(
std::list<std::pair<Citizen *, Citizen *>> fighters) {
for (std::pair<Citizen *, Citizen *> cts : fighters) {
if (g_gen32() % 100 < 50) {
if (g_gen32() % 100 < 65) {
deleteCitizen(cts.first);
cts.second->isBusy() = false;
} else {
......
......@@ -48,7 +48,7 @@ void testMakeAction() {
Citizen *ally = new Citizen(BLEU, 4, 3);
Citizen *enemy = new Citizen(VERT, 3, 3);
Cell **percetion = initPerception(10, 10);
Policy policy(30, 30, 30);
Policy policy(30, 30, 10, 8);
Map *map = new Map(20, 20);
map->get(4, 4).setCitizen(c);
map->get(4, 3).setCitizen(ally);
......@@ -82,8 +82,8 @@ int main(int, char**)
new Citizen(ROUGE, 18, 18),
new Citizen(ROUGE, 19, 19)
};
Civilisation *civB = new Civilisation(BLEU, blue, Policy(30, 10, 20));
Civilisation *civR = new Civilisation(ROUGE, red, Policy(30, 10, 20));
Civilisation *civB = new Civilisation(BLEU, blue, Policy(10, 10, 10, 10));
Civilisation *civR = new Civilisation(ROUGE, red, Policy(50, 5, 10, 10));
// map->printMap();
Simulator simu(map, std::list<Civilisation*>{civR, civB});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment