Advertisement
Guest User

BoardGiesel

a guest
Jul 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. package gj.kalah.player.giesel;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Board {
  6.  
  7.     public boolean first;
  8.  
  9.     public int[] boardField = new int[] { 4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0 };
  10.  
  11.     Random rand = new Random();
  12.  
  13.     public Board(boolean isFirst) {
  14.  
  15.         first = isFirst;
  16.  
  17.     }
  18.  
  19.     public int fillStrategy() {
  20.         int mossa = 50;
  21.         if (first) {
  22.             for (int i = 5; i >= 0; i--) {
  23.                 if (boardField[i] >= (boardField.length - i) - 1) {
  24.                     return i;
  25.                 }
  26.             }
  27.  
  28.         } else {
  29.             for (int i = 12; i >= 7; i--) {
  30.                 if (boardField[i] >= ((boardField.length - (i - 7) - 1))) {
  31.                     i = i - 7;
  32.                     return i;
  33.                 }
  34.             }
  35.         }
  36.         return mossa;
  37.     }
  38.  
  39.     public int enemyStorageStrategy() {
  40.         int n = 50;
  41.         int limiteInferiore = 0;
  42.         int limiteSuperiore = 6;
  43.         if (!first) {
  44.  
  45.             while (limiteInferiore <= 5 && limiteSuperiore >= 0) {
  46.                 if (boardField[limiteInferiore] == limiteSuperiore) {
  47.                     for (int i = 12; i <= 6; i--) {
  48.                         if (boardField[i] >= boardField[limiteInferiore]) {
  49.                             return i;
  50.  
  51.                         }
  52.  
  53.                     }
  54.                     return n;
  55.                 } else {
  56.                     limiteInferiore++;
  57.                     limiteSuperiore--;
  58.                 }
  59.             }
  60.         } else {
  61.             limiteInferiore += 7;
  62.             while (limiteInferiore <= 12 && limiteSuperiore >= 0) {
  63.                 if (boardField[limiteInferiore] == limiteSuperiore) {
  64.                     for (int i = 5; i <= 0; i--) {
  65.                         if (boardField[i] >= boardField[limiteInferiore]) {
  66.                             return i;
  67.                         }
  68.  
  69.                     }
  70.                     return n;
  71.  
  72.                 } else {
  73.                     limiteInferiore++;
  74.                     limiteSuperiore--;
  75.                 }
  76.             }
  77.  
  78.         }
  79.         return n;
  80.  
  81.     }
  82.  
  83.     public int storageStrategy() {
  84.         int n = 50;
  85.         int limiteInferiore = 0;
  86.         int limiteSuperiore = 6;
  87.         if (first) {
  88.             while (limiteInferiore <= 5 && limiteSuperiore >= 0) {
  89.                 if (boardField[limiteInferiore] == limiteSuperiore) {
  90.                     n = limiteInferiore;
  91.                     return n;
  92.                 } else {
  93.                     limiteInferiore++;
  94.                     limiteSuperiore--;
  95.                 }
  96.             }
  97.         } else {
  98.             limiteInferiore += 7;
  99.             while (limiteInferiore <= 12 && limiteSuperiore >= 0) {
  100.                 if (boardField[limiteInferiore] == limiteSuperiore) {
  101.                     n = limiteInferiore - 7;
  102.                     return n;
  103.                 } else {
  104.                     limiteInferiore++;
  105.                     limiteSuperiore--;
  106.                 }
  107.             }
  108.         }
  109.         return n;
  110.     }
  111.  
  112.     public int randomMoveStrategy() {
  113.         int x = rand.nextInt(5);
  114.  
  115.         if (first) {
  116.             if (boardField[x] != 0) {
  117.                 return x;
  118.             } else {
  119.                 for (int i = 0; i <= 5; i++) {
  120.                     if (boardField[i] != 0) {
  121.                         x = i;
  122.                         return x;
  123.                     }
  124.                 }
  125.             }
  126.         } else {
  127.             x = x + 7;
  128.             if (boardField[x] != 0) {
  129.                 x = x - 7;
  130.                 return x;
  131.             } else {
  132.                 for (int i = 7; i <= 12; i++) {
  133.                     if (boardField[i] != 0) {
  134.                         x = i;
  135.                         x = x - 7;
  136.                         return x;
  137.                     }
  138.                 }
  139.             }
  140.  
  141.         }
  142.         return x;
  143.     }
  144.  
  145.     public int eatEnemy() {
  146.         int[] rubare = new int[6];
  147.  
  148.         for (int i = 0; i < 6; i++) {
  149.             rubare[i] = 0;
  150.         }
  151.  
  152.         int risultatoMossa = 50;
  153.         int puntatore = 0;
  154.         int ca = 0;
  155.         if (first) {
  156.             while (puntatore <= 5) {
  157.                 int somma = puntatore + boardField[puntatore];
  158.  
  159.                 while (somma >= 14) {
  160.                     somma = somma - 14;
  161.                 }
  162.                 if (somma > 5 && somma < 14) {
  163.                     return risultatoMossa;
  164.                 }
  165.  
  166.                 if (somma <= 5 && boardField[somma] == 0 && boardField[puntatore] > 0) {
  167.                     int inversePointer = -puntatore + 12;
  168.                     risultatoMossa = puntatore;
  169.  
  170.                     rubare[ca] = inversePointer;
  171.                 }
  172.                 puntatore = puntatore + 1;
  173.                 ca = ca + 1;
  174.             }
  175.         } else {
  176.             puntatore = puntatore + 7;
  177.             while (puntatore <= 12) {
  178.                 int somma = puntatore + boardField[puntatore];
  179.  
  180.                 if (somma > 12 && somma < 6) {
  181.                     return risultatoMossa;
  182.                 }
  183.  
  184.                 if (somma <= 12 && boardField[somma] == 0 && boardField[puntatore] > 0) {
  185.                     int inversePointer = -puntatore + 12;
  186.                     inversePointer = inversePointer - 7;
  187.                     risultatoMossa = puntatore;
  188.                     risultatoMossa = risultatoMossa - 7;
  189.  
  190.                     rubare[ca] = inversePointer;
  191.  
  192.                 }
  193.                 puntatore = puntatore + 1;
  194.                 ca = ca + 1;
  195.             }
  196.         }
  197.         return EatBestEnemy(rubare, risultatoMossa);
  198.     }
  199.  
  200.     public int EatBestEnemy(int[] migliore, int risultatoMossa) {
  201.         int contatore = 0;
  202.         int posizione = -1;
  203.         int max = 0;
  204.  
  205.         if (risultatoMossa == 50) {
  206.             return risultatoMossa;
  207.         }
  208.         for (int e : migliore) {
  209.             if (e > max) {
  210.                 max = e;
  211.                 posizione = contatore;
  212.             }
  213.             contatore = contatore + 1;
  214.         }
  215.         if (posizione != -1) {
  216.             return posizione;
  217.         }
  218.  
  219.         return risultatoMossa;
  220.     }
  221.  
  222.     // choose strategy to apply
  223.     public int strategyPicker() {
  224.         int primaStrategia = eatEnemy();
  225.         int secondaStrategia = storageStrategy();
  226.         int terzaStrategia = fillStrategy();
  227.         int quartaStrategia = randomMoveStrategy();
  228.         int quintaStrategia = enemyStorageStrategy();
  229.  
  230.         if (first) {
  231.  
  232.             if (secondaStrategia != 50) {
  233.  
  234.                 return secondaStrategia;
  235.             }
  236.  
  237.             if (primaStrategia != 50) {
  238.  
  239.                 return primaStrategia;
  240.  
  241.             }
  242.             if (quintaStrategia != 50) {
  243.  
  244.                 return quintaStrategia;
  245.             }
  246.  
  247.             if (terzaStrategia != 50) {
  248.                 return terzaStrategia;
  249.             }
  250.  
  251.             return quartaStrategia;
  252.         } else {
  253.  
  254.             if (secondaStrategia != 50) {
  255.  
  256.                 return secondaStrategia;
  257.             }
  258.  
  259.             if (primaStrategia != 50) {
  260.  
  261.                 return primaStrategia;
  262.  
  263.             }
  264.             if (quintaStrategia != 50) {
  265.  
  266.                 return quintaStrategia;
  267.             }
  268.             if (terzaStrategia != 50) {
  269.                 return terzaStrategia;
  270.             }
  271.         }
  272.         return quartaStrategia;
  273.     }
  274.  
  275.     public void moveMyPlayer(int conca) {
  276.         int pointer = conca;
  277.         int pietra = boardField[conca];
  278.         boardField[conca] = 0;
  279.         while (pietra > 0) {
  280.             ++pointer;  
  281.             if (pointer == 13) {
  282.                 pointer = 0;
  283.             }
  284.             boardField[pointer]++;
  285.             pietra--;  
  286.             int inversePointer = -pointer + 12;  
  287.             if (pointer >= 0 && pointer <= 5 && pietra < 1 && boardField[pointer] == 1) {
  288.                 boardField[6] += boardField[inversePointer] + 1;
  289.                 boardField[pointer] = 0;
  290.                 boardField[inversePointer] = 0;
  291.  
  292.             }
  293.         }
  294.         conca++;
  295.  
  296.     }
  297.  
  298.     public void moveEnemyPlayer(int conca) {
  299.         conca = conca + 7;
  300.         int pointer = conca;
  301.         int pietra = boardField[conca];
  302.         boardField[conca] = 0;
  303.         while (pietra > 0) {
  304.             ++pointer;
  305.             if (pointer == 14) {
  306.                 pointer = 0;
  307.             }
  308.             if (pointer == 6) {
  309.  
  310.                 pointer = 7;
  311.             }
  312.             boardField[pointer]++;
  313.             pietra--;
  314.             int inversePointer = -pointer + 12;
  315.  
  316.             if (pointer >= 7 && pointer <= 12 && pietra < 1 && boardField[pointer] == 1) {
  317.                 boardField[13] += boardField[inversePointer] + 1;
  318.                 boardField[pointer] = 0;
  319.                 boardField[inversePointer] = 0;
  320.             }
  321.         }
  322.  
  323.     }
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement