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.

327 lines
8.8 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. 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 mv;
  47. double ev, max = Globals.NO_EVAL;
  48. int decision = -1;
  49. ArrayList<Node89978445> moves = new ArrayList<Node89978445> ();
  50. // loop the possible moves and fill them to list
  51. for (int i=0 ; i<4 ; ++i) {
  52. mv = new Node89978445 (Maze, currPosition[0], currPosition[1], i);
  53. if (mv.getEvaluation() > Globals.NO_EVAL)
  54. moves.add (mv);
  55. /*
  56. * how can i rant for not to have a "stay still" move?
  57. */
  58. }
  59. // Find the best of the moves in list
  60. for (int i=0 ; i<moves.size() ; ++i) {
  61. if ((ev = moves.get(i).getEvaluation()) >= max) {
  62. max = ev;
  63. decision = i;
  64. }
  65. }
  66. return moves.get (decision).getMove();
  67. /*
  68. * The stock version of function:
  69. *
  70. * int moveToReturn = (int) (4 * Math.random());
  71. * return moveToReturn;
  72. */
  73. }
  74. void createSubTreePacman (int depth, Node89978445 parent, Room[][] Maze, int[] currPacmanPosition)
  75. {
  76. // TODO Fill This
  77. }
  78. void createSubTreeGhosts (int depth, Node89978445 parent, Room[][] Maze, int[][] currGhostsPosition)
  79. {
  80. // TODO Fill This
  81. }
  82. public int[] getPacPos (Room[][] Maze)
  83. {
  84. int[] pacmanPos = new int[2];
  85. for (int i = 0; i < PacmanUtilities.numberOfRows; i++) {
  86. for (int j = 0; j < PacmanUtilities.numberOfColumns; j++) {
  87. if (Maze[i][j].isPacman()) {
  88. pacmanPos[0] = i;
  89. pacmanPos[1] = j;
  90. return pacmanPos;
  91. }
  92. }
  93. }
  94. return pacmanPos;
  95. }
  96. public boolean[] comAvPos (Room[][] Maze, int[][] currentPos, int[] moves, int currentGhost)
  97. {
  98. boolean[] availablePositions = { true, true, true, true };
  99. int[][] newPos = new int[4][2];
  100. for (int i = 0; i < 4; i++) {
  101. if (Maze[currentPos[currentGhost][0]][currentPos[currentGhost][1]].walls[i] == 0) {
  102. availablePositions[i] = false;
  103. continue;
  104. }
  105. if (PacmanUtilities.flagColision(Maze, currentPos[currentGhost], i)) {
  106. availablePositions[i] = false;
  107. }
  108. else if (currentGhost == 0)
  109. continue;
  110. else {
  111. switch (i) {
  112. case Room.WEST:
  113. newPos[currentGhost][0] = currentPos[currentGhost][0];
  114. newPos[currentGhost][1] = currentPos[currentGhost][1] - 1;
  115. break;
  116. case Room.SOUTH:
  117. newPos[currentGhost][0] = currentPos[currentGhost][0] + 1;
  118. newPos[currentGhost][1] = currentPos[currentGhost][1];
  119. break;
  120. case Room.EAST:
  121. newPos[currentGhost][0] = currentPos[currentGhost][0];
  122. newPos[currentGhost][1] = currentPos[currentGhost][1] + 1;
  123. break;
  124. case Room.NORTH:
  125. newPos[currentGhost][0] = currentPos[currentGhost][0] - 1;
  126. newPos[currentGhost][1] = currentPos[currentGhost][1];
  127. }
  128. for (int j = (currentGhost - 1); j > -1; j--) {
  129. switch (moves[j]) {
  130. case Room.WEST:
  131. newPos[j][0] = currentPos[j][0];
  132. newPos[j][1] = currentPos[j][1] - 1;
  133. break;
  134. case Room.SOUTH:
  135. newPos[j][0] = currentPos[j][0] + 1;
  136. newPos[j][1] = currentPos[j][1];
  137. break;
  138. case Room.EAST:
  139. newPos[j][0] = currentPos[j][0];
  140. newPos[j][1] = currentPos[j][1] + 1;
  141. break;
  142. case Room.NORTH:
  143. newPos[j][0] = currentPos[j][0] - 1;
  144. newPos[j][1] = currentPos[j][1];
  145. // break;
  146. }
  147. if ((newPos[currentGhost][0] == newPos[j][0]) && (newPos[currentGhost][1] == newPos[j][1])) {
  148. availablePositions[i] = false;
  149. continue;
  150. }
  151. if ((newPos[currentGhost][0] == currentPos[j][0]) && (newPos[currentGhost][1] == currentPos[j][1]) && (newPos[j][0] == currentPos[currentGhost][0])
  152. && (newPos[j][1] == currentPos[currentGhost][1])) {
  153. availablePositions[i] = false;
  154. }
  155. }
  156. }
  157. }
  158. return availablePositions;
  159. }
  160. public int comBestPos (boolean[] availablePositions, int[] pacmanPosition, int[] currentPos)
  161. {
  162. int[] newVerticalDifference = new int[2];
  163. for (int i = 0; i < 2; i++)
  164. newVerticalDifference[i] = currentPos[i] - pacmanPosition[i];
  165. int[] distanceSquared = new int[4];
  166. for (int i = 0; i < 4; i++) {
  167. if (availablePositions[i] == true) {
  168. switch (i) {
  169. case Room.WEST:
  170. newVerticalDifference[1]--;
  171. break;
  172. case Room.SOUTH:
  173. newVerticalDifference[0]++;
  174. break;
  175. case Room.EAST:
  176. newVerticalDifference[1]++;
  177. break;
  178. case Room.NORTH:
  179. newVerticalDifference[0]--;
  180. break;
  181. }
  182. distanceSquared[i] = newVerticalDifference[0] * newVerticalDifference[0] + newVerticalDifference[1] * newVerticalDifference[1];
  183. } else
  184. distanceSquared[i] = PacmanUtilities.numberOfRows * PacmanUtilities.numberOfRows + PacmanUtilities.numberOfColumns * PacmanUtilities.numberOfColumns + 1;
  185. }
  186. int minDistance = distanceSquared[0];
  187. int minPosition = 0;
  188. for (int i = 1; i < 4; i++) {
  189. if (minDistance > distanceSquared[i]) {
  190. minDistance = distanceSquared[i];
  191. minPosition = i;
  192. }
  193. }
  194. return minPosition;
  195. }
  196. public int[] calculateNextGhostPosition (Room[][] Maze, int[][] currentPos)
  197. {
  198. int[] moves = new int[PacmanUtilities.numberOfGhosts];
  199. int[] pacmanPosition = new int[2];
  200. pacmanPosition = getPacPos(Maze);
  201. for (int i = 0; i < PacmanUtilities.numberOfGhosts; i++) {
  202. moves[i] = comBestPos(comAvPos(Maze, currentPos, moves, i), pacmanPosition, currentPos[i]);
  203. }
  204. return moves;
  205. }
  206. public boolean[] checkCollision (int[] moves, int[][] currentPos)
  207. {
  208. boolean[] collision = new boolean[PacmanUtilities.numberOfGhosts];
  209. int[][] newPos = new int[4][2];
  210. for (int i = 0; i < moves.length; i++) {
  211. if (moves[i] == 0) {
  212. if (currentPos[i][1] > 0) {
  213. newPos[i][0] = currentPos[i][0];
  214. newPos[i][1] = currentPos[i][1] - 1;
  215. } else {
  216. newPos[i][0] = currentPos[i][0];
  217. newPos[i][1] = PacmanUtilities.numberOfColumns - 1;
  218. }
  219. } else if (moves[i] == 1) {
  220. if (currentPos[i][0] < PacmanUtilities.numberOfRows - 1) {
  221. newPos[i][0] = currentPos[i][0] + 1;
  222. newPos[i][1] = currentPos[i][1];
  223. } else {
  224. newPos[i][0] = 0;
  225. newPos[i][1] = currentPos[i][1];
  226. }
  227. } else if (moves[i] == 2) {
  228. if (currentPos[i][1] < PacmanUtilities.numberOfColumns - 1) {
  229. newPos[i][0] = currentPos[i][0];
  230. newPos[i][1] = currentPos[i][1] + 1;
  231. } else {
  232. newPos[i][0] = currentPos[i][0];
  233. newPos[i][1] = 0;
  234. }
  235. } else {
  236. if (currentPos[i][0] > 0) {
  237. newPos[i][0] = currentPos[i][0] - 1;
  238. newPos[i][1] = currentPos[i][1];
  239. } else {
  240. newPos[i][0] = PacmanUtilities.numberOfRows - 1;
  241. newPos[i][1] = currentPos[i][1];
  242. }
  243. }
  244. collision[i] = false;
  245. }
  246. for (int k = 0; k < moves.length; k++) {
  247. }
  248. for (int i = 0; i < moves.length; i++) {
  249. for (int j = i + 1; j < moves.length; j++) {
  250. if (newPos[i][0] == newPos[j][0] && newPos[i][1] == newPos[j][1]) {
  251. collision[j] = true;
  252. }
  253. 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]) {
  254. collision[j] = true;
  255. }
  256. }
  257. }
  258. return collision;
  259. }
  260. }