Advertisement
madegoff

rows

Jun 1st, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. import org.junit.Test;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.stream.Collector;
  7. import java.util.stream.Collectors;
  8.  
  9. /**
  10. * This class implements a game of Row of Bowls.
  11. * For the games rules see Blatt05. The goal is to find an optimal strategy.
  12. */
  13. public class RowOfBowls {
  14.  
  15. public RowOfBowls() {
  16. }
  17.  
  18. /**
  19. * Implements an optimal game using dynamic programming
  20. * @param values array of the number of marbles in each bowl
  21. * @return number of game points that the first player gets, provided both parties play optimally
  22. */
  23. public int maxGain(int[] values)
  24. {
  25. // TODO
  26. //die Tabelle fuer die besten scores erstellt
  27. int n = values.length;
  28. int[][] score_tab = new int[n][n]; //schon mit nullen initialisiert?
  29. //int length; //laenge der betrachteten zahlenfolge (1 fuer 1 schuessel, 2 fuer zwei schuessel usw)
  30. //int diag;
  31.  
  32. if (n==0) return 0; //keine schuessel
  33.  
  34. //diagonale fühlen - auf der diagonale steht uns immer eine schuessel zur verfuegung
  35. /*
  36. for (int i = 0; i < score_tab.length; i++) {
  37. for (int j = 0; j < score_tab[i].length; j++) {
  38.  
  39. diag= i - j;
  40. // n = 0 fuer "nulle" diagonale -> leange 1
  41. // n = 1 fuer die erste diagonale -> laenge = 2, usw...
  42. // diag or diag_counter is = n-1 -> bessere berechnung
  43.  
  44. //diagonale 0 -> laenge 1 -> eine schuessel steht zur verfuegung
  45. if (diag == 0) score_tab[i][j] = values[i];
  46. // {4 7 3} laenge 3 (diag_counter = 2) -> max(4 - {7 3}, 3 - {4 7})
  47. int pickLeft = score_tab[i-diag][j] - score_tab[i][j+1];
  48. int pickRight = score_tab[i][j+diag] - score_tab[i-1][j];
  49. score_tab[i][j] = Math.max(pickRight, pickLeft);
  50. }
  51. }
  52.  
  53. */
  54. // Basisfälle: Nur eine Schüssel
  55. // Basisfälle: Nur eine Schüssel
  56. for (int k = 0; k < n; k++) {
  57. score_tab[k][k] = values[k];
  58. }
  59.  
  60. for (int len = 2; len <= n; len++) {
  61. for (int i = len-1; i < n; i++) {
  62. int j = i - len + 1;
  63. int pickLeft = (j >= 0 && j+1 < n) ? values[j] - score_tab[i][j+1] : 0;
  64. int pickRight = (i-1 >= 0 && j >= 0) ? values[i] - score_tab[i-1][j] : 0;
  65. score_tab[i][j] = Math.max(pickLeft, pickRight);
  66. }
  67. }
  68.  
  69. return score_tab[n-1][0];
  70. }
  71.  
  72.  
  73.  
  74. /**
  75. * Implements an optimal game recursively.
  76. *
  77. * @param values array of the number of marbles in each bowl
  78. * @return number of game points that the first player gets, provided both parties play optimally
  79. */
  80. public int maxGainRecursive(int[] values) {
  81. if (values.length == 0) return 0;
  82. return maxGainRecursive(values, 0, values.length - 1);
  83. }
  84.  
  85. private int maxGainRecursive(int[] values, int left, int right) {
  86. // Basisfall: Wenn keine Murmeln mehr übrig sind
  87. /*if (left > right) {
  88. return scoreA - scoreB;
  89. }
  90.  
  91. int pickLeft, pickRight;
  92. if (player == 1) { // Spieler A ist dran, versucht zu maximieren
  93. pickLeft = maxGainRecursive(values, left + 1, right, -player, scoreA + values[left], scoreB);
  94. pickRight = maxGainRecursive(values, left, right - 1, -player, scoreA + values[right], scoreB);
  95. return Math.max(pickLeft, pickRight);
  96. } else { // Spieler B ist dran, versucht zu minimieren
  97. pickLeft = -maxGainRecursive(values, left + 1, right, -player, scoreA, scoreB + values[left]);
  98. pickRight = -maxGainRecursive(values, left, right - 1, -player, scoreA, scoreB + values[right]);
  99. return Math.max(pickLeft, pickRight); // Negation für Minimierung
  100. }
  101.  
  102. */
  103. //anker
  104. if (right == left){
  105. return values[right];
  106. }
  107.  
  108. int res;
  109. int score_right;
  110. int score_left;
  111.  
  112. //linken teilbaum
  113. int pickLeft = values[left];
  114. score_left = pickLeft-maxGainRecursive(values, left+1, right);
  115. // fuer 7 4 3 2 wenn 7 nehmen: score = 7 - maxScore(4 3 2)
  116.  
  117. //rechten teilbaum
  118. int pickRight = values[right];
  119. score_right = pickRight-maxGainRecursive(values, left, right-1);
  120. // fuer 7 4 3 2 wenn 2 nehmen: score = 2 - maxScore(7 4 3)
  121.  
  122. res = Math.max(score_left, score_right);
  123. return res;
  124. }
  125.  
  126.  
  127. /**
  128. * Calculates an optimal sequence of bowls using the partial solutions found in maxGain(int values)
  129. * @return optimal sequence of chosen bowls (represented by the index in the values array)
  130. */
  131. public Iterable<Integer> optimalSequence()
  132. {
  133. // TODO
  134. return null;
  135. }
  136.  
  137.  
  138.  
  139. public static void main(String[] args)
  140. {
  141. // For Testing
  142. RowOfBowls game = new RowOfBowls();
  143. int[] values = {7, 4, 3, 2};
  144. System.out.println("Max gain (recursive): " + game.maxGainRecursive(values));
  145. //game.testRowOfBowls();
  146. }
  147. }
  148.  
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement