Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Tile puzzle game getting tiles to move (help)

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Tile puzzle game getting tiles to move (help)

    I am building a class called TilePuzzle and it is a text base game and it looks like this.

    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 #

    The way I programmed the move method it looks for the empty space(#), after finding the empty space it looks at the legal moves and compares the numbers around it to see if it is the number you passed to it, if so then switch them.

     
        public void move(int tileNumber) {
            if (!isValid(tileNumber)) {
                return;
            }
     
            for (int r = 0; r < boardSize; r++) {
                for (int c = 0; c < boardSize; c++) {
     
                    if (tile[r][c] == EMPTY) {
     
                        if (r != 0 && tile[r - 1][c] == tileNumber) { // checks up position
                            tile[r][c] = tile[r - 1][c];
                            tile[r - 1][c] = EMPTY;
                        } else if (r != (boardSize - 1) && tile[r + 1][c] == tileNumber) { // checks down position
                            tile[r][c] = tile[r + 1][c];
                            tile[r + 1][c] = EMPTY;
                        } else if (c != 0 && tile[r][c - 1] == tileNumber) { // checks left position 
                            tile[r][c] = tile[r][c - 1];
                            tile[r][c - 1] = EMPTY;
                        } else if (c != (boardSize - 1) && tile[r][c + 1] == tileNumber) { // checks right position
                            tile[r][c] = tile[r][c + 1];
                            tile[r][c + 1] = EMPTY;
                        }
                    }
                }
            }
        }

    boardSize is equal to 4 and EMPTY is equal to #.

    now the problem, so the if statements that check for up and left work but the if statements that check for down and right don't work and I tried a couple different things like put a System.out.println(); in both body of the if statement and they are excusing when call but they don't switch the numbers. I also changed the (boardSize - 1) to 3 thinking that might have something to do with it but it still didn't work.

    any help or ideas would be greatly appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

    down and right don't work
    Can you explain what "don't work" means?
    What happens when a down move is chosen?
    what happens when a right move is chosen?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

    Here an example

    you enter in 12 and the move method will switch the 12 with the empty space like so.

    1 2 3 4
    5 6 7 8
    9 10 11 #
    13 14 15 12

    but if you enter 12 again it i will not switch, it just doesn't do anything. that is why I put System.out.println("test"); in the body of the down and right to see if they were being executed and they were but they don't switch the number with the empty space.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

    but they don't switch the number with the empty space.
    Try debugging the code by adding some println statements that print out the values of the variables used to control the moves the code makes. What are the indexes into the array for the from element and for the to element?


    Do you have a test driver that will compile, execute and show the problem?

    1 2 3 4
    5 6 7 8
    9 10 11 #
    13 14 15 12

    In the board position you posted, would the move be UP for 12 to where the # is?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

        public void move(int tileNumber) {
            if (!isValid(tileNumber)) {
                return;
            }
     
            for (int r = 0; r < boardSize; r++) {
                for (int c = 0; c < boardSize; c++) {
     
                    if (tile[r][c] == EMPTY) {
     
                        if (r != 0 && tile[r - 1][c] == tileNumber) { // checks up position
                            tile[r][c] = tile[r - 1][c];
                            tile[r - 1][c] = EMPTY;
                        } else if (r != (boardSize - 1) && tile[r + 1][c] == tileNumber) { // checks down position
                            System.out.println("value of tile[r][c]: " + tile[r][c] + " value of tile[r+1][c]" + tile[r+1][c]);
                            tile[r][c] = tile[r + 1][c];
                            System.out.println("value of tile[r][c]: " + tile[r][c] + " value of tile[r+1][c]" + tile[r+1][c]);
                            tile[r + 1][c] = EMPTY;
                            System.out.println("value of tile[r][c]: " + tile[r][c] + " value of tile[r+1][c]" + tile[r+1][c]);
                        } else if (c != 0 && tile[r][c - 1] == tileNumber) { // checks left position 
                            tile[r][c] = tile[r][c - 1];
                            tile[r][c - 1] = EMPTY;
                        } else if (c != (boardSize - 1) && tile[r][c + 1] == tileNumber) { // checks right position
                            tile[r][c] = tile[r][c + 1];
                            tile[r][c + 1] = EMPTY;
                        }
                    }
                }
            }
        }


    run:
    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 #

    Enter a number: 12
    1 2 3 4
    5 6 7 8
    9 10 11 #
    13 14 15 12
    Enter a number: 12
    value of tile[r][c]: 35 value of tile[r+1][c]12
    value of tile[r][c]: 12 value of tile[r+1][c]12
    value of tile[r][c]: 12 value of tile[r+1][c]35
    1 2 3 4
    5 6 7 8
    9 10 11 #
    13 14 15 12

    The value are right and it looks like they have switch but when it doesn't move.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

    You need to add println() calls to all the if statements, not just a couple of them
    and one println first thing inside the if when the empty slot is found.
    The messages should say what kind of move was made: up, left, etc
    You can't tell looking at the printout which move was made.

    Does the printout say that three moves were made when the move() method was called?
    Does that make sense?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

    I think i do,
    but what i posted before does what you have asked I believe.

    1 2 3 4
    5 6 7 8
    9 10 11 #
    13 14 15 12
    Enter a number: 12

    The user enter the number 12. so the if statement should switch the # and the 12 and the board should look like this.....

    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 #

    but it doesn't so i put some System.out statements in the if statement that move the number down.

    //the number 35 is the empty slot.

    // found the empty tile and the one above that the tile number the user enter (12)
    value of tile[r][c]: 35 value of tile[r+1][c]12
    // next I assign the empty tile to the value of the tile above it like so.
    value of tile[r][c]: 12 value of tile[r+1][c]12
    // now i set the tile that was above to empty
    value of tile[r][c]: 12 value of tile[r+1][c]35

    so logically everything seem to be working under the hood but its not switching them visually. It can't be the toString method because the it works when you switch is up or if the switch is left.

    --- Update ---

    Sorry as soon as I posted this my buddy sitting next to me suggested something and it works also figured out why my way didn't work.

    thank you for your time i really appreciated.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tile puzzle game getting tiles to move (help)

    suggested something and it works
    Glad you got a solution.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Recursive Assistance with a Word Puzzle Game
    By inflames098 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 09:33 AM
  2. Puzzle Game. Need some help
    By clydefrog in forum Java Theory & Questions
    Replies: 10
    Last Post: March 12th, 2012, 03:14 PM
  3. Sliding puzzle Restart button(same exact game)
    By carterb32 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2011, 04:29 AM
  4. Search Word puzzle game
    By lew1s in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 9th, 2011, 04:23 AM