Search code examples
javasliding-tile-puzzle

Starting the 8 puzzles


How would I go about creating the board given any x and y coordinates for the start nodes. For example, if x=3 and y=2 the board should like:

1 2 3
4 5 x
6 7 8

An example in java or pseudocode would be extremely useful.


Solution

  • hope this helps. Let me know if you have any questions.

    int x_lim = 2;
    int y_lim = 3;
    
    int count=1;
    for(int x=1;x<3+1;x++)
    {
        for(int y=1;y<3+1;y++)
        {
            if(x_lim==x && y_lim==y)  //skip case (blank tile)
            {
                System.out.println("x"+" ");
            }
            else  //other numbers
            {
                System.out.println(count+" ");
                count++;            
            }
    
        }
    }