This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes sokoban Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sokoban" Watch "sokoban" New topic
Author

sokoban

Alexandra Botova
Greenhorn

Joined: Mar 26, 2013
Posts: 2
Hi everyone. I am currently working on a game - Sokoban. I ran into a bit of an issue, my canMove and moveBox methods currently have four different directions, and should have only one, using dx and dy as direction offsets computed from which direction the player is moving to (for example, going up is represented by having dx=0 and dy=-1, and so on). Don,t know how to do it.
Here are parts of my code:




[Added code tags - see UseCodeTags for details]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
So where are your player classes?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
And welcome to the Ranch
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Hi Julia. Welcome to the Ranch!

Do you mean something like this (taking on the simpler one)?

You're still going to need the switch statement, but you can factor out the common behaviour for each one into a separate method.
Alexandra Botova
Greenhorn

Joined: Mar 26, 2013
Posts: 2
Hi Matthew. Thank you for your help. I don't really understand step 5 myself. Instructons here:


1.Write a constructor Sokoban(String fileName) for your class that calls the readLevels method of the LevelReader object.

2.Write a method void initLevel(int level) that asks the LevelReader object for the dimension of the current level, sets up a 2D array of that size, and then fills it tile by tile by asking the level reader for the contents of each tile, then calls repaint.

3. Write the method void paintComponent(Graphics g) that draws the contents of the current level (from the 2D array) on the component. You can initially just draw each type of tile as rectangles of different colours, and later when you get the game working, put in some better graphics, if you have time. Note that Swing will probably try to draw your component before its constructor has finished reading the level files. Therefore your class should have a boolean field isReady that is initially false, and your constructor turns it true when it has finished. Your paintComponent method should immediately return and draw nothing if isReady is false.

4.Add a key listener to your class, preferably a separate inner class (since its keyPressed method is going to end up quite long). Make it so that whenever some cursor key is pressed, the method loops through the room to find the coordinates of the player. When the player is found, then depending on which cursor key was pressed, move the player to that direction if that location is empty or a goal tile. At this point, the player doesn't need to be able to push boxes yet, but just move around.

5.Now go back to redesign the previous method so that it doesn't consist of four identical branches of code for the four different directions, but you have only one branch parameterized with movement offsets dx and dy (for example, going up is represented by having dx=0 and dy=-1, and so on). After you simplify and streamline your code so that instead of having to do the error prone dance of writing and maintaining four branches, there is only one branch.

6.Modify the key listening method so that if the player tries to move to a location that contains a box, check if the space behind the box is empty or a goal tile, and if so, move the box there, and move the player to the location where the box used to be. Be careful to update each tile so that the player or the box doesn't erase a goal on the floor as it moves over it.

7. Write a method private boolean checkWin() that loops through the room and returns true if every box is above some goal tile, and returns false otherwise. Call this method every time you move a box, and if it returns true, move on to the next level.

8. Modify the key listener so that the 'R' and 'N' keys work as required.
 
Don't get me started about those stupid light bulbs.
 
subject: sokoban
 
Similar Threads
JFrame not resizing when JPanel changes size
Drawing text inside a Graphics2D shape
Trying to make custom GUI
Creating Java applet
setting border to Panel, how-to?