Skip to content

Commit c99bb7f

Browse files
authored
Minimax refactor (eugenp#2212)
1 parent d6d5386 commit c99bb7f

File tree

4 files changed

+25
-52
lines changed

4 files changed

+25
-52
lines changed
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.baeldung.algorithms.minimax;
22

3-
import java.util.ArrayList;
43
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.IntStream;
56

6-
public class GameOfBones {
7-
public static List<Integer> getPossibleStates(int noOfBonesInHeap) {
8-
List<Integer> listOfPossibleHeaps = new ArrayList<>();
9-
for (int i = 1; i <= 3; i++) {
10-
int newHeapCount = noOfBonesInHeap - i;
11-
if (newHeapCount >= 0) {
12-
listOfPossibleHeaps.add(newHeapCount);
13-
}
14-
}
15-
return listOfPossibleHeaps;
7+
class GameOfBones {
8+
static List<Integer> getPossibleStates(int noOfBonesInHeap) {
9+
return IntStream.rangeClosed(1, 3).boxed()
10+
.map(i -> noOfBonesInHeap - i)
11+
.filter(newHeapCount -> newHeapCount >= 0)
12+
.collect(Collectors.toList());
1613
}
1714
}

algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.baeldung.algorithms.minimax;
22

3-
import java.util.Collections;
43
import java.util.Comparator;
54
import java.util.List;
5+
import java.util.NoSuchElementException;
66

77
public class MiniMax {
88
private Tree tree;
@@ -11,10 +11,6 @@ public Tree getTree() {
1111
return tree;
1212
}
1313

14-
public void setTree(Tree tree) {
15-
this.tree = tree;
16-
}
17-
1814
public void constructTree(int noOfBones) {
1915
tree = new Tree();
2016
Node root = new Node(noOfBones, true);
@@ -37,19 +33,15 @@ private void constructTree(Node node) {
3733
public boolean checkWin() {
3834
Node root = tree.getRoot();
3935
checkWin(root);
40-
return root.getScore() == 1 ? true : false;
36+
return root.getScore() == 1;
4137
}
4238

4339
private void checkWin(Node node) {
4440
List<Node> children = node.getChildren();
4541
boolean isMaxPlayer = node.isMaxPlayer();
4642
children.forEach(child -> {
4743
if (child.getNoOfBones() == 0) {
48-
if (isMaxPlayer) {
49-
child.setScore(1);
50-
} else {
51-
child.setScore(-1);
52-
}
44+
child.setScore(isMaxPlayer ? 1 : -1);
5345
} else {
5446
checkWin(child);
5547
}
@@ -59,10 +51,10 @@ private void checkWin(Node node) {
5951
}
6052

6153
private Node findBestChild(boolean isMaxPlayer, List<Node> children) {
62-
if (isMaxPlayer) {
63-
return Collections.max(children, Comparator.comparing(c -> c.getScore()));
64-
} else {
65-
return Collections.min(children, Comparator.comparing(c -> c.getScore()));
66-
}
54+
Comparator<Node> byScoreComparator = Comparator.comparing(Node::getScore);
55+
56+
return children.stream()
57+
.max(isMaxPlayer ? byScoreComparator : byScoreComparator.reversed())
58+
.orElseThrow(NoSuchElementException::new);
6759
}
6860
}

algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,27 @@ public Node(int noOfBones, boolean isMaxPlayer) {
1515
children = new ArrayList<>();
1616
}
1717

18-
public int getNoOfBones() {
18+
int getNoOfBones() {
1919
return noOfBones;
2020
}
2121

22-
public void setNoOfBones(int noOfBones) {
23-
this.noOfBones = noOfBones;
24-
}
25-
26-
public boolean isMaxPlayer() {
22+
boolean isMaxPlayer() {
2723
return isMaxPlayer;
2824
}
2925

30-
public void setMaxPlayer(boolean maxPlayer) {
31-
isMaxPlayer = maxPlayer;
32-
}
33-
34-
public int getScore() {
26+
int getScore() {
3527
return score;
3628
}
3729

38-
public void setScore(int score) {
30+
void setScore(int score) {
3931
this.score = score;
4032
}
4133

42-
public List<Node> getChildren() {
34+
List<Node> getChildren() {
4335
return children;
4436
}
4537

46-
public void setChildren(List<Node> children) {
47-
this.children = children;
48-
}
49-
50-
public void addChild(Node newNode) {
38+
void addChild(Node newNode) {
5139
children.add(newNode);
5240
}
5341

algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
public class Tree {
44
private Node root;
55

6-
public Tree() {
6+
Tree() {
77
}
88

9-
public Tree(Node root) {
10-
this.root = root;
11-
}
12-
13-
public Node getRoot() {
9+
Node getRoot() {
1410
return root;
1511
}
1612

17-
public void setRoot(Node root) {
13+
void setRoot(Node root) {
1814
this.root = root;
1915
}
2016
}

0 commit comments

Comments
 (0)