6
\$\begingroup\$

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;
}
\$\endgroup\$
3
  • \$\begingroup\$ This belongs to stackoverflow. \$\endgroup\$ Apr 12, 2012 at 8:28
  • 1
    \$\begingroup\$ The code displaying code does not handle tabs. You should remove them before posting. And overall just tidy up your indentation. Currently it is all over the place and it makes it impossable to tell your indent and thus where you are forgetting to use {} \$\endgroup\$ Apr 12, 2012 at 14:34
  • \$\begingroup\$ @fish: The request itself does, yes. On the other hand, the code-styling needs improvement and that aspect does belong here. I have now addressed just that. \$\endgroup\$
    – Jamal
    Aug 9, 2013 at 21:51

1 Answer 1

4
\$\begingroup\$

I'm going to try to review this in spite of the lack of decent indentation and whitespace (in some places). As such, I won't mention them below. I'd recommended improving on this first, as that will help you improve everything else.

  • The #include <...>s could be in alphabetical order for better organization. Before that's done, remove that needless whitespace before #include <iostream>.

  • #include <math.h> should be #include <cmath> instead; the former is a C library.

  • #define SPACE is unnecessary. It brings up another important point: always try to use consts instead of #defines.

  • Do not use using namespace std in global scope. If it must be used, it's best to limit the scope as much as possible (such as just putting in inside a function as needed). I have no idea why it's used twice in a row, though. What's even weirder is that you're still putting the std:: in the right places, even with the usings. Just get rid of the usings altogether.

  • I recommend two things for your g[] and r[] in global:

    1. Make them both const since they appear to be that way.

    2. Make them more C++-like and safer by using std::arrays instead:

      std::array<int, 16> g = {33,10,32,21,24,26,5,6,22,24,3,15,29,8,11,9};
      std::array<int, 16> r = {0,2,0,-1,2,0,0,0,-1,0,2,2,0,0,-5,0};
      

      Having such arrays will allow you to pass them around safely and will save you the "size" parameter since they already know their sizes (accessible via myArray.size()).

  • The commented-out code looks very messy and confusing. Are you uncertain about using such code, or does it indicate "work in progress" or something? There should at least be comments to describe the intent.

  • You should give your variables more descriptive names. It's not at all clear what they're supposed to do. On the other hand, your function names look okay.

  • Your functions don't need a 'void' parameter. That's only for C programs.

  • The char [] in main() implies C and not C++. In the latter, there's a type called std::string. It represents a char [], but is instead a class and uses a better implementation. However, if I understand this array's purpose, it's not meant to be a string per se. If that is the case, then I recommend an enum instead. Otherwise, always use std::string instead of char [].

  • Don't return 0 when something causes an unsuccessful termination, otherwise the compiler will think it was successful. In this case, it's failure to open a file. Instead, use return 1 or even return EXIT_FAILURE (same return value, but more readable).

  • Do not use system("PAUSE") when trying to pause the console. Especially do not "sprinkle" it around your program. That just reduces readability and strains the system with each pause. Here are two alternatives:

    1. If you must pause in between program operations, use std::cin.get() instead. Unlike system("PAUSE"), this is not system-specific and it's not nearly as demanding on the system. Even then, "sprinkling" this code does not look good.

    2. Set your IDE to pause automatically before program termination. If you're executing this in a command line instead, an explicit pause at the end is not needed.

  • Overall, your code needs major refactoring or at least commenting so that it can be understood. This is not just about the lack of indentation, though. I, as one of your readers, am having trouble understanding the processes. It may just be my lack of experience with its purpose, though. Even then, proper commenting and good, consistent indentation will move mountains.

\$\endgroup\$
2
  • \$\begingroup\$ Do you really sort your includes alphabetically? ...OCD right? :p +1 just for "proper commenting and good, consistent indentation will move mountains". \$\endgroup\$ Nov 16, 2013 at 3:15
  • 1
    \$\begingroup\$ @retailcoder: Another C++ reviewer has told me about that, and I agree. Also the same one who got me to use Type const& something instead of const Type& something (but the includes are more notable). \$\endgroup\$
    – Jamal
    Nov 16, 2013 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.