Minimax refactor (#2212)
This commit is contained in:
parent
d6d5386613
commit
c99bb7fced
|
@ -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<Integer> getPossibleStates(int noOfBonesInHeap) {
|
||||
List<Integer> 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<Integer> getPossibleStates(int noOfBonesInHeap) {
|
||||
return IntStream.rangeClosed(1, 3).boxed()
|
||||
.map(i -> noOfBonesInHeap - i)
|
||||
.filter(newHeapCount -> newHeapCount >= 0)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Node> children) {
|
||||
if (isMaxPlayer) {
|
||||
return Collections.max(children, Comparator.comparing(c -> c.getScore()));
|
||||
} else {
|
||||
return Collections.min(children, Comparator.comparing(c -> c.getScore()));
|
||||
}
|
||||
Comparator<Node> byScoreComparator = Comparator.comparing(Node::getScore);
|
||||
|
||||
return children.stream()
|
||||
.max(isMaxPlayer ? byScoreComparator : byScoreComparator.reversed())
|
||||
.orElseThrow(NoSuchElementException::new);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Node> getChildren() {
|
||||
List<Node> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<Node> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public void addChild(Node newNode) {
|
||||
void addChild(Node newNode) {
|
||||
children.add(newNode);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue