package reversi.model; /** * Class for a GameTree. */ public class GameTree { /** * Root gamenode of the gametree. */ private GameNode root; /** * Constructs a new gametree. * * @param board of the root node * @param level depth of the tree */ GameTree(final Reversi board, final int level) { this.root = new GameNode(new Field(0, 0), board.clone()); fillTree(root, level); generateScores(root); } /** * * @return Root of the gametree. */ public final GameNode getRoot() { return root; } /** * Fill the Tree with nodes. * * @param node Node to fill (in most cases root gamenode) * @param depth depth of the tree (level) */ private void fillTree(final GameNode node, final int depth) { if (depth > 0) { node.fillNode(); for (GameNode n : node.getChildren()) { fillTree(n, (depth - 1)); } } } /** * Generates and sets the score of a Gamenode. * * @param node Gamenode to set */ public final void generateScores(final GameNode node) { Reversi currentBoard = node.getState(); if (!node.hasChild()) { node.setScore(currentBoard.score()); } else { double score; score = currentBoard.score(); for (GameNode child : node.getChildren()) { generateScores(child); } if (!currentBoard.next().getType()) { score += node.getChildrensHighestScore().getScore(); } else { score += node.getChildrensLowestScore().getScore(); } node.setScore(score); } } }