Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. package maze;
  2. public class Maze {
  3.  
  4. // CONSTANTS -- MOVE
  5. public final static int MOVE_UP = 0;
  6. public final static int MOVE_DOWN = 1;
  7. public final static int MOVE_LEFT = 2;
  8. public final static int MOVE_RIGHT = 3;
  9.  
  10. // CONSTANTS -- FIELDS
  11. public final static int FREE = 0;
  12. public final static int WALL = 1;
  13. public final static int EXIT = 2;
  14.  
  15. // playfield
  16. /** The playfield is given as a rectangle where
  17. - walls are represented by entries of value Maze.WALL ('1')
  18. - the exit is represented as an entry of value Maze.EXIT ('2')
  19. - all other entries are MAZE.FREE ('0')
  20. - A playfield has <em>exactly</em> one exit.
  21. - The first number determines the column, the second determines
  22. the row. Row and column numbers start at 0.
  23. - Each row has the same number of columns.
  24.  
  25. Example: 6x6 maze with an exit at position (5,3) (5th row, 3rd
  26. column):
  27.  
  28. 0 1 1 1 1 0
  29. 0 0 0 0 0 0
  30. 1 1 1 1 0 1
  31. 0 1 0 0 0 1
  32. 0 0 0 1 0 0
  33. 1 1 0 2 1 0
  34.  
  35. */
  36.  
  37. /*@
  38. public invariant
  39. (\exists int colNr; colNr >= 0;
  40. (\forall int row; row >= 0 && row < maze.length;
  41. maze[row].length == colNr));
  42. @*/
  43.  
  44. /*@
  45. public invariant
  46. (\forall int row; row >= 0 && row < maze.length;
  47. (\forall int col; col >= 0 && col < maze[row].length;
  48. maze[row][col] >= FREE && maze[row][col] <= EXIT));
  49. @*/
  50.  
  51. /*@
  52. public invariant
  53. (\exists int row; row >= 0 && row < maze.length;
  54. (\exists int col; col >= 0 && col < maze[row].length;
  55. maze[row][col] == EXIT &&
  56. (\forall int row2;(\forall int col2;
  57. (row2 >= 0 && row2 < maze.length &&
  58. col2 >= 0 && col2 < maze[row].length);
  59. maze[row2][col2] == EXIT ==> (row2 == row && col2 == col)))));
  60. @*/
  61. private /*@ spec_public @*/ int[][] maze;
  62.  
  63. /** Player Position:
  64. the position of a player must always denote a field inside the maze
  65. which is not a wall
  66. */
  67. /*@
  68. private invariant
  69. playerRow >= 0 && playerRow < maze.length &&
  70. (playerCol >= 0 && playerCol < maze[playerRow].length && maze[playerRow][playerCol] != WALL);
  71. @*/
  72. private /*@ spec_public @*/ int playerRow, playerCol;
  73.  
  74.  
  75. public Maze(int[][] maze, int startRow, int startCol) {
  76. this.maze = maze;
  77. // set player on start position
  78. this.playerRow = startRow;
  79. this.playerCol = startCol;
  80. }
  81.  
  82. /**
  83. * returns true if the player has reached the exit field;
  84. * the method does not affect the state
  85. */
  86. /*@ public normal_behavior
  87. @ ensures \result == (maze[playerRow][playerCol] == EXIT);
  88. @*/
  89. public /*@ pure @*/ boolean won()
  90. {
  91. if (this.maze[this.playerRow][this.playerCol] == EXIT)
  92. return true;
  93. else
  94. return false;
  95. }
  96.  
  97. /**
  98. * A move to position (newRow,newCol) is possible iff. the
  99. * field is inside the maze and free (i.e. not a wall);
  100. * the method does not affect the state
  101. */
  102. /*@ public normal_behavior
  103. @ requires newRow >= 0 && newCol >= 0 &&
  104. @ newRow < maze.length && newCol < maze[newRow].length;
  105. @ ensures \result == (maze[newRow][newCol] != WALL);
  106. @
  107. @ also
  108. @
  109. @ public normal_behavior
  110. @ requires !(newRow >= 0 && newCol >= 0 &&
  111. @ newRow < maze.length && newCol < maze[newRow].length);
  112. @ ensures !\result;
  113. @*/
  114. public /*@ pure @*/ boolean isPossible(int newRow, int newCol)
  115. {
  116. // check if inside maze
  117. return newRow >= 0 && newCol >= 0 && newRow < maze.length && newCol < maze[newRow].length && maze[newRow][newCol] != WALL;
  118. }
  119.  
  120.  
  121. /**
  122. * takes a direction and performs the move if possible, otherwise
  123. * the player stays at the current position; the direction must be
  124. * one of the defined move directions (see constants
  125. * MOVE_xyz). The return value indicates if the move was successful.
  126. */
  127. /*@ public normal_behavior
  128. @ requires direction >= MOVE_UP && direction <= MOVE_RIGHT &&
  129. @ isPossible(playerRow +
  130. @ (direction == MOVE_UP ? -1 : direction == MOVE_DOWN ? 1 : 0),
  131. @ playerCol +
  132. @ (direction == MOVE_LEFT ? -1 : direction == MOVE_RIGHT ? 1 : 0));
  133. @ ensures \result &&
  134. @ (playerRow == \old(playerRow) +
  135. @ (direction == MOVE_UP ? -1 : direction == MOVE_DOWN ? 1 : 0)) &&
  136. @ (playerCol == \old(playerCol) +
  137. @ (direction == MOVE_LEFT ? -1 : direction == MOVE_RIGHT ? 1 : 0));
  138. @ assignable playerRow, playerCol;
  139. @
  140. @ also
  141. @
  142. @ public normal_behavior
  143. @ requires direction >= MOVE_UP && direction <= MOVE_RIGHT &&
  144. @ !isPossible(playerRow +
  145. @ (direction == MOVE_UP ? -1 : direction == MOVE_DOWN ? 1 : 0),
  146. @ playerCol +
  147. @ (direction == MOVE_LEFT ? -1 : direction == MOVE_RIGHT ? 1 : 0));
  148. @ ensures !\result;
  149. @ assignable \nothing;
  150. @
  151. @
  152. @ public exceptional_behavior
  153. @ requires direction < MOVE_UP || direction > MOVE_RIGHT;
  154. @ signals_only IllegalArgumentException;
  155. @
  156. @*/
  157. public boolean move(int direction) {
  158. int newRow = playerRow;
  159. int newCol = playerCol;
  160.  
  161. switch (direction) {
  162. case MOVE_UP:
  163. newRow = newRow - 1;
  164. break;
  165. case MOVE_DOWN:
  166. newRow = newRow + 1;
  167. break;
  168. case MOVE_LEFT:
  169. newCol = newCol - 1;
  170. break;
  171. case MOVE_RIGHT:
  172. newCol = newCol + 1;
  173. break;
  174. default:
  175. // wrong direction
  176. throw new IllegalArgumentException();
  177. }
  178.  
  179. if (isPossible(newRow, newCol)) {
  180. playerRow = newRow;
  181. playerCol = newCol;
  182. return true;
  183. } else {
  184. return false;
  185. }
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement