title: 2D array and detection if we fall off a map (JAVA)
body: <p>I am printing simple map of numbers 1,2,3,4,5,6,7,8 (10/10) and everytime i type LEFT or RIGHT it <strong>doesnt tell</strong> me if im falling off the map but he is asking me if next YX is bigger than 9(collision from bottom and right) or its less than 0(collision from top and right) but instead he asks me what is in next cell( is it 1,2,3,4,5,6,7,8)
  How the F*** do i check if he does not fall of the map?
  Help please!!!!!!!</p>
  <pre><code>import java.util.Scanner;
  
  public class Game {
  static String dir = &quot;&quot;;
  static int player;
  static int chest;
  static int monster;
  static int key;
  static int tile;
  static int wall;
  static int keysFound = 0;
  static int chestsOpened = 0;
  static int chestsLeft = 3;
  static int sword = 0;
  static int angel;
  static int pickaxe = 0;
  
  static Scanner scan = new Scanner(System.in);
  
  static int[][] map = new int[10][10];
  
  static int x = map[0][0];
  static int y = map[0][0];
  static int playerPos = map[x][y];
  static int life = 3;
  
  static boolean isInGame;
  static String NoTileFound = &quot;trying to knock this wall is pointless try some other move&quot;;
  static String FreeTile = &quot;you move to a free tile&quot;;
  
  public static void main(String[] args) {
      MapCreator mapcreate = new MapCreator();
      map = mapcreate.getMapcreated();
  
      int isPlaying;
      System.out.println(&quot; Welcome to the game, kill monsters and open three chests to win&quot;);
      System.out.println(&quot; Gather a key to open the chest, collect 3 of theese to win! that's it!&quot;);
      System.out.println(&quot; Press 1 to play..anything else to exit!&quot;);
      isPlaying = scan.nextInt();
      if (isPlaying == 1) {
          isInGame = true;
          enterGame();
  
      } else {
          System.out.println(&quot;You missed out on a good game...&quot;);
          System.exit(1);
      }
  }
  
  public static void enterGame() {
      System.out.println(
              &quot;Type, up, down, left, or right, to move. equipment to see what you have and exit to exit game&quot;);
      while (isInGame) {
          move();
  
      }
  
  }
  
  public static void move() {
      dir = scan.nextLine();
      switch (dir) {
          case &quot;up&quot;:
              entertileup();
              break;
          case &quot;down&quot;:
              entertiledown();
              break;
          case &quot;left&quot;:
              entertileright();
              break;
          case &quot;right&quot;:
              entertiledown();
              break;
  
          case &quot;equipement&quot;:
              System.out.println(&quot;you have &quot; + life + &quot; lifes, &quot; + key + &quot; keys and &quot; + chestsOpened
                      + &quot; chests opened, keep searching solider!&quot;);
              move();
              break;
  
          case &quot;exit&quot;:
              System.out.println(&quot;You missed out on a good game...&quot;);
              System.exit(1);
  
      }
      ShowMap mapprint = new ShowMap();
      mapprint.print(map);
  }
  
  public static void map() {
      wall = 1;
      tile = 2;
      monster = 3;
      chest = 4;
      key = 5;
      sword = 6;
      player = 0;
      angel = 7;
      pickaxe = 8;
  
      if (playerPos == tile) {
          System.out.println(FreeTile);
      }
      if (playerPos == monster) {
          monsterEncounter();
      }
      if (playerPos == chest) {
          openChest();
      }
      if (playerPos == key) {
          findKey();
      }
      if (playerPos == sword) {
          findsword();
  
          if (playerPos == angel) {
              findangel();
          }
          if (playerPos == pickaxe) {
              findpickaxe();
          }
      }
  }
  
  public static void entertileup() {
      if (x + 1 &gt; 9) {
          System.out.println(NoTileFound);
          move();
      } else if (map[x + 1][y] == 1) {
  
          if (pickaxe == 0) {
              System.out.println(NoTileFound);
              move();
          }
  
          else if (pickaxe == 1) {
              map[x][y] = 2;
              x = x + 1;
              playerPos = map[x][y];
              map();
          }
      } else if (map[x + 1][y] == 2)
          x = x + 1;
      playerPos = map[x][y];
      map();
  
  }
  
  public static void entertiledown() {
      if (x - 1 &lt; 0) {
          System.out.println(NoTileFound);
          move();
      } else if (map[x - +1][y] == 1) {
  
          if (pickaxe == 0) {
              System.out.println(NoTileFound);
              move();
          }
  
          else if (pickaxe == 1) {
              map[x][y] = 2;
              x = x - 1;
              playerPos = map[x][y];
              map();
          }
      } else if (map[x - 1][y] == 2)
          x = x - 1;
      playerPos = map[x][y];
      map();
  
  }
  
  public static void entertileright() {
      if (y - 1 &lt; 0) {
          System.out.println(NoTileFound);
          move();
      } else if (map[x][y - 1] == 1) {
  
          if (pickaxe == 0) {
              System.out.println(NoTileFound);
              move();
          }
  
          else if (pickaxe == 1) {
              map[x][y] = 2;
              y = y - 1;
              playerPos = map[x][y];
              map();
          }
      } else if (map[x][y - 1] == 2)
          y = y - 1;
      playerPos = map[x][y];
      map();
  
  }
  
  public static void entertileleft() {
      if (y + 1 &gt; 9) {
          System.out.println(NoTileFound);
          move();
      } else if (map[x][y + 1] == 1) {
  
          if (pickaxe == 0) {
              System.out.println(NoTileFound);
              move();
          }
  
          else if (pickaxe == 1) {
              map[x][y] = 2;
              y = y + 1;
              playerPos = map[x][y];
              map();
          }
      } else if (map[x][y + 1] == 2)
          y = y + 1;
      playerPos = map[x][y];
      map();
  
  }
  
  public static void findsword() {
      System.out.println(&quot;You found a sword, monsters can't kill you! &quot;);
      sword = sword + 1;
      map[x][y] = 0;
  }
  
  public static void findangel() {
      System.out.println(&quot;You have found angel! what a mighty creature! He gives you a life&quot;);
      life = life + 1;
      map[x][y] = 0;
  }
  
  public static void findpickaxe() {
      System.out.println(&quot;You have found a pickaxe Niceeee! you can destroy walls now! just simply walk over it&quot;);
      pickaxe = pickaxe + 1;
      map[x][y] = 0;
  }
  
  public static void monsterEncounter() {
      if (sword == 0) {
          if (life &gt; 0) {
  
              System.out.println(&quot;WOAH! A MONSTER! You kill him but you lose a life!&quot;);
              life = life - 1;
              System.out.println(&quot;You have: &quot; + life + &quot; life(s) left! Don't die out there!&quot;);
              map[x][y] = 0;
          } else {
              System.out.println(&quot;You have: &quot; + life + &quot; , You encountered too many monsters. GAME OVER.&quot;);
              System.exit(1);
          }
      } else {
          System.out.println(&quot; You killed a monster with mighty sword, no life loss, nice! &quot;);
          map[x][y] = 0;
      }
  
  }
  
  public static void findKey() {
      System.out.println(&quot;You found a key, nice!&quot;);
      keysFound += 1;
      map[x][y] = 2;
  }
  
  public static void openChest() {
      System.out.println(&quot;Chest, nice!!&quot;);
      if (keysFound &gt;= 1) {
          chestsOpened += 1;
          int totalChestsFound = chestsLeft - chestsOpened;
          chestsLeft -= 1;
          System.out.println(&quot;You have opened a chest! You have &quot; + totalChestsFound + &quot; chests to go!&quot;);
          map[x][y] = 2;
          keysFound -= 1;
      }
      if (chestsLeft == 0) {
          System.out.println(&quot;YOU WON! GOOD GAME SIR, GOOD GAME.&quot;);
          System.exit(1);
      } else {
          System.out.println(&quot;No keys, no chests. Keep searching soldier.&quot;);
      }
  
  }
  </code></pre>
  <p>}</p>