-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis.hs
67 lines (56 loc) · 1.87 KB
/
Analysis.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE PatternGuards #-}
module Analysis
( losingFirstMoves
, losingFirstMovesNaive
, bestMove
, solve
, SolvingResult(..)
) where
import AI
import Game
import Types
data SolvingResult =
Solved Player
| Unknown
deriving (Eq, Show)
toSolvingResult :: Int -> SolvingResult
toSolvingResult x
| x > 1000 = Solved Black
| x < -1000 = Solved White
| otherwise = Unknown
solve :: Algorithm -> Implementation -> Depth -> SolvingResult
solve a i d = toSolvingResult $ snd $
search a i SimpleEval initialPosition d
naiveSolve :: Board b => Depth -> Position b -> SolvingResult
naiveSolve _ r | Winner p <- roundResult r = Solved p
naiveSolve 0 _ = Unknown
naiveSolve depth r =
let i = pPlayer r
he = opponent i
iWon = (i `hasWon`)
heWon = (he `hasWon`)
solutions = map (naiveSolve (depth - 1)) $ nextPositions r
in if any iWon solutions
then Solved i
else if all heWon solutions
then Solved he
else Unknown
where
hasWon :: Player -> SolvingResult -> Bool
hasWon _ Unknown = False
hasWon p1 (Solved p2) = p1 == p2
losingFirstMovesNaive :: Int -> [Move]
losingFirstMovesNaive depth =
let variations = nextPositions initialPosition
solutions = map (naiveSolve depth) variations
varsols = variations `zip` solutions
in [ head $ pMoves v | (v,s) <- varsols, Solved {} <- [s]]
losingFirstMoves :: Algorithm -> Implementation -> Depth -> [Move]
losingFirstMoves a i depth =
let firstMoves = nextPositions initialPosition
solutions = map (\r -> toSolvingResult $ snd $ search a i SimpleEval r depth) firstMoves
movesSolutions = firstMoves `zip` solutions
in [ head $ pMoves r | (r,s) <- movesSolutions, Solved {} <- [s]]
bestMove :: Algorithm -> Implementation -> Depth -> (PV, Int)
bestMove a i = search a i ThreatBasedEval initialPosition