Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. package catchBox;
  2.  
  3. import java.awt.*;
  4. import java.util.Arrays;
  5. import java.util.ArrayList;
  6. import agentSearch.State;
  7. import agentSearch.Action;
  8.  
  9. public class CatchState extends State implements Cloneable {
  10.  
  11. private final ArrayList<EnvironmentListener> listeners;
  12. private int posLine, posColumn;
  13. private int stepCount;
  14. protected int[][] matrix;
  15.  
  16. public CatchState(int[][] matrix) {
  17.  
  18. this.listeners = new ArrayList<>();
  19.  
  20. this.posLine = -1;
  21. this.posColumn = -1;
  22. this.stepCount = 0;
  23.  
  24. this.matrix = new int[matrix.length][matrix.length];
  25.  
  26. for (int i = 0; i < matrix.length; i++) {
  27.  
  28. for (int j = 0; j < matrix.length; j++) {
  29.  
  30. this.matrix[i][j] = matrix[i][j];
  31.  
  32. if (matrix[i][j] == Properties.CATCH) {
  33.  
  34. posLine = i;
  35. posColumn = j;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. public void executeAction(Action action) {
  42.  
  43. action.execute(this);
  44.  
  45. this.stepCount++;
  46.  
  47. fireUpdatedEnvironment();
  48. }
  49.  
  50. public boolean canMoveUp() {
  51.  
  52. return this.posLine != 0;
  53. }
  54.  
  55. public boolean canMoveRight() {
  56.  
  57. return this.posColumn != this.matrix.length - 1;
  58. }
  59.  
  60. public boolean canMoveDown() {
  61. System.out.println(this.posLine != this.matrix.length - 1);
  62.  
  63. return this.posLine != this.matrix.length - 1;
  64. }
  65.  
  66. public boolean canMoveLeft() {
  67.  
  68. return this.posColumn != 0;
  69. }
  70.  
  71. public void moveUp() {
  72. this.matrix[posLine][posColumn] = Properties.EMPTY;
  73. this.matrix[--posLine][posColumn] = Properties.CATCH;
  74. }
  75.  
  76. public void moveRight() {
  77.  
  78. this.matrix[posLine][posColumn] = Properties.EMPTY;
  79. this.matrix[posLine][++posColumn] = Properties.CATCH;
  80. }
  81.  
  82. public void moveDown() {
  83.  
  84. this.matrix[posLine][posColumn] = Properties.EMPTY;
  85. this.matrix[++posLine][posColumn] = Properties.CATCH;
  86. }
  87.  
  88. public void moveLeft() {
  89.  
  90. this.matrix[posLine][posColumn] = Properties.EMPTY;
  91. this.matrix[posLine][--posColumn] = Properties.CATCH;
  92. }
  93.  
  94. public int getNumBox() {
  95.  
  96. int num = 0;
  97.  
  98. for (int i = 0; i < matrix.length; i++) {
  99.  
  100. for (int j = 0; j < matrix.length; j++) {
  101.  
  102. if (this.matrix[i][j] == Properties.BOX) {
  103.  
  104. num++;
  105. }
  106. }
  107. }
  108.  
  109. return num;
  110. }
  111.  
  112. public void setCellCatch(int line, int column) {
  113.  
  114. this.posLine = line;
  115. this.posColumn = column;
  116. this.matrix[line][column] = Properties.CATCH;
  117. }
  118.  
  119. public int[][] getMatrix() {
  120.  
  121. return matrix;
  122. }
  123.  
  124. public void setGoal(int line, int column) {
  125.  
  126. this.matrix[line][column] = Properties.DOOR;
  127. }
  128.  
  129. public int getSteps() {
  130.  
  131. return this.stepCount;
  132. }
  133.  
  134. public int getSize() {
  135.  
  136. return matrix.length;
  137. }
  138.  
  139. public Color getCellColor(int line, int column) {
  140.  
  141. switch (matrix[line][column]) {
  142.  
  143. case Properties.BOX:
  144. return Properties.COLORBOX;
  145.  
  146. case Properties.CATCH:
  147. return Properties.COLORCATCH;
  148.  
  149. case Properties.DOOR:
  150. return Properties.COLORDOOR;
  151.  
  152. case Properties.WALL:
  153. return Properties.COLORWALL;
  154.  
  155. default:
  156. return Properties.COLOREMPTY;
  157. }
  158. }
  159.  
  160. @Override
  161. public boolean equals(Object other) {
  162.  
  163. if (!(other instanceof CatchState)) {
  164.  
  165. return false;
  166. }
  167.  
  168. CatchState o = (CatchState) other;
  169.  
  170. if (matrix.length != o.matrix.length) {
  171.  
  172. return false;
  173. }
  174.  
  175. return Arrays.deepEquals(matrix, o.matrix);
  176. }
  177.  
  178. @Override
  179. public int hashCode() {
  180.  
  181. return 97 * 7 + Arrays.deepHashCode(this.matrix);
  182. }
  183.  
  184. @Override
  185. public String toString() {
  186.  
  187. StringBuilder buffer = new StringBuilder();
  188. buffer.append(matrix.length);
  189.  
  190. for (int i = 0; i < matrix.length; i++) {
  191.  
  192. buffer.append('\n');
  193.  
  194. for (int j = 0; j < matrix.length; j++) {
  195.  
  196. buffer.append(matrix[i][j]);
  197. buffer.append(' ');
  198. }
  199. }
  200.  
  201. return buffer.toString();
  202. }
  203.  
  204. @Override
  205. public CatchState clone() {
  206.  
  207. return new CatchState(this.matrix);
  208. }
  209.  
  210. public synchronized void addEnvironmentListener(EnvironmentListener l) {
  211.  
  212. if (!listeners.contains(l)) {
  213.  
  214. listeners.add(l);
  215. }
  216. }
  217.  
  218. public synchronized void removeEnvironmentListener(EnvironmentListener l) {
  219.  
  220. listeners.remove(l);
  221. }
  222.  
  223. public void fireUpdatedEnvironment() {
  224.  
  225. for (EnvironmentListener listener : listeners) {
  226.  
  227. listener.environmentUpdated();
  228. }
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement