1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Moving around a 2D array

Discussion in 'Java' started by cyrow, Feb 6, 2008.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Read the attached file.
    1.start with ‘X’ in array[0][0]
    2.Move to Right if end is reach and still Right, ‘X’ appears on left side.
    3.Move to Left if end is reach and still Left, ‘X’ appears on Right side.
    4.Move to Up if end is reach and still Up, ‘X’ appears from bottom.
    5.Move to Down if end is reach and still Down, ‘X’ appears from top.
    6. If there is an obstatcle in the way, program beep
    Code:
    import java.io.*;
    import java.util.*;
    class ***{
    	public static void main(String[] args)throws IOException{
    	//	Thread t = Thread.currentThread();
    		
    		int maxcol = 60;
    		int maxrow = 20;
    		int colpos = 0;
    		int rowpos = 0;
    		char[][]array = new char[20][60];
    
    	
    		Scanner in = new Scanner(new FileReader("obstruct.txt"));	
    
    
    			int i =0;
    			int j =0;
    			System.out.print(array[i][j]='X');
    			
    		 for (i =1; i<array.length;i++){
    		 	String ss = in.nextLine();
    			  for(j = 1; j<array[i].length;j++){
    			   	array[i][j]= ss.charAt(j);
    			   	System.out.print(array[i][j]);
    			   }
    			   System.out.println("  ");
    		 }
    	
    		int ans = check();
    		if (ans==1){
    		right(array,rowpos, colpos,ans,maxcol);	
    				PrintResults(array);
    		}else if(ans ==2){
    			left(array,rowpos, colpos,ans,maxcol);
    					PrintResults(array);
    		} else if (ans ==3){
    			up(array,rowpos, colpos,ans,maxrow,maxcol);	
    				PrintResults(array);	
    		}else if(ans ==4){
    			down(array,rowpos, colpos,ans,maxcol,maxrow);
    			PrintResults(array);
    		}else{
    				Quit(ans);
    		}
    	}//end main
    	
    	public static int check(){
    		Scanner in = new Scanner(System.in);
    			System.out.println("1.Right"+"  "+ "2. Left"+" "+"3. Up"+" "+"4. Down"+" "+"5.Quit");
    			int choice = in.nextInt();
    			return choice;
    	}	
    		
    	public static boolean isEmpty(char[][]arr, int rpos,int cpos){
    			return ((arr[rpos][cpos])==0);
    		}
    		
    	
    	public static void right(char[][]ar, int rpos, int cpos, int answ, int mcol){
    	
    	
    	if((rpos == 0)&&(cpos>=0)&&(cpos<mcol-1)){
    		cpos++;
    		
    		if(isEmpty(ar,rpos, cpos)){
    			
    			ar[rpos][cpos] = 'X';
    			System.out.print(ar[rpos][cpos]);
    		}else if(!isEmpty(ar,rpos, cpos)){
    			java.awt.Toolkit.getDefaultToolkit().beep();
    		}		
    	 }
    	
    	  
    	}	
    	
    	public static void left(char[][]ary,int ropos, int copos,int answer, int mxcol){
    		if((ropos >=0)&&(copos>=0)&&(copos<mxcol)){	
    			copos = mxcol-1;
    			
    			
    		if(isEmpty(ary,ropos, copos)){
    			ary[ropos][copos] = 'X';
    			 System.out.print(ary[ropos][copos]);
    		}else if(!isEmpty(ary,ropos, copos)){
    			java.awt.Toolkit.getDefaultToolkit().beep();
    		}
    			}
    }
    	
    	public static void up(char[][]arry,int rrpos, int ccpos,int answ, int mxrow, int mxcol){
    			if((rrpos < mxrow)&&(ccpos>=0)&&(ccpos<mxcol)){
    			rrpos = 0;
    		if(isEmpty(arry,rrpos, ccpos)){
    			arry[rrpos][ccpos] = 'X';
    			 //System.out.print(arry[rrpos][ccpos]);
    		}else if(!isEmpty(arry,rrpos, ccpos)){
    			java.awt.Toolkit.getDefaultToolkit().beep();
    		}
    	}
    }
    	
    	public static void down(char[][]array1,int rowpos1, int colpos1,int answer1, int mxcol1, int mxrow1){
    		if((rowpos1 <= 0)&&(colpos1>=0)&&(colpos1<mxcol1)){
    			rowpos1 = mxrow1 - 1;
    		
    		if(isEmpty(array1,rowpos1, colpos1)){
    			array1[rowpos1][colpos1] = 'X';
    			System.out.print(array1[rowpos1][colpos1]);
    		}else if(!isEmpty(array1,rowpos1, colpos1)){
    			java.awt.Toolkit.getDefaultToolkit().beep();
    			array1[rowpos1][colpos1++]='X';
    			System.out.print(array1[rowpos1][colpos1]);
    		}
    	}	
    }
    	
    	public static void Quit(int ansChoice){
    		System.exit(0);
    	}
    	
    	public static void PrintResults(char [][]a){
    		for (int i =1; i<a.length;i++){
    		  for(int j = 1; j<a[i].length;j++){
    			  System.out.print(a[i][j]);
    		  }
    			   System.out.println("  ");
    		 }
    		}
    }//end class
    Code not working the way it should, any insights would help
     
    Last edited by a moderator: Feb 6, 2008
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Added code block and removed attachment as attachment had nothing related to the query.
     
  3. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    You must specify how your code is not working.
    What's the problem?
     

Share This Page