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.

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