Welcome to Dream.In.Code
Become a Java Expert!

Join 150,370 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,808 people online right now. Registration is fast and FREE... Join Now!




Two Dimensional Arrays

 
Reply to this topicStart new topic

Two Dimensional Arrays, Moving a character around a 2D array

chetah
6 Feb, 2008 - 03:29 AM
Post #1

D.I.C Head
**

Joined: 17 Nov, 2007
Posts: 61



Thanked: 1 times
My Contributions
Read attached file
start with ‘X’ in array[0][0]
Move to Right if end is reach and still Right, ‘X’ appears on left side.
Move to Left if end is reach and still Left, ‘X’ appears on Right side.
Move to Up if end is reach and still Up, ‘X’ appears from bottom.
Move to Down if end is reach and still Down, ‘X’ appears from top.



CODE

import java.io.*;
import java.util.*;
class Ass{
    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

Description of problem
I am trying to move the charcter around but, the progrma is not working the way it should, for example when I try to move Right, the character moves Right but does not go back to the left when it gets to the end.


Attached File(s)
Attached File  Obstruct.txt ( 1.39k ) Number of downloads: 32
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Two Dimensional Arrays
6 Feb, 2008 - 07:06 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
First, everything should be from 0 to <len, e.g. not for (i =1; i<array.length;i++) but for (i =0; i<array.length;i++).

Your big problem is that this
CODE

void foo(int bar) { bar++; }
int n = 5;
System.out.println(n);
foo(n);
System.out.println(n);


Will print out two 5s, where you seem to be expecting 5 and 6. When dealing with object passing, you can expect the values to be passed back out. With primitives, like int, a copy gets passed in and now changes are returned.

Hope this helps.

This post has been edited by baavgai: 6 Feb, 2008 - 07:07 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month