Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    15 puzzle game (image swapping)

    hi everyone. i have to create the 15 puzzle game and im having a difficult time getting started. can anyone help? i need to create a table (4x4) that has one empty space. All the other spaces must have a random generated value between 1 and 15. All i got so far is a onclick image swapping function.... need help! thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Look at rule 1.5 here:
    http://www.codingforums.com/rules.htm

    You get something started, get a reasonable amount of code and effort into it, and then ask for *specific* help, and you'll get it.

    We just can't do the entire homework for you.

    I'll give you one hint:

    Generate an array of numbers from 0 to 15. Then "shuffle* the array using random numbers to do the shuffling. Then plunk those numbers in shuffled order into the cells of your table (as images, of course) and simply don't put any image into the place where the zero shows up.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    I don't know for sure about 15 tile games, but I know that it is possible to set up a 9 tile puzzle that cannot be solved.
    You may need to start with the sorted array, and then walk through a series of random legal swaps to get a solvable game.

    For the 3X3 tile game, with one 'blank', an initial array of valid moves is:
    [[2, 4], [1, 3, 5], [2, 6], [1, 5, 7], [2, 4, 6, 8], [3, 5, 9], [4, 8], [5, 7, 9], [6, 8]];

    1 2 3
    4 5 6
    7 8 9

  • #4
    New Coder
    Join Date
    Feb 2010
    Location
    New Zealand
    Posts
    76
    Thanks
    7
    Thanked 10 Times in 9 Posts
    What are you trying to write your puzzle game in?

    If Javascript you might want to check the puzzle scramble games at (with source code):
    http://javascript.internet.com/games/

    Might give you an idea or two.
    Last edited by Azzaboi; 03-09-2010 at 12:12 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Azzabol: Read how he wrote the question:
    i have to create the 15 puzzle game
    That means it is homework for a class. And we aren't supposed to give him the answers, without his doing most of the work. Now, if he were to turn in any of the code from the site you listed, his teacher would never believe it's his work, anyway. But even so...

    *********

    MrHoo: Yes, you are correct. Exactly one-half of all randomly created puzzles are insolvable. I think it was Sam Loyd, back in the 1800s, who offered a *huge* prize to anybody who solved one of his slider puzzles. You had to buy the slider puzzle from him and then you had to show all your steps to the solution. And of course it was impossible, so he never had to pay any prize and made a ton of money.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok I apologize I should have posted my code with my entry. So far I got some swap functions started. I created a table and within each cell it calls one of the functions listed below. Is it possible to create a function that will assign random pictures into an array without repeating the same picture?




    function swap(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
    // get the image in the square before clicked
    // replace it with blank or with new image
    {}

    function moveFirstSquare()
    // when the first square is clicked
    // first check to see if square two or five are blank
    // if they are, move first square into the blank spot
    // if not, don't do anything
    // after its been moved, the first position should be a new image
    {
    if (isSquareBlank(2))
    { swap(1,2);
    }
    else if (isSquareBlank(5))
    { swap(1,5);
    }

    }
    function moveSecondSquare()
    {
    if (isSquareBlank(1))
    { swap(2,1);
    }
    else if (isSquareBlank(3))
    { swap(2,3);
    }
    else if (isSquareBlank(6))
    { swap(2,6);
    }
    }
    function moveThirdSquare()
    {
    if (isSquareBlank(2))
    { swap(3,2);
    }
    else if (isSquareBlank(4))
    { swap(3,4);
    else if (isSquareBlank(7))
    { swap(3,7);
    }

    }
    function moveFourthSquare()
    {
    if (isSquareBlank(3))
    { swap(4,3);
    }
    else if (isSquareBlank(8))
    { swap(4,8);
    }

    }
    function moveFifthSquare()
    {
    if (isSquareBlank(1))
    { swap(5,1);
    }
    else if (isSquareBlank(6))
    { swap(5,6);
    else if (isSquareBlank(9))
    { swap(5,9);
    }

    }
    function moveSixthSquare()
    {
    if (isSquareBlank(5))
    { swap(6,5);
    }
    else if (isSquareBlank(2))
    { swap(6,2);
    else if (isSquareBlank(7))
    { swap(6,7);
    }
    else if (isSquareBlank(10))
    { swap(6,10);
    }
    }
    function moveSeventhSquare()
    {
    if (isSquareBlank(6))
    { swap(7,6);
    }
    else if (isSquareBlank(3))
    { swap(7,3);
    }
    else if (isSquareBlank(8))
    { swap(7,8);
    else if (isSquareBlank(11))
    { swap(7,11);
    }
    }
    function moveEigthSquare()
    {
    if (isSquareBlank(7))
    { swap(8,7);
    }
    else if (isSquareBlank(4))
    { swap(8,4);
    }
    else if (isSquareBlank(12))
    { swap(8,12);
    }

    }
    function moveNinthSquare()
    {
    if (isSquareBlank(5))
    { swap(9,5);
    }
    else if (isSquareBlank(10))
    { swap(9,10);
    }
    else if (isSquareBlank(13))
    { swap(9,13);
    }
    }
    function moveTenthSquare()
    {
    if (isSquareBlank(9))
    { swap(10,9);
    }
    else if (isSquareBlank(6))
    { swap(10,6);
    }
    else if (isSquareBlank(11))
    { swap(10,11);
    }
    else if (isSquareBlank(14))
    { swap(10,14);
    }
    }
    function moveEleventhSquare()
    {
    if (isSquareBlank(10))
    { swap(11,10);
    }
    else if (isSquareBlank(7))
    { swap(11,7);
    }
    else if (isSquareBlank(12))
    { swap(11,12);
    }
    else if (isSquareBlank(15))
    { swap(11,15);
    }
    }
    function moveTwelthSquare()
    {
    if (isSquareBlank(8))
    { swap(12,8);
    }
    else if (isSquareBlank(11))
    { swap(12,11);
    }
    else if (isSquareBlank(16))
    { swap(12,16);
    }
    }
    function moveThirteenthSquare()
    {
    if (isSquareBlank(9))
    { swap(13,9);
    }
    else if (isSquareBlank(14))
    { swap(13,14);
    }

    }
    function moveFourteenthSquare()
    {
    if (isSquareBlank(13))
    { swap(14,13);
    }
    else if (isSquareBlank(10))
    { swap(14,10);
    }
    else if (isSquareBlank(15))
    { swap(14,15);
    }
    }
    function moveFifteenthSquare()
    {
    if (isSquareBlank(14))
    { swap(15,14);
    }
    else if (isSquareBlank(11))
    { swap(15,11);
    }
    else if (isSquareBlank(16))
    { swap(15,16);
    }
    }
    function moveSixteenthSquare()
    {
    if (isSquareBlank(15))
    { swap(16,15);
    }
    else if (isSquareBlank(12))
    { swap(16,12);
    }

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Well, that's one way to approach it.

    You still have to write the swap() function, of course.

    I don't think you will get full credit for a brute-force approach like that. I think you should instead have *logic* that checks to see if the clicked on square is adacent to the blank one, but I give you credit for finding one solution.

    But you haven't done the "shuffle" part, yet. And you haven't shown how you will *represent* the squares. It's a start, but it's kind of the back end before the front end.

    Maybe you and "nexttuesday" need to get together and do your homework together?
    Javascript Puzzle -- Put the numbers in order to win.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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