Prześlij pliki do ''

Tested AStar
This commit is contained in:
Tomasz Dzierzbicki 2020-04-25 18:08:03 +00:00
parent 0df9493538
commit 914bdcca0e

789
Main.cpp
View File

@ -1,243 +1,546 @@
#include<iostream> #include<iostream>
#include<stdlib.h> #include<stdlib.h>
#include<windows.h> #include<windows.h>
#include<conio.h> #include<conio.h>
#include<string> #include<string>
#include<list>
using namespace std; #include<set>
#include<math.h>
char pole[27][27][2]; #include<stack>
int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1;
using namespace std;
void color(string foregroundColor,string backgroundColor) const int ROW = 27;
{ const int COL = 27;
HANDLE hOut; typedef pair<int, int> Pair;
hOut = GetStdHandle(STD_OUTPUT_HANDLE); typedef pair<double, pair<int, int>> pPair;
int foregroundCode = 15; struct cell
if (foregroundColor == "black") {
foregroundCode = 0; int parent_i, parent_j;
if (foregroundColor == "dark_blue") double f, g, h;
foregroundCode = 1; };
if (foregroundColor == "green")
foregroundCode = 2;
if (foregroundColor == "cyan") char pole[27][27][2];
foregroundCode = 3; int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1;
if (foregroundColor == "dark_red")
foregroundCode = 4; void color(string foregroundColor, string backgroundColor)
if (foregroundColor == "purple") {
foregroundCode = 5; HANDLE hOut;
if (foregroundColor == "dark_yellow") hOut = GetStdHandle(STD_OUTPUT_HANDLE);
foregroundCode = 6; int foregroundCode = 15;
if (foregroundColor == "light_gray") if (foregroundColor == "black")
foregroundCode = 7; foregroundCode = 0;
if (foregroundColor == "gray") if (foregroundColor == "dark_blue")
foregroundCode = 8; foregroundCode = 1;
if (foregroundColor == "blue") if (foregroundColor == "green")
foregroundCode = 9; foregroundCode = 2;
if (foregroundColor == "lime") if (foregroundColor == "cyan")
foregroundCode = 10; foregroundCode = 3;
if (foregroundColor == "light_blue") if (foregroundColor == "dark_red")
foregroundCode = 11; foregroundCode = 4;
if (foregroundColor == "red") if (foregroundColor == "purple")
foregroundCode = 12; foregroundCode = 5;
if (foregroundColor == "magenta") if (foregroundColor == "dark_yellow")
foregroundCode = 13; foregroundCode = 6;
if (foregroundColor == "yellow") if (foregroundColor == "light_gray")
foregroundCode = 14; foregroundCode = 7;
if (foregroundColor == "white") if (foregroundColor == "gray")
foregroundCode = 15; foregroundCode = 8;
if (foregroundColor == "blue")
int backgroundCode = 0; foregroundCode = 9;
if (backgroundColor == "black") if (foregroundColor == "lime")
backgroundCode = 0; foregroundCode = 10;
if (backgroundColor == "dark_blue") if (foregroundColor == "light_blue")
backgroundCode = 1; foregroundCode = 11;
if (backgroundColor == "green") if (foregroundColor == "red")
backgroundCode = 2; foregroundCode = 12;
if (backgroundColor == "cyan") if (foregroundColor == "magenta")
backgroundCode = 3; foregroundCode = 13;
if (backgroundColor == "dark_red") if (foregroundColor == "yellow")
backgroundCode = 4; foregroundCode = 14;
if (backgroundColor == "purple") if (foregroundColor == "white")
backgroundCode = 5; foregroundCode = 15;
if (backgroundColor == "dark_yellow")
backgroundCode = 6; int backgroundCode = 0;
if (backgroundColor == "light_gray") if (backgroundColor == "black")
backgroundCode = 7; backgroundCode = 0;
if (backgroundColor == "gray") if (backgroundColor == "dark_blue")
backgroundCode = 8; backgroundCode = 1;
if (backgroundColor == "blue") if (backgroundColor == "green")
backgroundCode = 9; backgroundCode = 2;
if (backgroundColor == "lime") if (backgroundColor == "cyan")
backgroundCode = 10; backgroundCode = 3;
if (backgroundColor == "light_blue") if (backgroundColor == "dark_red")
backgroundCode = 11; backgroundCode = 4;
if (backgroundColor == "red") if (backgroundColor == "purple")
backgroundCode = 12; backgroundCode = 5;
if (backgroundColor == "magenta") if (backgroundColor == "dark_yellow")
backgroundCode = 13; backgroundCode = 6;
if (backgroundColor == "yellow") if (backgroundColor == "light_gray")
backgroundCode = 14; backgroundCode = 7;
if (backgroundColor == "white") if (backgroundColor == "gray")
backgroundCode = 15; backgroundCode = 8;
SetConsoleTextAttribute(hOut, foregroundCode + backgroundCode*16); if (backgroundColor == "blue")
} backgroundCode = 9;
if (backgroundColor == "lime")
void SetWindow(int Width, int Height) backgroundCode = 10;
{ if (backgroundColor == "light_blue")
_COORD coord; backgroundCode = 11;
coord.X = Width; if (backgroundColor == "red")
coord.Y = Height; backgroundCode = 12;
if (backgroundColor == "magenta")
_SMALL_RECT Rect; backgroundCode = 13;
Rect.Top = 0; if (backgroundColor == "yellow")
Rect.Left = 0; backgroundCode = 14;
Rect.Bottom = Height - 1; if (backgroundColor == "white")
Rect.Right = Width - 1; backgroundCode = 15;
SetConsoleTextAttribute(hOut, foregroundCode + backgroundCode * 16);
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle }
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size void SetWindow(int Width, int Height)
} {
_COORD coord;
void updatePola() coord.X = Width;
{ coord.Y = Height;
system("cls");
for (int i = 0; i < 27; i++) _SMALL_RECT Rect;
{ Rect.Top = 0;
for (int j = 0; j < 27; j++) Rect.Left = 0;
{ Rect.Bottom = Height - 1;
char item = pole[i][j][0]; Rect.Right = Width - 1;
switch (item)
{ HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
case 'B': SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
{ SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
color("purple", "dark_yellow"); }
}break;
case 'T': void updatePola()
{ {
color("red", "dark_yellow"); system("cls");
}break; for (int i = 0; i < 27; i++)
case '.': {
{ for (int j = 0; j < 27; j++)
color("yellow", "dark_yellow"); {
}break; char item = pole[i][j][0];
case '#': switch (item)
{ {
color("light_gray", "gray"); case 'B':
}break; {
} color("purple", "dark_yellow");
cout << pole[i][j][0]; }break;
case 'T':
} {
cout << endl; color("red", "dark_yellow");
color("white", "black"); }break;
} case '.':
} {
color("yellow", "dark_yellow");
void Move(char kierunek) }break;
{ case '#':
switch (kierunek) {
{ color("light_gray", "gray");
//góra-(w) }break;
case 'w': }
{ cout << pole[i][j][0];
if (pole[pozycjaTraktoraX - 1][pozycjaTraktoraY][0] != '#')
{ }
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; cout << endl;
pozycjaTraktoraX--; color("white", "black");
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; }
}
}
updatePola(); void Move(char kierunek)
}break; {
//dó³-(s) switch (kierunek)
case 's': {
{ //góra-(w)
if (pole[pozycjaTraktoraX + 1][pozycjaTraktoraY][0] != '#') case 'w':
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; if (pole[pozycjaTraktoraX - 1][pozycjaTraktoraY][0] != '#')
pozycjaTraktoraX++; {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
pozycjaTraktoraX--;
} pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
updatePola();
}break; }
//lewo-(a) updatePola();
case 'a': }break;
{ //dó³-(s)
if (pole[pozycjaTraktoraX][pozycjaTraktoraY - 1][0] != '#') case 's':
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; if (pole[pozycjaTraktoraX + 1][pozycjaTraktoraY][0] != '#')
pozycjaTraktoraY--; {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
pozycjaTraktoraX++;
} pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
updatePola();
}break; }
//prawo-(d) updatePola();
case 'd': }break;
{ //lewo-(a)
if (pole[pozycjaTraktoraX][pozycjaTraktoraY + 1][0] != '#') case 'a':
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; if (pole[pozycjaTraktoraX][pozycjaTraktoraY - 1][0] != '#')
pozycjaTraktoraY++; {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
pozycjaTraktoraY--;
} pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
updatePola();
}break; }
} updatePola();
} }break;
//prawo-(d)
int main() case 'd':
{ {
SetWindow(50, 30); if (pole[pozycjaTraktoraX][pozycjaTraktoraY + 1][0] != '#')
{
//create pola// pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
for (int i = 0; i < 27; i++) pozycjaTraktoraY++;
{ pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
pole[i][0][0] = '#';
pole[0][i][0] = '#'; }
pole[26][i][0] = '#'; updatePola();
pole[i][26][0] = '#'; }break;
} }
for (int i = 1; i < 26; i++) }
{
for (int j = 1; j < 26; j++) void MoveOwn(char akcja)
{ {
pole[i][j][0] = '.'; /*
} switch (akcja)
} {
//do przodu
for (int i = 0; i < 25; i++) case 'd':
{ {
pole[i+1][i+1][0] = 'B'; if (pole[pozycjaTraktoraX - 1][pozycjaTraktoraY][0] != '#')
} {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
pole[1][1][0] = 'T'; pozycjaTraktoraX--;
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
updatePola();
}
updatePola();
//---------start---------// }break;
bool traktorDziala = true; //dół-(s)
case 's':
char akcja; {
if (pole[pozycjaTraktoraX + 1][pozycjaTraktoraY][0] != '#')
do {
{ pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
akcja = _getch(); pozycjaTraktoraX++;
if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd') pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
{
Move(akcja); }
} updatePola();
if (akcja == '0') }break;
{ //lewo-(a)
traktorDziala = false; case 'a':
} {
} while (traktorDziala); if (pole[pozycjaTraktoraX][pozycjaTraktoraY - 1][0] != '#')
//---------end---------// {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.';
return 0; pozycjaTraktoraY--;
} pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T';
}
updatePola();
}break;
}*/
}
bool isValid(int x, int y)
{
if (pole[x][y][0] != '#')
{
return true;
}
return false;
}
bool isDestination(int x, int y,Pair dest)
{
if (dest.first == x && dest.second == y)
{
return true;
}
return false;
}
double calculateHValue(int x, int y, Pair dest)
{
return abs(x - dest.first) + abs(y - dest.second);
}
void tracePath(cell cellDetails[][COL], Pair dest)
{
printf("\nThe Path is ");
int row = dest.first;
int col = dest.second;
stack<Pair> Path;
while (!(cellDetails[row][col].parent_i == row
&& cellDetails[row][col].parent_j == col))
{
Path.push(make_pair(row, col));
int temp_row = cellDetails[row][col].parent_i;
int temp_col = cellDetails[row][col].parent_j;
row = temp_row;
col = temp_col;
}
Path.push(make_pair(row, col));
while (!Path.empty())
{
pair<int, int> p = Path.top();
Path.pop();
printf("-> (%d,%d) ", p.first, p.second);// jeśli sec+1=posTraktorX to koleny ruch w prawo Move('d')
}
return;
}
void aStarSearch(int grid[][COL],Pair src, Pair dest)
{
bool closedList[ROW][COL];
memset(closedList, false, sizeof(closedList));
cell cellDetails[ROW][COL];
int i, j;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
cellDetails[i][j].f = FLT_MAX;
cellDetails[i][j].g = FLT_MAX;
cellDetails[i][j].h = FLT_MAX;
cellDetails[i][j].parent_i = -1;
cellDetails[i][j].parent_j = -1;
}
}
i = src.first, j = src.second;
cellDetails[i][j].f = 0.0;
cellDetails[i][j].g = 0.0;
cellDetails[i][j].h = 0.0;
cellDetails[i][j].parent_i = i;
cellDetails[i][j].parent_j = j;
set<pPair> openList;
openList.insert(make_pair(0.0, make_pair(i, j)));
bool foundDest = false;
while (!openList.empty())
{
pPair p = *openList.begin();
openList.erase(openList.begin());
i = p.second.first;
j = p.second.second;
closedList[i][j] = true;
double gNew, hNew, fNew;
//----------- 1st Successor (North) ------------
if (isValid(i - 1, j) == true)
{
if (isDestination(i - 1, j, dest) == true)
{
cellDetails[i - 1][j].parent_i = i;
cellDetails[i - 1][j].parent_j = j;
//printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
}
else if (closedList[i - 1][j] == false)
{
gNew = cellDetails[i][j].g + 1.0; //???????-waga
hNew = calculateHValue(i - 1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i - 1][j].f == FLT_MAX ||
cellDetails[i - 1][j].f > fNew)
{
openList.insert(make_pair(fNew,
make_pair(i - 1, j)));
cellDetails[i - 1][j].f = fNew;
cellDetails[i - 1][j].g = gNew;
cellDetails[i - 1][j].h = hNew;
cellDetails[i - 1][j].parent_i = i;
cellDetails[i - 1][j].parent_j = j;
}
}
}
//----------- 2nd Successor (South) ------------
if (isValid(i + 1, j) == true)
{
if (isDestination(i + 1, j, dest) == true)
{
cellDetails[i + 1][j].parent_i = i;
cellDetails[i + 1][j].parent_j = j;
//printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
}
else if (closedList[i + 1][j] == false)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue(i + 1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i + 1][j].f == FLT_MAX ||
cellDetails[i + 1][j].f > fNew)
{
openList.insert(make_pair(fNew, make_pair(i + 1, j)));
cellDetails[i + 1][j].f = fNew;
cellDetails[i + 1][j].g = gNew;
cellDetails[i + 1][j].h = hNew;
cellDetails[i + 1][j].parent_i = i;
cellDetails[i + 1][j].parent_j = j;
}
}
}
//----------- 3rd Successor (East) ------------
if (isValid(i, j + 1) == true)
{
if (isDestination(i, j + 1, dest) == true)
{
cellDetails[i][j + 1].parent_i = i;
cellDetails[i][j + 1].parent_j = j;
//printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
}
else if (closedList[i][j + 1] == false)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue(i, j + 1, dest);
fNew = gNew + hNew;
if (cellDetails[i][j + 1].f == FLT_MAX ||
cellDetails[i][j + 1].f > fNew)
{
openList.insert(make_pair(fNew,
make_pair(i, j + 1)));
cellDetails[i][j + 1].f = fNew;
cellDetails[i][j + 1].g = gNew;
cellDetails[i][j + 1].h = hNew;
cellDetails[i][j + 1].parent_i = i;
cellDetails[i][j + 1].parent_j = j;
}
}
}
//----------- 4th Successor (West) ------------
if (isValid(i, j - 1) == true)
{
if (isDestination(i, j - 1, dest) == true)
{
cellDetails[i][j - 1].parent_i = i;
cellDetails[i][j - 1].parent_j = j;
//printf("The destination cell is found\n");
tracePath(cellDetails, dest);
foundDest = true;
return;
}
else if (closedList[i][j - 1] == false)
{
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue(i, j - 1, dest);
fNew = gNew + hNew;
if (cellDetails[i][j - 1].f == FLT_MAX ||
cellDetails[i][j - 1].f > fNew)
{
openList.insert(make_pair(fNew,
make_pair(i, j - 1)));
cellDetails[i][j - 1].f = fNew;
cellDetails[i][j - 1].g = gNew;
cellDetails[i][j - 1].h = hNew;
cellDetails[i][j - 1].parent_i = i;
cellDetails[i][j - 1].parent_j = j;
}
}
}
}
if (foundDest == false)
printf("Failed to find the Destination Cell\n");
return;
}
void gogo()
{
int grid[27][27];
for (int i = 0; i < 27; i++)
{
for (int j = 0; j < 27; j++)
{
grid[i][j] = 0;
}
}
Pair src = make_pair(pozycjaTraktoraX, pozycjaTraktoraY);
Pair dest = make_pair(pozycjaTraktoraX+2, pozycjaTraktoraY+3);
aStarSearch(grid, src, dest);
}
int main()
{
SetWindow(50, 30);
//create pola//
for (int i = 0; i < 27; i++)
{
pole[i][0][0] = '#';
pole[0][i][0] = '#';
pole[26][i][0] = '#';
pole[i][26][0] = '#';
}
for (int i = 1; i < 26; i++)
{
for (int j = 1; j < 26; j++)
{
pole[i][j][0] = '.';
pole[i][j][1] = '1';
}
}
for (int i = 0; i < 25; i++)
{
pole[i + 1][i + 1][0] = 'B';
pole[i + 1][i + 1][1] = '10';
}
pole[1][1][0] = 'T';
updatePola();
gogo();
//---------start---------//
bool traktorDziala = true;
char akcja;
do
{
akcja = _getch();
if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd')
{
Move(akcja);
}
if (akcja == '0')
{
traktorDziala = false;
}
} while (traktorDziala);
//---------end---------//
return 0;
}