每个节点中特定于节点的变量都在变化

时间:2018-10-22 03:11:06

标签: java tree nodes

我试图生成一个包含所有井字棋盘状态的树,但是每次创建新的子节点时,它都会覆盖父节点,直到使用当前生成的棋盘一直覆盖到根节点。

>

这是我的TreeNode类:

public class TreeNode<T> implements Iterable<TreeNode<T>>{
    boolean isX = false;
    private int weight = 0, depth = 0;
    private TreeNode<T> parent;
    private List<TreeNode<T>> children;
    Board myBoard = new Board();

    public TreeNode(Board myBoard, boolean isX, int depth) {
        this.isX = !isX;
        this.depth = depth + 1;
        this.children = new LinkedList<TreeNode<T>>();
        this.myBoard = myBoard;
    }

    public TreeNode<T> addChild(Board board) {
        TreeNode<T> childNode = new TreeNode<T>(board, isX, depth);
        childNode.parent = this;
        this.children.add(childNode);
        return childNode;
    }

    @Override
    public Iterator<TreeNode<T>> iterator() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

这就是我所谓的创建根节点并生成树的方法:

public void chartCreation(boolean ifStart, Board board) {
    TreeNode<TreeNode> root = new TreeNode<>(board, false, 0);
    calculation(root);
}

public void calculation(TreeNode<TreeNode> newNode) {
    for (int i = 0; i < newNode.myBoard.states.length; i++) {
        if (newNode.myBoard.states[i] == ' ') {
            Board tempData = newNode.myBoard;
            if (newNode.isX) {
                tempData.states[i] = 'x';
            } else {
                tempData.states[i] = 'o';
            }
            calculation(newNode.addChild(tempData));
        }
    }
}

使用的董事会课程:

public class Board {
    char states[] = new char[9];
    char winningValue = ' ';

    public Board() {
        for (int i = 0; i < 9; i ++) {
            states[i] = ' ';
        }
    }

    public void printBoard() {

        System.out.printf("   |   |           |   |   \n %c | %c | %c       0 | 1 | 2 \n   |   |           |   |   \n--- --- ---     --- --- ---\n   |   |           |   |   \n %c | %c | %c       3 | 4 | 5 \n   |   |           |   |   \n--- --- ---     --- --- ---\n   |   |           |   |   \n %c | %c | %c       6 | 7 | 8 \n   |   |           |   |   \n", states[0], states[1], states[2], states[3], states[4], states[5], states[6], states[7], states[8]);
    }

    public boolean isWon() {
        if ( states[0] == states[1] && states[1] == states[2] && states[0] != ' ' ){
            winningValue = states[0];
            return true;
        } else if ( states[3] == states[4] && states[4] == states[5] && states[3] != ' ' ){
            winningValue = states[3];
            return true;
        } else if ( states[6] == states[7] && states[7] == states[8] && states[6] != ' ' ){
            winningValue = states[6];
            return true;
        } else if ( states[0] == states[3] && states[3] == states[6] && states[0] != ' ' ){
            winningValue = states[0];
            return true;
        } else if ( states[1] == states[4] && states[4] == states[7] && states[1] != ' ' ){
            winningValue = states[1];
            return true;
        } else if ( states[2] == states[5] && states[5] == states[8] && states[2] != ' ' ){
            winningValue = states[2];
            return true;
        } else if ( states[0] == states[4] && states[4] == states[8] && states[0] != ' ' ){
            winningValue = states[0];
            return true;
        } else if ( states[2] == states[4] && states[4] == states[6] && states[2] != ' ' ){
            winningValue = states[2];
            return true;
        }
        return false;
    }


    public void makeMove(char player) {
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        boolean valid = false;
        int n = 0;
        while (valid == false) {
            System.out.println("Place your marker: ");
            n = reader.nextInt(); // Scans the next token of the input as an int.
            if (n < 0 || n > 8) {
                System.out.println("Invalid square.");
            } else if(states[n] != ' ') {
                System.out.println("Square already filled.");
            } else {
                valid = true;
            }
        }
        states[n] = player;
        //once finished
    }

    public void makeMoveAi(int input, char player) {
        states[input] = player;
        //once finished
    }
}

0 个答案:

没有答案