+ Reply to Thread
Results 1 to 11 of 11

Sliding Number Puzzle

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Cool Sliding Number Puzzle

    I'm not sure whether this thread belongs here or in the Water Cooler forum.

    I recently saw a plastic sliding puzzle. It looked interesting so I recreated it in Excel.

    The puzzle is a set of 20 numbered discs that can slide within a track. The track resembles a rectangle with semicircle ends (like an elongated oval except that the top & bottom sides are straight. I didn't top geometry!). There is a turntable that can reverse the order of 4 discs. The goal is to place the 20 numbers in sequential order.


    Some thoughts/questions:
    1. (SOLVED) I set up the puzzle by doing a random shuffle of numbers on Form Initialize. I'm worried that because this shuffle is truly random, it *may* create an unwinnable situation? Can anyone confirm if the puzzle can be beaten in its current version? Or how should I best go about doing a series of random valid moves to shuffle the numbers 1 to 20?
    2. The labels that represent the discs should be circles. How do I change these labels to circle shapes?
    3. As above but for the Frame / turntable
    4. As you can see, my 'track' is very sloppy. How can I get a perfect stadium/racetrack layout (oblong with semi-circle curve on the discs on the ends)?
    5. (CLOSED) Any suggestions for improving it?
    6. (SOLVED) Does anyone know what this puzzle is called and/or who designed it so I can credit them in the Read Me?
    Attached Files Attached Files
    Last edited by mc84excel; 10-30-2014 at 07:08 AM. Reason: Updated solved questions, clarify point 4
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,423

    Re: Sliding Number Puzzle

    Just to comment on your number 1:

    Years (and years) ago (in the 1980's) I wrote a program to generate sliding block puzzles on a 3 x 3, 4 x 4 or 5 x 5 grid (user-selectable - I used letters of the alphabet, so couldn't allow it to be bigger), and I just generated a number of random moves to begin with. This did create unwinnable situations. I got round it by generating random numbers in the range 1 to 4 where each number represented a direction (up/down, left/right) and effectively moved a tile into the "blank" from those directions.

    As for who designed it (your number 6), I remember playing with a plastic version when I was a kid in the 60's, so I think the game has a very long history.

    Hope this helps.

    Pete

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Sliding Number Puzzle

    Quote Originally Posted by Pete_UK View Post
    Years (and years) ago (in the 1980's) I wrote a program to generate sliding block puzzles on a 3 x 3, 4 x 4 or 5 x 5 grid (user-selectable - I used letters of the alphabet, so couldn't allow it to be bigger), and I just generated a number of random moves to begin with. This did create unwinnable situations. I got round it by generating random numbers in the range 1 to 4 where each number represented a direction (up/down, left/right) and effectively moved a tile into the "blank" from those directions.
    I had a vague idea that truly random shuffles (as opposed to doing a number of random but valid moves) could create unwinnable situations in sliding block puzzles. Thanks for confirming it.

    BTW slightly off-topic but have you made any sliding block puzzles in Excel? I'd be interested in seeing it if that's OK with you.


    Quote Originally Posted by Pete_UK View Post
    As for who designed it (your number 6), I remember playing with a plastic version when I was a kid in the 60's, so I think the game has a very long history.
    That's interesting. Someone has recently pointed out to me one made by Binary Arts and it's called Top Spin (TopSpin? Top-Spin?). I googled Top Spin and the pictures are the same as the one I saw. Is the same as the one you saw?
    Last edited by mc84excel; 10-30-2014 at 07:06 AM.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sliding Number Puzzle

    About the positioning, I've added a PlaceLabels routine to make them be circular. (I note that my routine swaps left and right.) It is based on the formula for a circle x=R*Cos(theta) y=R*Sin(theta)

    I find the "how many spaces left or right" dialog box to be a distraction. A Move One Left button and Move One Right button would be smoother user interface (IMO).

    About solvability. You could
    1) set to correct order
    2) randomly move L, move R, or Spin 1000 times
    3) display the result

    That way the puzzle is solvable.

    I also wonder if a Label might be better than a Frame for the Turntable. A label can be placed behind the number boxes and is a little easier to control than a Frame. Since you aren't moving Boxes, but moving numbers within boxes, you know which boxes are on the turntable.

    Attached is my PlaceLabel code for circularization. It is far from optimized. Run the code and press the Circlular Lable button.
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Sliding Number Puzzle

    Quote Originally Posted by mikerickson View Post
    About the positioning, I've added a PlaceLabels routine to make them be circular. (I note that my routine swaps left and right.) It is based on the formula for a circle x=R*Cos(theta) y=R*Sin(theta)
    I'm impressed! +1

    I don't want to sound ungrateful but I only want the labels on the left and right edge to be placed in a semicircle layout (see Question 4). The track should resemble a rectangle but with the two short sides as semicircles (I'm sure there is a name for this kind of shape but if there is, I can't remember it now!)


    Quote Originally Posted by mikerickson View Post
    I find the "how many spaces left or right" dialog box to be a distraction. A Move One Left button and Move One Right button would be smoother user interface (IMO).
    Good point. Thank you.

    I have added these buttons to the next version. (And kept the old buttons so users can have a choice of slide input)


    Quote Originally Posted by mikerickson View Post
    About solvability. You could
    1) set to correct order
    2) randomly move L, move R, or Spin 1000 times
    3) display the result

    That way the puzzle is solvable.
    I have added this to the next version.


    Quote Originally Posted by mikerickson View Post
    I also wonder if a Label might be better than a Frame for the Turntable. A label can be placed behind the number boxes and is a little easier to control than a Frame. Since you aren't moving Boxes, but moving numbers within boxes, you know which boxes are on the turntable.
    Good point. I will make this change soon. (Then we can press the Turntable label to run the SPIN move)
    Last edited by mc84excel; 10-29-2014 at 09:55 PM. Reason: Update

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Sliding Number Puzzle

    Quote Originally Posted by mc84excel View Post
    I only want the labels on the left and right edge to be placed in a semicircle layout (see Question 4). The track should resemble a rectangle but with the two short sides as semicircles (I'm sure there is a name for this kind of shape but if there is, I can't remember it now!)
    A few google searches later.

    I have come across the following names for this shape:
    • Truncated Circle
    • Stadium
    • Obround
    • Race Track


    One of the answers at this page here ( http://math.stackexchange.com/questi...horse-racetrac ) has a mathematical formula which I presume will recreate the shape.

    Now if I had VBA code to run this formula to set the positions of the controls on the form

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sliding Number Puzzle

    While I haven't dug into your code, your earlier comment about looping through the labels got me thinking that its not necessary.

    It can be arranged that the labels are fixed with lbl0, lbl1, lbl2 and lbl3 in the turntable.
    An array NumberOrder(0 to 19) initially holds the values 1,2,3,...,20
    A value Pointer indicates which member of the array is in lbl0, so that
    lbl0.Caption = NumberOrder(Pointer)
    lbl1.Caption = NumberOrder((Pointer + 1) Mod 20)
    lbl2.Caption = NumberOrder((Pointer + 2) Mod 20)
    Me.Controls("lbl" & i).Caption = NumberOrder((Pointer + i) Mod 20)
    One rotates the track by changing Pointer.
    On turns the turntable by swapping NumberOrder(Pointer) with NumberOrder(Pointer + 3) and NumberOrder(Pointer+1) with NumberOrder(Pointer+2).
    Randomizing is done by rotating a random number of squares and then turning the turntable, 1000 times.
    Sub RandomNumberOrder()
        Dim i As Long
        For i = 1 To 1000
            Pointer = WorksheetFunction.RandBetween(0, 19)
            TurnTheTable
        Next i
    End Sub
    And then I got into it and produced the attached workbook.
    The size of the rotation is entered into the text box, if a number <1 or >19 is entered, it changes to the DefaultRotationValue that is set by the calling code.
    With UserForm1
        .DefaultRotationValue = 4
        .Show
    End With
    In the attached, the labels are created and positioned by the MakeTrackLabels routine.
    PostionControls is called in the Activate event because the .InsideHeight property is unreliable in the Intialize event.
    This will alert the user when they have completed the puzzle. Note that 20,19,18,...,3,2,1 counts as victory as well as 1,2,3...18,19,10 and that the 1 doesn't need to be in any particular position. Also note that these conditions mean that victory will never be achieved after a Rotation, only after a TurnTheTable.
    Attached Files Attached Files
    Last edited by mikerickson; 10-29-2014 at 11:00 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Sliding Number Puzzle

    Quote Originally Posted by mikerickson View Post
    In the attached, the labels are created and positioned by the MakeTrackLabels routine.
    Incredible work. I wish I had seen this before the time I spent yesterday and today upgrading to v0.3! (v0.3 includes the suggestions from your previous post btw - I will upload v0.3 in my next post)

    Quote Originally Posted by mikerickson View Post
    This will alert the user when they have completed the puzzle. Note that 20,19,18,...,3,2,1 counts as victory as well as 1,2,3...18,19,10 and that the 1 doesn't need to be in any particular position.
    Nice! I had briefly considered a win message when I first started but I dismissed it as being too hard because of how to detect a win clockwise/counterclockwise and also if 1 wasn't in position 0.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Talking Re: Sliding Number Puzzle

    v0.3 uploaded.

    I'm not certain about previous versions but this one IS winnable (even though I still haven't solved it).

    Contains a number of changes suggested by mikerickson so if you like the improvements consider repping him.


    LEFT TO DO:
    • Add Win detection to v0.4 (This feature already created by mikerikson - see his Track.xlsm on an earlier post)
    • Points 2 & 3 of OP - replace 'disk' & 'turntable' labels with circular shapes
    • Point 4 of OP - alter positions of 'disks' so that the 'track' forms a perfect 'stadium' shape


    Re points 2 & 3 - I've been looking into code by Andy Pope MVP. Mr Pope designed a method whereby circles can be pasted onto the userform. (See http://www.andypope.info/vba/userformdraw.htm )
    My only problem is that I cant find a way to save the shapes after they're pasted - not even with VBIDE.
    Attached Files Attached Files
    Last edited by mc84excel; 10-30-2014 at 07:10 AM.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Sliding Number Puzzle

    About the size and shape of stuff.
    Nothing about this, except for the "rotate how many squares" textbox in my effort, is particularly "userform".

    You could do this on a worksheet, with shapes as the turntable and as the 20 labels and make them any size you want.

  11. #11
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Sliding Number Puzzle

    I have recently lost interest in this project (the puzzle isn't much fun after working out how to solve it)(plus I have other projects on the go that are demanding focus)

    So I am marking this thread as solved.


    (Should anyone wish to take over and/or create their own branch/updated version - I have no objections)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Filling up a Number Puzzle
    By tariqhasan75 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-21-2012, 05:52 AM
  2. Filling up a Number Puzzle
    By tariqhasan75 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2012, 05:06 AM
  3. number puzzle
    By mkron in forum Excel General
    Replies: 14
    Last Post: 01-28-2007, 12:13 AM
  4. Number Puzzle
    By PetaPan via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2006, 12:45 PM
  5. [SOLVED] Prime number puzzle
    By johnT in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-27-2005, 09:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1