summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/Character.hpp6
-rw-r--r--src/Character.cpp95
-rw-r--r--src/List.cpp2
-rw-r--r--src/main.cpp4
4 files changed, 75 insertions, 32 deletions
diff --git a/inc/Character.hpp b/inc/Character.hpp
index 67fcfd2..f47589a 100644
--- a/inc/Character.hpp
+++ b/inc/Character.hpp
@@ -6,6 +6,8 @@
#include <vector>
#include <string>
#include "Location.hpp"
+#include <cmath>
+#include <iostream>
using namespace std;
class Character
@@ -16,7 +18,8 @@ class Character
void draw(WINDOW * w);
void action(vector <Character> men, vector <Location> impassable);
bool check(Location L, vector <Character> men, vector <Location> impassable);
- void astar(vector <Character> men, vector <Location> impassable);
+ void heuristic(vector <Character> men, vector <Location> impassable);
+ vector <Location> getLocal(Location L, vector <Character> men, vector <Location> impassable);
void wander(vector <Character> men, vector <Location> impassable);
Location getLocation(){return l;}
char getSymbol(){return symbol;}
@@ -29,6 +32,7 @@ class Character
int row;
int col;
int color;
+// vector <Location> open;
};
#endif
diff --git a/src/Character.cpp b/src/Character.cpp
index 8102b4c..012b1a2 100644
--- a/src/Character.cpp
+++ b/src/Character.cpp
@@ -5,6 +5,7 @@ Character::Character(char nSymbol, int nColor, Location L)
symbol = nSymbol;
color = nColor;
l = L;
+ d = Location(100,100);
}
void Character::draw(WINDOW * w)
@@ -18,7 +19,7 @@ void Character::action(vector <Character> men, vector <Location> impassable)
if(order == "wander")
wander(men, impassable);
else if(order == "move")
- astar(men,impassable);
+ heuristic(men,impassable);
}
bool Character::check(Location L, vector <Character> men, vector <Location> impassable)
@@ -36,38 +37,76 @@ bool Character::check(Location L, vector <Character> men, vector <Location> impa
void Character::wander(vector <Character> men, vector <Location> impassable)
{
- int r = rand()%15+1;
- bool stay = false;
+ vector <Location> possible = getLocal(l,men,impassable);
+ int r = rand()%2;
+
+ if(r && possible.size())
+ {
+ int i = rand() % possible.size();
+ move(possible[i]);
+ }
+}
+
+void Character::heuristic(vector <Character> men, vector <Location> impassable)
+{
+ vector <Location> open = getLocal(l,men,impassable);
+
+ float v,u;
Location L;
- if(r==1)
- L = Location(l.x+1,l.y);
- else if(r==2)
- L = Location(l.x-1,l.y);
- else if(r==3)
- L = Location(l.x,l.y+1);
- else if(r==4)
- L = Location(l.x,l.y-1);
- else if(r==5)
- L = Location(l.x+1,l.x+1);
- else if(r==6)
- L = Location(l.x-1,l.y-1);
- else if(r==7)
- L = Location(l.x+1,l.y-1);
- else if(r==8)
- L = Location(l.x-1,l.y+1);
- else
- stay = true;
-
- if (!stay)
- if(check(L,men,impassable))
- move(L);
+ if(open.size())
+ {
+ v = sqrt(((open[0].x - d.x)^2) + ((open[0].y - d.y)^2));
+ L = open[0];
+ }
+ for(int i = 0; i < open.size(); i++)
+ {
+ u = sqrt(((open[i].x - d.x)^2) + ((open[i].y - d.y)^2));
+ cout << u << " ";
+ if (u<v)
+ {
+ L = open[i];
+ v = u;
+ }
+ }
+ move(L);
}
-void Character::astar(vector <Character> men, vector <Location> impassable)
+vector <Location> Character::getLocal(Location L, vector <Character> men, vector <Location> impassable)
{
- vector <Location> closed;
- vector <Location> open;
+ vector <Location> x;
+ Location Lo;
+
+ Lo=Location(L.x+1,L.y);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+ Lo=Location(L.x-1,L.y);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+ Lo=Location(L.x,L.y+1);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+ Lo=Location(L.x,L.y-1);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+
+ Lo=Location(L.x+1,L.y+1);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+
+ Lo=Location(L.x-1,L.y-1);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+
+ Lo=Location(L.x+1,L.y-1);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+
+ Lo=Location(L.x-1,L.y+1);
+ if(check(Lo,men,impassable))
+ x.push_back(Lo);
+
+ return x;
}
diff --git a/src/List.cpp b/src/List.cpp
index fefb37d..bd0f5d4 100644
--- a/src/List.cpp
+++ b/src/List.cpp
@@ -2,7 +2,7 @@
List::List(vector <Location> i)
{
- for(int i = 0; i < 10; i++)
+ for(int i = 0; i < 3; i++)
{
Location L;
L.x = 150;
diff --git a/src/main.cpp b/src/main.cpp
index 8f5208f..75847a2 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -26,9 +26,9 @@ int main()
else if (c == 'l')
l.y = l.y+1;
else if (c == 'k')
- l.x = l.x+1;
- else if (c == 'j')
l.x = l.x-1;
+ else if (c == 'j')
+ l.x = l.x+1;
else if (c == 'q')
break;
else if (c == 'p')