Reputation: 7817
I am trying to implment a tree for my project.
This tree will contain nodes which are different board states after some move.
Right now my project is structured like so:
src
Agent Support Test Threes
Tree.java Some class.java some class Board.java
I want my tree to store these Board objects kept in package src.Threes. However in my Tree class it is not letting me access my public board methods.
I am confident that I am just missing some really basic thing but I cannot figure this out after spending time hunting down what I am missing.
Please see my Tree.java code below:
package src.Agent;
import java.util.ArrayList;
import src.Threes.Board; // unused import
import java.util.List;
public class Tree<Board> {
private Node<Board> root;
public Tree(Board RootData){
root = new Node<Board>(RootData);
//root.data = RootData;
//root.children = new ArrayList<Node<Board>>(); // maximum length of 4
}
public static class Node<Board>{
private Board data;
private Node<Board> parent;
private List<Node<Board>> children;
public Node(Board board){
data = board;
children = new ArrayList<Node<Board>>();
}
private boolean addChild(Board board){
return !board.gameOver(); // Cannot resolve method gameOver() (public method in Board).
}
}
public static void main(String[] args){
}
}
Upvotes: 2
Views: 3931
Reputation: 1502076
This is the problem:
public static class Node<Board>
That's declaring a generic class called Node
, and Board
is a type parameter. Within Node
, the identifier Board
refers to the type parameter called Board
, not the type Board
itself.
It looks like you don't really want it to be generic at all, so you can just change it to:
public static class Node {
...
}
Then change every time you use Node<Board>
to just Node
within the rest of the code.
Likewise, I suspect you don't want Tree
to be generic.
If you do want Tree
and Node
to be generic, you should declare them like this:
public static class Node<T> {
private T data;
private Node<T> parent;
private List<Node<T>> children;
...
}
... but then you can't refer to Board
-specific members within Node
. You could constrain T
to be Board
or a subtype:
public static class Node<T extends Board> {
private T data;
private Node<T> parent;
private List<Node<T>> children;
...
}
... and at that point your gameOver
etc calls will work - but that would be an odd mix of being Board-specific and generic at the same time. I wouldn't expect a Node
or Tree
class to have any concept of "game over". Separate your concerns!
Upvotes: 2