Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * @author: Spencer LaValle
- */
- public class Tree{
- private ArrayList<Integer> moves;
- private int
- depth,
- initVal;
- private Node root;
- private boolean isPlayer;
- public Tree(ArrayList<Integer> moves, int depth, int initVal, boolean isPlayer){
- this.moves = moves;
- this.depth = depth;
- this.initVal = initVal;
- this.isPlayer = isPlayer;
- }
- public void generateTree(int depth){
- if(depth > 0){
- root = new Node(initVal, isPlayer, depth);
- System.out.println("Root:\t Value: " + initVal + "\tIs Human: " + isPlayer);
- depth--;
- if(depth > 0){
- root.makeChildren();
- //System.out.println(root.getScore());
- Node temp;
- depth--;
- for(int i = 0; i < root.getChildren().size(); i++){
- temp = root.getChildren().get(i);
- temp.generate(depth);
- //System.out.println(depth-1);
- }
- }
- }
- }
- public boolean canWin(Node temp){
- for(int i = 0; i < temp.getChildren().size(); i++){
- }
- return true;
- }
- class Node{
- protected int
- value,
- depth,
- score;//0 is win, -1 is lose, 1 is unknown
- protected ArrayList<Node> children;
- protected boolean isPlayer;
- protected Node(int value, boolean isPlayer, int depth){
- this.value = value;
- this.depth = depth;
- this.isPlayer = isPlayer;
- }
- protected Node(){
- }
- protected void makeChildren(){
- children = new ArrayList<Node>(moves.size());
- int temp;
- for(int i = 0; i < moves.size(); i++){
- temp = value-moves.get(i);
- //if(!(value-moves.get(i) < 1)){
- if(!isWinNode(temp) && temp > 0){
- children.add(new Node(temp, !isPlayer, depth));
- // children.add(i, new Node());
- // children.get(i).setValue(value-moves.get(i));
- children.get(i).setScore(-1);
- System.out.println("Node Value: " + this.getValue() + " - " + moves.get(i) + " = " + children.get(i).getValue());
- }
- else if(isWinNode(temp)){
- children.add(new Node(temp, !isPlayer, depth));
- children.get(i).setScore(0);
- this.setScore(0);
- System.out.println("Node Value: " + this.getValue() + " - " + moves.get(i) + " = " + children.get(i).getValue() + "\t WIN" + "\tisHuman: " + children.get(i).isPlayer);
- }
- }
- }
- protected boolean isWinNode(int val){
- if(val == 0 || val == 1){
- return true;
- }
- else{
- return false;
- }
- }
- protected void generate(int depth) {
- // System.out.println("generate depth : " + depth);
- if(depth > 0){
- this.makeChildren();
- }
- Node temp;//keeping for for clarity
- if (depth <= 0){
- return;
- }
- for (int i = 0; i < children.size(); i++) {
- temp = this.getChildren().get(i);
- temp.generate(depth-1);
- //increment count and print depth if necessary. u may need to add Count as a parameter.
- }
- }
- public ArrayList<Node> getChildren(){
- return children;
- }
- public boolean isLeaf(){
- if(children.size() == 0){
- return true;
- }
- else{
- return false;
- }
- }
- public void setPlayer(boolean isPlayer){
- this.isPlayer = isPlayer;
- }
- public boolean isPlayer(){
- return this.isPlayer();
- }
- public void setValue(int value){
- this.value = value;
- }
- public int getValue(){
- return this.value;
- }
- public void setScore(int score){
- this.score = score;
- }
- public int getScore(){
- return this.score;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement