Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.32/5 (6 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace chapterOneAssigment12
{
    class Program
    {
        // Declare array

        char[,] grid = new char[10, 10];               
             
        static void Main(string[] args)
        {
            // Open file

            StreamReader input = new StreamReader("maze.txt");         
                    
            // Char Variable

            char value;

            // Junk String

            string junk;

            // Declare array

            char[,] grid = new char[10, 10];
            
            
            // Algorithm To read in maze

            for (int row = 0; row < 10; row++)
            {
                for (int column = 0; column < 10; column++)
                {
                    value = (char)input.Read();
                    grid[row, column] = value;
                }

                // Dump extra characters 

                junk = input.ReadLine();
            }
            
            input.Close();

            // Other Algorithm To read in maze  

            for (int row1 = 0; row1 < 10; row1++)
            {
                for (int column1 = 0; column1 < 10; column1++)
                {
                    Console.Write(grid[row1, column1]);
                }
                
                Console.WriteLine("");
            }

            MazeSolve(1, 1);    
            
            // Display 

            StreamWriter output = new StreamWriter("maze.out");  
          
            // Call the solve method

            
        }

        // Exit Found Method To Find the exit

        public static void MazeSolve( int row, int column)
        {  
            // Declare boolean 

            bool exitFound;

            exitFound = false;

            if (row == 1)
            {
                exitFound = true;
            }
            else if (row == 9)
            {
                exitFound = true;
            }
            else if (column == 1)
            {
                exitFound = true;
            }
            else if (column == 9)
            {
                exitFound = true;
            }
            else
            {
                if (!exitFound && )
                {
                    
                }
                if (!exitFound &&)
                {
                }
                if (!exitFound &&)
                {
                }
                if (!exitFound &&)
                {
                }
            }

            if (exitFound == true)
            {

                
            }

        }


    } 
}
Posted
Updated 31-Oct-13 3:38am
v2
Comments
Fredrik Bornander 31-Oct-13 9:37am    
Is this a school assignment?
Thomas Daniels 31-Oct-13 9:43am    
What's the content of the file? If we don't know the characters that are in the file, we can't help you.
Rob Philpott 31-Oct-13 10:05am    
That's fun, and you're right - recursion is an excellent way to solve it. You haven't provided any information on what the characters in the grid mean. But either way you're unlikely to find help here, stuff like this is there to teach you about computer theory and so you should do it yourself.
Sampath Lokuge 31-Oct-13 10:05am    
Can you tell us what kind of help do you need from us ?

There are a number of things you might want to deal with first: the most significant being that the local declaration of grid inside your Main method masks the class level version you declare outside it.
Since the version inside the method will not be available to methods outside Main such as MazeSolve, that means whatever you do in Main is pretty much irrelevant... :laugh:

Get rid of the one inside, or pass it as a parameter to the MazeSolve method.

When it comes to solving a maze, there are a number of ways to do it: http://en.wikipedia.org/wiki/Maze_solving_algorithm[^] - some are better than others.

Recursion is one way to code it - and not a bad one, because it lets you "back up" and try another route if this doesn't work.
So, the FindTheWayOut method needs to be passed a cell reference (the start point), and a direction it can't go in (the way you entered, to prevent you looping back out the way you came in) and to return a value which describes the route, or "no route this way" if it can't find it.
Inside the method, you try each direction in turn by the routine for each of the possible directions you can leave the cell. If one of the calls succeeds, pass the route on back up. If it doesn't try the next direction. If no direction works, pass back "no route this way" and exit.

Try it on a smaller grid to start with (2x2 with just a way in and a way out is simple) and see what happens in the debugger before you work up to more complex mazes with actual walls!

If you still aren't sure, try it on paper first - that should give you a better idea.
 
Share this answer
 
There are some problems with your code in terms of organization, legibility, etc. First, when you declare the exitFound variable you made the declaration and later the assignment, I don't see why. Also I don't see why you consider that being in the first row means that you've found the exit. You don't have to use recursion, as you must know every recursive algorithm has its iterative version and viceversa. Even though the recursive version could be elegant and short-coded the iterative version might be easier to build. In your problem the recursive method makes a call to itself in every feasible direction, until it reaches the exit, which is the stopping criteria.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900