package com.meta1203.ChessPlayer;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/*
 * A turn that is sorted into a tree to make calculating moves easier
 * The status of the board in the Turn object is AFTER the turn is made by the corresponding player
 * 
 * Lovingly yanked from https://github.com/gt4dev/yet-another-tree-structure
 */

public class Turn implements Comparable <Turn>, Comparator<Turn> {

	private Board board;
	private Turn parent;
	private boolean whiteTurn;
	private List<Turn> children;
	private int depth;
	
	// Create a root turn
	public Turn(Board data) {
		this.board = data;
		this.children = new LinkedList<Turn>();
		this.depth = 1;
		this.parent = null;
		this.whiteTurn = false;
	}
	
	// Constructor for child turns
	private Turn(Board data, int depth, boolean white) {
		this.board = data;
		this.children = new LinkedList<Turn>();
		this.depth = depth;
	}
	
	// Create a child turn
	public Turn addChild(Board child) {
		Turn childNode = new Turn(child, this.depth + 1, !this.whiteTurn);
		childNode.parent = this;
		this.children.add(childNode);
		return childNode;
	}
	
	// Sorting mechanism, will probably be replaced with a largest function instead
	public void sort() {
		if (children.isEmpty()) return;
		Collections.sort(children);
		for (Turn x : children) {
			x.sort();
		}
	}
	
	// Getters
	public Board getBoard() {
		return board;
	}
	public Turn getParent() {
		return parent;
	}
	public List<Turn> getChildren() {
		return children;
	}
	public boolean isWhiteTurn() {
		return whiteTurn;
	}
	
	// Assign a hard point value to turns based on the black score of the turn and all of its children
	public int getValue() {
		int ret = this.board.getBlackPointValue() - this.board.getWhitePointValue();
		// Point values should decrease exponentially by turn
		for (Turn x : this.getChildren()) {
			ret += x.getValue()/2;
		}
		return ret;
	}
	
	// Comparison
	@Override
	public int compare(Turn one, Turn another) {
		// Weigh both points and # of turns made
		return one.getValue() - another.getValue();
	}

	@Override
	public int compareTo(Turn other) {
		return this.compare(this, other);
	}
}
@@ -32,38 +32,8 @@
 r;%0A%0A
-import java.util.Collections;%0A
 impo
@@ -552,21 +552,21 @@
 ate int 
-depth
+value
 ;%0A%09%0A%09// 
@@ -678,26 +678,8 @@
 ();%0A
-%09%09this.depth = 1;%0A
 %09%09th
@@ -722,16 +722,34 @@
  false;%0A
+%09%09this.value = 0;%0A
 %09%7D%0A%09%0A%09//
@@ -806,19 +806,8 @@
 ata,
- int depth,
  boo
@@ -894,21 +894,69 @@
 his.
-depth = depth
+value = data.getBlackPointValue() - data.getWhitePointValue()
 ;%0A%09%7D
@@ -1057,24 +1057,8 @@
 ild,
- this.depth + 1,
  !th
@@ -1137,195 +1137,219 @@
 ;%0A%09%09
-return childNode;%0A%09%7D%0A%09%0A%09// Sorting mechanism, will probably be replaced with a largest function instead%0A%09public void sort() %7B%0A%09%09if (children.isEmpty()) return;%0A%09%09Collections.sort(
+// Update value of the Turn when a child is added%0A%09%09this.value = calculateValue();%0A%09%09return childNode;%0A%09%7D%0A%09%0A%09// Get best available computer move from the children%0A%09public Turn bestMove() %7B%0A%09%09Turn best = 
 children
 );%0A%09
@@ -1344,16 +1344,22 @@
 children
+.get(0
 );%0A%09%09for
@@ -1388,21 +1388,83 @@
 %0A%09%09%09
-x.sort();%0A%09%09%7D
+if (x.getValue() %3E best.getValue()) %7B%0A%09%09%09%09best = x;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return best;
 %0A%09%7D%0A
@@ -1678,24 +1678,346 @@
 iteTurn;%0A%09%7D%0A
+%09public int getValue() %7B%0A%09%09return value;%0A%09%7D%0A%09%0A%09// Get max depth of children%0A%09public int getDepth() %7B%0A%09%09int depth = 0;%0A%09%09Turn t = this;%0A%09%09boolean containsChild = !t.children.isEmpty();%0A%09%09%0A%09%09while (containsChild) %7B%0A%09%09%09t = t.children.get(0);%0A%09%09%09depth += 1;%0A%09%09%09containsChild = !t.children.isEmpty();%0A%09%09%7D%0A%09%09%0A%09%09return depth;%0A%09%7D%0A
 %09%0A%09// Assign
@@ -2108,29 +2108,36 @@
 ldren%0A%09p
-ublic int get
+rivate int calculate
 Value() 
@@ -2323,19 +2323,25 @@
 et += x.
-get
+calculate
 Value()/

Modify the Turn class for efficiency