methods - Print out 2D array radios in Java -


I am making Java dongone game and so far it imports ASCII map in 2D array. A player is added and can move around the dungeon using CLI.

The problem is that you can currently see the entire basement. For example ..

  #################### ............... ## ...... g ........ # # ........... g ..... # # .. e ....... ....... # # ............ ...... ...... # # ............. P ... # .. .............. ###################  

I like it so You can use the look command and you can see 2 tile radios around the player. For example ..

  x ... x .... .p ... ..x ### x  

So far I've come with this.

  Public Zero Watch ({{system.out.print ("X") {try}; System.out.print (MyWorld [posX-2] [saying-1]); System .out.print (MyWorld [posX-2] [Proverb]; System.out.print (MyWorld [posX-2] [Proverb + 1]) System.out.print ("X"); System.out Print ("\ n"); System.out.print (MyWorld [posX-1] [saying-2]) System.out.print (MyWorld [posX-1] [saying-1]); System.out Print (MyWorld [posX-1] [adage]; System.out.print (MyWorld [posX-1] [saying + 1]; System.out.print (MyWorld [posX-1] [saying + 2]) ; System.out.print ("\ n"); System.out.print (MyWorld [posX] [saying-2]; System.out.print (MyWorld [posX] [saying-1]); System.out .print (MyWorld [posX] [Proverb]; System.out.print (MyWorld [posX] [saying + 1]; System.out.print (MyWorld [posX] [adage + 2]); System.out Print ("\ n"); System.out.print (MyWorld [posX + 1] [Say System-out.print (MyWorld [posX + 1] [saying-1]) System.out.print (MyWorld [posX + 1] [saying]; System.out.print (MyWorld [ PosX + 1] [saying + 1]; System.out.print (MyWorld [posX + 1] [saying + 2]); System.out.print ("\ n"); System.out.print ("X"); System.out.print (MyWorld [posX + 2] [saying-1]); System.out.print (MyWorld [posX + 2] [saying]); System.out.print (MyWorld [posX + 2] [saying + 1]); Println ("X"); } Hold (index octown exception e) {System.out.println ("against war ...."); }}  

I'm sure there is a better way to do this but I can not understand it. This method works only till the player is not against the wall, is there a better way of doing this and how can I make a buffer around my map? 'For tiles which are not in the map.

As others have suggested that you should use 2 for loop. I've added the required argument, the result of which you are looking

  look public invisible (StringBuffer = sb = new stringbuffer (25); For {int y = posY-2; y & lt; = posys + 2; y ++} {for (int x = posx-2; x & lt; = posX + 2; x ++) {try ( If x & lt; 0 || y & lt; 0 || x> myWorld.length || y & gt; myworld [x] length) sb.append ("#"); // wall or outer map other if (Math .abs (posX-x) + Math .abs (posY-y) 4) sb.append (myWorld [x] [y]); // Other valid part of the map sb.append ("X"); // unreachable corner} hold (index autobuff exception e) {} // it should not be :) sb.append ("\ n"); } System.out.println (sb); }  

will be placed below the execution time with large amounts of data using stringbuff


Comments