Advertisement
madegoff

RowOfBowls(todo)

May 30th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import java.util.stream.Collector;
  5. import java.util.stream.Collectors;
  6.  
  7. /**
  8. * This class implements a game of Row of Bowls.
  9. * For the games rules see Blatt05. The goal is to find an optimal strategy.
  10. */
  11. public class RowOfBowls {
  12.  
  13. public RowOfBowls() {
  14. }
  15.  
  16. /**
  17. * Implements an optimal game using dynamic programming
  18. * @param values array of the number of marbles in each bowl
  19. * @return number of game points that the first player gets, provided both parties play optimally
  20. */
  21. /*public int maxGain(int[] values)
  22. {
  23. // TODO
  24.  
  25. }
  26.  
  27. */
  28.  
  29. /**
  30. * Implements an optimal game recursively.
  31. *
  32. * @param values array of the number of marbles in each bowl
  33. * @return number of game points that the first player gets, provided both parties play optimally
  34. */
  35. public int maxGainRecursive(int[] values) {
  36. // TODO
  37.  
  38. LinkedList<Integer> list_values
  39. = (LinkedList<Integer>) Arrays.stream(values).boxed().collect(Collectors.toList());
  40.  
  41. return maxGainRecursive(list_values, 0, 0, 1);
  42.  
  43. }
  44.  
  45. public int maxGainRecursive(LinkedList<Integer> values, int score_a, int score_b, int player) {
  46. // TODO
  47.  
  48. //rekursionsende wenn das spiel beendet
  49. if (values.isEmpty()){
  50. int max = Math.max(score_a, score_b);
  51. int min = Math.min(score_a, score_b);
  52. return max - min;
  53. }
  54.  
  55. // 7 4 3 2 -> 7 2 koennen in diesem zug genommen werden
  56. int num_l = values.getFirst(); //7
  57. int num_r = values.getLast(); //2
  58.  
  59. LinkedList<Integer> values_left = new LinkedList<>(values.subList(1, values.size()));
  60. LinkedList<Integer> values_right = new LinkedList<>(values.subList(0, values.size() - 1));
  61.  
  62. //rekursiver aufruf linker teilbaum
  63. if (player == -1){ //spieler:in A
  64. score_a += num_l;
  65. }
  66. else score_b += num_l;
  67. int left = -maxGainRecursive(values_left, score_a, score_b, -player);
  68.  
  69.  
  70. //rekursiver aufruf rechter teilbaum
  71. if (player == -1){ //spieler:in A
  72. score_a += num_r;
  73. }
  74. else score_b += num_r;
  75. int right = -maxGainRecursive(values_right, score_a, score_b, -player);
  76.  
  77. return Math.max(left, right);
  78. }
  79.  
  80.  
  81. /**
  82. * Calculates an optimal sequence of bowls using the partial solutions found in maxGain(int values)
  83. * @return optimal sequence of chosen bowls (represented by the index in the values array)
  84. */
  85. /*public Iterable<Integer> optimalSequence()
  86. {
  87. // TODO
  88. }
  89.  
  90. */
  91.  
  92.  
  93. public static void main(String[] args)
  94. {
  95. // For Testing
  96. RowOfBowls game = new RowOfBowls();
  97. int[] values = {7, 4, 3, 2};
  98. System.out.println("Max gain (recursive): " + game.maxGainRecursive(values));
  99.  
  100. }
  101. }
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement