Minimax refactor (#2212)

This commit is contained in:
Grzegorz Piwowarek 2017-07-06 12:01:49 +02:00 committed by GitHub
parent d6d5386613
commit c99bb7fced
4 changed files with 25 additions and 52 deletions

View File

@ -1,17 +1,14 @@
package com.baeldung.algorithms.minimax; package com.baeldung.algorithms.minimax;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class GameOfBones { class GameOfBones {
public static List<Integer> getPossibleStates(int noOfBonesInHeap) { static List<Integer> getPossibleStates(int noOfBonesInHeap) {
List<Integer> listOfPossibleHeaps = new ArrayList<>(); return IntStream.rangeClosed(1, 3).boxed()
for (int i = 1; i <= 3; i++) { .map(i -> noOfBonesInHeap - i)
int newHeapCount = noOfBonesInHeap - i; .filter(newHeapCount -> newHeapCount >= 0)
if (newHeapCount >= 0) { .collect(Collectors.toList());
listOfPossibleHeaps.add(newHeapCount);
}
}
return listOfPossibleHeaps;
} }
} }

View File

@ -1,8 +1,8 @@
package com.baeldung.algorithms.minimax; package com.baeldung.algorithms.minimax;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
public class MiniMax { public class MiniMax {
private Tree tree; private Tree tree;
@ -11,10 +11,6 @@ public class MiniMax {
return tree; return tree;
} }
public void setTree(Tree tree) {
this.tree = tree;
}
public void constructTree(int noOfBones) { public void constructTree(int noOfBones) {
tree = new Tree(); tree = new Tree();
Node root = new Node(noOfBones, true); Node root = new Node(noOfBones, true);
@ -37,7 +33,7 @@ public class MiniMax {
public boolean checkWin() { public boolean checkWin() {
Node root = tree.getRoot(); Node root = tree.getRoot();
checkWin(root); checkWin(root);
return root.getScore() == 1 ? true : false; return root.getScore() == 1;
} }
private void checkWin(Node node) { private void checkWin(Node node) {
@ -45,11 +41,7 @@ public class MiniMax {
boolean isMaxPlayer = node.isMaxPlayer(); boolean isMaxPlayer = node.isMaxPlayer();
children.forEach(child -> { children.forEach(child -> {
if (child.getNoOfBones() == 0) { if (child.getNoOfBones() == 0) {
if (isMaxPlayer) { child.setScore(isMaxPlayer ? 1 : -1);
child.setScore(1);
} else {
child.setScore(-1);
}
} else { } else {
checkWin(child); checkWin(child);
} }
@ -59,10 +51,10 @@ public class MiniMax {
} }
private Node findBestChild(boolean isMaxPlayer, List<Node> children) { private Node findBestChild(boolean isMaxPlayer, List<Node> children) {
if (isMaxPlayer) { Comparator<Node> byScoreComparator = Comparator.comparing(Node::getScore);
return Collections.max(children, Comparator.comparing(c -> c.getScore()));
} else { return children.stream()
return Collections.min(children, Comparator.comparing(c -> c.getScore())); .max(isMaxPlayer ? byScoreComparator : byScoreComparator.reversed())
} .orElseThrow(NoSuchElementException::new);
} }
} }

View File

@ -15,39 +15,27 @@ public class Node {
children = new ArrayList<>(); children = new ArrayList<>();
} }
public int getNoOfBones() { int getNoOfBones() {
return noOfBones; return noOfBones;
} }
public void setNoOfBones(int noOfBones) { boolean isMaxPlayer() {
this.noOfBones = noOfBones;
}
public boolean isMaxPlayer() {
return isMaxPlayer; return isMaxPlayer;
} }
public void setMaxPlayer(boolean maxPlayer) { int getScore() {
isMaxPlayer = maxPlayer;
}
public int getScore() {
return score; return score;
} }
public void setScore(int score) { void setScore(int score) {
this.score = score; this.score = score;
} }
public List<Node> getChildren() { List<Node> getChildren() {
return children; return children;
} }
public void setChildren(List<Node> children) { void addChild(Node newNode) {
this.children = children;
}
public void addChild(Node newNode) {
children.add(newNode); children.add(newNode);
} }

View File

@ -3,18 +3,14 @@ package com.baeldung.algorithms.minimax;
public class Tree { public class Tree {
private Node root; private Node root;
public Tree() { Tree() {
} }
public Tree(Node root) { Node getRoot() {
this.root = root;
}
public Node getRoot() {
return root; return root;
} }
public void setRoot(Node root) { void setRoot(Node root) {
this.root = root; this.root = root;
} }
} }