title: Using HashMaps in creating a game of 15 puzzle?
body: <p>I am creating a simple game of 15 puzzle (Please see here: <a href="https://en.wikipedia.org/wiki/15_puzzle" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/15_puzzle</a>). I have an array of 4 by 4 buttons and want to know the best way to handle a user clicking on a button and moving around the numbers.</p>
  
  <p>I have started off by creating a JavaFX gridpane to display the buttons as well as a HashMap (suggested by multiple people) to store a mapping between the displayed number (1-15) and the button at that location.</p>
  
  <p>I populate the map with class objects "Btn"</p>
  
  <pre><code>public class Btn {
      private int ID;
      private int x;
      private int y;
      public Button button;
  </code></pre>
  
  <pre><code>for (int i = 1; i &lt;= 4; i++) {
              for (int j = 1; j &lt;= 4; j++) {
  
                  Btn btn = new Btn(count, j, i, new Button("" + count));
                  map.put(count, btn);
                  btn.button.setMinSize(100, 100);
                  btn.button.setOnAction(e -&gt; {
                      if (isMoveLegal(btn) == true) {
                          move(btn);
                      }
                  });
                  count++;
  
                  grid.add(btn.button, j, i);
              }
          }
  </code></pre>
  
  <p>So far it has worked quite well. In the isMoveLegal() method I check if the empty field is above, left, right, or below the clicked button. Which gets quite complicated because of the HashMaps nature of not preserving any order. But my real problem lies in the move() method. Since there is no order, I can't just switch 2 map nodes or can I?</p>
  
  <pre><code>if (MoveDir.equals("Right")) {
     map.get((btn.getYs()+1)*4 - (4-btn.getXs())).button.setText("" + map.get(btn.getID()).getID());
     map.get(btn.getID()).button.setText("" + map.get((btn.getYs()+1)*4 - (4-btn.getXs())).getID());
  
     int tempID = map.get(btn.getID()).getID();
     map.get(btn.getID()).setID(16);
     map.get((btn.getYs()+1)*4 - (4-btn.getXs())).setID(tempID);
  //   System.out.println(map.get(btn.getID()).getID());
  //   System.out.println(map.get((btn.getYs()+1)* 4 - (4-btn.getXs())).getID());
  
     map.get(btn.getID()).setYs(map.get(btn.getID()).getYs()+1);
     map.get((btn.getYs()+1)* 4 - (4-btn.getXs())).setYs(map.get((btn.getYs()+1)* 4 - (4-btn.getXs())).getYs()-1);
  </code></pre>
  
  <p>This is what I have come up with so far, which extremely complicated and tedious. To quickly sum it up, I first switch the labels of the buttons, then I change the ID variables of the 2 buttons, and then I change the cord variables (x &amp; y) to indicate their new location in map/grid. 
  I'm pretty sure I have gone way too far with this and there is a simpler solution to all of this but I am just unsure what that would be. Should I ditch the HashMap entirely and just use an object array of Btns? </p>
  
  <p>Thanks for the help.</p>