This site requires JavaScript, please enable it in your browser!
Greenfoot back
blissmadrid
blissmadrid wrote ...

2015/11/18

SOS - Sliding Puzzle

blissmadrid blissmadrid

2015/11/18

#
Hello! We are trying to code a sliding puzzle for a school project due in 48 hours. The blank tile should switch positions with the tile beside it when the arrow keys are pressed, but we do not know how to switch their positions. Also, each puzzle piece is its own actor. We need help in coding the switching part, but if any of you know an easy way to code a sliding puzzle game, it will be much appreciated. Thank you!
davmac davmac

2015/11/18

#
You can start by providing more information (including the code that you have), and trying to break the problem up into smaller parts. Please read these general guidelines.
blissmadrid blissmadrid

2015/11/18

#
This is the code for one of the puzzle pieces
1
2
3
4
5
public void act()
    {
        Greenfoot.delay(25);
        setLocation(329, 444);
    }
And this is the code for the blank tile
1
2
3
4
5
public void act()
    {
        Greenfoot.delay(25);
        setLocation(379, 182);
    }
Neither have any variables yet because we don't know what variables are needed so we can switch their places.
davmac davmac

2015/11/18

#
If you set the location of the piece and the tile in their respective act() methods, they will always move back to that location. You should not set the location like that in the act() method. I don't see any code to handle key presses. I suggest you should put that in the blank tile class or in your world subclass. If you don't know how to handle keyboard input, go through the tutorials and videos. To switch the location of the blank tile with a puzzle piece, you get the location of the puzzle piece and store it (into two variables - one for the X and one for the Y coordinate), then set the location of the puzzle piece to the location of the blank tile, then set the location of the blank tile to the (stored) previous position of the puzzle piece.
blissmadrid blissmadrid

2015/11/18

#
Thank you @davmac! We will do our best to fix it. Your comments are very helpful. You were right about the piece and tile going back to their locations, and we were able to add the controls keys already. Thank you so much again :)
blissmadrid blissmadrid

2015/11/19

#
hello would you (or someone) please elaborate your third paragraph? :( This is our new code for the blank tile and we still have not gotten to the point of switching it with other tiles. HELP IS MUCH APPRECIATED THANK YOU SO MUCH :((((
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public void act()
   {
       controls();
       limitations();
   }
 
   public void controls()
   {
       if(Greenfoot.isKeyDown("down"))
       {
           setLocation(getX(), getY() + 119);
       }
       if(Greenfoot.isKeyDown("up"))
       {
           setLocation(getX(), getY() - 119);
       }
       if(Greenfoot.isKeyDown("right"))
       {
           setLocation(getX()+ 125, getY());
       }
       if(Greenfoot.isKeyDown("left"))
       {
           setLocation(getX()- 125, getY());
       }
   }
 
   public void limitations()
   {
       if (  getX()>378 ) //limitations start here. feeling setLocation yung kailangan ayusin pero baka yung if part pala
       {
           setLocation(378, getY());
       }
       if (  getX()<128 )
       {
           setLocation(128, getY());
       }
       if (  getX()==254 )
       {
           setLocation(254, getY());
       }
       if (getY()<181)
       {
           setLocation(getX(), 182);
       }
       if (getY()>422)
       {
           setLocation(getX(), 422);
       }
   }
danpost danpost

2015/11/19

#
Instead of just moving the blank tile and checking the validity of its new location, would it not be easier to test for a non-blank tile in the direction it is to move and, if one exists at that location, use that location with its current location to set the new location of both?
blissmadrid blissmadrid

2015/11/19

#
how would we be able to test for a non-blank tile and how do we set directions? are directions supposed to be stored as variables?
danpost danpost

2015/11/19

#
blissmadrid wrote...
how would we be able to test for a non-blank tile and how do we set directions? are directions supposed to be stored as variables?
You could at least store the horizontal and vertical directions using values of zero, one, and negative one, since the offset distance is a constant 125 horizontally and 119 vertically. The product of the direction and distance will determine where to look for a non-blank tile. Using 'getWorld().getObjectsAt' and checking to see if the returned list is not empty would determine whether a non-blank tile is there (using the 'isEmpty' method of the List class on the List object). Then, using the 'get' method of the List class on the List object and casting the object returned to an Actor object is enough to acquire the 'setLocation' method on the object for moving it. There is no need for the "limitation" check as if the list is empty, no swap can be made anyway. That is, once you initially set the location of your tile actors, no other locations would be used; so, using their current locations to set the swapping tiles ensures that they are in appropriate locations.
Royalblue64 Royalblue64

2015/11/19

#
There's a nice and easy way to do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
private int newX;
private int newY;
 
public void act()
{
    controls();
}
 
public void controls()
{
    deltaX = 0;
    deltaY = 0;
     
    if (Greenfoot.isKeyDown("Up"))
    {
        deltaY = -119;
    }
    else if (Greenfoot.isKeyDown("Down"))
    {
        deltaY = 119;
    }
    else if (Greenfoot.isKeyDown("Left"))
    {
        deltaX = -125;
    }
    else if (Greenfoot.isKeyDown("Right"))
    {
        deltaX = 125;
    }
     
    if (getX() + deltaX > getWorld().getWidth() || getX() + deltaX < 0 ||
        getY() + deltaY > getWorld().getHeight() || getY() + deltaY < 0)
    {
        newX = 0;
        newY = 0;
    }
    else
    {
        getWorld().getOneObjectAtOffset(deltaX, deltaY, Tile.class).setLocation(getX(), getY());
        setLocation(getX() + deltaX, getY() + deltaY);
    }
}
The "Tile.class" being referenced in the getObjectsAt function would be whatever the class is for the visible puzzle pieces in your game. You shouldn't need anything other than this in the invisible tile's class. Also, it's best practice to use the delay method in "act" in the world class for your level, rather than in one of the actors. Good luck! =D Edit: I forgot to explain how the bottom bit works. That long if statement that is checking the attempted move location of the player's tile is comparing this location to the world boundaries and will not try to switch the invisible tile with a normal tile if the player has attempted to move off the screen.
You need to login to post a reply.