Question

I'm writing a program in java called Sokoban. I'm pretty far in, but there are a...

I'm writing a program in java called Sokoban. I'm pretty far in, but there are a few methods that I have no idea where to go from where I am now.

Method 1:

/**

   * Moves a box on the board.

   *

   * Step 1: Use your checkDelta method to check that the move is valid. Recall that there are 2

   * characters that can represent a box. Step 2: Use your togglePos method to correctly change

   * the character at the new position to the appropriate box character. Step 3: Again use your

   * togglePos method to correctly change the character at pos to the the appropriate character

   * without a box.

   *

   * @param board The current board.

   * @param pos The position to change. A length 2 array, where index 0 is the row and index 1 is

   * the column.

   * @param delta The move distance. A length 2 array, where index 0 is the change in row and

   * index 1 is the change in column.

   * @return The return value of checkDelta if less than 1. Otherwise 1.

   */

public static int shiftBox(char[][] board, int[] pos, int[] delta) {

char[] valid = {Config.BOX_CHAR, Config.BOX_GOAL_CHAR};

if (checkDelta(board, pos, delta, valid) == 1) {

   char val = Config.GOAL_CHAR;

   char opt1 = Config.BOX_GOAL_CHAR;

   char opt2 = Config.BOX_CHAR;

   togglePos(board, pos, val, opt1, opt2);

   opt1 = Config.GOAL_CHAR;

   opt2 = Config.BOX_CHAR;

   togglePos(board, pos, val, opt1, opt2);

   return 1;

}

else {

   return (checkDelta(board, pos, delta, valid));

}

return -99;

}

//incomplete

Method 2:

/**

   * Processes a move of the worker step-by-step.

   *

   * Go through the delta step-by-step, calling doMove for each step. That is, if the delta is {0,

   * -3}, your method should call doMove three times with an argument of {0, -1} for the delta

   * parameter of doMove. Or, if the delta is {6, 0}, it would call the doMove six times with an

   * argument of {1, 0} for the delta parameter of the doMove method.

   *

   * During the processing of the move, if ever a call to doMove returns a value less than 1, your

   * method should stop processing and return that value.

   *

   * Note: You can assume that one of the cells of delta will be 0.

   *

   * @param board The current board.

   * @param pos The position to change. A length 2 array, where index 0 is the row and index 1 is

   * the column.

   * @param delta The move distance. A length 2 array, where index 0 is the change in row and

   * index 1 is the change in column.

   * @return If both of the cells of delta are 0, return 0. If the call to doMove returns a value

   * less than 1, return that value. Otherwise, return 1.

   */

public static int processMove(char[][] board, int[] pos, int[] delta) {

  

return -99;

}

Method 3:

/**

   * Moves the worker on the board.

   *

   * Step 1: Use your checkDelta method to check that the move is valid. Recall that there are 2

   * characters that can represent the worker. Step 2: If checkDelta returns -5, use your shiftBox

   * method to move the box by delta before moving the worker. Step 3: Use your togglePos method

   * to correctly change the character at the new position to the appropriate worker character.

   * Step 4: Again use your togglePos method to correctly change the character at pos to the the

   * appropriate character without a worker. Step 5: Update the position of the worker in pos.

   *

   * @param board The current board.

   * @param pos The position to change. A length 2 array, where index 0 is the row and index 1 is

   * the column.

   * @param delta The move distance. A length 2 array, where index 0 is the change in row and

   * index 1 is the change in column.

   * @return If checkDelta returns a value less than 1 that is not -5, return that value. If

   * checkDelta returns -5 and shiftBox returns a value less than 0, return 0. Otherwise,

   * return 1.

   */

public static int doMove(char[][] board, int[] pos, int[] delta) {

// FIX ME

return -99;

}

Any help would be greatly appreciated!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. package sokoban;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;
  6. import java.util.ArrayList;
  7. import javax.swing.JPanel;
  8. @SuppressWarnings("serial")
  9. public class Board extends JPanel
  10. {
  11. private final int DISTANCE_BORDER_GAMEGRAPHICS = 30;
  12. private final int SPACE = 20;
  13. private final int LEFT_COLLISION = 1;
  14. private final int RIGHT_COLLISION = 2;
  15. private final int UP_COLLISION = 3;
  16. private final int DOWN_COLLISION = 4;
  17. private ArrayList<Wall> Walls;
  18. private ArrayList<Objective> Objectives;
  19. private ArrayList<Box> Boxes;
  20. private Player soko;
  21. private int width = 0;
  22. private int height = 0;
  23. private boolean completed = false;
  24. private String level =
  25. " ######\n"
  26. + " ##** #\n"
  27. + " ##$ #\n"
  28. + " #### @ $##\n"
  29. + " #########\n";
  30. public Board() {
  31. addKeyListener(new SokoAdapter());
  32. setFocusable(true);
  33. initiateGame();
  34. }
  35. public int getBoardWidth() {
  36. return this.width;
  37. }
  38. public int getBoardHeight() {
  39. return this.height;
  40. }
  41. public final void initiateGame() {
  42. int x = DISTANCE_BORDER_GAMEGRAPHICS;
  43. int y = DISTANCE_BORDER_GAMEGRAPHICS;
  44. Wall wall;
  45. Box box;
  46. Objective objective;
  47. for (int i = 0; i < level.length(); i++) {
  48. char item = level.charAt(i);
  49. if (item == '\n') {
  50. y += SPACE;
  51. if (this.width < x) {
  52. this.width = x;
  53. }
  54. x = DISTANCE_BORDER_GAMEGRAPHICS;
  55. } else if (item == '#') {
  56. wall = new Wall(x, y);
  57. Walls.add(wall);
  58. x += SPACE;
  59. } else if (item == '$') {
  60. box = new Box(x, y);
  61. Boxes.add(box);
  62. x += SPACE;
  63. } else if (item == '*') {
  64. objective = new Objective(x, y);
  65. Objectives.add(objective);
  66. x += SPACE;
  67. } else if (item == '@') {
  68. soko = new Player(x, y);
  69. x += SPACE;
  70. } else if (item == ' ') {
  71. x += SPACE;
  72. }
  73. height = y;
  74. }
  75. }
  76. public void buildGame(Graphics g) {
  77. g.setColor(new Color(250, 240, 170));
  78. g.fillRect(0, 0, this.getWidth(), this.getHeight());
  79. ArrayList world = new ArrayList();
  80. world.addAll(Walls);
  81. world.addAll(Objectives);
  82. world.addAll(Boxes);
  83. world.add(soko);
  84. for (int i = 0; i < world.size(); i++) {
  85. Actor item = (Actor) world.get(i);
  86. if ((item instanceof Player)
  87. || (item instanceof Box)) {
  88. g.drawImage(item.getImage(), item.x() + 2, item.y() + 2, this);
  89. } else {
  90. g.drawImage(item.getImage(), item.x(), item.y(), this);
  91. }
  92. if (completed) {
  93. g.setColor(new Color(0, 0, 0));
  94. g.drawString("Completed", 25, 20);
  95. }
  96. }
  97. }
  98. @Override
  99. public void paint(Graphics g) {
  100. super.paint(g);
  101. buildGame(g);
  102. }
  103. class SokoAdapter extends KeyAdapter {
  104. @Override
  105. public void keyPressed(KeyEvent e) {
  106. if (completed) {
  107. return;
  108. }
  109. int key = e.getKeyCode();
  110. if (key == KeyEvent.VK_LEFT) {
  111. if (checkWallCollision(soko, LEFT_COLLISION)) {
  112. return;
  113. }
  114. if (checkBoxCollision(LEFT_COLLISION)) {
  115. return;
  116. }
  117. soko.move(-SPACE, 0);
  118. } else if (key == KeyEvent.VK_RIGHT) {
  119. if (checkWallCollision(soko,
  120. RIGHT_COLLISION)) {
  121. return;
  122. }
  123. if (checkBoxCollision(RIGHT_COLLISION)) {
  124. return;
  125. }
  126. soko.move(SPACE, 0);
  127. } else if (key == KeyEvent.VK_UP) {
  128. if (checkWallCollision(soko,
  129. UP_COLLISION)) {
  130. return;
  131. }
  132. if (checkBoxCollision(UP_COLLISION)) {
  133. return;
  134. }
  135. soko.move(0, -SPACE);
  136. } else if (key == KeyEvent.VK_DOWN) {
  137. if (checkWallCollision(soko,
  138. DOWN_COLLISION)) {
  139. return;
  140. }
  141. if (checkBoxCollision(DOWN_COLLISION)) {
  142. return;
  143. }
  144. soko.move(0, SPACE);
  145. } else if (key == KeyEvent.VK_R) {
  146. restartLevel();
  147. }
  148. repaint();
  149. }
  150. }
  151. private boolean checkWallCollision(Actor actor, int type) {
  152. if (type == LEFT_COLLISION) {
  153. for (int i = 0; i < Walls.size(); i++) {
  154. Wall wall = (Wall) Walls.get(i);
  155. if (actor.isLeftCollision(wall)) {
  156. return true;
  157. }
  158. }
  159. return false;
  160. } else if (type == RIGHT_COLLISION) {
  161. for (int i = 0; i < Walls.size(); i++) {
  162. Wall wall = (Wall) Walls.get(i);
  163. if (actor.isRightCollision(wall)) {
  164. return true;
  165. }
  166. }
  167. return false;
  168. } else if (type == UP_COLLISION) {
  169. for (int i = 0; i < Walls.size(); i++) {
  170. Wall wall = (Wall) Walls.get(i);
  171. if (actor.isTopCollision(wall)) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. } else if (type == DOWN_COLLISION) {
  177. for (int i = 0; i < Walls.size(); i++) {
  178. Wall wall = (Wall) Walls.get(i);
  179. if (actor.isBottomCollision(wall)) {
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. return false;
  186. }
  187. private boolean checkBoxCollision(int type) {
  188. if (type == LEFT_COLLISION) {
  189. for (int i = 0; i < Boxes.size(); i++) {
  190. Box box = (Box) Boxes.get(i);
  191. if (soko.isLeftCollision(box)) {
  192. for (int j=0; j < Boxes.size(); j++) {
  193. Box item = (Box) Boxes.get(j);
  194. if (!box.equals(item)) {
  195. if (box.isLeftCollision(item)) {
  196. return true;
  197. }
  198. }
  199. if (checkWallCollision(box,
  200. LEFT_COLLISION)) {
  201. return true;
  202. }
  203. }
  204. box.move(-SPACE, 0);
  205. isCompleted();
  206. }
  207. }
  208. return false;
  209. } else if (type == RIGHT_COLLISION) {
  210. for (int i = 0; i < Boxes.size(); i++) {
  211. Box box = (Box) Boxes.get(i);
  212. if (soko.isRightCollision(box)) {
  213. for (int j=0; j < Boxes.size(); j++) {
  214. Box item = (Box) Boxes.get(j);
  215. if (!box.equals(item)) {
  216. if (box.isRightCollision(item)) {
  217. return true;
  218. }
  219. }
  220. if (checkWallCollision(box,
  221. RIGHT_COLLISION)) {
  222. return true;
  223. }
  224. }
  225. box.move(SPACE, 0);
  226. isCompleted();
  227. }
  228. }
  229. return false;
  230. } else if (type == UP_COLLISION) {
  231. for (int i = 0; i < Boxes.size(); i++) {
  232. Box box = (Box) Boxes.get(i);
  233. if (soko.isTopCollision(box)) {
  234. for (int j = 0; j < Boxes.size(); j++) {
  235. Box item = (Box) Boxes.get(j);
  236. if (!box.equals(item)) {
  237. if (box.isTopCollision(item)) {
  238. return true;
  239. }
  240. }
  241. if (checkWallCollision(box,
  242. UP_COLLISION)) {
  243. return true;
  244. }
  245. }
  246. box.move(0, -SPACE);
  247. isCompleted();
  248. }
  249. }
  250. return false;
  251. } else if (type == DOWN_COLLISION) {
  252. for (int i = 0; i < Boxes.size(); i++) {
  253. Box box = (Box) Boxes.get(i);
  254. if (soko.isBottomCollision(box)) {
  255. for (int j = 0; j < Boxes.size(); j++) {
  256. Box item = (Box) Boxes.get(j);
  257. if (!box.equals(item)) {
  258. if (box.isBottomCollision(item)) {
  259. return true;
  260. }
  261. }
  262. if (checkWallCollision(box,
  263. DOWN_COLLISION)) {
  264. return true;
  265. }
  266. }
  267. box.move(0, SPACE);
  268. isCompleted();
  269. }
  270. }
  271. }
  272. return false;
  273. }
  274. public void isCompleted() {
  275. int num = Boxes.size();
  276. int compl = 0;
  277. for (int i = 0; i < num; i++) {
  278. Box box = (Box) Boxes.get(i);
  279. for (int j = 0; j < num; j++) {
  280. Objective Objective = (Objective) Objectives.get(j);
  281. if (box.x() == Objective.x()
  282. && box.y() == Objective.y()) {
  283. compl += 1;
  284. }
  285. }
  286. }
  287. if (compl == num) {
  288. completed = true;
  289. repaint();
  290. }
  291. }
  292. public void restartLevel() {
  293. Objectives.clear();
  294. Boxes.clear();
  295. Walls.clear();
  296. initiateGame();
  297. if (completed) {
  298. completed = false;
  299. }
  300. }
  301. }
Add a comment
Know the answer?
Add Answer to:
I'm writing a program in java called Sokoban. I'm pretty far in, but there are a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Can someone help me with the writing of this code please? public static int promptInt(Scanner input,...

    Can someone help me with the writing of this code please? public static int promptInt(Scanner input, String prompt, int min, int max) { return 0; //TODO replace }    /**    * Returns the index within arr of the first occurrence of the specified character.    * If arr is null or 0 length then return -1. For all arrays, don't assume a length    * but use the array .length attribute.    * @param arr The array to look...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • Anybody Know how to Solve this Problem Thank you very much // ***** 1. This method...

    Anybody Know how to Solve this Problem Thank you very much // ***** 1. This method has been coded as an example /** Fills the array with random numbers between 50 and 80 * The instance variable named intArray is the integer array to be * filled with values */ public void fillValues( ) { Random rand = new Random( ); for ( int row = 0; row < intArray.length; row++ ) { System.out.print( row + "\t" ); for (...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • I need help with this one method in java. Here are the guidelines. Only public Employee[]...

    I need help with this one method in java. Here are the guidelines. Only public Employee[] findAllBySubstring(String find). EmployeeManager EmployeeManager - employees : Employee[] - employeeMax : final int = 10 -currentEmployees : int <<constructor>> EmployeeManager + addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) + removeEmployee( index : int) + listAll() + listHourly() + listSalary() + listCommision() + resetWeek() + calculatePayout() :...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { ...

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { }; struct game_board { }; void game_piece_init_default(struct game_piece* piece) { } void game_piece_init(struct game_piece* piece, char* new_label) { } char* game_piece_get_label(struct game_piece* piece) { return ""; } char* game_piece_to_string(struct game_piece* piece) { return ""; } void game_board_init(struct game_board* game_board, int rows, int cols) { } int game_board_is_space_valid(struct game_board* game_board, int row, int col) { return 0; } int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col) { return 0;...

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT