Package ai.nettogrof.battlesnake.snakes
Class AbstractTreeSearchSnakeAI
- java.lang.Object
 - 
- ai.nettogrof.battlesnake.snakes.AbstractSnakeAI
 - 
- ai.nettogrof.battlesnake.snakes.AbstractSearchSnakeAI
 - 
- ai.nettogrof.battlesnake.snakes.AbstractTreeSearchSnakeAI
 
 
 
 
- 
- Direct Known Subclasses:
 AbstractMultiThreadSnakeAI
public abstract class AbstractTreeSearchSnakeAI extends AbstractSearchSnakeAI
Any snake using a tree-search could extend this abstract class it contains basic constant fields, related to the field name in json call from BattleSnake. Also some method that any snake must implements.- Version:
 - Spring 2022
 - Author:
 - carl.lajeunesse
 
 
- 
- 
Field Summary
Fields Modifier and Type Field Description protected AbstractNodelastRootKeep the root node from the previous move, to be able to continue search from the previous "tree"protected static StringNODECOUNTConstant Field nameprotected Constructor<? extends AbstractSearch>searchTypeWhat kind of search that gonna be use- 
Fields inherited from class ai.nettogrof.battlesnake.snakes.AbstractSearchSnakeAI
losing, minusbuffer, nodeTotalCount, timeout, timeTotal, winning 
- 
Fields inherited from class ai.nettogrof.battlesnake.snakes.AbstractSnakeAI
apiversion, BOARD, BODY, DOWN, fileConfig, FOUR_SNAKE, gameId, HEAD, HEALTH, height, HEIGHT_FIELD, LEFT, log, MOVESTR, NAME, RIGHT, rules, SHOUT, SINGLE_SNAKE, SNAKES, SQUAD, TURN, TWO_SNAKE, UPWARD, width, WIDTH_FIELD, YOU 
 - 
 
- 
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractTreeSearchSnakeAI()Basic and unsed constructorprotectedAbstractTreeSearchSnakeAI(String gameId)Constructor with the gameid, 
- 
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected AbstractNodechooseBestMove(AbstractNode root)Choose the best move, based on the assumption that the opponent will always choose the best counter.private voidfillList(gnu.trove.list.array.TFloatArrayList[] upward, gnu.trove.list.array.TFloatArrayList[] down, gnu.trove.list.array.TFloatArrayList[] left, gnu.trove.list.array.TFloatArrayList[] right, AbstractNode node)This method fill 4 list (one for each direction ) with the score of each node based on the move directionprotected AbstractNodefindChildNewRoot(List<SnakeInfo> snakes, FoodInfo food, HazardSquare hazard)Check in the previous search if a child from root, is equals the current board situation.,protected AbstractNodefinishHim(AbstractNode root, AbstractNode winner)In a winning position, this method try to find the shortest way to win.protected abstract AbstractNodegenRoot(com.fasterxml.jackson.databind.JsonNode moveRequest)Generate the root node based on the /move requestprivate intgetMaxDepth(AbstractNode root)Check the depth of the biggest branchprotected AbstractNodelastChance(AbstractNode root)In a losing position, this method try to find the longest way, hpoing that the opponent make a mistake.private voidlogValue(gnu.trove.list.array.TFloatArrayList[] upward, gnu.trove.list.array.TFloatArrayList[] down, gnu.trove.list.array.TFloatArrayList[] left, gnu.trove.list.array.TFloatArrayList[] right)Log value for each possible move, that help to debug/understand why the snake choose which move.Map<String,String>move(com.fasterxml.jackson.databind.JsonNode moveRequest)This method will be call on each move request receive by BattleSnakevoidsetTimeout(int timeout)Set the move timeoutvoidsingleThreadTreeSearch(AbstractNode root, Long startTime, GameRuleset rules)Execute the single Thread tree searchprotected voidtreeSearch(AbstractNode root, Long startTime, GameRuleset rules)Execute the tree search- 
Methods inherited from class ai.nettogrof.battlesnake.snakes.AbstractSearchSnakeAI
addNodeTotalCount, addTimeTotal, end, generateResponse, genSearchType, setProperties 
- 
Methods inherited from class ai.nettogrof.battlesnake.snakes.AbstractSnakeAI
getFileConfig, getInfo, ping, setFileConfig, start 
 - 
 
 - 
 
- 
- 
Field Detail
- 
NODECOUNT
protected static final String NODECOUNT
Constant Field name- See Also:
 - Constant Field Values
 
 
- 
lastRoot
protected AbstractNode lastRoot
Keep the root node from the previous move, to be able to continue search from the previous "tree" 
- 
searchType
protected Constructor<? extends AbstractSearch> searchType
What kind of search that gonna be use 
 - 
 
- 
Constructor Detail
- 
AbstractTreeSearchSnakeAI
protected AbstractTreeSearchSnakeAI()
Basic and unsed constructor 
- 
AbstractTreeSearchSnakeAI
protected AbstractTreeSearchSnakeAI(String gameId)
Constructor with the gameid,- Parameters:
 gameId- String of the gameid field receive in the start request.
 
 - 
 
- 
Method Detail
- 
move
public Map<String,String> move(com.fasterxml.jackson.databind.JsonNode moveRequest)
This method will be call on each move request receive by BattleSnake- Specified by:
 movein classAbstractSnakeAI- Parameters:
 moveRequest- Json call received- Returns:
 - map of field to be return to battlesnake, example "move" , "up"
 
 
- 
getMaxDepth
private int getMaxDepth(AbstractNode root)
Check the depth of the biggest branch- Parameters:
 root- Root node- Returns:
 - depth (int)
 
 
- 
treeSearch
protected void treeSearch(AbstractNode root, Long startTime, GameRuleset rules) throws ReflectiveOperationException
Execute the tree search- Parameters:
 root- The root nodestartTime- The start time in millisecondrules- Game ruleset- Throws:
 ReflectiveOperationException- In case of invalid search type
 
- 
genRoot
protected abstract AbstractNode genRoot(com.fasterxml.jackson.databind.JsonNode moveRequest)
Generate the root node based on the /move request- Parameters:
 moveRequest- Json request- Returns:
 - AbstractNode the root
 
 
- 
finishHim
protected AbstractNode finishHim(AbstractNode root, AbstractNode winner)
In a winning position, this method try to find the shortest way to win.- Specified by:
 finishHimin classAbstractSearchSnakeAI- Parameters:
 root- The current Root nodewinner- The predetermine best move- Returns:
 - the best AbstractNode
 
 
- 
lastChance
protected AbstractNode lastChance(AbstractNode root)
In a losing position, this method try to find the longest way, hpoing that the opponent make a mistake.- Specified by:
 lastChancein classAbstractSearchSnakeAI- Parameters:
 root- The current Root node- Returns:
 - the best AbstractNode
 
 
- 
chooseBestMove
protected AbstractNode chooseBestMove(AbstractNode root)
Choose the best move, based on the assumption that the opponent will always choose the best counter.- Parameters:
 root- The current Root node- Returns:
 - the best AbstractNode
 
 
- 
fillList
private void fillList(gnu.trove.list.array.TFloatArrayList[] upward, gnu.trove.list.array.TFloatArrayList[] down, gnu.trove.list.array.TFloatArrayList[] left, gnu.trove.list.array.TFloatArrayList[] right, AbstractNode node)This method fill 4 list (one for each direction ) with the score of each node based on the move direction- Parameters:
 upward- float array listdown- float array listleft- float array listright- float array listnode- parent node
 
- 
logValue
private void logValue(gnu.trove.list.array.TFloatArrayList[] upward, gnu.trove.list.array.TFloatArrayList[] down, gnu.trove.list.array.TFloatArrayList[] left, gnu.trove.list.array.TFloatArrayList[] right)Log value for each possible move, that help to debug/understand why the snake choose which move.- Parameters:
 upward- float array listdown- float array listleft- float array listright- float array list
 
- 
findChildNewRoot
protected AbstractNode findChildNewRoot(List<SnakeInfo> snakes, FoodInfo food, HazardSquare hazard)
Check in the previous search if a child from root, is equals the current board situation.,- Parameters:
 snakes- List of snakesfood- Food infohazard- Hazard Info- Returns:
 - null or child node
 
 
- 
setTimeout
public void setTimeout(int timeout)
Set the move timeout- Parameters:
 timeout- the timeout to set
 
- 
singleThreadTreeSearch
public void singleThreadTreeSearch(AbstractNode root, Long startTime, GameRuleset rules) throws ReflectiveOperationException
Execute the single Thread tree search- Parameters:
 root- The root nodestartTime- The start time in millisecondrules- Game ruleset- Throws:
 ReflectiveOperationException- In case of invalid search type
 
 - 
 
 -