CherubiAI.java @author: Derek Omuro @team : No Artificial Flavors Components getMove(…) Iterative-deepening DFS: explore the tree examine from leaf to root 0 DFS 1 Bookkeeping: remember which moves are best manage array of values Evaluation function: evaluate a board configuration use for leaf nodes 2 3 … depth-1 depth h(x) Interaction between components Iterative-deepening DFS public Point getMove(...) A depthLimit = 1 while depthLimit != maxDepth bestMove = search(depthLimit++) return bestMove private Point search(...) B D C E F push current state on stack Example tree; depth limit is 2 while stack isn’t empty if out of time, return null node = stack.peek() while node is not a leaf && node has not been visited stack.push(possible moves of node) mark node as visited node = stack.peek() stack.pop() // evaluate and bookkeeping... G Bookkeeping int values[] = 0 1 2 3 ... max depth -inf +inf -inf +inf ... ±inf private Point search(...) node = stack.pop() B int value; if node is a leaf value = node.evaluate() 1 5 else //node has been visited value = values[node.depth] reset values from node.depth to depthLimit if value is better than values[node.depth-1] values[node.depth-1] = value if node.depth == 1 bestMove = node.lastMove A max C 3 min 2 Evaluation Function Look at every possible win vector public int evaluate(...) value = 0 for every possible win vector value += score(winVector[]) return value Update points based on pieces in each win vector O =5 O O private int score(byte winVector[]) if different pieces in winVector return 0 else return 5^(# of pieces)*(±1) = 25 O O X =0 Putting it all together Point getMove(…) Iterative-deepening DFS: Iterative: keep searching one level deeper DFS: pop from leaves to root 0 Bookkeeping: You only need a single integer array Update parent based on child If updating root, save the last move DFS 1 2 3 … depth-1 depth Evaluation function: Use to evaluate nodes at depth limit Look at every possible win vector … max depth h(x) Interaction between components Coding Tips Do not code everything up at once! 1. IDDFS: pop from leaf to root 2. Simple evaluation function 3. Bookkeeping: manage int values[] 4. Improve evaluation function To do: Alpha-beta pruning Much more…
© Copyright 2026 Paperzz