This discussion is archived
3 Replies Latest reply: Feb 12, 2008 5:08 PM by 807601 RSS

Multi-noded trees

807601 Newbie
Currently Being Moderated
Hi

I'm semi-new to Java. Been writing a tic-tac-toe application for assignment. I've got my TicTacToeApp, TicTacToeJFrame, TicTacToeJPanel, and TreeNode classes all together and compiling fine; but I'm struggling to create my BuildTree class.

So, I can create my nodes, each with an array of children (9 kids each), but I can't join them up. I'm needing some direction on building my tree (probably recursively) so that I can min-max etc and pass the computer move back to the JPanel.

I'll post whatever code you want to prove I'm actually doing some work before I ask for help; just don't want to post everything and give the non-homeworkers free code.

Thanks
  • 1. Re: Multi-noded trees
    807601 Newbie
    Currently Being Moderated
    ctwi001 wrote:
    Hi

    I'm semi-new to Java. Been writing a tic-tac-toe application for assignment. I've got my TicTacToeApp, TicTacToeJFrame, TicTacToeJPanel, and TreeNode classes all together and compiling fine; but I'm struggling to create my BuildTree class.
    What is a BuildTree and why do you need it for TicTacToe?

    >
    So, I can create my nodes, each with an array of children (9 kids each), but I can't join them up. I'm needing some direction on building my tree (probably recursively) so that I can min-max etc and pass the computer move back to the JPanel.
    array of children?

    Sorry. Can you expand a bit on what you have done and what specifically you are trying to do. The array of children kind of loses me. I could sort of understand if you had one player but what you are doing with a bunch I don't know.
  • 2. Re: Multi-noded trees
    807601 Newbie
    Currently Being Moderated
    Sorry

    Attempting to use a multi-noded tree to play human vs computer tic-tac-toe.

    To clarify my terminology "children" = "child nodes".

    BuildTree is a class I am trying to write which will look to my TreeNode class to build each node. Should I just be doing the tree construction within the TreeNode class?

    I'll post my TreeNode so you can see what it's doing:
    import java.*;
    import java.awt.*;
    
    public class TreeNode {
         private String[] boardLayout = new String[9];
         private TreeNode parent = null;
         TreeNode[] child = new TreeNode[9];
         private int score;
    
         String charX = new String("X");
         String charO = new String("O");
         String charBlank = new String(" ");
    
    
         public TreeNode(String[] newBoardLayout) {
             boardLayout = newBoardLayout;
             for (int i = 0; i<9; i++){
                   child[i] = null;
              }
              score = 0;
         }
    
         public TreeNode(String[] newBoardLayout, TreeNode one1, TreeNode two2, TreeNode three3, TreeNode four4,
                 TreeNode five5, TreeNode six6, TreeNode seven7, TreeNode eight8, TreeNode nine9) {
              boardLayout = newBoardLayout;
              child[0] = one1;
              child[1] = two2;
              child[2] = three3;
              child[3] = four4;
              child[4] = five5;
              child[5] = six6;
              child[6] = seven7;
              child[7] = eight8;
              child[8] = nine9;
              setScore();
         }
    
           public String[] getBoardLayout() {
              return boardLayout;
         }
    
         public void setBoardLayout(String[] newBoardLayout) {
              boardLayout = newBoardLayout;
              setScore();
         }
    
         public TreeNode getChild(int ID) {
              return child[ID];
         }
    
         public void setChild(TreeNode input, int ID) {
             child[ID] = input;
             setScore();
         }
    
         private void setScore(){
              score = 0;
              if ((boardLayout[0] == charX && boardLayout[1] == charX && boardLayout[2] == charX)
              || (boardLayout[3] == charX && boardLayout[4] == charX && boardLayout[5] == charX)
              || (boardLayout[6] == charX && boardLayout[7] == charX && boardLayout[8] == charX)
              || (boardLayout[0] == charX && boardLayout[3] == charX && boardLayout[6] == charX)
              || (boardLayout[1] == charX && boardLayout[4] == charX && boardLayout[7] == charX)
              || (boardLayout[2] == charX && boardLayout[5] == charX && boardLayout[8] == charX)
              || (boardLayout[0] == charX && boardLayout[4] == charX && boardLayout[8] == charX)
              || (boardLayout[2] == charX && boardLayout[4] == charX && boardLayout[6] == charX)) {
                   score = -1;
              }
              if ((boardLayout[0] == charO && boardLayout[1] == charO && boardLayout[2] == charO)
              || (boardLayout[3] == charO && boardLayout[4] == charO && boardLayout[5] == charO)
              || (boardLayout[6] == charO && boardLayout[7] == charO && boardLayout[8] == charO)
              || (boardLayout[0] == charO && boardLayout[3] == charO && boardLayout[6] == charO)
              || (boardLayout[1] == charO && boardLayout[4] == charO && boardLayout[7] == charO)
              || (boardLayout[2] == charO && boardLayout[5] == charO && boardLayout[8] == charO)
              || (boardLayout[0] == charO && boardLayout[4] == charO && boardLayout[8] == charO)
              || (boardLayout[2] == charO && boardLayout[4] == charO && boardLayout[6] == charO)) {
                   score = 1;
              }
         }
    
         public int getScore(){
              return score;
         }
    }
    The tree will enable the computer to plot out the potential future plays, and thereby calculate a min-max.

    I know I could do all this with if-statements, however the specs of my assignment specify using a tree.

    Thanks
  • 3. Re: Multi-noded trees
    807601 Newbie
    Currently Being Moderated
    I can find a few examples of creating binary trees, but multi-noded appears to be completely different in construct as it is not just simply a matter of
    TNode add(TNode, value){
            if (TNode == null){
                    TNode = newTNode(value, null, null);
                    return t;
            }
            if (value < TNode.value){
                    TNode.setleft(add(TNode.left, value);
            } else {
                    TNode.setright(add(TNode.right, value);
            }
            return TNode;
    }
    but having to somehow test and establish up to nine child nodes.