Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy My code doesn't turn left

    Hello,

    I have a 300 lines program that should solve a maze made of a 2 dimensional array objects called Cell. The program is called FinalSolve and it makes use of
    • findCellWithValue
    • findNeighbors
    • setTheValue


    findCellWithValue returns an array made of Cell objects that have the value specified as an argument

    Code:
    	public Cell[] findCellWithValue(int theValue)
    	{
    		int currentNoOfCells = 0;
    		Cell[] theCells = new Cell[INITIAL_ARRAY_SIZE];
    			for(int row = 0; row < HEIGHT; row++)
    			{
    				for(int column = 0; column < WIDTH; column++)
    				{
    					if(maze[column][row].value() == theValue)
    					{
    						if(currentNoOfCells == theCells.length)
    						{
    							Cell[] biggerArray = new Cell[theCells.length + ARRAY_RESIZE_FACTOR];
                                System.arraycopy(theCells, 0, biggerArray, 0, theCells.length);
    							theCells = biggerArray;
    							
    						}
    						theCells[currentNoOfCells] = maze[column][row];
    						currentNoOfCells++;
    						
    					}
    					
    				}
    				
    			}
    		return theCells;
    	}
    setTheValue just sets the value given as arguement to a cell given as argument

    findNeighbors is the most complicated method from this program I might say. Here it is
    Code:
    	public Cell[] findNeighbors(Cell theCell)
    		{
    			Cell[] neighborsCell = new Cell[1];
    			int neighbors = 0;
    			int column = theCell.column();
    			int row = theCell.row();
                            if(column - 1 < 0)
                            {
                            
                            }
                            else
                            {
                            	char someElse = maze[column-1][row].symbol();
                            	if(someElse == ' ')
                            	{
                                        System.out.println("Good");
    								if(neighbors == neighborsCell.length)
                            		{
                            			Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                            			System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                            			neighborsCell = biggerArray;
                                                    System.out.println("Once");
    
                            		}
                            		neighborsCell[neighbors] = maze[column-1][row];
                                            neighbors++;
    
                            	}
                            }
                            if(column + 1 >= 20 )
                            {
                            
                            }
                            else
                            {
                            	char someElse = maze[column+1][row].symbol();
                            	if(someElse == ' ')
                            	{
                                        System.out.println("Good");
    								if(neighbors == neighborsCell.length)
                            		{
                            			Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                            			System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                            			neighborsCell = biggerArray;
                                                    System.out.println("Once");
    
                            		}
                            		neighborsCell[neighbors] = maze[column+1][row];
                                            neighbors++;
    
                            	}
                            }
                            if(row - 1 < 0){}
                            else
                            {
    							char someElse = maze[column][row-1].symbol();
                            	if(someElse == ' ')
                            	{
                                        System.out.println("Good");
    								if(neighbors == neighborsCell.length)
                            		{
                            			Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                            			System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                            			neighborsCell = biggerArray;
                                                    System.out.println("Once");
    
                            		}
                            		neighborsCell[neighbors] = maze[column][row-1];
                                            neighbors++;
    
                            	}
    
                            }
                            if(row + 1 >= 10){}
                            else
                            {
    							char someElse = maze[column][row+1].symbol();
                            	if(someElse == ' ')
                            	{
                                        System.out.println("Good");
    								if(neighbors == neighborsCell.length)
                            		{
                            			Cell[] biggerArray = new Cell[neighborsCell.length + 1];
                            			System.arraycopy(neighborsCell, 0, biggerArray, 0, neighbors - 1);
                            			neighborsCell = biggerArray;
                                                    System.out.println("Once");
    
                            		}
                            		neighborsCell[neighbors] = maze[column][row+1];
                                            neighbors++;
    
                            	}
    
                            }
    
                            
                            
    			return neighborsCell;
    		}
    So there you go here are the main methods. I won't bother you with the constructor and with the toString since it doesn't matter I believe

    Now the solve method combines all these methods together in the desperate try to solve it ( for 1 month )

    Here it is:

    public void solve()
    {
    Cell[] entrance = findCellWithValue(250);
    Cell[] neighborsOf = findNeighbors(entrance[0]);
    for(Cell neighbor : neighborsOf)
    {
    setTheValue(1, neighbor);
    }
    int moveCount = 1;
    boolean foundExit = false;

    while(!foundExit && moveCount <= 200)
    {
    Cell[] theCells = findCellWithValue(moveCount);
    for(Cell justOne : theCells)
    {
    if(justOne != null){
    if(justOne.symbol() == '!')
    {
    foundExit = true;
    markPath();
    break;
    }
    else
    {
    Cell[] moreNeighbors = findNeighbors(justOne);
    for(Cell prostie : moreNeighbors)
    {
    if(prostie != null)
    {
    if(prostie.value() == 0)
    {
    System.out.println("" + moveCount);
    setTheValue(moveCount+1, prostie);
    }
    }
    }
    }
    }
    }
    moveCount++;
    }
    System.out.println("" + moveCount);
    System.out.println(var_dump() + "");




    }// solve

    I also have a var_dump method ( yes inspired by the php function, however is nothing like it). This var_dump method prints the entire maze according to the values. So here is an image to how it actually looks. The top bit is var_dump the bottom is just the toString

    http://imgur.com/R090YnS

    So as you can see my maze does not want what so ever to turn left. It either goes right or down. Any ideas what might be causing this?

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Mazes with graphs are complex enough, let alone with a matrix instead.
    Lets get some information on the usage first. This findNeighbours method, is this designed to find all immediate neighbours, or only traversable ones (in this case the ones with symbol type ' ', ?, and ! from that I can tell of the image)? I would suggest its doing a bit too much work overall on its own. I would perceive that a neighbour would only be up to 4 possible objects (8 if you can move diagonally, but I only see 4 when using the CLI which makes more sense). So should we only be finding the immediate neighbours for an item that we can actually move to?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •