import java.util.*;
public class GameTreeNode implements PlayConstants {
    static public int nodeCount = 0;
     
    private int value;
    private int type;
    private int bestMove = -1;
    private Position pos;    
    private GameTreeNode[] child = new GameTreeNode[9];  

    public int getBestMove() {
        return bestMove;
    }
    
    public GameTreeNode(Position p) { nodeCount++;
        pos = p; type = p.getPlToMv();
        // hab ich schon verloren?
        if (p.won() != NONE) { value = p.won(); return; } 
        // no more moves --> no winner
        Iterator<Integer> moves = p.getMoves();
        if (!moves.hasNext()) { value = DRAW; return; } 
        value = -2*type;
        while (moves.hasNext()) {
            int m = moves.next();
            child[m] = new GameTreeNode(p.makeMove(m));
            if (type == MIN && child[m].value < value ||
                type == MAX && child[m].value > value) {
                value = child[m].value;
                bestMove = m;
}   }   }   } 
