Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. import java.util.*;
  2. /**
  3.  * @author: Spencer LaValle
  4.  */
  5. public class Tree{
  6.     private ArrayList<Integer> moves;
  7.     private int
  8.         depth,
  9.         initVal;
  10.     private Node root;
  11.     private boolean isPlayer;
  12.    
  13.     public Tree(ArrayList<Integer> moves, int depth, int initVal, boolean isPlayer){
  14.         this.moves = moves;
  15.         this.depth = depth;
  16.         this.initVal = initVal;
  17.         this.isPlayer = isPlayer;
  18.        
  19.     }
  20.     public void generateTree(int depth){
  21.         if(depth > 0){
  22.             root = new Node(initVal, isPlayer, depth);
  23.             System.out.println("Root:\t Value: " + initVal + "\tIs Human: " + isPlayer);
  24.             depth--;
  25.            
  26.             if(depth > 0){
  27.                 root.makeChildren();
  28.                 //System.out.println(root.getScore());
  29.                 Node temp;
  30.                 depth--;
  31.                 for(int i = 0; i < root.getChildren().size(); i++){
  32.                     temp = root.getChildren().get(i);
  33.                     temp.generate(depth);
  34.                     //System.out.println(depth-1);
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     public boolean canWin(Node temp){
  40.         for(int i = 0; i < temp.getChildren().size(); i++){
  41.            
  42.         }
  43.         return true;
  44.     }
  45.    
  46.     class Node{
  47.         protected int
  48.             value,
  49.             depth,
  50.             score;//0 is win, -1 is lose, 1 is unknown
  51.         protected ArrayList<Node> children;
  52.         protected boolean isPlayer;
  53.        
  54.         protected Node(int value, boolean isPlayer, int depth){
  55.             this.value = value;
  56.             this.depth = depth;
  57.             this.isPlayer = isPlayer;
  58.         }
  59.         protected Node(){
  60.            
  61.         }
  62.         protected void makeChildren(){
  63.             children = new ArrayList<Node>(moves.size());
  64.             int temp;
  65.             for(int i = 0; i < moves.size(); i++){
  66.                 temp = value-moves.get(i);
  67.                 //if(!(value-moves.get(i) < 1)){
  68.                 if(!isWinNode(temp) && temp > 0){
  69.                     children.add(new Node(temp, !isPlayer, depth));
  70. //                    children.add(i, new Node());
  71. //                    children.get(i).setValue(value-moves.get(i));
  72.                     children.get(i).setScore(-1);
  73.                     System.out.println("Node Value: " + this.getValue() + " - " + moves.get(i) + " = " + children.get(i).getValue());
  74.                 }
  75.                 else if(isWinNode(temp)){
  76.                     children.add(new Node(temp, !isPlayer, depth));
  77.                     children.get(i).setScore(0);
  78.                     this.setScore(0);
  79.                     System.out.println("Node Value: " + this.getValue() + " - " + moves.get(i) + " = " + children.get(i).getValue() + "\t WIN" + "\tisHuman: " + children.get(i).isPlayer);
  80.                 }
  81.             }
  82.         }
  83.         protected boolean isWinNode(int val){
  84.             if(val == 0 || val == 1){
  85.                 return true;
  86.             }
  87.             else{
  88.                 return false;
  89.             }
  90.         }
  91.         protected void generate(int depth) {
  92.            // System.out.println("generate depth : " + depth);
  93.             if(depth > 0){
  94.                 this.makeChildren();
  95.             }
  96.             Node temp;//keeping for for clarity
  97.             if (depth <= 0){
  98.                 return;
  99.             }
  100.             for (int i = 0; i < children.size(); i++) {
  101.                 temp = this.getChildren().get(i);
  102.                 temp.generate(depth-1);
  103.                 //increment count and print depth if necessary.  u may need to add Count as a parameter.
  104.             }
  105.         }
  106.         public ArrayList<Node> getChildren(){
  107.             return children;
  108.         }
  109.         public boolean isLeaf(){
  110.             if(children.size() == 0){
  111.                 return true;
  112.             }
  113.             else{
  114.                 return false;
  115.             }
  116.         }
  117.         public void setPlayer(boolean isPlayer){
  118.             this.isPlayer = isPlayer;
  119.         }
  120.         public boolean isPlayer(){
  121.             return this.isPlayer();
  122.         }
  123.         public void setValue(int value){
  124.             this.value = value;
  125.         }
  126.         public int getValue(){
  127.             return this.value;
  128.         }
  129.         public void setScore(int score){
  130.             this.score = score;
  131.         }
  132.         public int getScore(){
  133.             return this.score;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement