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 6 of 6

Thread: multidimensional array 'advancing'?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default multidimensional array 'advancing'?

    hi,

    when you have this for example:
    String[][] table = new String[4][4];
    		table[0][0] = ". ";
    		table[0][1] = ". ";
    		table[0][2] = ". ";
    		table[0][3] = ". ";
    		table[1][0] = ". ";
    		table[1][1] = ". ";
    		table[1][2] = ". ";
    		table[1][3] = ". ";
    		table[2][0] = ". ";
    		table[2][1] = ". ";
    		table[2][2] = ". ";
    		table[2][3] = ". ";
    		table[3][0] = ". ";
    		table[3][1] = ". "; 
    		table[3][2] = ". ";
    		table[3][3] = ". ";

    which is printed like this:
    . . . .
    . . . .
    . . . .
    . . . .

    is there a way to move around the board?
    kinda like this:
    table[1][1] = snake;
    		//move is input
                    switch (move) {
    		case "a":
    			table[]([]--)

    so when you input "a", the snake (which is an o instead of a .) stays in the same primary table but decreases in the secondary table.
    e.g. original location table[1][1]
    input a: table[1][0]

    it would look like this:
    . . . .
    o. . .
    . . . .

    any ideas?
    iam pretty new to Java and programming at all, so i thought to making a command prompt snake game would be fun, iam planning to add random apple generation, eating them etc..

    and, is there a way to determine the current table the snake is in?
    so i can say current snake location becomes a . again once the snake moves?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: multidimensional array 'advancing'?

    Think about using 2 variables, say x and y, each access the appropriate portion of the array. You can then use these to access the array elements, and increment/decrement/set them to 'move' through the array

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimensional array 'advancing'?

    you mean like this:

    	switch (move){
    			case "a": x--;
    			break;
    			case "w": y++
    			break;
    			case "d": x++
    			break;
    			case "s": y--
     
     
    			if (x == 0 & y==0) {
    				table[0][0] = o
    			}
    			if (x == 0 & y==1) {
    				table[0][1] = o
    			}
    etc...
    ?

    oh and, it says:
    Cannot switch on a value of type String. Only convertible int values or enum constants are permitted,
    is there a way via switch to compare an input (in this case called "move") with different cases and then let them be carried out if true?

    real problem, the input (the reader (buffered reader thingy)) cante be an int... but i cant switch a string.
    Last edited by kitube; January 24th, 2011 at 11:34 PM.

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimensional array 'advancing'?

    i did this now:
    int y = 0;
    			int x = 0;
     
    			if (move == '8'){
    				y++;
    			}
    			if (move == '5'){
    				y--;
    			}
    			if (move == '4'){
    				x--;
    			}
    			if (move == '6'){
    				x++;
    			}
    			System.out.println ("\r" + move);
    			System.out.println ("\r" + x);
    			System.out.println ("\r" + y);

    that kinda works, but to check if it works, i let it print out the x and y after each use, and they never changed, they didnt decrease or increase, they always stayed the same.
    any ideas?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: multidimensional array 'advancing'?

    Quote Originally Posted by kitube View Post
    you mean like this...
    The scope of the program you provide is limited, but I meant something more like (pseudo-code)
    int x, y;
    ...
    conditional:
        x--;
    conditional:
        y++;
    ...
    table[x][y] = somevalue;


    Quote Originally Posted by kitube View Post
    Cannot switch on a value of type String. Only convertible int values or enum constants are permitted,
    is there a way via switch to compare an input (in this case called "move") with different cases and then let them be carried out if true?
    You cannot switch on a string: use a primitive or enum. Without knowing exactly what you wish to switch and the full scope of your program on its hard to throw out advice along these lines.

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: multidimensional array 'advancing'?

    works now,
    my snake moves, the old gets deleted, it can eat apples, they get randomly generated ect...

Similar Threads

  1. prininting out a 4x4 array according to an order by a 1d array
    By fortune2k in forum Collections and Generics
    Replies: 7
    Last Post: November 25th, 2010, 11:54 AM
  2. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 04:29 PM
  3. Advancing day w/ if/else & switch statement
    By rbread80 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 10:43 PM
  4. Need help in multidimensional array
    By Stefan_Lam in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 14th, 2010, 07:52 PM
  5. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM

Tags for this Thread