Search code examples
mathequationpuzzle

Creating a number-puzzle that can scale to be greater/less (i.e. Can be adjusted for bigger puzzles)


I'm trying to create a scale-able picture puzzle (like this) but can't figure out an equation to use to cover more than 3x3 and 4x4. I'm generating the puzzle dynamically based on the number of tiles I want to add (for instance 8 for a 3x3 and 15 for a 4x4).

So far to generate the rows I simply divide the tile number by the row/col number.

The hard part is doing the columns.

Here is the column equation I have used:

A = Tile Number Index (Start at 0 and end at 8 for 3x3)
B = Row/Col (3 for 3x3)

//A and B are both ints to start. The final divide B/() I convert to float
and then use a rounding to get the final number

(B/((A/B+1)*B-A))-1
  • Here is what rows need to be like for 3x3: 000 111 222
  • Here is what columns need to look like for 3x3: 012 012 012
  • If using a 4x4 columns would look like this: 0123 0123 0123 0123
  • etc.

This equation only works for a 3x3 puzzle. I have another equation for 4x4 but it doesn't scale at all. How can I fix this up and make all larger puzzles scale.


Solution

  • I assume you are working on the 8 or 15 puzzle (as in your link), you want to find the row and column where a tile will end up when the puzzle is solved, and the tile numbers, rows, and columns all start at zero. (This is what I gather from your last comment and your last edit.) If so, you could use, in Python,

    def tile_end_coords(tilenum):
        '''Return the row and column coordinates for the end (solved) tile given
        its number. Global variable *n* is the number of squares on each side
        of the square puzzle.'''
        row = tilenum // n
        col = tilenum % n
        return row, col
    

    The expression for row is the quotient after an integer division. Some languages, such as Object Pascal, use div rather than //. In some languages you would need to use int(tilenum / n).

    The expression for col is the remainder after an integer division. Some languages, such as Object Pascal, use mod rather than %. In some languages you would need to use tilenum - row * n.

    I showed you some easy-to-understand, more portable code. In Python you could replace the entire function with just one line:

    row, col = divmod(tilenum, n)
    

    If the tile numbers, rows, or columns are one-based rather than zero-based, just add or subtract 1 in the appropriate places--it should be clear where.