title: Java game development. Making the monster move random in a maze
body: <p>I was writing a pacman styled game in which the monsters are assumed to move in a random direction in the maze. But I can't figure it out how to make them adapt the right direction they want. I was using some constants for the directions.</p>
  
  <pre><code>public class Monster extends GObject {
  
      private final int DIRECTION_UP = 0;
      private final int DIRECTION_DOWN = 1;
      private final int DIRECTION_LEFT = 2;
      private final int DIRECTION_RIGHT = 3;
  
      private int DIRECTION = 2;
  
      private float speed = 0.06f;
  
      public Monster(float x, float y){
          super(Treasure.MONSTER_LEFT);
          setX(x);
          setY(y);
          DIRECTION = random(3);
      }
  
      public void update(long elapsedTime){
          setVelocityX(0);
          setVelocityY(0);
          switch (DIRECTION){
              case DIRECTION_UP :
                  setVelocityY(-speed);
                  setImage(Treasure.MONSTER_UP);
                  break;
              case DIRECTION_DOWN :
                  setVelocityY(speed);
                  setImage(Treasure.MONSTER_DOWN);
                  break;
              case DIRECTION_LEFT :
                  setVelocityX(-speed);
                  setImage(Treasure.MONSTER_LEFT);
                  break;
              case DIRECTION_RIGHT :
                  setVelocityX(speed);
                  setImage(Treasure.MONSTER_RIGHT);
                  break;
          }
          changeDirection();
      }
  
      private int random(int x){
          return (int)Math.floor(Math.random() * (x+1));
      }
  
      public void collision(GObject other){
          if (other instanceof Wall){
              switch (DIRECTION){
                  case DIRECTION_UP :
                      setY(other.getY()+other.getHeight());
                      break;
                  case DIRECTION_DOWN :
                      setY(other.getY()-getHeight());
                      break;
                  case DIRECTION_LEFT :
                      setX(other.getX()+other.getWidth());
                      break;
                  case DIRECTION_RIGHT :
                      setX(other.getX()-getWidth());
                      break;
              }
          }
      }
  
  }
  </code></pre>
  
  <p>Here's the changeDirection() method I've using.</p>
  
  <pre><code>private void changeDirection(){
      int random = random(1);
      boolean opposite = (random==1);
      int dir = DIRECTION;
      switch (Explorer.DIRECTION){
          case DIRECTION_UP :
              if (opposite){
                  dir = DIRECTION_DOWN;
              } else {
                  dir = DIRECTION_UP;
              }
              break;
          case DIRECTION_DOWN :
              if (opposite){
                  dir = DIRECTION_UP;
              } else {
                  dir = DIRECTION_DOWN;
              }
              break;
          case DIRECTION_LEFT :
              if (opposite){
                  dir = DIRECTION_RIGHT;
              } else {
                  dir = DIRECTION_LEFT;
              }
              break;
          case DIRECTION_RIGHT :
              if (opposite){
                  dir = DIRECTION_LEFT;
              } else {
                  dir = DIRECTION_RIGHT;
              }
              break;
      }
      switch (dir){
          case DIRECTION_UP :
              if (Map.isObjectCollisionFree(getX(), getY()-1, true, this)){
                  DIRECTION = dir;
              }
              break;
          case DIRECTION_DOWN :
              if (Map.isObjectCollisionFree(getX(), getY()+1, true, this)){
                  DIRECTION = dir;
              }
              break;
          case DIRECTION_LEFT :
              if (Map.isObjectCollisionFree(getX()-1, getY(), true, this)){
                  DIRECTION = dir;
              }
              break;
          case DIRECTION_RIGHT :
              if (Map.isObjectCollisionFree(getX()+1, getY(), true, this)){
                  DIRECTION = dir;
              }
      }
  }
  </code></pre>
  
  <p>Could anybody help me with how to make the monster move random in the maze...</p>
  
  <p>The current issue I'm facing is that the monsters are not moving.. they are jumping in the game. If they are outside the view, they never comes back.. Monitoring them said me that their co-ordinates reach as much as -12000 and stops there. This is how I check for walls.</p>
  
  <pre><code>if (Map.isObjectCollisionFree(&lt;x-pos&gt;, &lt;y-pos&gt;, &lt;solid-state&gt;, &lt;object&gt;))
  </code></pre>
  
  <p>and this has worked for the player. In the collision method, I'm aligning the monster with the grid of the map.</p>
  
  <p>EDIT : changed the changeDirection() method</p>
  
  <pre><code>    private void changeDirection(){     
          DIRECTION = random(3);
  }
  
  the update() method
  
      public void update(long elapsedTime){
      if (MapView.isVisible(this)){
          if ((getX()%Map.TILE_SIZE==0) &amp;&amp; (getY()%Map.TILE_SIZE==0)){
              changeDirection();
          }
          setVelocityX(0);
          setVelocityY(0);
          switch (DIRECTION){
              case DIRECTION_UP :
                  setVelocityY(-speed);
                  setImage(Treasure.MONSTER_UP);
                  break;
              case DIRECTION_DOWN :
                  setVelocityY(speed);
                  setImage(Treasure.MONSTER_DOWN);
                  break;
              case DIRECTION_LEFT :
                  setVelocityX(-speed);
                  setImage(Treasure.MONSTER_LEFT);
                  break;
              case DIRECTION_RIGHT :
                  setVelocityX(speed);
                  setImage(Treasure.MONSTER_RIGHT);
                  break;
          }
      }
  }
  </code></pre>