Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game.pm.strategies;
- /*
- Mestint beadando
- */
- import game.engine.Engine;
- import game.engine.utils.Pair;
- import game.pm.Ghost;
- import game.pm.PMAction;
- import game.pm.PMGame;
- import game.pm.PMPlayer;
- import game.pm.ui.GameGraphicsController;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Random;
- public class Agent extends Strategy {
- public static boolean isValidCoord(int row,int col){return row>=0&&col>=0&&row<31&&col<28;}
- public static boolean isIt(int bitField, int CODE){
- return 0 < (bitField&CODE);
- }
- //lehetséges:
- // 1,0 -1,0 , 0,1, , 0,-1
- //Bejáráshoz
- int[] A = {1,0};
- int[] B = {-1,0};
- int[] C = {0,1};
- int[] D = {0,-1};
- int[][][] BFSType = {
- {C,B,A,D},
- { B,C,A,D},
- { C,A,B,D},
- { C,A,D,B},
- { C,B,A,D},
- { B,C,A,D},
- { C,B,D,A},
- { C,D,A,B},
- { C,D,B,A},
- { B,A,C,D},
- { B,A,D,C},
- { B,C,D,A},
- { B,D,B,C},
- { B,D,C,A},
- };
- //Eredmény
- static class Result{
- public Result(int score, int live, PMGame game){
- this.score = score;
- this.live = live;
- this.game = game;
- }
- int score;
- int live;
- PMGame game;
- };
- int counter = 0;
- int[] scores = new int[10];
- public Agent(){
- }
- public static double hamiltonDistanceBetweenPlayer(PMPlayer a, PMPlayer b){
- int posy = a.getPixelI();
- int posx = a.getPixelJ();
- return Math.sqrt(Math.abs(posx - b.getPixelJ()) + Math.abs(posy - b.getPixelI()));
- }
- public Pair<Integer,Integer> findClosestFoodPosition(PMGame game,Pair<Integer,Integer> deniedCoord, int typeOfBFS){
- PMPlayer pm = game.pacmans[0];
- //olyan lépés amivel a lehető legközelebb kerülök a kajához
- //szélességi keresés.
- Pair<Integer,Integer> currentpos;
- currentpos = new Pair<Integer,Integer>(pm.getTileI(), pm.getTileJ());
- boolean[][] visited = new boolean[50][50];
- Queue<Pair<Integer,Integer>> q = new LinkedList<Pair<Integer,Integer>>();
- q.add(currentpos);
- Pair<Integer,Integer> targetpos = new Pair<>(currentpos.first,currentpos.second);
- while(!q.isEmpty()){
- Pair<Integer,Integer> node = q.remove();
- if(!isValidCoord(node.first,node.second)){
- continue;
- }
- if(visited[node.first][node.second]){
- continue;
- }else{
- visited[node.first][node.second] = true;
- }
- if(!PMGame.allowedTile(game.maze[node.first][node.second])){
- continue;
- }
- if(isIt(game.maze[node.first][node.second],PMGame.TILES.FOOD)){
- if(!deniedCoord.first.equals(node.first) && !deniedCoord.second.equals(node.second)){
- targetpos = node;
- break;
- }
- }
- for(int i =0;i<4;i++){
- q.add(new Pair<Integer,Integer>(
- node.first+this.BFSType[typeOfBFS][i][0],node.second+this.BFSType[typeOfBFS][i][1]
- ));
- }
- }
- return targetpos;
- }
- static class threadResult{
- public int bestScore = 0;
- public int[][] bestConfig = new int[10][4];
- }
- /*
- Legjobb paraméterek megkeresése, precalc
- */
- public int getBestScoreOnPos(PMGame game){
- PMPlayer pm = game.pacmans[0];
- final threadResult[] results = new threadResult[16];
- for(int i =0;i<16;i++){
- results[i] = new threadResult();
- }
- Thread[] th = new Thread[16];
- final int[] totalBestScore = {0};
- final int[][] totalBestConfig = new int[10][4];
- for(int f =0;f<16;f++){
- int finalF = f;
- th[f] = new Thread(() -> {
- for(int i =0;i<100000000;i++){
- int[][] currentConfig = new int[10][4];
- PMGame currentGame = game.clone();
- for(int j=0;j<7;j++){
- int tillfright = Math.abs(new Random().nextInt() % 30) + 2;
- int ghostDist = Math.abs(new Random().nextInt() % 30) + 4;
- int willidiechecker = Math.abs(new Random().nextInt() % 17) + 2;
- int bfstype = Math.abs(new Random().nextInt()) % 10;
- currentConfig[j][0] = tillfright;//tillfright;
- currentConfig[j][1] = ghostDist;//ghostDist;
- currentConfig[j][2] = willidiechecker;//willidiechecker;
- currentConfig[j][3] = bfstype;
- Result ret = eval(currentGame,tillfright,ghostDist,willidiechecker,bfstype);//tillfright,ghostDist,willidiechecker);
- if(ret.live == 0 || ret.game.isFinished()){
- if(currentGame.score>results[finalF].bestScore){
- if(totalBestScore[0] < currentGame.score){
- totalBestScore[0] = currentGame.score;
- System.out.println("Új total score: " + currentGame.score);
- results[finalF].bestScore = currentGame.score;
- results[finalF].bestConfig = currentConfig;
- System.out.println("BFSType: " + bfstype);
- for(int c =0;c<7;c++){
- totalBestConfig[c][0] = currentConfig[c][0];
- totalBestConfig[c][1] = currentConfig[c][1];
- totalBestConfig[c][2] = currentConfig[c][2];
- totalBestConfig[c][3] = currentConfig[c][3];
- System.out.println(totalBestConfig[c][0] + " " + totalBestConfig[c][1] + " " + totalBestConfig[c][2] + " " + totalBestConfig[c][3]);
- }
- System.out.println("\n");
- }
- }
- break;
- }
- }
- }
- });
- th[f].start();
- }
- for(int i =0;i<16;i++){
- try {
- th[i].join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- return 0;
- }
- //kiértékeli az adott paraméterekkel
- Result eval(PMGame game, int tillFrightened, double ghostDist, int willidie, int bfstype){
- int score = 0;
- int currentLevel = game.level;
- int beforescore = game.score;
- while(true){
- int nextmove = getDirectionByTactic(game,tillFrightened,ghostDist,bfstype);
- score = game.score;
- if(willIDie(game.clone(),willidie, tillFrightened,ghostDist,bfstype)){
- game.setAction(game.pacmans[0],new PMAction((nextmove+2)%4),0);
- }else {
- game.setAction(game.pacmans[0],new PMAction(nextmove),0);
- }
- if(game.level != currentLevel){
- if(game.score - beforescore < 2200 && !game.isFinished()){
- game.lives = 0;
- }
- break;
- }
- if(game.isFinished()){
- break;
- }
- score = game.score;
- }
- return new Result(score,game.lives,game);
- }
- //tillfright, ghostdist, willidie, bfstype
- int[][] parameters = {
- {7,21,16,7},
- {27,20,5,9},
- {12,18,8,3},
- {10,20,12,2},
- {31,10,13,8}
- };
- @Override
- public int getDirection(int id, PMGame game){
- this.getBestScoreOnPos(game);
- System.exit(0);
- int[] currentparam = parameters[game.level];
- int nextmove = getDirectionByTactic(game,currentparam[0],currentparam[1],currentparam[3]);
- //Ha meghalnánk a tactic alapján akkor inkább forduljunk meg. RIP
- if(willIDie(game.clone(),currentparam[2],currentparam[0],currentparam[1],currentparam[3])){
- return (nextmove+2)%4;
- }else {
- return nextmove;
- }
- }
- public int getDirectionByTactic(PMGame game, int tillfright, double ghostdist, int bfstype){
- Pair<Integer,Integer> closest = findClosestFoodPosition(game,new Pair<Integer,Integer>(0,0),bfstype);
- PMPlayer pm = game.pacmans[0];
- boolean hasPotencial = false;
- Ghost bestGhost = null;
- //Megnézni, hogy melyiket éri meg úgy kergetni h el is tudjam kapni
- for(int i =0;i<4;i++){
- Ghost g = (Ghost)game.ghosts[i];
- double currentbest = 999999;
- if(g.tillFrightened > tillfright){
- double dist = hamiltonDistanceBetweenPlayer(g,pm);
- if(dist < ghostdist){
- if(currentbest>dist){
- currentbest = dist;
- hasPotencial = true;
- bestGhost = g;
- }
- }
- }
- }
- if(hasPotencial){
- //Ha van potenciális szellem akkor irány érte.
- return Strategy.closestToTarget(pm.getTilePosition(),bestGhost.getTilePosition(),(pm.getDirection() + 2) % 4,game);
- }
- return Strategy.closestToTarget(pm.getTilePosition(), closest,(pm.getDirection() + 2) % 4,game);
- }
- public boolean willIDie(PMGame clonedGame,int check, int tillfright, double ghostdist, int bfstype){
- //pár lépést leszimulálunk, ha meghalunk változtatunk az irányon
- int liveBefore = clonedGame.lives;
- for(int i =0;i<check;i++){
- clonedGame.setAction(clonedGame.pacmans[0],new PMAction(getDirectionByTactic(clonedGame,tillfright, ghostdist,bfstype)),1000);
- int after = clonedGame.lives;
- if(after<liveBefore){
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement