Wow. I've been working on this problem all day, and it's driving me up the wall. I know there are some programmers here, so hopefully you guys can help me out with this.
Anyway, as many of you may know from my posts, I am currently taking my first semester in college. One of my classes is a standard Computer Science course, which is exclusively devoted to C++ programming. I'm slightly more than halfway through it, and it's getting tougher. There are labs twice a week, and quizzes every Monday. Homework is extremely rare (only four projects total), but they can be challenging.
I don't know when it will be given, but I suspect that the second homework is just around the corner, and I've heard that one commonly issued homework assignment in this class is a program that will navigate a maze. Though I don't know if that's the one I'll be getting, I thought I might as well practice and try to write a C++ algorithm for maze navigation on my own.
For a while, it was going pretty well. I have a matrix that produces a hollow, empty chamber onscreen. No maze yet, but I'm trying to work on the algorithm for collision detection and turning. The walls are represented by ones, the empty spaces are zeroes, and the moving character is a two.
Here's my problem. The moving character is supposed to check each of the four adjacent nodes in a clockwise circle--left, up, right, down. It can go left and up perfectly, but whenever it needs to go right or down, it stays put! I've been exploring as many solutions as I can think of, to no avail.
Do you guys think you can have a look at my code and tell me if you have a solution? I have of course commented each function and the more crucial blocks of code within them.
Anyway, as many of you may know from my posts, I am currently taking my first semester in college. One of my classes is a standard Computer Science course, which is exclusively devoted to C++ programming. I'm slightly more than halfway through it, and it's getting tougher. There are labs twice a week, and quizzes every Monday. Homework is extremely rare (only four projects total), but they can be challenging.
I don't know when it will be given, but I suspect that the second homework is just around the corner, and I've heard that one commonly issued homework assignment in this class is a program that will navigate a maze. Though I don't know if that's the one I'll be getting, I thought I might as well practice and try to write a C++ algorithm for maze navigation on my own.
For a while, it was going pretty well. I have a matrix that produces a hollow, empty chamber onscreen. No maze yet, but I'm trying to work on the algorithm for collision detection and turning. The walls are represented by ones, the empty spaces are zeroes, and the moving character is a two.
Here's my problem. The moving character is supposed to check each of the four adjacent nodes in a clockwise circle--left, up, right, down. It can go left and up perfectly, but whenever it needs to go right or down, it stays put! I've been exploring as many solutions as I can think of, to no avail.
Do you guys think you can have a look at my code and tell me if you have a solution? I have of course commented each function and the more crucial blocks of code within them.
Code:
#include <iostream>
#include <Windows.h>
using namespace std;
//PURPOSE: Specify a structure of walls, empty spaces, and place
//the start and exit points.
void construct(int maze[20][20])
{
//start point
maze[19][9] = 2;
//exit point
maze[0][10] = 0;
}
//PURPOSE: Create walls around every edge of the maze.
void initialize(int maze[20][20], int &i, int &j)
{
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
if(i == 0 || i == 19 || j == 0 || j == 19)
{
maze[i][j] = 1;
}
else
{
maze[i][j] = 0;
}
}
cout << endl;
}
construct(maze);
}
//PURPOSE: Display the maze on the console.
void display(int maze[20][20], int &i, int &j)
{
system("cls");
cout << endl;
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
cout << " " << maze[i][j];
}
cout << endl;
}
cout << endl;
}
//PURPOSE: Make the AI move through the maze.
void step(int maze[20][20], int &i, int &j)
{
for(i = 0; i < 20; i++)
{
for(j = 0; j < 20; j++)
{
if(maze[i][j] == 2)
{
//tries to go left
if(maze[i][j - 1] == 0)
{
maze[i][j - 1] = 2;
maze[i][j] = 0;
}
//tries to go up
else if(maze[i - 1][j] == 0)
{
maze[i - 1][j] = 2;
maze[i][j] = 0;
}
//tries to go right
else if(maze[i][j + 1] == 0)
{
maze[i][j + 1] = 2;
maze[i][j] = 0;
}
//tries to go down
else if(maze[i + 1][j] == 0)
{
maze[i + 1][j] = 2;
maze[i][j] = 0;
}
//unable to move at all (this should never happen)
else
{
system("cls");
cout << "Error: Dynamic value is trapped!";
}
}
}
}
display(maze, i, j);
}
//PURPOSE: Initialize the program, call the other functions,
//and refresh the maze every third of a second.
int main()
{
//counter variables (these are used throughout the program)
int i,j;
//the matrix for the maze itself
int maze[20][20];
initialize(maze, i, j);
display(maze, i, j);
while (maze[0][10] != 2)
{
//one move through the maze
step(maze, i, j);
//delays the next move so that the individual moves can be seen
Sleep(333);
}
return 0;
}
Last edited: