Advertisement
Luisoq21

Player Minimax

May 15th, 2021
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.65 KB | None | 0 0
  1. package ipvc.estg.prog2.LinesAction.players;
  2.  
  3.  
  4. import ipvc.estg.prog2.LinesAction.Player;
  5. import ipvc.estg.prog2.LinesAction.Result;
  6. import ipvc.estg.prog2.LinesAction.Space;
  7. import ipvc.estg.prog2.LinesAction.State;
  8.  
  9. public class MinimaxPlayer extends Player {
  10.  
  11.     public MinimaxPlayer(String name) {
  12.         super(name);
  13.     }
  14.  
  15.  
  16.     private int heuristic(State state) {
  17.         Space[][] grid = state.getGrid();
  18.         int contador=0;
  19.         for(int c=0;c<grid.length;c++){
  20.             for(int l=0;l<grid.length;l++){
  21.                 if(grid[l][c].getPiece() == state.getPlayingColor()){
  22.                     contador++;
  23.                 }
  24.                 if(grid[l][c].getPiece()!= state.getPlayingColor() && grid[l][c].getPiece() != ' '){
  25.                     contador--;
  26.                 }
  27.             }
  28.         }
  29.         return contador;
  30.         /**
  31.         // verificar todas as linhas
  32.         int longest = 0;
  33.         // por cada linha
  34.         for (int row = 0; row < grid.length; ++row) {
  35.             int seq = 0;
  36.             // por cada coluna dessa linha
  37.             for (int col = 0; col < grid[row].length; ++col) {
  38.                 if (grid[row][col].getPiece() == this.getCurrentColor()) {
  39.                     seq++;
  40.                 } else {
  41.                     if (seq > longest) {
  42.                         longest = seq;
  43.                     }
  44.                     seq = 0;
  45.                 }
  46.             }
  47.  
  48.             if (seq > longest) {
  49.                 longest = seq;
  50.             }
  51.         }
  52.  
  53.         // por cada coluna
  54.         for (int col = 0; col < grid[0].length; ++col) {
  55.  
  56.             int seq = 0;
  57.  
  58.             // por cada linha dessa coluna
  59.             for (int row = 0; row < grid.length; ++row) {
  60.                 if (grid[row][col].getPiece() == this.getCurrentColor()) {
  61.                     seq++;
  62.                 } else {
  63.                     if (seq > longest) {
  64.                         longest = seq;
  65.                     }
  66.                     seq = 0;
  67.                 }
  68.             }
  69.  
  70.             if (seq > longest) {
  71.                 longest = seq;
  72.             }
  73.         }
  74.  
  75.         //check upward diagonal
  76.         for (int row = 3; row < grid.length; row++) {
  77.             for (int col = 0; col < grid[0].length - 3; col++) {
  78.  
  79.                 int seq1 = (grid[row][col].getPiece() == this.getCurrentColor() ? 1 : 0) +
  80.                         (grid[row - 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
  81.                         (grid[row - 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0);
  82.  
  83.                 int seq2 = (grid[row - 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
  84.                         (grid[row - 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0) +
  85.                         (grid[row - 3][col + 3].getPiece() == this.getCurrentColor() ? 1 : 0);
  86.  
  87.                 if (seq1 > longest) {
  88.                     longest = seq1;
  89.                 }
  90.  
  91.                 if (seq2 > longest) {
  92.                     longest = seq2;
  93.                 }
  94.             }
  95.         }
  96.         //check downward diagonal
  97.         for (int row = 0; row < state.getGrid().length - 3; row++) {
  98.             for (int col = 0; col < state.getGrid()[0].length - 3; col++) {
  99.                 int seq1 = (grid[row][col].getPiece() == this.getCurrentColor() ? 1 : 0) +
  100.                         (grid[row + 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
  101.                         (grid[row + 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0);
  102.  
  103.                 int seq2 = (grid[row + 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
  104.                         (grid[row + 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0) +
  105.                         (grid[row + 3][col + 3].getPiece() == this.getCurrentColor() ? 1 : 0);
  106.  
  107.                 if (seq1 > longest) {
  108.                     longest = seq1;
  109.                 }
  110.  
  111.                 if (seq2 > longest) {
  112.                     longest = seq2;
  113.                 }
  114.             }
  115.         }
  116.  
  117.         return longest;**/
  118.  
  119.     }
  120.  
  121.     private class MinimaxOutput{
  122.         public int score;
  123.         public Space[] action;
  124.  
  125.         public MinimaxOutput(int score, Space[] action) {
  126.             this.score = score;
  127.             this.action = action;
  128.  
  129.         }
  130.  
  131.     }
  132.  
  133.     /**
  134.      * Efetua uma pesquisa minimax (recursivamente)
  135.      *
  136.      * @param state         estado atual sobre o qual a pesquisa deve ser feita
  137.      * @param depth         profundidade máxima de pesquisa
  138.      * @param alpha         parametro alpha para pruning
  139.      * @param beta          parametro beta para pruning
  140.      * @return
  141.      */
  142.     private MinimaxOutput minimax(State state, int depth, int alpha, int beta) {
  143.  
  144.         // verificar se o jogo terminou (vitória ou derrota)
  145.         if (state.hasWinner()) {
  146.             Result result = state.getResult(this.getCurrentColor());
  147.             switch (result) {
  148.                 case WIN:
  149.                     return new MinimaxOutput(4,null);
  150.                 case LOOSE:
  151.                     return new MinimaxOutput(-4,null);
  152.             }
  153.         }
  154.  
  155.         // profundidade máxima atingida
  156.         if (depth == 0) {
  157.             return new MinimaxOutput(heuristic(state),null);
  158.         }
  159.  
  160.         Space[] jogPossiveis = new Space[8];
  161.  
  162.         // se for o minimax a jogar
  163.  
  164.         if (this.getCurrentColor() == state.getPlayingColor()) {
  165.  
  166.             int value = Integer.MIN_VALUE;
  167.             Space[] selectedAction= null;
  168.  
  169.             for (int posI = 0; posI < state.getGrid()[0].length; posI++) {
  170.                 for (int posF = 0; posF < state.getGrid()[0].length; posF++) {
  171.                     Space[] action = new Space[2];
  172.                     if(state.getGrid()[posI][posF].getPiece()==state.getPlayingColor()){
  173.                         action[0] = new Space(posI, posF);
  174.                         jogPossiveis = state.possiblePlay(action[0]);
  175.  
  176.                         for(int i =0; i<jogPossiveis.length;i++){
  177.                            if (jogPossiveis[i]!=null){
  178.                                 action[1] = jogPossiveis[i];
  179.  
  180.                                 if (state.validate(action[0], action[1])) {
  181.                                     int previousA = value;
  182.                                     State nextState = state.getClone();
  183.                                     nextState.next(action);
  184.                                     value = Math.max(value, minimax(nextState, depth - 1, alpha, beta).score);
  185.                                     alpha = Math.max(alpha, value);
  186.  
  187.                                     if (value >= previousA) {
  188.                                         selectedAction = action;
  189.                                     }
  190.                                     /**System.out.println("\nPeça Inicial "+selectedAction[0].getRow()+" "+selectedAction[0].getColumn()+
  191.                                             " - Peça Final "+selectedAction[1].getRow()+" "+selectedAction[1].getColumn()+" "+value+"\n");**/
  192.                                     if (alpha >= beta) {
  193.                                         break;
  194.                                     }
  195.                                 }
  196.                            }
  197.                         }
  198.                     }
  199.  
  200.                 }
  201.             }
  202.             return new MinimaxOutput(value,selectedAction);
  203.         }
  204.         else { // adversário a jogar
  205.             int value = Integer.MAX_VALUE;
  206.             Space[] selectedAction= null;
  207.             for (int posI = 0; posI < state.getGrid()[0].length; posI++) {
  208.                 for (int posF = 0; posF < state.getGrid()[0].length; posF++) {
  209.                     Space[] action = new Space[2];
  210.                     if(state.getGrid()[posI][posF].getPiece()==state.getPlayingColor()) {
  211.                         action[0] = new Space(posI, posF);
  212.                         jogPossiveis = state.possiblePlay(action[0]);
  213.                         for (int i = 0; i < jogPossiveis.length; i++) {
  214.                             if (jogPossiveis[i] != null) {
  215.                                 action[1] = jogPossiveis[i];
  216.                                 if (state.validate(action[0], action[1])) {
  217.                                     int previousA = value;
  218.                                     State nextState = state.getClone();
  219.                                     nextState.next(action);
  220.                                     value = Math.min(value, minimax(nextState, depth - 1, alpha, beta).score);
  221.                                     beta = Math.min(beta, value);
  222.                                     if (value <= previousA) {
  223.                                         selectedAction = action;
  224.                                     }
  225.                                     if (beta <= alpha) {
  226.                                         break;
  227.                                     }
  228.                                 }
  229.                             }
  230.                         }
  231.                     }
  232.  
  233.                 }
  234.             }
  235.             return new MinimaxOutput(value,selectedAction);
  236.         }
  237.  
  238.     }
  239.  
  240.     @Override
  241.     public Space[] getAction(State state) {
  242.         Space[] action = new Space[2];
  243.         action = minimax(state, 3, Integer.MIN_VALUE, Integer.MAX_VALUE).action;
  244.         System.out.println("\nBot HARD - "+"Peça Inicial "+action[0].getRow()+" "+action[0].getColumn()+
  245.                 " - Peça Final "+action[1].getRow()+" "+action[1].getColumn()+"\n");
  246.  
  247.         return action;
  248.  
  249.     }
  250.  
  251.     @Override
  252.     public void eventNewGame(char color) {
  253.     }
  254.  
  255.     @Override
  256.     public void eventWin(State state) {
  257.  
  258.     }
  259.  
  260.     @Override
  261.     public void eventLoose(State state) {
  262.  
  263.     }
  264.  
  265. }
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement