3 Replies - 653 Views - Last Post: 04 November 2015 - 09:40 AM Rate Topic: -----

#1 toppen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-November 15

Move position in 2d Array based on user input (and print pos)

Posted 04 November 2015 - 06:45 AM

I have a program where the user writes in commands U,D,R,L. Based on the user commands I want position to move in 2D array. For example if the user writes UDUDU then I want the position to go 'up' 'down' 'up' 'down' 'up'. So far I have only managed to move it "once". I guess I have to update the current position, its here I have trouble and I need help!

For example use decides the size [3][3] and position [1][1].

0 1 2
0 0 0 0
1 0 1 0
2 0 0 0

If the user presses UDUDU then the new position to be like this (the x to be +1 in this case)

0 1 2
0 0 0 0
1 0 0 0
2 0 1 0

Here is the code:

 Scanner scan = new Scanner(System.in);

 System.out.println("Enter length(x) and width(y)");
                    x = scan.nextInt();
                    y = scan.nextInt();

  int[][] roomsize = new int[x][y];//The array

  System.out.println("Enter the starting position");
                    x = scan.nextInt();
                    y = scan.nextInt();

  roomsize[x][y] = 1; //Starting position


System.out.println("Enter commands U(up), D(down), R(right) or  L(left)");//User input          
                   String command = scan.next();    

for (char directionCommand : command.toCharArray()){
                      move(directionCommand, roomsize, x, y);

                     }
                //Should I print it out here? It   
                /*for(int i = 0; i < roomsize.length; i++)
                   {
                      for(int j = 0; j < roomsize[i].length; j++)
                        {
                            System.out.print(roomsize[i][j]);
                            if(j < roomsize[i].length - 1)     System.out.print(" ");
                        }
                        System.out.println();
                    }*/


public static void move(int i, int[][] roomsize, int x, int y){
                    int px = 0;//Current position
                    int py = 0;//Current position
                    switch(i){
                  case 'U': px+=1; break;//Is this the right way to update position?
                  case 'D': roomsize[x-1][y]; break;//Or this waY?
                  case 'R': roomsize[x][y+1] = 1; break;
                  case 'L': roomsize[x][y-1] = 1;break;                
                  default:; break;
                  }             
                    roomsize[px][py] = 1;//Do I set the new position like this?

 for(int i1 = 0; i1 < roomsize.length; i1++)
                       {
                          for(int j = 0; j < roomsize[i1].length; j++)
                            {
                                System.out.print(roomsize[i1][j]);
                                if(j < roomsize[i1].length - 1) System.out.print(" ");
                            }
                            System.out.println();
                        }  


And also when and where exactly do I need to "Print out" the position in array? It seems wrong doing it exactly after the switch statement? Or? Thanks in advance guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Move position in 2d Array based on user input (and print pos)

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 710
  • View blog
  • Posts: 5,434
  • Joined: 25-December 13

Re: Move position in 2d Array based on user input (and print pos)

Posted 04 November 2015 - 06:51 AM

Instead of testing with "UDUDUDU" try using a simple single move test: "U" or "D" and see if the code works properly.
If that works, then try two steps: "UD" or "DU" and see if that works.
Move from a simple test to the longer ones slowly to see if each part is working correctly.

Note; What do the values in the 2Dim array mean? I see the values 0, 1 and 2

This post has been edited by NormR: 04 November 2015 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 toppen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-November 15

Re: Move position in 2d Array based on user input (and print pos)

Posted 04 November 2015 - 07:04 AM

View PostNormR, on 04 November 2015 - 06:51 AM, said:

Instead of testing with "UDUDUDU" try using a simple single move test: "U" or "D" and see if the code works properly.
If that works, then try two steps: "UD" or "DU" and see if that works.
Move from a simple test to the longer ones slowly to see if each part is working correctly.

Note; What do the values in the 2Dim array mean? I see the values 0, 1 and 2


I have tried only U, it does the job and moves the position forward (but at the same time leaves the "old position" as (1), I guess I have to fill the "current position". When I try more then one inputs then it doesn't react to the next input (UU only the first).

The values is just the index in the array.. 0,1,2.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 710
  • View blog
  • Posts: 5,434
  • Joined: 25-December 13

Re: Move position in 2d Array based on user input (and print pos)

Posted 04 November 2015 - 09:40 AM

Quote

When I try more then one inputs then it doesn't react to the next input (UU only the first).

What are you doing to debug the code to see what it is doing wrong? I use println() statements that print out the values of variables as they are used and as their values are changed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1