Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Puzzle Random

Puzzle Random

Scheduled Pinned Locked Moved C#
comgame-devhelptutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Midnight Ahri
    wrote on last edited by
    #1

    Hi guys, I created a 3x3 puzzle game and I randomize the button at form load. It's a success, but sometimes I can't solve the puzzle no matter how hard I try. This is where the puzzle stuck at, http://i39.tinypic.com/9k17dh.jpg[^] Maybe there is a way to random puzzle? Or maybe I don't know how to play puzzle?

    L OriginalGriffO 2 Replies Last reply
    0
    • M Midnight Ahri

      Hi guys, I created a 3x3 puzzle game and I randomize the button at form load. It's a success, but sometimes I can't solve the puzzle no matter how hard I try. This is where the puzzle stuck at, http://i39.tinypic.com/9k17dh.jpg[^] Maybe there is a way to random puzzle? Or maybe I don't know how to play puzzle?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Usually it's called "8-puzzle" (special case of N-puzzle), and exactly half of them are unsolvable. You can check whether it's solvable[^] and then generate a new one if it isn't.

      1 Reply Last reply
      0
      • M Midnight Ahri

        Hi guys, I created a 3x3 puzzle game and I randomize the button at form load. It's a success, but sometimes I can't solve the puzzle no matter how hard I try. This is where the puzzle stuck at, http://i39.tinypic.com/9k17dh.jpg[^] Maybe there is a way to random puzzle? Or maybe I don't know how to play puzzle?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        There are a couple of ways to randomise it: 1) Insert each piece of the puzzle at a random location on the board, by using the random number generator to specify the location. This is quick to generate, but as Harold says, it can generate puzzles that can't be solved - you need a post-processing step to ensure that it can. 2) Use the random number generator to generate a direction, and move the pieces that way, exactly as if the user had done so. You then repeat this a number of times to "scramble" the puzzle. This is slower to generate, but the result is always solvable, and it means that a "difficulty level" can be added very simply by changing the number of times the random move is implemented.

        The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          There are a couple of ways to randomise it: 1) Insert each piece of the puzzle at a random location on the board, by using the random number generator to specify the location. This is quick to generate, but as Harold says, it can generate puzzles that can't be solved - you need a post-processing step to ensure that it can. 2) Use the random number generator to generate a direction, and move the pieces that way, exactly as if the user had done so. You then repeat this a number of times to "scramble" the puzzle. This is slower to generate, but the result is always solvable, and it means that a "difficulty level" can be added very simply by changing the number of times the random move is implemented.

          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I thought about that second method, but concluded that I couldn't guarantee anything about how random the result would be, so I decided against recommending it. It could be faster though, the way I mentioned has a probability of 0.5 at every step to terminate, you could have bad luck.

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            I thought about that second method, but concluded that I couldn't guarantee anything about how random the result would be, so I decided against recommending it. It could be faster though, the way I mentioned has a probability of 0.5 at every step to terminate, you could have bad luck.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            It generally works out pretty random: since at any stage you will have 4, 3, or two possible moves (depending on where the hole is in respect of the edges) it's a pretty simple matter to "ban" the move which reverses the previous one. That does give good randomness in practice - it's down to how many iterations you go for.

            The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            L 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              It generally works out pretty random: since at any stage you will have 4, 3, or two possible moves (depending on where the hole is in respect of the edges) it's a pretty simple matter to "ban" the move which reverses the previous one. That does give good randomness in practice - it's down to how many iterations you go for.

              The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I suppose so, but I had to think of that argument for why the "swap any two random elements n times" algorithm doesn't shuffle an array correctly - some configurations have more paths to them than others, so they get chosen too often. I'm not sure that isn't the case here.

              M 1 Reply Last reply
              0
              • L Lost User

                I suppose so, but I had to think of that argument for why the "swap any two random elements n times" algorithm doesn't shuffle an array correctly - some configurations have more paths to them than others, so they get chosen too often. I'm not sure that isn't the case here.

                M Offline
                M Offline
                Midnight Ahri
                wrote on last edited by
                #7

                I was able to solve it using a timer that randomly click the movable button in 5 second and it worked. Thank you very much for the help guys. :laugh:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups