A java PacMan game application for A.U.TH (data structures class)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
8.7 KiB

  1. package gr.auth.ee.dsproject.pacman;
  2. import java.util.ArrayList;
  3. import gr.auth.ee.dsproject.node.Globals;
  4. import gr.auth.ee.dsproject.node.Node89978445;
  5. /**
  6. * <p>
  7. * Title: DataStructures2011
  8. * </p>
  9. *
  10. * <p>
  11. * Description: Data Structures project: year 2011-2012
  12. * </p>
  13. *
  14. * <p>
  15. * Copyright: Copyright (c) 2011
  16. * </p>
  17. *
  18. * <p>
  19. * Company: A.U.Th.
  20. * </p>
  21. *
  22. * @author Michael T. Tsapanos
  23. * @version 1.0
  24. */
  25. public class Creature implements gr.auth.ee.dsproject.pacman.AbstractCreature
  26. {
  27. public String getName ()
  28. {
  29. return "Mine";
  30. }
  31. private int step = 1;
  32. private boolean amPrey;
  33. public Creature (boolean isPrey)
  34. {
  35. amPrey = isPrey;
  36. }
  37. /**
  38. * @brief
  39. * Create node for each one of the possible moves (0, 1, 2, 3) and
  40. * evaluate these moves. After that feeds the valid ones to an ArrayList
  41. * and select the best of them to return
  42. */
  43. public int calculateNextPacmanPosition (Room[][] Maze, int[] currPosition)
  44. {
  45. Node89978445 mv;
  46. double ev, max = Globals.NO_EVAL;
  47. int decision = -1;
  48. ArrayList<Node89978445> moves = new ArrayList<Node89978445> ();
  49. // loop the possible moves and fill them to list
  50. for (int i=0 ; i<4 ; ++i) {
  51. mv = new Node89978445 (Maze, currPosition, i);
  52. if (mv.getEvaluation() > Globals.NO_EVAL)
  53. moves.add (mv);
  54. /*
  55. * how can i rant for not to have a "stay still" move?
  56. */
  57. }
  58. // Find the best of the moves in list
  59. for (int i=0 ; i<moves.size() ; ++i) {
  60. if ((ev = moves.get(i).getEvaluation()) >= max) {
  61. max = ev;
  62. decision = i;
  63. }
  64. }
  65. return moves.get (decision).getMove();
  66. /*
  67. * The stock version of function:
  68. *
  69. * int moveToReturn = (int) (4 * Math.random());
  70. * return moveToReturn;
  71. */
  72. }
  73. void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] currPacmanPosition)
  74. {
  75. // TODO Fill This
  76. }
  77. void createSubTreeGhosts (int depth, Node89978445 parent, Room[][] Maze, int[][] currGhostsPosition)
  78. {
  79. // TODO Fill This
  80. }
  81. public int[] getPacPos (Room[][] Maze)
  82. {
  83. int[] pacmanPos = new int[2];
  84. for (int i = 0; i < PacmanUtilities.numberOfRows; i++) {
  85. for (int j = 0; j < PacmanUtilities.numberOfColumns; j++) {
  86. if (Maze[i][j].isPacman()) {
  87. pacmanPos[0] = i;
  88. pacmanPos[1] = j;
  89. return pacmanPos;
  90. }
  91. }
  92. }
  93. return pacmanPos;
  94. }
  95. public boolean[] comAvPos (Room[][] Maze, int[][] currentPos, int[] moves, int currentGhost)
  96. {
  97. boolean[] availablePositions = { true, true, true, true };
  98. int[][] newPos = new int[4][2];
  99. for (int i = 0; i < 4; i++) {
  100. if (Maze[currentPos[currentGhost][0]][currentPos[currentGhost][1]].walls[i] == 0) {
  101. availablePositions[i] = false;
  102. continue;
  103. }
  104. if (PacmanUtilities.flagColision(Maze, currentPos[currentGhost], i)) {
  105. availablePositions[i] = false;
  106. }
  107. else if (currentGhost == 0)
  108. continue;
  109. else {
  110. switch (i) {
  111. case Room.WEST:
  112. newPos[currentGhost][0] = currentPos[currentGhost][0];
  113. newPos[currentGhost][1] = currentPos[currentGhost][1] - 1;
  114. break;
  115. case Room.SOUTH:
  116. newPos[currentGhost][0] = currentPos[currentGhost][0] + 1;
  117. newPos[currentGhost][1] = currentPos[currentGhost][1];
  118. break;
  119. case Room.EAST:
  120. newPos[currentGhost][0] = currentPos[currentGhost][0];
  121. newPos[currentGhost][1] = currentPos[currentGhost][1] + 1;
  122. break;
  123. case Room.NORTH:
  124. newPos[currentGhost][0] = currentPos[currentGhost][0] - 1;
  125. newPos[currentGhost][1] = currentPos[currentGhost][1];
  126. }
  127. for (int j = (currentGhost - 1); j > -1; j--) {
  128. switch (moves[j]) {
  129. case Room.WEST:
  130. newPos[j][0] = currentPos[j][0];
  131. newPos[j][1] = currentPos[j][1] - 1;
  132. break;
  133. case Room.SOUTH:
  134. newPos[j][0] = currentPos[j][0] + 1;
  135. newPos[j][1] = currentPos[j][1];
  136. break;
  137. case Room.EAST:
  138. newPos[j][0] = currentPos[j][0];
  139. newPos[j][1] = currentPos[j][1] + 1;
  140. break;
  141. case Room.NORTH:
  142. newPos[j][0] = currentPos[j][0] - 1;
  143. newPos[j][1] = currentPos[j][1];
  144. // break;
  145. }
  146. if ((newPos[currentGhost][0] == newPos[j][0]) && (newPos[currentGhost][1] == newPos[j][1])) {
  147. availablePositions[i] = false;
  148. continue;
  149. }
  150. if ((newPos[currentGhost][0] == currentPos[j][0]) && (newPos[currentGhost][1] == currentPos[j][1]) && (newPos[j][0] == currentPos[currentGhost][0])
  151. && (newPos[j][1] == currentPos[currentGhost][1])) {
  152. availablePositions[i] = false;
  153. }
  154. }
  155. }
  156. }
  157. return availablePositions;
  158. }
  159. public int comBestPos (boolean[] availablePositions, int[] pacmanPosition, int[] currentPos)
  160. {
  161. int[] newVerticalDifference = new int[2];
  162. for (int i = 0; i < 2; i++)
  163. newVerticalDifference[i] = currentPos[i] - pacmanPosition[i];
  164. int[] distanceSquared = new int[4];
  165. for (int i = 0; i < 4; i++) {
  166. if (availablePositions[i] == true) {
  167. switch (i) {
  168. case Room.WEST:
  169. newVerticalDifference[1]--;
  170. break;
  171. case Room.SOUTH:
  172. newVerticalDifference[0]++;
  173. break;
  174. case Room.EAST:
  175. newVerticalDifference[1]++;
  176. break;
  177. case Room.NORTH:
  178. newVerticalDifference[0]--;
  179. break;
  180. }
  181. distanceSquared[i] = newVerticalDifference[0] * newVerticalDifference[0] + newVerticalDifference[1] * newVerticalDifference[1];
  182. } else
  183. distanceSquared[i] = PacmanUtilities.numberOfRows * PacmanUtilities.numberOfRows + PacmanUtilities.numberOfColumns * PacmanUtilities.numberOfColumns + 1;
  184. }
  185. int minDistance = distanceSquared[0];
  186. int minPosition = 0;
  187. for (int i = 1; i < 4; i++) {
  188. if (minDistance > distanceSquared[i]) {
  189. minDistance = distanceSquared[i];
  190. minPosition = i;
  191. }
  192. }
  193. return minPosition;
  194. }
  195. public int[] calculateNextGhostPosition (Room[][] Maze, int[][] currentPos)
  196. {
  197. int[] moves = new int[PacmanUtilities.numberOfGhosts];
  198. int[] pacmanPosition = new int[2];
  199. pacmanPosition = getPacPos(Maze);
  200. for (int i = 0; i < PacmanUtilities.numberOfGhosts; i++) {
  201. moves[i] = comBestPos(comAvPos(Maze, currentPos, moves, i), pacmanPosition, currentPos[i]);
  202. }
  203. return moves;
  204. }
  205. public boolean[] checkCollision (int[] moves, int[][] currentPos)
  206. {
  207. boolean[] collision = new boolean[PacmanUtilities.numberOfGhosts];
  208. int[][] newPos = new int[4][2];
  209. for (int i = 0; i < moves.length; i++) {
  210. if (moves[i] == 0) {
  211. if (currentPos[i][1] > 0) {
  212. newPos[i][0] = currentPos[i][0];
  213. newPos[i][1] = currentPos[i][1] - 1;
  214. } else {
  215. newPos[i][0] = currentPos[i][0];
  216. newPos[i][1] = PacmanUtilities.numberOfColumns - 1;
  217. }
  218. } else if (moves[i] == 1) {
  219. if (currentPos[i][0] < PacmanUtilities.numberOfRows - 1) {
  220. newPos[i][0] = currentPos[i][0] + 1;
  221. newPos[i][1] = currentPos[i][1];
  222. } else {
  223. newPos[i][0] = 0;
  224. newPos[i][1] = currentPos[i][1];
  225. }
  226. } else if (moves[i] == 2) {
  227. if (currentPos[i][1] < PacmanUtilities.numberOfColumns - 1) {
  228. newPos[i][0] = currentPos[i][0];
  229. newPos[i][1] = currentPos[i][1] + 1;
  230. } else {
  231. newPos[i][0] = currentPos[i][0];
  232. newPos[i][1] = 0;
  233. }
  234. } else {
  235. if (currentPos[i][0] > 0) {
  236. newPos[i][0] = currentPos[i][0] - 1;
  237. newPos[i][1] = currentPos[i][1];
  238. } else {
  239. newPos[i][0] = PacmanUtilities.numberOfRows - 1;
  240. newPos[i][1] = currentPos[i][1];
  241. }
  242. }
  243. collision[i] = false;
  244. }
  245. for (int k = 0; k < moves.length; k++) {
  246. }
  247. for (int i = 0; i < moves.length; i++) {
  248. for (int j = i + 1; j < moves.length; j++) {
  249. if (newPos[i][0] == newPos[j][0] && newPos[i][1] == newPos[j][1]) {
  250. collision[j] = true;
  251. }
  252. 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]) {
  253. collision[j] = true;
  254. }
  255. }
  256. }
  257. return collision;
  258. }
  259. }