1

I am making a tree based AI for a game originating in Nepal called Tigers and Goats (or Tigers and Sheep). I am now starting to make the classes for the trees, but I am running into an error where my constructors are the same, although they are using different types of list.

Here are my two constructors:

public MoveTree(List<MoveTree> children, MoveTree parent) { this.children = children; this.parent = parent; } public MoveTree(List<Move> moves, MoveTree parent) { this.moves = moves; this.parent = parent; } 

I am using intellij and it is giving me the error shown here 'MoveTree(List, MoveTree)' clashes with 'MoveTree(List, MoveTree)'; both methods have same erasure

How can I avoid this error while still having my two constructors? I want to be able to do this without changing my constructors too much so that I can have different ways of implementing this class for different purposes

1

1 Answer 1

1

You can't have both. Use the builder pattern (formal style - not shown here), or a factory method (easier - shown):

private MoveTree(MoveTree parent) { this.parent = parent; } public static MoveTree createWithMoveTree(List<MoveTree> children, MoveTree parent) { MoveTree moveTree = new MoveTree(parent); moveTree.children = children; return moveTree; } public static MoveTree createWithMoves(List<Move> moves, MoveTree parent) { MoveTree moveTree = new MoveTree(parent); moveTree.moves = moves; return moveTree; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.