Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Infinite Loop in my code.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Infinite Loop in my code.

    Basically we are give a 2D array and we have to navigate a path out. 1's represent path and 0's represent "swamp" The game is set to stop you if you come to an edge, and change your current space to "-1", to stop you program to keep going back and forth.
    All I get is an infinte loop saying STEP TO [2,3]

    /*
    	Program9.java
     
    */
     
    import java.io.*;
    import java.util.*;
     
     
    public class Program9
    {
        public static void main(String[] args)
        {
    		int myRow=2, myCol=3;  // you are dropped into the swamp at: [2][3]
     
    		int[][] swamp1 =
    		{
    			{ 0,0,0,0,0,0,0,0,0,0 },
    			{ 0,0,0,0,0,0,0,0,0,1 },
    			{ 0,0,0,1,0,0,0,0,1,0 },
    			{ 0,0,0,1,0,0,0,1,0,0 },
    			{ 0,0,0,0,1,0,1,0,0,0 },
    			{ 0,0,0,0,1,0,0,1,0,0 },
    			{ 0,0,0,0,1,0,0,0,1,0 },
    			{ 0,0,0,0,0,1,0,1,0,0 },
    			{ 0,0,0,0,0,0,1,0,0,0 },
    			{ 0,0,0,0,0,0,0,0,0,0 }
    		};
     
    		printSwamp( "SWAMP1",swamp1 );
    		printEscapePath( swamp1, myRow,myCol );
     
     
    		// Now swamp2
     
    		myRow=2; myCol=8;  // you are dropped into the swamp at [2][8]
     
    		int[][] swamp2 =
    		{
    			{ 0,0,0,0,0,0,0,0,0,0 },
    		 	{ 0,0,0,0,0,0,0,0,0,0 },
    	 		{ 0,0,1,0,0,0,0,1,1,0 },
    		 	{ 0,0,0,1,0,0,1,0,0,0 },
    	 		{ 0,0,0,0,1,1,0,0,0,0 },
    		 	{ 0,0,0,0,0,0,0,0,0,0 },
    	 		{ 0,0,0,0,0,0,0,0,0,0 },
    		 	{ 0,0,0,0,0,0,0,0,0,0 },
    	 		{ 0,0,0,0,0,0,0,0,0,0 },
    		 	{ 0,0,0,0,0,0,0,0,0,0 }
    	 	};
     
    		printSwamp( "\nSWAMP2",swamp2 );
    		printEscapePath( swamp2, myRow,myCol );
     
    	} // END MAIN
     
    	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     
    	// 	DO NOT MODIFY THIS METHOD - USE AS GIVEN
     
        private static void printSwamp(String label, int[][] swamp )
        {
     
            System.out.println( label );
            for(int r = 0; r < swamp.length; r++)
    		{
                for(int c = 0; c < swamp[r].length; c++)
                     System.out.print( swamp[r][c] + " ");
                System.out.println();
            }
        }
     
    	// 	DO NOT MODIFY THIS METHOD - USE AS GIVEN
     
        private static void printEscapePath(int[][] swamp, int myRow, int myCol)
        {
     
    		int[] coords = new int[2];
    		coords[0]=myRow;
    		coords[1]=myCol;
     
    		System.out.println("STARTING AT: " + "["+ myRow + "][" + myCol + "]");
     
    		while ( !onEdge( swamp, coords ) )
    		{
    			// nextStep returns true if there IS a safe step,  else returns false
    			// If it returns true then before it returns, it marks the spot you are sitting on as -1
    			// then updates  the  x and y inside the coords to be the new coords of where it is stepping to.
    			// You must mark your curr position as -1 so you don't keep going back and forth
     
    			if ( nextStep( swamp, coords ) )
    			{
    				System.out.println( "STEPPED  TO: [" + coords[0] + "][" + coords[1] + "]" );
    			}
    			else // YOU'RE CROC MEAT :=(
    			{
    				System.out.println( "STRANDED AT: [" + coords[0] + "][" + coords[1] + "]" );
    				return;
     
    			} // END WHILE NOT OUT OF SWAMP YET
     
    			// IF YOU MAKE IT HERE THEN YOU ARE ON THE EDGE .. FREEDOM!!!
    		}
     
    		System.out.println( "ESCAPED  AT: [" + coords[0] + "][" + coords[1] + "]" );
     
    	} // END PRINT ESCAPE PATH
     
    // ###############################################################
     
    	// 	YOU MUST WRITE THIS METHOD
     
    	// LOOKS AT EVERY ADJACENT SQUARE TO SEE IF IT'S A 1
    	// IF IT IS, THEN THE CURRENT X,Y IS CHANGED TO -1 (i.e. BEEN HERE)
    	// AND THE COORDS ARE CHANGED TO THOSE OF THE NEARBY SQAURE WITH A 1 IN IT
    	// IF NO SAFE STEP IS FOUND THEN JUST RETURN FALSE. DO NOT MODIFY COORDS
     
    	private static boolean nextStep( int[][] swamp, int[] coords )
    	{
    	int myRow = coords[0];
    	int myCol = coords[1];
    	int temp = 0;
    	int x = 0;
    	int y = 0;
    	y = myRow;
    	x = myCol;
    		if (swamp[x+1][y] == 1)//Moving North
    		{
    			temp = swamp[x-1][y];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x+1][y];
    			return true;
    		}
    		if (swamp[x+1][y] == 1)//Moving South
    		{
    			temp = swamp[x][y];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x+1][y];
    			return true;
    		}
    		if (swamp[x][y-1] == 1)//Moving East
    		{
    			temp = swamp[x][y-1];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x][y-1];
    			return true;
    		}
    		if (swamp[x][y+1] == 1)//Moving West
    		{
     
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x][y+1];
    			return true;
    		}
    		if( swamp[x-1][y-1] == 1)//Moving NorthWest [-1][-1]
    		{
    			temp = swamp[x][y];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x-1][y-1];
    			return true;
    		}
    		if(swamp[x-1][y+1] == 1)//Moving NorthEast [-1][+1}
    		{
    			temp = swamp[x][y];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x-1][y+1];
    			return true;
    		}
    		if(swamp[x+1][y-1] == 1)//Moving SouthWest [+1][-1]
    		{
    			temp = swamp[x][y];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x+1][y-1];
    			return true;
    		}
    		if(swamp[x+1][x+1] == 1)//Moving SouthEast [+1][+1]
    		{
    			temp = swamp[x][y];
    			swamp[x][y] = -1;
    			swamp[x][y] = swamp[x+1][y+1];
    			return true;
    		}
     
     
    		else 
    		{
    			return false;  // just to make it compile you edit as needed
    		}
    	}
     
     
    	// 	YOU MUST WRITE THIS METHOD
     
    	// RETURNS TRUE IF YOUR CURR COORDS ARE ON THE FIRST OR LAST ROW OR COLUMN
    	// i.e RETURNS TRUE IF YOU ARE ON THE EDGE/BORDER OF THE SWAMP
    	// ELSE RETURNS FALSE
     
    	private static boolean onEdge( int[][] swamp, int[] coords )
    	{
    		if (coords[0] == 0)
    		{
    			return true;
    		}
    		if (coords[0] == 10)
    		{
    			return true;
    		}
    		if (coords[1] == 0)
    		{
    			return true;
    		}
    		if (coords[1] == 10)
    		{
    			return true;
    		}
     
    		else 
    		{
    			return false;  // just to make it compile you edit as needed
    		}
    	}
    }


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Infinite Loop in my code.

    Your if statements in nextStep() need to be updated
    //Moving North: [x-1][y]
    //Moving SouthEast [x+1][y+1]

    onEdge should be comparing to 9 to see if it on an outer edge, not 10. The index value of the 10th entry is 9

Similar Threads

  1. Infinite loop?
    By jackfletcher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 01:06 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  3. help me terminate the infinite loop.
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 10th, 2012, 02:40 AM
  4. Confused about infinite loop
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: March 9th, 2011, 06:45 AM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM