title: 2D Game Tile Placement Algorithm
body: <p><a href="https://i.stack.imgur.com/6pRGA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6pRGA.png" alt="explanation" /></a></p>
  
  <p>Hey Guys,</p>
  <p>Ok so I have been trying to figure this out for the past few days and I seriously have nothing left in me, Hence why I am here.</p>
  <p>I am in the early stages of making a 2D game. I created an &quot;Inventory&quot; for developer mode which displays a grid of tiles (As seen at the bottom of the image).</p>
  <p>You can place these tiles in the map grid, where they will then be saved as a char to a text file for future map loading.</p>
  <p>Everything works as it should. However, I would like it so that when i place a path tile (Red Border) on the map when surrounded by other path tiles (Blue Border) it changes to the correct sided tile.</p>
  <p>Here is my Tilemap.java.</p>
  <pre><code> public class TileMap {
  
  private Tile[][] tiles;
  
  public TileMap(int width, int height) {
      tiles = new Tile[width][height];
  }
  
  public int getWidth() {
      return tiles.length;
  }
  
  public int getHeight() {
      return tiles[0].length;
  }
  
  public Tile getTile(int x, int y) {
      if (x &lt; 0 || x &gt;= getWidth() || y &lt; 0 || y &gt;= getHeight()) {
          return new VoidTile();
      } else {
          return tiles[x][y];
      }
  }
  
  public void setTile(int x, int y, Tile tile) {
      if (x &gt;= 0 &amp;&amp; y &gt;= 0) {
          tiles[x][y] = tile;
      }
  }
  
  }
  </code></pre>
  <p>ok so in my Main game class I have a TileMap map;</p>
  <p>I am trying to update the tiles as I place them down in the mouseClicked(MouseEvent e) method by checking the up, down, left and right tiles of the soon to be placed tile like so.</p>
  <pre><code>    public void mouseClicked(MouseEvent e) {
      Inventory.click(e);
  
      if (e.getButton() == 1) {
          tempTile = Inventory.getSelectedTile();
          System.out.println(&quot;Selected Tile is &quot; + tempTile);
      } else if (e.getButton() == 3) {
          
          boolean u = map.getTile(mouseGridX, mouseGridY - 1).connectsToPath;
          boolean d = map.getTile(mouseGridX, mouseGridY + 1).connectsToPath;
          boolean l = map.getTile(mouseGridX - 1, mouseGridY).connectsToPath;
          boolean r = map.getTile(mouseGridX + 1, mouseGridY).connectsToPath;
          boolean ul = map.getTile(mouseGridX - 1, mouseGridY - 1).connectsToPath;
          boolean ur = map.getTile(mouseGridX + 1, mouseGridY - 1).connectsToPath;   
          boolean dl = map.getTile(mouseGridX - 1, mouseGridY + 1).connectsToPath; 
          boolean dr = map.getTile(mouseGridX + 1, mouseGridY + 1).connectsToPath;   
               
                      if (!u &amp;&amp; !l &amp;&amp; !d &amp;&amp; !r) {
                         map.setTile(mouseGridX, mouseGridY new PathTile(5));
                      }
              }
    }
  </code></pre>
  <p>here is my PathTile.java constructor which uses a grid based sprite sheet. getSprite(GridX,GridY)</p>
  <pre><code>    public PathTile(int tile) {
      this.tile = tile;
      connectsToPath = true;
      
      switch (tile) {
      case 1:
          this.image = Sprite.getSprite(1, 1);
          break;
      case 2:
          this.image = Sprite.getSprite(2, 1);
          break;
      case 3:
          this.image = Sprite.getSprite(3, 1);
          break;
      case 4:
          this.image = Sprite.getSprite(1, 2);
          break;
      case 5:
          this.image = Sprite.getSprite(2, 2);
          break;
      case 6:
          this.image = Sprite.getSprite(3, 2);
          break;
      case 7:
          this.image = Sprite.getSprite(1, 3);
          break;
      case 8:
          this.image = Sprite.getSprite(2, 3);
          break;
      case 9:
          this.image = Sprite.getSprite(3, 3);
          break;
      case 10:
          this.image = Sprite.getSprite(4, 2);
          break;
      case 11:
          this.image = Sprite.getSprite(5, 2);
          break;
      case 12:
          this.image = Sprite.getSprite(4, 3);
          break;
      case 13:
          this.image = Sprite.getSprite(5, 3);
          break;
      }
  }
  </code></pre>
  <p>This is based off of notch's Ludum Dare Game &quot;MiniCraft&quot;. However, I cant seem to figure out an efficient way to implement it with my methods.</p>
  <p>If my math is correct (which it probably isn't) but wouldn't that mean there is 65,535 possible outcomes? (4 * 4 * 4 * 4 * 4 * 4 * 4 * 4)??</p>
  <p>I love programming and all but not that much lol...</p>
  <p>Can anyone help me figure out an algorithm or at least point me in the right direction please?</p>
  <p>I know it's a really big question but all the help would be greatly appreciated!</p>
  <p>Sav.</p>
  <p>EDIT:
  This is the way notch solves this issue.
  He uses a grid sprite sheet similar to mine. However, to get the intended sprite he has an x &amp; y co-ordinate which he multiplies by 32 (size of grid box).</p>
  <pre><code>    boolean u = !level.getTile(x, y - 1).connectsToGrass;
      boolean d = !level.getTile(x, y + 1).connectsToGrass;
      boolean l = !level.getTile(x - 1, y).connectsToGrass;
      boolean r = !level.getTile(x + 1, y).connectsToGrass;
  
      if (!u &amp;&amp; !l) {
          screen.render(x * 16 + 0, y * 16 + 0, 0, col, 0);
      } else
          screen.render(x * 16 + 0, y * 16 + 0, (l ? 11 : 12) + (u ? 0 : 1) * 32, transitionColor, 0);
  
      if (!u &amp;&amp; !r) {
          screen.render(x * 16 + 8, y * 16 + 0, 1, col, 0);
      } else
          screen.render(x * 16 + 8, y * 16 + 0, (r ? 13 : 12) + (u ? 0 : 1) * 32, transitionColor, 0);
  
      if (!d &amp;&amp; !l) {
          screen.render(x * 16 + 0, y * 16 + 8, 2, col, 0);
      } else
          screen.render(x * 16 + 0, y * 16 + 8, (l ? 11 : 12) + (d ? 2 : 1) * 32, transitionColor, 0);
      if (!d &amp;&amp; !r) {
          screen.render(x * 16 + 8, y * 16 + 8, 3, col, 0);
      } else
          screen.render(x * 16 + 8, y * 16 + 8, (r ? 13 : 12) + (d ? 2 : 1) * 32, transitionColor, 0);
  }
  </code></pre>
  <p>As you can see he uses the (boolean ? true : false) method to receive the desired number to add for the sprite.</p>
  <p>The issue i am having is figuring out a way i can use a similar algorithm to figure out which sprite I need to use.</p>
  <p>This is what i am asking for. I would even be happy with seeing how anybody else has overcome this problem.</p>
  <p>Thanks</p>