From e63a678a8fdde888620b6d5a5059bd0f37141592 Mon Sep 17 00:00:00 2001 From: tom Date: Wed, 25 Nov 2015 13:27:43 -0600 Subject: cleaned wander and added heuristic movement that doesnt work --- src/Character.cpp | 95 +++++++++++++++++++++++++++++++++++++++---------------- src/List.cpp | 2 +- src/main.cpp | 4 +-- 3 files changed, 70 insertions(+), 31 deletions(-) (limited to 'src') 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 men, vector impassable) if(order == "wander") wander(men, impassable); else if(order == "move") - astar(men,impassable); + heuristic(men,impassable); } bool Character::check(Location L, vector men, vector impassable) @@ -36,38 +37,76 @@ bool Character::check(Location L, vector men, vector impa void Character::wander(vector men, vector impassable) { - int r = rand()%15+1; - bool stay = false; + vector 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 men, vector impassable) +{ + vector 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 men, vector impassable) +vector Character::getLocal(Location L, vector men, vector impassable) { - vector closed; - vector open; + vector 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 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') -- cgit v1.2.3