Prześlij pliki do ''

This commit is contained in:
Tomasz Dzierzbicki 2020-04-27 20:59:36 +00:00
parent e7b3a0ef06
commit 9ada76303b

841
Main.cpp
View File

@ -1,243 +1,606 @@
#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 float maxFloat=FLT_MAX;
HANDLE hOut; const int ROW = 27;
hOut = GetStdHandle(STD_OUTPUT_HANDLE); const int COL = 27;
int foregroundCode = 15; typedef pair<int, int> Pair;
if (foregroundColor == "black") typedef pair<double, pair<int, int>> pPair;
foregroundCode = 0; struct cell
if (foregroundColor == "dark_blue") {
foregroundCode = 1; int parent_i, parent_j;
if (foregroundColor == "green") double f, g, h;
foregroundCode = 2; };
if (foregroundColor == "cyan")
foregroundCode = 3; char pole[27][27][2];
if (foregroundColor == "dark_red") int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1;
foregroundCode = 4; char currentWay = 'S';
if (foregroundColor == "purple")
foregroundCode = 5; void color(string foregroundColor, string backgroundColor)
if (foregroundColor == "dark_yellow") {
foregroundCode = 6; HANDLE hOut;
if (foregroundColor == "light_gray") hOut = GetStdHandle(STD_OUTPUT_HANDLE);
foregroundCode = 7; int foregroundCode = 15;
if (foregroundColor == "gray") if (foregroundColor == "black")
foregroundCode = 8; foregroundCode = 0;
if (foregroundColor == "blue") if (foregroundColor == "dark_blue")
foregroundCode = 9; foregroundCode = 1;
if (foregroundColor == "lime") if (foregroundColor == "green")
foregroundCode = 10; foregroundCode = 2;
if (foregroundColor == "light_blue") if (foregroundColor == "cyan")
foregroundCode = 11; foregroundCode = 3;
if (foregroundColor == "red") if (foregroundColor == "dark_red")
foregroundCode = 12; foregroundCode = 4;
if (foregroundColor == "magenta") if (foregroundColor == "purple")
foregroundCode = 13; foregroundCode = 5;
if (foregroundColor == "yellow") if (foregroundColor == "dark_yellow")
foregroundCode = 14; foregroundCode = 6;
if (foregroundColor == "white") if (foregroundColor == "light_gray")
foregroundCode = 15; foregroundCode = 7;
if (foregroundColor == "gray")
int backgroundCode = 0; foregroundCode = 8;
if (backgroundColor == "black") if (foregroundColor == "blue")
backgroundCode = 0; foregroundCode = 9;
if (backgroundColor == "dark_blue") if (foregroundColor == "lime")
backgroundCode = 1; foregroundCode = 10;
if (backgroundColor == "green") if (foregroundColor == "light_blue")
backgroundCode = 2; foregroundCode = 11;
if (backgroundColor == "cyan") if (foregroundColor == "red")
backgroundCode = 3; foregroundCode = 12;
if (backgroundColor == "dark_red") if (foregroundColor == "magenta")
backgroundCode = 4; foregroundCode = 13;
if (backgroundColor == "purple") if (foregroundColor == "yellow")
backgroundCode = 5; foregroundCode = 14;
if (backgroundColor == "dark_yellow") if (foregroundColor == "white")
backgroundCode = 6; foregroundCode = 15;
if (backgroundColor == "light_gray")
backgroundCode = 7; int backgroundCode = 0;
if (backgroundColor == "gray") if (backgroundColor == "black")
backgroundCode = 8; backgroundCode = 0;
if (backgroundColor == "blue") if (backgroundColor == "dark_blue")
backgroundCode = 9; backgroundCode = 1;
if (backgroundColor == "lime") if (backgroundColor == "green")
backgroundCode = 10; backgroundCode = 2;
if (backgroundColor == "light_blue") if (backgroundColor == "cyan")
backgroundCode = 11; backgroundCode = 3;
if (backgroundColor == "red") if (backgroundColor == "dark_red")
backgroundCode = 12; backgroundCode = 4;
if (backgroundColor == "magenta") if (backgroundColor == "purple")
backgroundCode = 13; backgroundCode = 5;
if (backgroundColor == "yellow") if (backgroundColor == "dark_yellow")
backgroundCode = 14; backgroundCode = 6;
if (backgroundColor == "white") if (backgroundColor == "light_gray")
backgroundCode = 15; backgroundCode = 7;
SetConsoleTextAttribute(hOut, foregroundCode + backgroundCode*16); if (backgroundColor == "gray")
} backgroundCode = 8;
if (backgroundColor == "blue")
void SetWindow(int Width, int Height) backgroundCode = 9;
{ if (backgroundColor == "lime")
_COORD coord; backgroundCode = 10;
coord.X = Width; if (backgroundColor == "light_blue")
coord.Y = Height; backgroundCode = 11;
if (backgroundColor == "red")
_SMALL_RECT Rect; backgroundCode = 12;
Rect.Top = 0; if (backgroundColor == "magenta")
Rect.Left = 0; backgroundCode = 13;
Rect.Bottom = Height - 1; if (backgroundColor == "yellow")
Rect.Right = Width - 1; backgroundCode = 14;
if (backgroundColor == "white")
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle backgroundCode = 15;
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size SetConsoleTextAttribute(hOut, foregroundCode + backgroundCode * 16);
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size }
}
void SetWindow(int Width, int Height)
void updatePola() {
{ _COORD coord;
system("cls"); coord.X = Width;
for (int i = 0; i < 27; i++) coord.Y = Height;
{
for (int j = 0; j < 27; j++) _SMALL_RECT Rect;
{ Rect.Top = 0;
char item = pole[i][j][0]; Rect.Left = 0;
switch (item) Rect.Bottom = Height - 1;
Rect.Right = Width - 1;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
}
void updatePola()
{
system("cls");
for (int i = 0; i < 27; i++)
{
for (int j = 0; j < 27; j++)
{
char item = pole[i][j][0];
switch (item)
{
case 'B':
{
color("purple", "dark_yellow");
}break;
case 'T':
{
color("red", "dark_yellow");
}break;
case 'G':
{
color("lime", "dark_yellow");
}break;
case '.':
{
color("yellow", "dark_yellow");
}break;
case '#':
{
color("light_gray", "gray");
}break;
}
cout << pole[i][j][0];
}
cout << endl;
color("white", "black");
}
}
void correctMovement(char wantedWay)
{
while (currentWay != wantedWay)
{
switch (currentWay)
{
case 'N':
{ {
case 'B': if (wantedWay == 'S')
{ currentWay = wantedWay;
color("purple", "dark_yellow"); else
}break; currentWay = 'W';
case 'T': }break;
{ case 'S':
color("red", "dark_yellow");
}break;
case '.':
{
color("yellow", "dark_yellow");
}break;
case '#':
{
color("light_gray", "gray");
}break;
}
cout << pole[i][j][0];
}
cout << endl;
color("white", "black");
}
}
void Move(char kierunek)
{
switch (kierunek)
{
//góra-(w)
case 'w':
{
if (pole[pozycjaTraktoraX - 1][pozycjaTraktoraY][0] != '#')
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; if (wantedWay == 'N')
pozycjaTraktoraX--; currentWay = wantedWay;
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; else
currentWay = 'W';
} }break;
updatePola(); case 'W':
}break;
//dó³-(s)
case 's':
{
if (pole[pozycjaTraktoraX + 1][pozycjaTraktoraY][0] != '#')
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; if (wantedWay == 'E')
pozycjaTraktoraX++; currentWay = wantedWay;
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; else
currentWay = 'N';
} }break;
updatePola(); case 'E':
}break;
//lewo-(a)
case 'a':
{
if (pole[pozycjaTraktoraX][pozycjaTraktoraY - 1][0] != '#')
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; if (wantedWay == 'W')
pozycjaTraktoraY--; currentWay = wantedWay;
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; else
currentWay = 'N';
} }break;
updatePola(); }
}break; }
//prawo-(d) }
case 'd': void Move(char kierunek)
{ {
if (pole[pozycjaTraktoraX][pozycjaTraktoraY + 1][0] != '#') switch (kierunek)
{ {
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = '.'; //góra-(w)
pozycjaTraktoraY++; case 'w':
pole[pozycjaTraktoraX][pozycjaTraktoraY][0] = 'T'; {
if (pole[pozycjaTraktoraY - 1][pozycjaTraktoraX][0] != '#')
} {
updatePola(); correctMovement('N');
}break; pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = '.';
} pozycjaTraktoraY--;
} pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
}
int main() updatePola();
{ }break;
SetWindow(50, 30); //dół-(s)
case 's':
//create pola// {
for (int i = 0; i < 27; i++) if (pole[pozycjaTraktoraY + 1][pozycjaTraktoraX][0] != '#')
{ {
pole[i][0][0] = '#'; correctMovement('S');
pole[0][i][0] = '#'; pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = '.';
pole[26][i][0] = '#'; pozycjaTraktoraY++;
pole[i][26][0] = '#'; pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
} }
for (int i = 1; i < 26; i++) updatePola();
{ }break;
for (int j = 1; j < 26; j++) //lewo-(a)
{ case 'a':
pole[i][j][0] = '.'; {
} if (pole[pozycjaTraktoraY][pozycjaTraktoraX - 1][0] != '#')
} {
correctMovement('W');
for (int i = 0; i < 25; i++) pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = '.';
{ pozycjaTraktoraX--;
pole[i+1][i+1][0] = 'B'; pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
} }
updatePola();
pole[1][1][0] = 'T'; }break;
//prawo-(d)
updatePola(); case 'd':
{
if (pole[pozycjaTraktoraY][pozycjaTraktoraX + 1][0] != '#')
//---------start---------// {
bool traktorDziala = true; correctMovement('E');
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = '.';
char akcja; pozycjaTraktoraX++;
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
do }
{ updatePola();
akcja = _getch(); }break;
if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd') }
{ }
Move(akcja);
} bool isValid(int x, int y)
if (akcja == '0') {
{ if (pole[x][y][0] != '#')
traktorDziala = false; {
} return true;
} while (traktorDziala); }
//---------end---------// return false;
}
return 0; 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 "); //----start info
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();
if (p.first > pozycjaTraktoraX)
Move('d');
if (p.first < pozycjaTraktoraX)
Move('a');
if (p.second > pozycjaTraktoraY)
Move('s');
if (p.second < pozycjaTraktoraY)
Move('w');
//printf("-> (%d,%d) ", p.first, p.second); //---- informacja wierzchołku
Sleep(1000);
}
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 = maxFloat;
cellDetails[i][j].g = maxFloat;
cellDetails[i][j].h = maxFloat;
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;
double waga = 1.0;
waga = ((double)pole[j][i][1] - 48)*1.0;//----waga
//----------- 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 + waga;
hNew = calculateHValue(i - 1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i - 1][j].f == maxFloat ||
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 + waga;
hNew = calculateHValue(i + 1, j, dest);
fNew = gNew + hNew;
if (cellDetails[i + 1][j].f == maxFloat ||
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 + waga;
hNew = calculateHValue(i, j + 1, dest);
fNew = gNew + hNew;
if (cellDetails[i][j + 1].f == maxFloat ||
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 + waga;
hNew = calculateHValue(i, j - 1, dest);
fNew = gNew + hNew;
if (cellDetails[i][j - 1].f == maxFloat ||
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 endX,int endY)
{
updatePola();
Sleep(1000);
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(endX, endY);
aStarSearch(grid, src, dest);
}
void test1()
{
pole[1][3][0] = 'B';
pole[1][3][1] = '9';
pole[3][1][0] = 'B';
pole[3][1][1] = '9';
}
void test2()
{
for (int i = 1; i < 26; i++)
{
for (int j = 1; j < i; j++)
{
pole[i][j][0] = 'B';
pole[i][j][1] = '9';
}
}
test1();
updatePola();
}
void start1()
{
int goalX = 3, goalY = 4;
test1();
pole[1][1][0] = 'T';
pole[1][1][1] = '1';
pole[goalY][goalX][0] = 'G';
pole[goalY][goalX][1] = '9';
gogo(goalX, goalY);
}
void start2()
{
int goalX = 6, goalY = 6;
test2();
pole[1][1][0] = 'T';
pole[1][1][1] = '1';
pole[goalY][goalX][0] = 'G';
pole[goalY][goalX][1] = '9';
gogo(goalX, goalY);
}
void start3()
{
int goalX = 6, goalY = 9;
test2();
pole[1][1][0] = 'T';
pole[1][1][1] = '1';
pole[goalY][goalX][0] = 'G';
pole[goalY][goalX][1] = '9';
gogo(goalX, goalY);
}
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] = '#';
pole[i][0][1] = '9';
pole[0][i][1] = '9';
pole[26][i][1] = '9';
pole[i][26][1] = '9';
}
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] = '9';
}
updatePola();
start3(); // testy start 1-3
//---------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;
}