Thread: The puzzle again...Swapping elements of 2D array

  1. #31
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    what alternate way can i do the do move function?

  2. #32
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by cprogrammer22 View Post
    what alternate way can i do the do move function?
    Growing tired of seeing the same question over and over....

    Perhaps:
    Code:
    int doMove(int puzzle[][PUZZLE_SIDE], int move)
    {
        int looping_variable[6] = {0};
     
        for(looping_variable[0] = 0; looping_variable[0] < 4; looping_variable[0]++)
        {
            for(looping_variable[1] = 0; looping_variable[1] < 4; looping_variable[1]++)
            {
                if(puzzle[looping_variable[0]][looping_variable[k]] == move)
                {
                    looping_variable[2] = looping_variable[0];
                    looping_variable[3] = looping_variable[1];
                }
                else if(puzzle[looping_variable[0]][looping_variable[1]] == EMPTY_SLOT)
                {
                    looping_variable[4] = looping_variable[0];
                    looping_variable[5] = looping_variable[1];
                }
            }
        }
    
        if(!puzzle[looping_variable[2]-1][looping_variable[3]] || !puzzle[looping_variable[2]+1][looping_variable[3]] || !puzzle[looping_variable[2]][b-1] || !puzzle[looping_variable[2]][looping_variable[3]+1])
        {
            swap(&puzzle[looping_variable[2]][looping_variable[3]],&puzzle[looping_variable[4]][looping_variable[5]]);
            return 1;
        }
    
        return 0;
    }

  3. #33
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which certainly doesn't incorporate what tabstop was saying and I am definitely not saying one should write their code like this. It is just something different.

  4. #34
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    wow, thanks alot, whenever i replace this with the alternate way (previous in program before, the program crashes after entering a number..

  5. #35
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Really? Can you show me your alternate ways? I mean mine wasn't much of an alternative. Its messy to read, and is basically the same thing.

  6. #36
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    yours was fine, im just trying to code it differently and that helped me alot actually, for some reason i am just having trouble with this stuff and havent gotten the hang of it yet, you thiink you could do something similar to the last function:

    int solved(int puzzle[][PUZZLE_SIDE])

  7. #37
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by cprogrammer22 View Post
    yours was fine, im just trying to code it differently and that helped me alot actually, for some reason i am just having trouble with this stuff and havent gotten the hang of it yet, you thiink you could do something similar to the last function:

    int solved(int puzzle[][PUZZLE_SIDE])
    Where is Elysia to point out how not fine my code is when I need her? Or tabstop is on, so why can't he?

  8. #38
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm still trying to pick myself up off the floor.

    Again, the basic algorithm is not really very changeable, so apart from just picking a whole new data structure you aren't going to be able to make a great deal of changes.

  9. #39
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think solved() is written just fine. To be frank, you went the extra nine yards and didn't just loop through values like a lot of folks might (which is an example of a seasoned pro, or a rookie who doesn't quite think in terms of loops just yet).

    Your algorithm looks fine, cprogrammer22

  10. #40
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    hmmm... you think you can show me what that other option would look like, sorry to be so much trouble

  11. #41
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You aren't of any trouble at all... But honestly, what I am showing you is not as good as the beautiful piece of software you conjured on your own. But here goes...

    Code:
    int solved(int puzzle[][PUZZLE_SIDE]) 
    {
      int i, x, y, r;
    
      for(r = 1, i = 1, x = 0; x < 4; ++x)
        for(y = 0; y < 4; ++y)
          if(puzzle[x][y] != i++)
          {
            r = 0;
            break;
          }
    
      return r;
    }
    You get less of a hand crap typing the loops, but your optimizer may not unroll all those loops, thus you are less with a sub-optimal way of doing this check.
    Last edited by master5001; 11-05-2008 at 12:31 PM.

  12. #42
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    when i load this new function into the old program, it will let me move one piece and regardless if the puzzle is in order, it wil say i solved the puzzle and end the program

  13. #43
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    messing around with it again, i can make the program complete in correct order, but now the program will not say puzzle completed successfully when in correct order

  14. #44
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ooops. I fixed it. I had i = 0 instead of r = 0. My bad. Though you should not be using code that I keep telling is not as optimal as your own.

  15. #45
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    hmm.. i dont know why i keep having trouble with it.. i added the new code and still it wont complete when the numbers are in order from 1 - 15.. it will just keep allowing me to move the puzzle pieces around

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:04 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM