diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java b/algorithms/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java index 2949faede4..8e14afcf7a 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/minimax/GameOfBones.java @@ -1,17 +1,14 @@ package com.baeldung.algorithms.minimax; -import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; -public class GameOfBones { - public static List getPossibleStates(int noOfBonesInHeap) { - List listOfPossibleHeaps = new ArrayList<>(); - for (int i = 1; i <= 3; i++) { - int newHeapCount = noOfBonesInHeap - i; - if (newHeapCount >= 0) { - listOfPossibleHeaps.add(newHeapCount); - } - } - return listOfPossibleHeaps; +class GameOfBones { + static List getPossibleStates(int noOfBonesInHeap) { + return IntStream.rangeClosed(1, 3).boxed() + .map(i -> noOfBonesInHeap - i) + .filter(newHeapCount -> newHeapCount >= 0) + .collect(Collectors.toList()); } } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java b/algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java index 9857a44db6..ce2ba03af5 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/minimax/MiniMax.java @@ -1,8 +1,8 @@ package com.baeldung.algorithms.minimax; -import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.NoSuchElementException; public class MiniMax { private Tree tree; @@ -11,10 +11,6 @@ public class MiniMax { return tree; } - public void setTree(Tree tree) { - this.tree = tree; - } - public void constructTree(int noOfBones) { tree = new Tree(); Node root = new Node(noOfBones, true); @@ -37,7 +33,7 @@ public class MiniMax { public boolean checkWin() { Node root = tree.getRoot(); checkWin(root); - return root.getScore() == 1 ? true : false; + return root.getScore() == 1; } private void checkWin(Node node) { @@ -45,11 +41,7 @@ public class MiniMax { boolean isMaxPlayer = node.isMaxPlayer(); children.forEach(child -> { if (child.getNoOfBones() == 0) { - if (isMaxPlayer) { - child.setScore(1); - } else { - child.setScore(-1); - } + child.setScore(isMaxPlayer ? 1 : -1); } else { checkWin(child); } @@ -59,10 +51,10 @@ public class MiniMax { } private Node findBestChild(boolean isMaxPlayer, List children) { - if (isMaxPlayer) { - return Collections.max(children, Comparator.comparing(c -> c.getScore())); - } else { - return Collections.min(children, Comparator.comparing(c -> c.getScore())); - } + Comparator byScoreComparator = Comparator.comparing(Node::getScore); + + return children.stream() + .max(isMaxPlayer ? byScoreComparator : byScoreComparator.reversed()) + .orElseThrow(NoSuchElementException::new); } } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java b/algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java index 4768935dcb..4ceef0073d 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/minimax/Node.java @@ -15,39 +15,27 @@ public class Node { children = new ArrayList<>(); } - public int getNoOfBones() { + int getNoOfBones() { return noOfBones; } - public void setNoOfBones(int noOfBones) { - this.noOfBones = noOfBones; - } - - public boolean isMaxPlayer() { + boolean isMaxPlayer() { return isMaxPlayer; } - public void setMaxPlayer(boolean maxPlayer) { - isMaxPlayer = maxPlayer; - } - - public int getScore() { + int getScore() { return score; } - public void setScore(int score) { + void setScore(int score) { this.score = score; } - public List getChildren() { + List getChildren() { return children; } - public void setChildren(List children) { - this.children = children; - } - - public void addChild(Node newNode) { + void addChild(Node newNode) { children.add(newNode); } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java b/algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java index edc16ee962..34c56cdd58 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/minimax/Tree.java @@ -3,18 +3,14 @@ package com.baeldung.algorithms.minimax; public class Tree { private Node root; - public Tree() { + Tree() { } - public Tree(Node root) { - this.root = root; - } - - public Node getRoot() { + Node getRoot() { return root; } - public void setRoot(Node root) { + void setRoot(Node root) { this.root = root; } }