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:
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.
Joined: Mar 26, 2013
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.