A java PacMan game application for A.U.TH (data structures class)
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

290 lignes
7.6 KiB

  1. package gr.auth.ee.dsproject.pacman;
  2. import gr.auth.ee.dsproject.node.Node;
  3. /**
  4. * <p>
  5. * Title: DataStructures2011
  6. * </p>
  7. *
  8. * <p>
  9. * Description: Data Structures project: year 2011-2012
  10. * </p>
  11. *
  12. * <p>
  13. * Copyright: Copyright (c) 2011
  14. * </p>
  15. *
  16. * <p>
  17. * Company: A.U.Th.
  18. * </p>
  19. *
  20. * @author Michael T. Tsapanos
  21. * @version 1.0
  22. */
  23. public class Creature implements gr.auth.ee.dsproject.pacman.AbstractCreature
  24. {
  25. public String getName ()
  26. {
  27. return "Mine";
  28. }
  29. private int step = 1;
  30. private boolean amPrey;
  31. public Creature (boolean isPrey)
  32. {
  33. amPrey = isPrey;
  34. }
  35. public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition)
  36. {
  37. // TODO Fill This
  38. return 0;
  39. }
  40. void createSubTreePacman (int depth, Node parent, Room[][] Maze, int[] currPacmanPosition)
  41. {
  42. // TODO Fill This
  43. }
  44. void createSubTreeGhosts (int depth, Node parent, Room[][] Maze, int[][] currGhostsPosition)
  45. {
  46. // TODO Fill This
  47. }
  48. public int[] getPacPos (Room[][] Maze)
  49. {
  50. int[] pacmanPos = new int[2];
  51. for (int i = 0; i < PacmanUtilities.numberOfRows; i++) {
  52. for (int j = 0; j < PacmanUtilities.numberOfColumns; j++) {
  53. if (Maze[i][j].isPacman()) {
  54. pacmanPos[0] = i;
  55. pacmanPos[1] = j;
  56. return pacmanPos;
  57. }
  58. }
  59. }
  60. return pacmanPos;
  61. }
  62. public boolean[] comAvPos (Room[][] Maze, int[][] currentPos, int[] moves, int currentGhost)
  63. {
  64. boolean[] availablePositions = { true, true, true, true };
  65. int[][] newPos = new int[4][2];
  66. for (int i = 0; i < 4; i++) {
  67. if (Maze[currentPos[currentGhost][0]][currentPos[currentGhost][1]].walls[i] == 0) {
  68. availablePositions[i] = false;
  69. continue;
  70. }
  71. if (PacmanUtilities.flagColision(Maze, currentPos[currentGhost], i)) {
  72. availablePositions[i] = false;
  73. }
  74. else if (currentGhost == 0)
  75. continue;
  76. else {
  77. switch (i) {
  78. case Room.WEST:
  79. newPos[currentGhost][0] = currentPos[currentGhost][0];
  80. newPos[currentGhost][1] = currentPos[currentGhost][1] - 1;
  81. break;
  82. case Room.SOUTH:
  83. newPos[currentGhost][0] = currentPos[currentGhost][0] + 1;
  84. newPos[currentGhost][1] = currentPos[currentGhost][1];
  85. break;
  86. case Room.EAST:
  87. newPos[currentGhost][0] = currentPos[currentGhost][0];
  88. newPos[currentGhost][1] = currentPos[currentGhost][1] + 1;
  89. break;
  90. case Room.NORTH:
  91. newPos[currentGhost][0] = currentPos[currentGhost][0] - 1;
  92. newPos[currentGhost][1] = currentPos[currentGhost][1];
  93. }
  94. for (int j = (currentGhost - 1); j > -1; j--) {
  95. switch (moves[j]) {
  96. case Room.WEST:
  97. newPos[j][0] = currentPos[j][0];
  98. newPos[j][1] = currentPos[j][1] - 1;
  99. break;
  100. case Room.SOUTH:
  101. newPos[j][0] = currentPos[j][0] + 1;
  102. newPos[j][1] = currentPos[j][1];
  103. break;
  104. case Room.EAST:
  105. newPos[j][0] = currentPos[j][0];
  106. newPos[j][1] = currentPos[j][1] + 1;
  107. break;
  108. case Room.NORTH:
  109. newPos[j][0] = currentPos[j][0] - 1;
  110. newPos[j][1] = currentPos[j][1];
  111. // break;
  112. }
  113. if ((newPos[currentGhost][0] == newPos[j][0]) && (newPos[currentGhost][1] == newPos[j][1])) {
  114. availablePositions[i] = false;
  115. continue;
  116. }
  117. if ((newPos[currentGhost][0] == currentPos[j][0]) && (newPos[currentGhost][1] == currentPos[j][1]) && (newPos[j][0] == currentPos[currentGhost][0])
  118. && (newPos[j][1] == currentPos[currentGhost][1])) {
  119. availablePositions[i] = false;
  120. }
  121. }
  122. }
  123. }
  124. return availablePositions;
  125. }
  126. public int comBestPos (boolean[] availablePositions, int[] pacmanPosition, int[] currentPos)
  127. {
  128. int[] newVerticalDifference = new int[2];
  129. for (int i = 0; i < 2; i++)
  130. newVerticalDifference[i] = currentPos[i] - pacmanPosition[i];
  131. int[] distanceSquared = new int[4];
  132. for (int i = 0; i < 4; i++) {
  133. if (availablePositions[i] == true) {
  134. switch (i) {
  135. case Room.WEST:
  136. newVerticalDifference[1]--;
  137. break;
  138. case Room.SOUTH:
  139. newVerticalDifference[0]++;
  140. break;
  141. case Room.EAST:
  142. newVerticalDifference[1]++;
  143. break;
  144. case Room.NORTH:
  145. newVerticalDifference[0]--;
  146. break;
  147. }
  148. distanceSquared[i] = newVerticalDifference[0] * newVerticalDifference[0] + newVerticalDifference[1] * newVerticalDifference[1];
  149. } else
  150. distanceSquared[i] = PacmanUtilities.numberOfRows * PacmanUtilities.numberOfRows + PacmanUtilities.numberOfColumns * PacmanUtilities.numberOfColumns + 1;
  151. }
  152. int minDistance = distanceSquared[0];
  153. int minPosition = 0;
  154. for (int i = 1; i < 4; i++) {
  155. if (minDistance > distanceSquared[i]) {
  156. minDistance = distanceSquared[i];
  157. minPosition = i;
  158. }
  159. }
  160. return minPosition;
  161. }
  162. public int[] calculateNextGhostPosition (Room[][] Maze, int[][] currentPos)
  163. {
  164. int[] moves = new int[PacmanUtilities.numberOfGhosts];
  165. int[] pacmanPosition = new int[2];
  166. pacmanPosition = getPacPos(Maze);
  167. for (int i = 0; i < PacmanUtilities.numberOfGhosts; i++) {
  168. moves[i] = comBestPos(comAvPos(Maze, currentPos, moves, i), pacmanPosition, currentPos[i]);
  169. }
  170. return moves;
  171. }
  172. public boolean[] checkCollision (int[] moves, int[][] currentPos)
  173. {
  174. boolean[] collision = new boolean[PacmanUtilities.numberOfGhosts];
  175. int[][] newPos = new int[4][2];
  176. for (int i = 0; i < moves.length; i++) {
  177. if (moves[i] == 0) {
  178. if (currentPos[i][1] > 0) {
  179. newPos[i][0] = currentPos[i][0];
  180. newPos[i][1] = currentPos[i][1] - 1;
  181. } else {
  182. newPos[i][0] = currentPos[i][0];
  183. newPos[i][1] = PacmanUtilities.numberOfColumns - 1;
  184. }
  185. } else if (moves[i] == 1) {
  186. if (currentPos[i][0] < PacmanUtilities.numberOfRows - 1) {
  187. newPos[i][0] = currentPos[i][0] + 1;
  188. newPos[i][1] = currentPos[i][1];
  189. } else {
  190. newPos[i][0] = 0;
  191. newPos[i][1] = currentPos[i][1];
  192. }
  193. } else if (moves[i] == 2) {
  194. if (currentPos[i][1] < PacmanUtilities.numberOfColumns - 1) {
  195. newPos[i][0] = currentPos[i][0];
  196. newPos[i][1] = currentPos[i][1] + 1;
  197. } else {
  198. newPos[i][0] = currentPos[i][0];
  199. newPos[i][1] = 0;
  200. }
  201. } else {
  202. if (currentPos[i][0] > 0) {
  203. newPos[i][0] = currentPos[i][0] - 1;
  204. newPos[i][1] = currentPos[i][1];
  205. } else {
  206. newPos[i][0] = PacmanUtilities.numberOfRows - 1;
  207. newPos[i][1] = currentPos[i][1];
  208. }
  209. }
  210. collision[i] = false;
  211. }
  212. for (int k = 0; k < moves.length; k++) {
  213. }
  214. for (int i = 0; i < moves.length; i++) {
  215. for (int j = i + 1; j < moves.length; j++) {
  216. if (newPos[i][0] == newPos[j][0] && newPos[i][1] == newPos[j][1]) {
  217. collision[j] = true;
  218. }
  219. if (newPos[i][0] == currentPos[j][0] && newPos[i][1] == currentPos[j][1] && newPos[j][0] == currentPos[i][0] && newPos[j][1] == currentPos[i][1]) {
  220. collision[j] = true;
  221. }
  222. }
  223. }
  224. return collision;
  225. }
  226. }