Advertisement
Guest User

Untitled

a guest
May 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package awale;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Minimax {
  6. private AwaleBoard firstBoard;
  7. private final int profondeurChoisie;
  8.  
  9. /**
  10. * @param firstBoard
  11. */
  12. public Minimax(AwaleBoard firstBoard, int profondeur) {
  13. this.firstBoard = firstBoard;
  14. this.profondeurChoisie = profondeur;
  15.  
  16. }
  17.  
  18. public ArrayList<Integer> getCoupsPossible(AwaleBoard awaleBoard, int playerNumber) {
  19. ArrayList<Integer> indicesCoupsPossibles = new ArrayList<>();
  20. int[] getBoard = awaleBoard.getBoard();
  21.  
  22. if (awaleBoard.getNbTurns() % 2 == 0 && playerNumber == 0) {
  23. for (int i = 0; i < 6; i++) {
  24. if (getBoard[i] > 0)
  25. indicesCoupsPossibles.add(i);
  26.  
  27. }
  28. }
  29. if (awaleBoard.getNbTurns() % 2 == 1 && playerNumber == 1) {
  30. for (int i = 6; i < 12; i++) {
  31. if (getBoard[i] > 0)
  32. indicesCoupsPossibles.add(i);
  33. }
  34. }
  35.  
  36. return indicesCoupsPossibles;
  37. }
  38.  
  39. public int miniMax() {
  40.  
  41. return max(new AwaleBoard(firstBoard), profondeurChoisie, -1).getIndiceDuCoupAyantEteJoue();
  42. }
  43.  
  44. /**
  45. * This function returns the best min() if deep is > 1 for each possible plays of an AwaleBoard.
  46. * Else, returns the best move for this AwaleBoard.
  47. * @param awaleBoard
  48. * @param profondeur
  49. * @param premierCoup
  50. * @return
  51. */
  52. public Move max(AwaleBoard awaleBoard, int profondeur, int premierCoup) {
  53.  
  54. ArrayList<Integer> indicesCoupsPossibles = getCoupsPossible(awaleBoard, 1);
  55. Moves coupsJoues = new Moves(new AwaleBoard(awaleBoard), new Move[indicesCoupsPossibles.size()]);
  56. Move bestCoup = new Move(new AwaleBoard(), new AwaleBoard(), -1, 0, 0);
  57. profondeur--;
  58. if (indicesCoupsPossibles.size() > 0) {
  59. for (int i = 0; i < coupsJoues.size(); i++) {
  60. coupsJoues.coups[i] = new Move(new AwaleBoard(awaleBoard),
  61. new AwaleBoard(awaleBoard).play(indicesCoupsPossibles.get(i)), indicesCoupsPossibles.get(i),
  62. awaleBoard.getScore(1), awaleBoard.getScore(0));
  63. coupsJoues.coups[i].setGainGenereMachine(
  64. coupsJoues.coups[i].gainGenereParMachine + coupsJoues.coups[i].calculeGainGenere(1));
  65.  
  66. }
  67. // ATTENTION
  68. if (profondeur > 0) {
  69. for (int i = 0; i < coupsJoues.size(); i++) {
  70. if (getCoupsPossible(coupsJoues.coups[i].tableauApresCoup, 0).size() > 0)
  71. coupsJoues.coups[i] = min(coupsJoues.coups[i].tableauApresCoup, profondeur,
  72. coupsJoues.coups[i].indiceDuCoupAyantEteJoue);
  73.  
  74. }
  75. }
  76. // ATTENTION
  77. bestCoup = coupsJoues.getBestCoup();
  78.  
  79. }
  80.  
  81. return bestCoup;
  82. }
  83. /**
  84. * This function returns the badest max() if deep is > 1 for each possible plays of an AwaleBoard.
  85. * Else, returns the badest move for this AwaleBoard.
  86. * @param awaleBoard
  87. * @param profondeur
  88. * @param premierCoup
  89. * @return
  90. */
  91. public Move min(AwaleBoard awaleBoard, int profondeur, int premierCoup) {
  92. ArrayList<Integer> indicesCoupsPossibles = getCoupsPossible(awaleBoard, 0);
  93. Moves coupsJoues = new Moves(awaleBoard, new Move[indicesCoupsPossibles.size()]);
  94. Move badestCoup = null;
  95. profondeur--;
  96. if (indicesCoupsPossibles.size() > 0) {
  97.  
  98. for (int i = 0; i < coupsJoues.size(); i++) {
  99.  
  100. coupsJoues.coups[i] = new Move(awaleBoard, awaleBoard.play(indicesCoupsPossibles.get(i)), premierCoup,
  101. awaleBoard.getScore(1), awaleBoard.getScore(0));
  102. coupsJoues.coups[i].setGainGenereHumain(
  103. coupsJoues.coups[i].gainGenereParHumain + coupsJoues.coups[i].calculeGainGenere(0));
  104.  
  105. }
  106.  
  107. // ATTENTION
  108. if (profondeur > 1) {
  109. for (int i = 0; i < coupsJoues.size(); i++) {
  110. if (getCoupsPossible(coupsJoues.coups[i].tableauApresCoup, 1).size() > 0)
  111. coupsJoues.coups[i] = max(coupsJoues.coups[i].tableauApresCoup, profondeur,
  112. coupsJoues.coups[i].indiceDuCoupAyantEteJoue);
  113.  
  114. }
  115. }
  116. // ATTENTION
  117.  
  118. badestCoup = coupsJoues.getBadestCoup();
  119. }
  120. return badestCoup;
  121. }
  122.  
  123. /**
  124. * @return the firstBoard
  125. */
  126. public AwaleBoard getFirstBoard() {
  127. return new AwaleBoard(firstBoard);
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement