Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.46 KB | None | 0 0
  1. package game.pm.strategies;
  2.  
  3. /*
  4. Mestint beadando
  5. */
  6.  
  7. import game.engine.Engine;
  8. import game.engine.utils.Pair;
  9. import game.pm.Ghost;
  10. import game.pm.PMAction;
  11. import game.pm.PMGame;
  12. import game.pm.PMPlayer;
  13. import game.pm.ui.GameGraphicsController;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.LinkedList;
  17. import java.util.Queue;
  18. import java.util.Random;
  19.  
  20.  
  21. public class Agent extends Strategy {
  22. public static boolean isValidCoord(int row,int col){return row>=0&&col>=0&&row<31&&col<28;}
  23. public static boolean isIt(int bitField, int CODE){
  24. return 0 < (bitField&CODE);
  25. }
  26.  
  27. //lehetséges:
  28. // 1,0 -1,0 , 0,1, , 0,-1
  29. //Bejáráshoz
  30. int[] A = {1,0};
  31. int[] B = {-1,0};
  32. int[] C = {0,1};
  33. int[] D = {0,-1};
  34.  
  35. int[][][] BFSType = {
  36. {C,B,A,D},
  37. { B,C,A,D},
  38.  
  39. { C,A,B,D},
  40. { C,A,D,B},
  41. { C,B,A,D},
  42. { B,C,A,D},
  43.  
  44. { C,B,D,A},
  45. { C,D,A,B},
  46. { C,D,B,A},
  47.  
  48. { B,A,C,D},
  49. { B,A,D,C},
  50.  
  51.  
  52. { B,C,D,A},
  53. { B,D,B,C},
  54. { B,D,C,A},
  55. };
  56.  
  57.  
  58. //Eredmény
  59. static class Result{
  60. public Result(int score, int live, PMGame game){
  61. this.score = score;
  62. this.live = live;
  63. this.game = game;
  64. }
  65. int score;
  66. int live;
  67. PMGame game;
  68. };
  69.  
  70.  
  71. int counter = 0;
  72. int[] scores = new int[10];
  73. public Agent(){
  74. }
  75.  
  76. public static double hamiltonDistanceBetweenPlayer(PMPlayer a, PMPlayer b){
  77. int posy = a.getPixelI();
  78. int posx = a.getPixelJ();
  79.  
  80. return Math.sqrt(Math.abs(posx - b.getPixelJ()) + Math.abs(posy - b.getPixelI()));
  81. }
  82.  
  83. public Pair<Integer,Integer> findClosestFoodPosition(PMGame game,Pair<Integer,Integer> deniedCoord, int typeOfBFS){
  84. PMPlayer pm = game.pacmans[0];
  85.  
  86. //olyan lépés amivel a lehető legközelebb kerülök a kajához
  87. //szélességi keresés.
  88. Pair<Integer,Integer> currentpos;
  89. currentpos = new Pair<Integer,Integer>(pm.getTileI(), pm.getTileJ());
  90.  
  91. boolean[][] visited = new boolean[50][50];
  92.  
  93. Queue<Pair<Integer,Integer>> q = new LinkedList<Pair<Integer,Integer>>();
  94. q.add(currentpos);
  95.  
  96. Pair<Integer,Integer> targetpos = new Pair<>(currentpos.first,currentpos.second);
  97.  
  98. while(!q.isEmpty()){
  99. Pair<Integer,Integer> node = q.remove();
  100.  
  101. if(!isValidCoord(node.first,node.second)){
  102. continue;
  103. }
  104.  
  105. if(visited[node.first][node.second]){
  106. continue;
  107. }else{
  108. visited[node.first][node.second] = true;
  109. }
  110.  
  111. if(!PMGame.allowedTile(game.maze[node.first][node.second])){
  112. continue;
  113. }
  114.  
  115.  
  116. if(isIt(game.maze[node.first][node.second],PMGame.TILES.FOOD)){
  117. if(!deniedCoord.first.equals(node.first) && !deniedCoord.second.equals(node.second)){
  118. targetpos = node;
  119. break;
  120. }
  121. }
  122.  
  123. for(int i =0;i<4;i++){
  124. q.add(new Pair<Integer,Integer>(
  125. node.first+this.BFSType[typeOfBFS][i][0],node.second+this.BFSType[typeOfBFS][i][1]
  126. ));
  127. }
  128. }
  129.  
  130. return targetpos;
  131. }
  132.  
  133.  
  134. static class threadResult{
  135. public int bestScore = 0;
  136. public int[][] bestConfig = new int[10][4];
  137. }
  138.  
  139. /*
  140. Legjobb paraméterek megkeresése, precalc
  141. */
  142. public int getBestScoreOnPos(PMGame game){
  143. PMPlayer pm = game.pacmans[0];
  144.  
  145. final threadResult[] results = new threadResult[16];
  146.  
  147. for(int i =0;i<16;i++){
  148. results[i] = new threadResult();
  149. }
  150.  
  151.  
  152. Thread[] th = new Thread[16];
  153.  
  154. final int[] totalBestScore = {0};
  155. final int[][] totalBestConfig = new int[10][4];
  156.  
  157.  
  158. for(int f =0;f<16;f++){
  159. int finalF = f;
  160. th[f] = new Thread(() -> {
  161.  
  162. for(int i =0;i<100000000;i++){
  163. int[][] currentConfig = new int[10][4];
  164.  
  165. PMGame currentGame = game.clone();
  166.  
  167. for(int j=0;j<7;j++){
  168. int tillfright = Math.abs(new Random().nextInt() % 30) + 2;
  169. int ghostDist = Math.abs(new Random().nextInt() % 30) + 4;
  170. int willidiechecker = Math.abs(new Random().nextInt() % 17) + 2;
  171. int bfstype = Math.abs(new Random().nextInt()) % 10;
  172.  
  173. currentConfig[j][0] = tillfright;//tillfright;
  174. currentConfig[j][1] = ghostDist;//ghostDist;
  175. currentConfig[j][2] = willidiechecker;//willidiechecker;
  176.  
  177. currentConfig[j][3] = bfstype;
  178.  
  179.  
  180. Result ret = eval(currentGame,tillfright,ghostDist,willidiechecker,bfstype);//tillfright,ghostDist,willidiechecker);
  181.  
  182.  
  183. if(ret.live == 0 || ret.game.isFinished()){
  184. if(currentGame.score>results[finalF].bestScore){
  185. if(totalBestScore[0] < currentGame.score){
  186. totalBestScore[0] = currentGame.score;
  187.  
  188. System.out.println("Új total score: " + currentGame.score);
  189. results[finalF].bestScore = currentGame.score;
  190. results[finalF].bestConfig = currentConfig;
  191.  
  192. System.out.println("BFSType: " + bfstype);
  193. for(int c =0;c<7;c++){
  194. totalBestConfig[c][0] = currentConfig[c][0];
  195. totalBestConfig[c][1] = currentConfig[c][1];
  196. totalBestConfig[c][2] = currentConfig[c][2];
  197. totalBestConfig[c][3] = currentConfig[c][3];
  198.  
  199. System.out.println(totalBestConfig[c][0] + " " + totalBestConfig[c][1] + " " + totalBestConfig[c][2] + " " + totalBestConfig[c][3]);
  200. }
  201. System.out.println("\n");
  202.  
  203. }
  204.  
  205. }
  206. break;
  207. }
  208. }
  209. }
  210. });
  211. th[f].start();
  212. }
  213.  
  214. for(int i =0;i<16;i++){
  215. try {
  216. th[i].join();
  217. } catch (InterruptedException e) {
  218. e.printStackTrace();
  219. }
  220. }
  221.  
  222.  
  223. return 0;
  224. }
  225.  
  226. //kiértékeli az adott paraméterekkel
  227. Result eval(PMGame game, int tillFrightened, double ghostDist, int willidie, int bfstype){
  228. int score = 0;
  229. int currentLevel = game.level;
  230.  
  231. int beforescore = game.score;
  232.  
  233. while(true){
  234. int nextmove = getDirectionByTactic(game,tillFrightened,ghostDist,bfstype);
  235.  
  236. score = game.score;
  237.  
  238.  
  239. if(willIDie(game.clone(),willidie, tillFrightened,ghostDist,bfstype)){
  240. game.setAction(game.pacmans[0],new PMAction((nextmove+2)%4),0);
  241. }else {
  242. game.setAction(game.pacmans[0],new PMAction(nextmove),0);
  243. }
  244.  
  245.  
  246. if(game.level != currentLevel){
  247. if(game.score - beforescore < 2200 && !game.isFinished()){
  248. game.lives = 0;
  249. }
  250. break;
  251. }
  252.  
  253. if(game.isFinished()){
  254. break;
  255. }
  256.  
  257. score = game.score;
  258. }
  259.  
  260.  
  261. return new Result(score,game.lives,game);
  262. }
  263.  
  264.  
  265.  
  266. //tillfright, ghostdist, willidie, bfstype
  267. int[][] parameters = {
  268. {7,21,16,7},
  269. {27,20,5,9},
  270. {12,18,8,3},
  271. {10,20,12,2},
  272. {31,10,13,8}
  273. };
  274.  
  275.  
  276. @Override
  277. public int getDirection(int id, PMGame game){
  278. this.getBestScoreOnPos(game);
  279. System.exit(0);
  280.  
  281.  
  282. int[] currentparam = parameters[game.level];
  283.  
  284. int nextmove = getDirectionByTactic(game,currentparam[0],currentparam[1],currentparam[3]);
  285.  
  286. //Ha meghalnánk a tactic alapján akkor inkább forduljunk meg. RIP
  287. if(willIDie(game.clone(),currentparam[2],currentparam[0],currentparam[1],currentparam[3])){
  288. return (nextmove+2)%4;
  289. }else {
  290. return nextmove;
  291. }
  292. }
  293.  
  294.  
  295. public int getDirectionByTactic(PMGame game, int tillfright, double ghostdist, int bfstype){
  296. Pair<Integer,Integer> closest = findClosestFoodPosition(game,new Pair<Integer,Integer>(0,0),bfstype);
  297.  
  298. PMPlayer pm = game.pacmans[0];
  299.  
  300.  
  301. boolean hasPotencial = false;
  302. Ghost bestGhost = null;
  303.  
  304. //Megnézni, hogy melyiket éri meg úgy kergetni h el is tudjam kapni
  305. for(int i =0;i<4;i++){
  306. Ghost g = (Ghost)game.ghosts[i];
  307. double currentbest = 999999;
  308.  
  309. if(g.tillFrightened > tillfright){
  310. double dist = hamiltonDistanceBetweenPlayer(g,pm);
  311.  
  312. if(dist < ghostdist){
  313. if(currentbest>dist){
  314. currentbest = dist;
  315. hasPotencial = true;
  316. bestGhost = g;
  317. }
  318. }
  319. }
  320. }
  321.  
  322. if(hasPotencial){
  323. //Ha van potenciális szellem akkor irány érte.
  324. return Strategy.closestToTarget(pm.getTilePosition(),bestGhost.getTilePosition(),(pm.getDirection() + 2) % 4,game);
  325. }
  326.  
  327.  
  328. return Strategy.closestToTarget(pm.getTilePosition(), closest,(pm.getDirection() + 2) % 4,game);
  329. }
  330.  
  331. public boolean willIDie(PMGame clonedGame,int check, int tillfright, double ghostdist, int bfstype){
  332. //pár lépést leszimulálunk, ha meghalunk változtatunk az irányon
  333. int liveBefore = clonedGame.lives;
  334. for(int i =0;i<check;i++){
  335. clonedGame.setAction(clonedGame.pacmans[0],new PMAction(getDirectionByTactic(clonedGame,tillfright, ghostdist,bfstype)),1000);
  336. int after = clonedGame.lives;
  337. if(after<liveBefore){
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement