-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatch.hs
67 lines (57 loc) · 1.89 KB
/
Match.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
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
module Match
( match
, humanStrategy
, aiStrategy
, analysisStrategy
) where
import Types
import Game
import AI
import Text.Printf
type PlayStrategy = GamePosition -> IO Move
match :: PlayStrategy -> PlayStrategy -> IO ()
match = step initialPosition
step :: GamePosition -> PlayStrategy -> PlayStrategy -> IO ()
step p0 s1 s2 = do
printf "* %s's turn\n" (show $ pPlayer p0)
move <- s1 p0
let p1 = doMove move p0
case roundResult p1 of
Winner p -> printf "* Game finished. Winner: %s\n" (show p)
InProgress -> step p1 s2 s1
humanStrategy :: PlayStrategy
humanStrategy pos = do
putStr "Enter your turn: "
turnStr <- getLine
case parseMove turnStr of
Nothing -> do
printf "Cannot parse move %s, please retype.\n" turnStr
humanStrategy pos
Just move -> return move
aiStrategy :: Algorithm -> Implementation -> Depth -> PlayStrategy
aiStrategy alg impl d pos = do
let (aiPV, score) = search alg impl ThreatBasedEval pos d
aiMove = head aiPV
printf "AI evaluation (algo = %s, depth = %d): %d. PV: %s\n" (show alg) d score (show aiPV)
printf "AI move: %s\n" (show aiMove)
return aiMove
analysisStrategy :: Algorithm -> Implementation -> Depth -> PlayStrategy
analysisStrategy alg impl depth pos = do
putStr "Enter your turn or depth to analyse: "
turnStr <- getLine
case parseMove turnStr of
Nothing -> do
case parseAnalyse turnStr of
Nothing -> do
printf "Cannot parse move %s, please retype.\n" turnStr
Just newDepth -> do
let (aiPV, score) = search alg impl ThreatBasedEval pos newDepth
printf "Depth: %d. Score: %d. PV: %s\n" newDepth score (show aiPV)
analysisStrategy alg impl depth pos
Just move -> return move
where
parseAnalyse :: String -> Maybe Int
parseAnalyse xs = case reads xs of
[(i, "")] -> Just i
_ -> Nothing