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.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Read ASCII art map and store the information in 2D array (int[][] or char[][])

  1. #26
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    See post #18. Option 2 is probably the simplest.

  2. #27
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])


  3. #28
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    Hi, I've nearly finished everything now, and it wouldn't have been possible without your advice, so thanks!

    One last question. I have to make it so entering "LOOK" will reply with a 5x5 grid of what is surrounding the player. I've done it using this code:
    System.out.print(Map.matrix[playerX-2][playerY-2]);
    			System.out.print(Map.matrix[playerX-2][playerY-1]);
    			System.out.print(Map.matrix[playerX-2][playerY]);
    			System.out.print(Map.matrix[playerX-2][playerY+2]);
    			System.out.print(Map.matrix[playerX-2][playerY+2]);
    			System.out.println("\n");
    			System.out.print(Map.matrix[playerX-1][playerY-2]);
    			System.out.print(Map.matrix[playerX-1][playerY-1]);
    			System.out.print(Map.matrix[playerX-1][playerY]);
    			System.out.print(Map.matrix[playerX-1][playerY+1]);
    			System.out.print(Map.matrix[playerX-1][playerY+2]);
    			System.out.println("\n");
    			System.out.print(Map.matrix[playerX][playerY-2]);
    			System.out.print(Map.matrix[playerX][playerY-1]);
    			System.out.print(Map.matrix[playerX][playerY]);
    			System.out.print(Map.matrix[playerX][playerY+1]);
    			System.out.print(Map.matrix[playerX][playerY+2]);
    			System.out.println("\n");
    			System.out.print(Map.matrix[playerX+1][playerY-2]);
    			System.out.print(Map.matrix[playerX+1][playerY-1]);
    			System.out.print(Map.matrix[playerX+1][playerY]);
    			System.out.print(Map.matrix[playerX+1][playerY+1]);
    			System.out.print(Map.matrix[playerX+1][playerY+2]);
    			System.out.println("\n");
    			System.out.print(Map.matrix[playerX+2][playerY-2]);
    			System.out.print(Map.matrix[playerX+2][playerY-1]);
    			System.out.print(Map.matrix[playerX+2][playerY]);
    			System.out.print(Map.matrix[playerX+2][playerY+1]);
    			System.out.print(Map.matrix[playerX+2][playerY+2]);

    Which works fine, unless a player is in a corner or against the wall of the map because then the player coordinate -2 leads to it trying to find something in the array that doesn't exist. I want to make it so if that happens it prints a "?" but I can't figure out how. The error I get is:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

    Thanks again!



    EDIT:
    Just had a brainwave - I did it using try and catch statements and it seems to be working perfectly!

  4. #29
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    I think you should stop at the edge of the playing field and NOT print a status for those areas of the playing field that don't exist. That would be easy enough to do with an 'if' statement.

    However, if you insist on that approach, then enlarge your array so that there's a 2-column buffer on each side and a 2-row buffer across the top and bottom. The buffer area will not be involved in play but will give you the option to present '?' or whatever you'd like to show the 5x5 area.

    --- Update ---

    I think you should stop at the edge of the playing field and NOT print a status for those areas of the playing field that don't exist. That would be easy enough to do with an 'if' statement.

    However, if you insist on that approach, then enlarge your array so that there's a 2-column buffer on each side and a 2-row buffer across the top and bottom. The buffer area will not be involved in play but will give you the option to present '?' or whatever you'd like to show the 5x5 area.

  5. #30
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    Hi, following on from my last question kind of, I'm trying to use a for loop to print out the 2d array that is around it. I'm trying to do this my creating a new 2d array of these coordinates but I'm having some trouble. Here's my code so far, but I don't know exactly how to do what I want to:

    		char[][] theLookMatrix = new char[5][5];
     
     
    		for (int i=-2; i<3; i++) {
    		     for (int j=-2; j<3; j++)
    		        theLookMatrix[][] =  Map.matrix[playerX+i][playerY+j]);
    		        System.out.print(" ");
    			    System.out.println("");
    		        }

    But there's an error on the line:
    theLookMatrix[][] =  Map.matrix[playerX+i][playerY+j]);

    Please can you help me fix it, thanks!

  6. #31
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    What's the error?

  7. #32
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    Quote Originally Posted by GregBrannon View Post
    What's the error?
    There isn't a runtime error as I can't run it. But it wants me to change - char theLookMatrix[][] to just char theLookMatrix. If I do this then there are still more errors.

  8. #33
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    In the line with the error, this term:

    Map.matrix[playerX+i][playerY+j]

    is one element of the matrix[][] which must be a char. You can't assign a single char to theLookMatrix[][] as that line is trying to do.

    You're trying to translate a 5x5 portion of matrix[][] to theLookMatrix[][], such that

    theLookMatrix[0][0] = matrix[x][y]

    where x, y are the coordinates of the upper left corner of the desired 5x5 area. To generalize:

    theLookMatrix[i][j] = matrix[x + i][y + j]

    where i and j range from 0 through 4.

  9. #34
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    Sorry, I don't understand why I can't use Map.matrix[playerX+i][playerY+j]. I thought I would need to use the player's X and Y coordinates to print the 5x5 of what is around the player. I'll show you what I mean with the maps. Here is a full map:
    # # # # # # # # # # # # # # # # # # # 
    # . . . . . . . . . . . G . . . . . # 
    # . . . . . . G . . . . . . G . E . # 
    # . . . . . . G . . . . G . . . . . # 
    # . . E . . . . G . G P . G . . . . # 
    # . . . . . . . . . . G . . . . . . # 
    # . . . . . . G . . . . . . . G G . # 
    # . . . . G . . . . . . . . G . . . # 
    # # # # # # # # # # # # # # # # # # #

    And here is what I want the code about to look, a 5 by 5 grid with the player P at the centre.
    .....
    ...G.
    .GP.G
    ..G..
    .....

    Thanks!

  10. #35
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    I didn't suggest you couldn't use anything. You must have misread. I gave you a general approach that you can use to fit your specifics. Try rereading more slowly for comprehension.

  11. #36
    Member
    Join Date
    Dec 2013
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    EDIT: DON'T WORRY, I'VE FIXED THE PROBLEM!


    Hi I've figured that out thanks! But now I'm having trouble printing a string in a grid. I've managed to create a string of the surrounding area and am trying to print in a 5 x 5 grid but can't get it exactly right. Here's the code:
    		StringBuilder builder = new StringBuilder(); 
    			for (int i = GameLogic.playerX-2; i<GameLogic.playerX+3; i++){
    				for (int j=GameLogic.playerY-2; j<GameLogic.playerY+3; j++){
    						try{
    						builder.append(Map.matrix[i][j]);
    						}catch(ArrayIndexOutOfBoundsException e){
    							builder.append('?');
    					}
    						if(i == GameLogic.playerX+2){
    							builder.append("\n");
    						}
    						if(j == GameLogic.playerY+2){
    							builder.append("\n");
    						}
    				}
    			}
    			System.out.print(builder.toString());
     
    			return answer;
    		}

    But that prints out like:
    .....
    ...G.
    .....
    .....
    #
    #
    #
    #
    #

    Instead of:
    ? # # # ? 
    ? # . . . 
    ? # . . . 
    ? # . . . 
    ? # . . ?


    Thanks again for the help!
    Last edited by JaAnTr; February 21st, 2014 at 06:30 PM. Reason: Partially fixed issue, made it better but not completely right.

  12. #37
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Read ASCII art map and store the information in 2D array (int[][] or char[][])

    This would be a great time to write a method that accepts a char matrix and prints it out in the desired form. You could use it for the full game field, for the 5x5 view, and any other time you needed it. Since you're already doing it for the full game field, you can easily generalize it to a method and use it for the 5x5 view.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  2. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 09:04 AM
  3. How to store char variable in an array
    By ashboi in forum Java Theory & Questions
    Replies: 1
    Last Post: November 9th, 2012, 11:48 AM
  4. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 08:52 AM
  5. Read A File and Store Values into a 2-Dimensional Integer Array?
    By Kimimaru in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 08:13 PM