Skip to content

Instantly share code, notes, and snippets.

@fgoinai
Created February 23, 2018 19:09
Show Gist options
  • Save fgoinai/d6750298b9f60948c3dfc7141f11e1f1 to your computer and use it in GitHub Desktop.
Save fgoinai/d6750298b9f60948c3dfc7141f11e1f1 to your computer and use it in GitHub Desktop.
minimaxWithLeastDepth :: Tree Grid -> Tree (Grid, Player, Int)
minimaxWithLeastDepth (Node g [])
| wins O g = Node (g,O,1) []
| wins X g = Node (g,X,1) []
| otherwise = Node (g,B,1) []
minimaxWithLeastDepth (Node g ts)
| turn g == O = Node (g, minimum ps, minimum cs) ts'
| turn g == X = Node (g, maximum ps, minimum cs) ts'
where
ts' = map minimaxWithLeastDepth ts
raw = [(p,c) | Node (_,p,c) _ <- ts']
ps = [p | (p,_) <- raw]
cs = [c + 1 | (_,c) <- raw]
type GridWDepth = (Grid,Int)
leastDepth :: [GridWDepth] -> GridWDepth
leastDepth [] = ([],0)
leastDepth (x:xs) = checkBranchDepth x xs
checkBranchDepth :: GridWDepth -> [GridWDepth] -> GridWDepth
checkBranchDepth m [] = m
checkBranchDepth m (y:ys) | (snd y) < (snd m) = checkBranchDepth y ys
| otherwise = checkBranchDepth m ys
leastDepthBestmove :: Grid -> Player -> Grid
leastDepthBestmove g p = fst $ leastDepth [(g',c) | Node (g',p',c) _ <- ts, p' == best]
where
tree = prune depth (gametree g p)
Node (_,best,_) ts = minimaxWithLeastDepth tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment