How could I make this path-finding code dynamic? This is to allow an agent to search the path on its own (from one corner of the grid to another) while hiding it until moved to the grid.
I am new to C++ and tried to write this program, but it appears to be static. I need to make it dynamic, or I could just have the agent do the path-searching on its own (not by assigning the path). So far, I've made 5 different paths because the agent can choose as many as needed. However, the path length should be 6 and can move only up or to the right.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <math.h>
#include <ctime>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
using namespace std;
using namespace System;
using namespace System::IO;
/*int Grid[ 4 ][ 4 ] =
{
{ 29, 8, 11, 9 },
{ 22, 24, 3, 15 },
{ 24, 26, 5, 6 },
{ 33, 10, 32, 21}
};
int Grid[ 4 ][ 4 ] =
{
{ 0, 0, -5, 0.0 },
{ -1, 0, 2, 2 },
{ 2, 0, 0, 0 },
{ 0, 2, 0, -1}
};
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
int g[16] = {33,10,32,21,24,26,5,6,22,24,3,15,29,8,11,9};
int r[16] = {0,2,0,-1,2,0,0,0,-1,0,2,2,0,0,-5,0};
#define SPACE "[ ]"
bool Finished( false );
int m[4];
//int E = 6;
int E, E1, E2, E3;
void printmatrix(char arg[], int length)
{
for (int j = length; j >= 0; j=-4)
{
cout <<"\n" << arg[j] << "\n ";
}
//}
}
void FindNextPos()
{ std::cout<<"\n"<<"Search Path 1"<<"\n";
for (int c= 1; c < 5; c++)
{ if(r[c] != r[3])
{
m[c] = 1;
E = E + r[c] - 1;
//std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<<m[c]<< std::endl;
//system("pause");
}
else
{
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<<m[c]<< std::endl;
}
}
for (int c= 7; c <= 16; c= c+ 4)
{
if(r[c] != r[16]){
m[c] = 2;
E = E + r[c] - 1;
//std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<< std::endl;
//system("pause");
}
else
{
//m[c] = 2;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E <<")"<< std::endl;
}
}
}
//int E1 = 6;
void nextpos1(){
std::cout<<"\n"<<"Search Path 2"<<"\n";
for (int c= 0; c < 3; c++)
{ if(r[c] != r[3])
{
m[c] = 3;
E1 = E1 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
//system("pause");
}
else
{
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
}
}
for (int c= 6; c < 15; c= c + 4)
{
if(r[c] != r[16]){
m[c] = 4;
E1 = E1 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 4;
Finished = true;
std::cout<<"(ID,C,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 + r[c] - 1 <<")"<< std::endl;
}
}
for (int c= 15; c < 16; ++c)
{
if(r[c] != 0){
m[c] = 5;
E1 = E1 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 5;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E1 <<")"<< std::endl;
std::cout << "\n"<<"finish-->" << r[c] << "\t" <<"E1-->"<< E1 <<"\n" ;
}
}
}
//int E2 = 6;
void nextpos2(){
std::cout<<"\n"<<"Search Path 3"<<"\n";
for (int c= 0; c < 2; c++)
{ if(r[c] != r[3])
{
m[c] = 6;
E2 = E2 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
//system("pause");
}
else
{
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
}
}
for (int c= 1; c < 12; c= c + 4)
{
if(r[c] != r[14]){
m[c] = 7;
E2 = E2 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
//system("pause");
}
else
{
//m[c] = 7;
Finished = true;
std::cout<<"(C,E)"<<"-->"<<"("<<r[c]<<","<<E2 + r[c] - 1 <<")"<< std::endl;
}
}
for (int c= 13; c < 16; ++c)
{ m[c] = 8;
if(r[c] != r[16]){
E2 = E2 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
//system("pause");
}
else
{
// m[c] = 8;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E2 <<")"<< std::endl;
std::cout << "\n"<<"finish-->" << r[c] << "\t" <<"E-->"<< E1 <<"\n" ;
}
}
}
//int E3 = 6;
void nextpos3(){
std::cout<<"\n"<<"Search Path 4"<<"\n";
for (int c= 0; c <= 12; c= c+ 4)
//for (int c= 4; c <= 12; c= c+ 4)
{ if(r[c] != r[12])
{
m[c] = 11;
E3 = E3 + r[c] - 1;
std::cout<<"\n";
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 11;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 + r[c] - 1 <<")"<< std::endl;
}
}
E3 = E3 -1;
for (int c= 13; c < 16; c++)
{
if(r[c] != r[15]){
m[c] = 12;
E3 = E3 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 12;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 + r[c] - 1 <<")"<< std::endl;
}
}
}
void nextpos4(){
int E4 = 6;
std::cout<<"\n"<<"Search Path 5"<<"\n";
for (int c= 4; c <= 8; c= c+ 4)
//for (int c= 4; c <= 12; c= c+ 4)
{ if(r[c] != r[8])
{ cout<<E4;
m[c] = 13;
E4 = E4 + r[c] - 1;
std::cout<<"\n";
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 13;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 + r[c] - 1 <<")"<< std::endl;
}
}
E4 = E4 -1;
for (int c= 9; c < 12; c++)
{
if(r[c] != r[11]){
m[c] = 14;
E4 = E4 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 14;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E3 + r[c] - 1 <<")"<< std::endl;
}
}
for (int c= 11; c < 16; c = c + 4)
{
if(r[c] != r[15]){
//m[c] = 15;
E4 = E4 + r[c] - 1;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 <<")"<< std::endl;
//system("pause");
}
else
{
m[c] = 15;
Finished = true;
std::cout<<"(ID,R,E)"<<"-->"<<"("<<g[c]<<","<<r[c]<<","<<E4 + r[c] - 1 <<")"<< std::endl;
}
}
}
void PrintRoute( void )
{//std::cout << "Start Node : " << g[0]<<"\t" <<"Finish Node : " << g[15] <<"\n";
std::cout << "\n"<< "Route 1 : "<<"\n";
for (int c= 1; c < 14; c++)
{
if( m[c] == 1)
std::cout<< "[r]-->"<<"\t";
else if (m[c] == 2)
std::cout<< "[u]-->"<<"\t";
else
std::cout <<"";
}
std::cout << "\n";
}
void PrintRoute1( void )
{//std::cout << "\n"<< "Start Node : " << g[0]<<"\t" << "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 2 : "<<"\n";
for (int c= 1; c < 16; c++)
{
if( m[c] == 3 )
std::cout<< "[r]-->"<<"\t";
else if (m[c] == 4 )
std::cout<< "[u]-->"<<"\t";
else if (m[c] == 5)
std::cout<<"[r]-->"<<"\t";
else
std::cout <<"";
}
std::cout << "\n";
}
void PrintRoute2( void )
{//std::cout << "\n"<< "Start Node : " << g[0] <<"\t"<< "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 3: "<<"\n";
for (int c= 0; c < 15; c++)
{
if( m[c] == 6 || m[c] == 8 )
std::cout<< "[r]-->"<<"\t";
else if (m[c] == 7 )
std::cout<< "[u]-->"<<"\t";
//else if (m[c] == 8)
// std::cout<<"[r]"<<"\t";
else
std::cout <<"";
}
std::cout << "\n";
}
void PrintRoute3( void )
{//std::cout << "Start Node : " << g[0] <<"\t"<< "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 4 : "<<"\n";
for (int c = 0; c <= 15; c++)
{
if( m[c] == 11)
std::cout<< "[u]-->"<<"\t";
else if (m[c] == 12)
std::cout<< "[r]-->"<<"\t";
else
std::cout <<"";
}
std::cout << "\n";
}
void PrintRoute4( void )
{//std::cout << "\n"<< "Start Node : " << g[0]<<"\t" << "Finish Node : " << g[15]<<"\n";
std::cout << "\n"<< "Route 2 : "<<"\n";
for (int c= 1; c < 16; c++)
{
if( m[c] == 13 )
std::cout<< "[u]-->"<<"\t";
else if (m[c] == 14 )
std::cout<< "[r]-->"<<"\t";
else if (m[c] == 15)
std::cout<<"[u]-->"<<"\t";
else
std::cout <<"";
}
}
int main( )
{ int E,E1,E2,E3;
int a, b;
int m[4][4];
ifstream in;
char state1[] = {'d','p','S','1','3'};
printmatrix(state1, 4);
cout<<"open test file : press";
in.open("C:/Users /test.txt");
if (!in) {
cout << "Cannot open file.\n";
system("pause");
return 0;
}
for (b = 1; b <= 4; b++) {cout << "\n";
for (a = 1; a <= 4; a++) {
in >> m[a][b];
cout <<"\t" << "["<<m[a][b] <<"]" ;
}
cout << "\n";
//
}
in.close();
std::cout << "\n";
int mt[6][6];
cout<<"open training file : press";
ifstream i;
i.open("C:/Users/training.txt");
if (!i) {
cout << "Cannot open file.\n";
return 0;
}
cout << "\n";
for (b = 1; b <= 6; b++) {cout << "\n";
for (a = 1; a <= 6; a++) {
i >> mt[a][b];
cout <<"\t" << "["<<mt[a][b] <<"]" ;
}
cout << "\n";
}
i.close();
std::cout << "\n";
system("pause");
int F = E = E1 = E2= E3;
std::cout<< "Enter Energy E";
//std::cin>>E >> E1 >> E2 >> E3 >>E4;
std::cin>>F;
std::cout << "Start Node : " << g[0] <<"\t"<< "Finish Node : " << g[15]<<"\n";
while( !Finished )
FindNextPos();
PrintRoute( );
nextpos1();
PrintRoute1();
nextpos2();
PrintRoute2();
nextpos3();
PrintRoute3();
nextpos4();
PrintRoute4();
std::cin.get( );
system("pause");
return 0;
}