generated from sigonasr2/CPlusPlusProjectTemplate
A* implemented!
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
fcdbb1e8b4
commit
a7f729d41a
Binary file not shown.
65
main.cpp
65
main.cpp
@ -1,20 +1,31 @@
|
|||||||
#define OLC_PGE_APPLICATION
|
#include <vector>
|
||||||
#include "pixelGameEngine.h"
|
#include <fstream>
|
||||||
#include "olcutils.h"
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
using namespace olc;
|
struct vi2d{
|
||||||
|
int x,y;
|
||||||
|
friend std::ostream&operator<<(std::ostream&out,vi2d&rhs){
|
||||||
|
out<<"("<<rhs.x<<","<<rhs.y<<")";
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct node{
|
struct node{
|
||||||
|
private:
|
||||||
char elevation='a';
|
char elevation='a';
|
||||||
|
public:
|
||||||
bool visited=false;
|
bool visited=false;
|
||||||
std::vector<node*>connections;
|
std::vector<node*>connections;
|
||||||
int myDist=INFINITY;
|
int myDist=INFINITY;
|
||||||
int storedDist=INFINITY;
|
|
||||||
vi2d pos;
|
vi2d pos;
|
||||||
node*parent=nullptr;
|
node*parent=nullptr;
|
||||||
node(char elevation){
|
node(char elevation){
|
||||||
this->elevation=elevation;
|
this->elevation=elevation;
|
||||||
};
|
};
|
||||||
|
char getElevation(){
|
||||||
|
return elevation=='S'?'a':elevation=='E'?'z'+1:elevation;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +37,7 @@ node*endNode;
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::ifstream file("testinput2");
|
std::ifstream file("input");
|
||||||
while (file.good()){
|
while (file.good()){
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(file,line);
|
std::getline(file,line);
|
||||||
@ -48,18 +59,18 @@ int main()
|
|||||||
std::cout<<"================"<<std::endl;
|
std::cout<<"================"<<std::endl;
|
||||||
for (int y=0;y<grid.size();y++){
|
for (int y=0;y<grid.size();y++){
|
||||||
for (int x=0;x<grid[y].size();x++){
|
for (int x=0;x<grid[y].size();x++){
|
||||||
std::cout<<grid[y][x].elevation;
|
std::cout<<grid[y][x].getElevation();
|
||||||
grid[y][x].pos={x,y};
|
grid[y][x].pos={x,y};
|
||||||
if (x>0){
|
if (x>0&&grid[y][x-1].getElevation()-1<=grid[y][x].getElevation()){
|
||||||
grid[y][x].connections.push_back(&grid[y][x-1]);
|
grid[y][x].connections.push_back(&grid[y][x-1]);
|
||||||
}
|
}
|
||||||
if (x<grid[y].size()-1){
|
if (x<grid[y].size()-1&&grid[y][x+1].getElevation()-1<=grid[y][x].getElevation()){
|
||||||
grid[y][x].connections.push_back(&grid[y][x+1]);
|
grid[y][x].connections.push_back(&grid[y][x+1]);
|
||||||
}
|
}
|
||||||
if (y>0){
|
if (y>0&&grid[y-1][x].getElevation()-1<=grid[y][x].getElevation()){
|
||||||
grid[y][x].connections.push_back(&grid[y-1][x]);
|
grid[y][x].connections.push_back(&grid[y-1][x]);
|
||||||
}
|
}
|
||||||
if (y<grid.size()-1){
|
if (y<grid.size()-1&&grid[y+1][x].getElevation()-1<=grid[y][x].getElevation()){
|
||||||
grid[y][x].connections.push_back(&grid[y+1][x]);
|
grid[y][x].connections.push_back(&grid[y+1][x]);
|
||||||
}
|
}
|
||||||
if (x==playerPos.x&&y==playerPos.y){
|
if (x==playerPos.x&&y==playerPos.y){
|
||||||
@ -76,37 +87,25 @@ int main()
|
|||||||
|
|
||||||
std::vector<node*>searchNodes;
|
std::vector<node*>searchNodes;
|
||||||
searchNodes.push_back(startNode);
|
searchNodes.push_back(startNode);
|
||||||
|
startNode->myDist=0.0f;
|
||||||
while (searchNodes.size()>0){
|
while (searchNodes.size()>0){
|
||||||
node*currentNode=searchNodes.front();
|
node*currentNode=searchNodes.front();
|
||||||
if (currentNode==startNode){
|
if (currentNode->visited){
|
||||||
currentNode->myDist=pow(currentNode->pos.x-endNode->pos.x,2)+pow(currentNode->pos.y-endNode->pos.y,2);
|
searchNodes.erase(searchNodes.begin());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
currentNode->visited=true;
|
currentNode->visited=true;
|
||||||
if (currentNode->elevation=='S'){
|
|
||||||
currentNode->elevation='a'-1;
|
|
||||||
}
|
|
||||||
if (currentNode->elevation=='E'){
|
|
||||||
currentNode->elevation='z'+1;
|
|
||||||
}
|
|
||||||
for (int i=0;i<currentNode->connections.size();i++){
|
for (int i=0;i<currentNode->connections.size();i++){
|
||||||
node*neighbor=currentNode->connections[i];
|
node*neighbor=currentNode->connections[i];
|
||||||
if (neighbor==endNode){
|
if (!neighbor->visited){
|
||||||
//We can't go any higher.
|
searchNodes.push_back(neighbor);
|
||||||
endNode->parent=currentNode;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (neighbor->elevation-1<=currentNode->elevation){
|
float lowerGoal=currentNode->myDist+pow(neighbor->pos.x-currentNode->pos.x,2)+pow(neighbor->pos.y-currentNode->pos.y,2);
|
||||||
if (neighbor->parent==nullptr||currentNode->myDist<neighbor->storedDist){
|
if (lowerGoal<neighbor->myDist){
|
||||||
neighbor->parent=currentNode;
|
neighbor->parent=currentNode;
|
||||||
neighbor->storedDist=currentNode->myDist;
|
neighbor->myDist=lowerGoal;
|
||||||
neighbor->myDist=pow(neighbor->pos.x-endNode->pos.x,2)+pow(neighbor->pos.y-endNode->pos.y,2);
|
|
||||||
if (!neighbor->visited){
|
|
||||||
searchNodes.push_back(neighbor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
searchNodes.erase(searchNodes.begin());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
node*searchNode=endNode;
|
node*searchNode=endNode;
|
||||||
|
2
sig
2
sig
@ -3,7 +3,7 @@ export AUTO_UPDATE=true
|
|||||||
source utils/define.sh
|
source utils/define.sh
|
||||||
|
|
||||||
define PROJECT_NAME "C++ProjectTemplate"
|
define PROJECT_NAME "C++ProjectTemplate"
|
||||||
define CUSTOM_PARAMS "-std=c++17 -lX11 -lGL -lpthread -lpng -lstdc++fs -lpulse -lpulse-simple -I/usr/include/lua5.3"
|
define CUSTOM_PARAMS "-std=c++17 -lX11 -lpthread -lpng -lstdc++fs -I/usr/include/lua5.3"
|
||||||
define LANGUAGE "C++"
|
define LANGUAGE "C++"
|
||||||
|
|
||||||
source utils/main.sh
|
source utils/main.sh
|
||||||
|
Loading…
x
Reference in New Issue
Block a user