Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class HelloWorld
  5. {
  6. public static void main(String args[])
  7. {
  8. Mancala m = new Mancala(true);
  9. }
  10. }
  11.  
  12. class Mancala
  13. {
  14. private int[] board;
  15. private boolean avalanche;
  16. private boolean free = true;
  17. Scanner scan;
  18.  
  19. public Mancala(Boolean avalanche)
  20. {
  21. this.avalanche = avalanche;
  22. scan = new Scanner(System.in);
  23.  
  24. board = new int[14];
  25. for(int i = 0; i < 14; i++) // 0 = opponent's mancala, moving counterclockwise
  26. {
  27. board[i] = scan.nextInt();
  28. }
  29.  
  30. while(!isEmpty(board) && free)
  31. {
  32. int bestMove = bestMove(board, 0)[0];
  33. System.out.println(bestMove);
  34. if(board[bestMove] == 0)
  35. break;
  36. moveBoard(bestMove);
  37. print(board);
  38. }
  39. }
  40.  
  41. public boolean isEmpty(int[] b)
  42. {
  43. for(int i = 1; i < 7; i++)
  44. {
  45. if(b[i] != 0)
  46. return false;
  47. }
  48. System.out.println("Empty board");
  49. return true;
  50. }
  51.  
  52. public void moveBoard(int slot)
  53. {
  54. print(board);
  55. int stones = board[slot];
  56. if(stones == 0)
  57. System.out.println("Something's wrong, Jim.");
  58.  
  59. board[slot] = 0;
  60.  
  61. while(stones > 0)
  62. {
  63. slot = slot % 13 + 1; // next hole
  64. board[slot]++; // add one to the hole
  65. stones--; // subtract one from stones in hand
  66. }
  67. if(board[slot] != 1 && slot != 7)
  68. moveBoard(slot);
  69. else if(slot == 7)
  70. free = true;
  71. else
  72. free = false;
  73. }
  74.  
  75. public int[] bestMove(int[] s, int currentScore) // call move() 6 times, returning the slot of the best option
  76. {
  77. boolean match = true;
  78. int[] state = new int[14]; // duplicate the array to avoid aliasing
  79. for(int i = 0; i < 14; i++)
  80. {
  81. if(s[i] != board[i])
  82. match = false;
  83. state[i] = s[i];
  84. }
  85.  
  86. boolean empty = true; // if your board is empty, return best possible value
  87. for(int i = 1; i < 7; i++)
  88. {
  89. if(state[i] != 0)
  90. empty = false;
  91. }
  92. if(empty)
  93. return new int[]{-1, currentScore};
  94.  
  95. int best = -1;
  96. int bestScore = -1;
  97.  
  98. for(int i = 1; i < 7; i++) // range constricted for testing
  99. {
  100. int thisScore = move(i, state, currentScore);
  101. if(match)
  102. System.out.println("Slot: " + i + " Score: " + thisScore);
  103.  
  104. if(thisScore > bestScore)
  105. {
  106. best = i;
  107. bestScore = thisScore;
  108. }
  109. }
  110.  
  111. return new int[]{best, bestScore};
  112. }
  113.  
  114. public int move(int position, int[] s, int score) // return the number of stones that this move scores
  115. {
  116. int[] state = new int[14]; // duplicate the array to avoid aliasing
  117. for(int i = 0; i < 14; i++)
  118. {
  119. state[i] = s[i];
  120. }
  121.  
  122. if(state[position] == 0)
  123. {
  124. return score;
  125. }
  126.  
  127. int stones = state[position];
  128. state[position] = 0; // empty the current hole
  129. while(stones > 0)
  130. {
  131. position = position % 13 + 1;
  132. state[position]++; // add one to the hole
  133. stones--; // subtract one from stones in hand
  134. if(position == 7) // award a point if caching a stone
  135. score++;
  136. }
  137. if(position == 7) // check if free move
  138. {
  139. int[] bestMove = bestMove(state, score);
  140. score = bestMove[1];
  141. return score;
  142. }
  143. if(state[position] == 1) // if last stone was placed in empty hole, end recursion
  144. return score;
  145. return move(position, state, score); // otherwise, continue the avalanche
  146. }
  147.  
  148. public void print(int[] b)
  149. {
  150. for(int i = 0; i < 14; i++)
  151. {
  152. System.out.print(b[i] + " ");
  153. }
  154. System.out.println();
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement