Advertisement
Aseron

MARSAKK2.0

Nov 7th, 2018
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.56 KB | None | 0 0
  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.Iterator;
  6.  
  7. import com.sun.javafx.runtime.SystemProperties;
  8.  
  9. import game.engine.utils.Pair;
  10. import game.mc.MCAction;
  11. import game.mc.MCPlayer;
  12. import game.mc.MCGame;
  13.  
  14. public class h353036 extends MCPlayer {
  15.  
  16.   private MCAction prevAction;
  17.   private MCAction shadowPrevAction;
  18.   private int myScore;
  19.   private int shadowMyScore;
  20.   private int enemyScore;
  21.   private int shadowEnemyScore;
  22.   private MCAction bestAction = null;
  23.   private int playersParity = 1;
  24.   private int deep = 0;
  25.   private MCAction lastAction = null;
  26.   private List<MCAction> ancestors = new ArrayList<MCAction>();
  27.   private int count = 0;
  28.  
  29.  
  30.  
  31.  
  32.   public h353036(int color, int[][] board, Random r) {
  33.     super(color, board, r);
  34.     myScore = 0;
  35.     enemyScore = 0;
  36.  
  37.   }
  38.  
  39.  
  40.   public class Node
  41.     {
  42.         List<Node> children = null;
  43.         MCAction ancestor = null;
  44.         int value;
  45.    
  46.         public Node(int value, MCAction ancestor)
  47.         {
  48.             this.ancestor = ancestor;
  49.             this.children = new LinkedList<>();
  50.             this.value = value;
  51.         }
  52.    
  53.         public void addChild(Node child)
  54.         {
  55.             children.add(child);
  56.         }
  57.  
  58.         public int getValue(int value){
  59.             return this.value;
  60.           }
  61.  
  62.         public void setValue(int value){
  63.           this.value = value;
  64.         }
  65.  
  66.         public MCAction getAncestor(){
  67.             return this.ancestor;
  68.         }
  69.  
  70.        
  71.    
  72.     }
  73.    
  74.  
  75.   @Override
  76.   public MCAction getAction(List<Pair<Integer, MCAction>> prevActions) {
  77.  
  78.     int prevScore = 0;
  79.     for (Pair<Integer, MCAction> action : prevActions) {
  80.       if (action.second == null) {
  81.         continue;
  82.       }
  83.       prevAction = action.second;
  84.       boolean samePart = (action.second.x1 < 4 && action.second.x2 < 4) || (4 <= action.second.x1 && 4 <= action.second.x2);
  85.       if (samePart) {
  86.         // merge / move
  87.         board[action.second.x2][action.second.y2] += board[action.second.x1][action.second.y1];
  88.       } else {
  89.         // capture / move
  90.         prevScore = board[action.second.x2][action.second.y2];
  91.         board[action.second.x2][action.second.y2] = board[action.second.x1][action.second.y1];
  92.       }
  93.       board[action.second.x1][action.second.y1] = MCGame.empty;
  94.     }
  95.     enemyScore += prevScore;
  96.  
  97.  
  98.  
  99.     int maxScore = 0;
  100.     List<Pair<Integer, MCAction>> actions = new LinkedList<Pair<Integer, MCAction>>();
  101.     // generate actions
  102.     for (int i = 4 * color; i < 4 * (color + 1); i++) {
  103.       for (int j = 0; j < 4; j++) {
  104.         if (board[i][j] != MCGame.empty) {
  105.           for (int i2 = 0; i2 < 8; i2++) {
  106.             for (int j2 = 0; j2 < 4; j2++) {
  107.               MCAction action = new MCAction(i, j, i2, j2);
  108.               int score = -1;
  109.               try {
  110.                 score = score(board, prevAction, prevScore, color, action, myScore - enemyScore);
  111.               } catch (Exception e) {
  112.                 System.out.println("ACTION: " + action);
  113.                 e.printStackTrace();
  114.                 System.exit(1);
  115.               }
  116.               if (maxScore < score) {
  117.                 maxScore = score;
  118.                 actions.clear();
  119.               }
  120.               if (maxScore == score) {
  121.                 actions.add(new Pair<Integer, MCAction>(maxScore, action));
  122.                
  123.               }
  124.             }
  125.           }
  126.         }
  127.       }
  128.     }
  129.  
  130.         List<Integer> indexList = new ArrayList<Integer>();
  131.         List<Integer> scoreList = new ArrayList<Integer>();
  132.         List<MCAction> actionList = new ArrayList<MCAction>();
  133.  
  134.         int iteratorIndex = 0;
  135.         Iterator<Pair<Integer,MCAction>> it = actions.iterator();
  136.         while (it.hasNext()) {
  137.             Pair<Integer, MCAction> pair = it.next();
  138.             scoreList.add(pair.first);
  139.             actionList.add(pair.second);
  140.             indexList.add(iteratorIndex);
  141.             iteratorIndex++ ;
  142.            
  143.         }
  144.    
  145.        
  146.         MCAction localAction = null;
  147.         int localMaxScore = Integer.MIN_VALUE;
  148.         int localIndex = Integer.MIN_VALUE;
  149.         int[][] shadowBoard = board;
  150.  
  151.         MCAction action = null;
  152.  
  153.         shadowMyScore = myScore;
  154.         shadowEnemyScore = enemyScore;
  155.  
  156.         for(int i = 0; i < actionList.size(); i++){
  157.  
  158.             localAction = actionList.get(i);
  159.             localMaxScore = scoreList.get(i);
  160.             localIndex = indexList.get(i);
  161.  
  162.             action = getShadowAction(localAction, localMaxScore, shadowMyScore, shadowEnemyScore, localIndex, shadowBoard, 1, color, 0);
  163.  
  164.  
  165.         }
  166.  
  167.         for(int i = 0; i < 8; i++){
  168.               System.out.println(board[i][0] + "" + board[i][1] + "" + board[i][2] + "" + board[i][3]);
  169.         }
  170.        
  171.         if (maxScore == 0 && board[action.x2][action.y2] != MCGame.empty) {
  172.             board[action.x2][action.y2] += board[action.x1][action.y1];
  173.           } else {
  174.             board[action.x2][action.y2] = board[action.x1][action.y1];
  175.           }
  176.           board[action.x1][action.y1] = MCGame.empty;
  177.           myScore += maxScore;
  178.  
  179.     return action;
  180.   }
  181.  
  182.  
  183.   public MCAction getShadowAction(MCAction localAction, int localMaxScore, int shadowMyScore, int shadowEnemyScore, int localIndex, int[][] shadowBoard, int isFirst, int color, int depth) {
  184.  
  185.     int shadowColor = color;
  186.  
  187.     playersParity
  188.  
  189.  
  190.     if(isFirst == 1){
  191.         ancestors.add(localAction);
  192.     }
  193.  
  194.     if (localMaxScore == 0 && board[localAction.x2][localAction.y2] != MCGame.empty) {
  195.         shadowBoard[localAction.x2][localAction.y2] += shadowBoard[localAction.x1][localAction.y1];
  196.       } else {
  197.         shadowBoard[localAction.x2][localAction.y2] = shadowBoard[localAction.x1][localAction.y1];
  198.       }
  199.       shadowBoard[localAction.x1][localAction.y1] = MCGame.empty;
  200.  
  201.       if(playersParity % 2 == 1){
  202.         shadowMyScore += localMaxScore;
  203.       }else{
  204.         shadowEnemyScore += localMaxScore;
  205.       }
  206.  
  207.       int[][] reversedShadowBoard;
  208.  
  209.       for(int i = 0; i < 8; i++){
  210.           for(int j = 0; j < 4; j++){
  211.                 reversedShadowBoard[i][j] = shadowBoard[7-i][3-j];
  212.           }
  213.       }
  214.  
  215.  
  216.  
  217.  
  218.   // innentol az ellenfel szemevel nezzuk a tablat
  219.  
  220.       int shadowEnemyMaxScore = 0;
  221.       List<Pair<Integer, MCAction>> shadowEnemyActions = new LinkedList<Pair<Integer, MCAction>>();
  222.       // generate shadowEnemyActions
  223.       for (int i = 4 * color; i < 4 * (color + 1); i++) {
  224.         for (int j = 0; j < 4; j++) {
  225.           if (reversedShadowBoard[i][j] != MCGame.empty) {
  226.             for (int i2 = 0; i2 < 8; i2++) {
  227.               for (int j2 = 0; j2 < 4; j2++) {
  228.                 MCAction action = new MCAction(i, j, i2, j2);
  229.                 int score = -1;
  230.                 try {
  231.                   score = score(reversedShadowBoard, localAction, localMaxScore, color, action, shadowEnemyScore - shadowMyScore);
  232.                 //score = score(board, prevAction, prevScore, color, action, myScore - enemyScore);
  233.                 } catch (Exception e) {
  234.                   System.out.println("ACTION: " + action);
  235.                   e.printStackTrace();
  236.                   System.exit(1);
  237.                 }
  238.                 if (shadowEnemyMaxScore < score) {
  239.                     shadowEnemyMaxScore = score;
  240.                     shadowEnemyActions.clear();
  241.                 }
  242.                 if (shadowEnemyMaxScore == score) {
  243.                     shadowEnemyActions.add(new Pair<Integer, MCAction>(shadowEnemyMaxScore, action));
  244.                  
  245.                 }
  246.               }
  247.             }
  248.           }
  249.         }
  250.       }
  251.  
  252.           List<Integer> indexEnemyList = new ArrayList<Integer>();
  253.           List<Integer> scoreEnemyList = new ArrayList<Integer>();
  254.           List<MCAction> actionEnemyList = new ArrayList<MCAction>();
  255.  
  256.           int iteratorIndex = 0;
  257.           Iterator<Pair<Integer,MCAction>> it = shadowEnemyActions.iterator();
  258.           while (it.hasNext()) {
  259.               Pair<Integer, MCAction> pair = it.next();
  260.               scoreEnemyList.add(pair.first);
  261.               actionEnemyList.add(pair.second);
  262.               indexEnemyList.add(iteratorIndex);
  263.               iteratorIndex++ ;
  264.              
  265.           }
  266.      
  267.          
  268.           MCAction localEnemyAction = null;
  269.           int localEnemyMaxScore = Integer.MIN_VALUE;
  270.           int localEnemyIndex = Integer.MIN_VALUE;
  271.           int[][] shadowEnemyBoard = reversedShadowBoard;
  272.  
  273.           MCAction action = null;
  274.  
  275.           for(int i = 0; i < actionEnemyList.size(); i++){
  276.  
  277.             localEnemyAction = actionEnemyList.get(i);
  278.             localEnemyMaxScore = scoreEnemyList.get(i);
  279.             localEnemyIndex = indexEnemyList.get(i);
  280.            
  281.             if(localEnemyAction == null || localEnemyMaxScore == Integer.MIN_VALUE || localEnemyIndex == Integer.MIN_VALUE){
  282.                 if(playersParity % 2 == 1){
  283.                     break; //csak akkor torjon meg, ha az en lepesemet akarja visszaadni
  284.                 }else{
  285.                     return lastAction; //ha az ellenfelet, akkor a legutobbi lepesemet adja vissza
  286.                 }
  287.             }else if(depth<4){
  288.                 action = getShadowAction(localEnemyAction, localEnemyMaxScore, shadowEnemyScore, shadowMyScore, localEnemyIndex, shadowEnemyBoard, 2, 1-shadowColor, depth+1)
  289.                
  290.             }else{
  291.                 break;
  292.             }
  293.  
  294.           }
  295.  
  296.           action = actionEnemyList.size() == 0 ? null : actionEnemyList.get(r.nextInt(actionEnemyList.size()));
  297.           lastAction = action;
  298.      
  299.     return action;
  300.   }
  301.  
  302.  
  303.  
  304.  
  305.  
  306.   private int score(int[][] board, MCAction prevAction, int prevScore, int color, MCAction action, int scoreDiff) {
  307.     int[][] figures = new int[2][3];
  308.     for (int i = 0; i < board.length; i++) {
  309.       for (int j = 0; j < board[i].length; j++) {
  310.         if (board[i][j] != MCGame.empty) {
  311.           if (i < board.length / 2) {
  312.             figures[0][board[i][j] - 1]++;
  313.           } else {
  314.             figures[1][board[i][j] - 1]++;
  315.           }
  316.         }
  317.       }
  318.     }
  319.     int numFigures = figures[color][0] + figures[color][1] + figures[color][2];
  320.     int score = MCGame.score(board, figures, prevAction, prevScore, color, action);
  321.     boolean samePart = (action.x1 < 4 && action.x2 < 4) || (4 <= action.x1 && 4 <= action.x2);
  322.     if (0 <= score && !samePart && numFigures == 1) {
  323.       if (scoreDiff < 0) {
  324.         score = -1;
  325.       } else {
  326.         score = 10;
  327.       }
  328.     }
  329.     return score;
  330.   }
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement