patterncppMinor
How could I make this path-finding code dynamic?
Viewed 0 times
thispathmakecouldfindingdynamichowcode
Problem
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.
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
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 """"""""""" """""""" """""""""""""""""""""""> m[a][b];
cout > mt[a][b];
cout >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;
}Solution
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
-
-
-
Do not use
-
I recommend two things for your
-
Make them both
-
Make them more C++-like and safer by using
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
-
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
-
Don't
-
Do not use
-
If you must pause in between program operations, use
-
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.
-
The
#include s could be in alphabetical order for better organization. Before that's done, remove that needless whitespace before #include .-
#include should be #include 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:-
Make them both
const since they appear to be that way.-
Make them more C++-like and safer by using
std::arrays instead:std::array g = {33,10,32,21,24,26,5,6,22,24,3,15,29,8,11,9};
std::array 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:-
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.-
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.
Code Snippets
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};Context
StackExchange Code Review Q#10805, answer score: 4
Revisions (0)
No revisions yet.