-
Notifications
You must be signed in to change notification settings - Fork 1
/
AI.elm
108 lines (79 loc) · 2.21 KB
/
AI.elm
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module AI
exposing
( Level
, blackAI
)
import Array exposing (Array)
import MiniMax exposing (..)
import Othello exposing (..)
type alias Level =
Int
blackAI : Level -> Board -> Maybe Move
blackAI level board =
let
( mv, _ ) =
findMax othelloProblem level board
in
case mv of
Just mv' ->
if movePlayer mv' == Black then
mv
else
Nothing
Nothing ->
Nothing
othelloProblem : Problem Board Move
othelloProblem =
Problem heuristic moves applyMove
moves : PlayerType -> Board -> Maybe ( PlayerType, List Move )
moves playerType board =
case validNextMoves board (toStone playerType) of
NoValidMoves ->
Nothing
ValidMoves player moves ->
Just ( toPlayerType player, toMoveList moves )
heuristic : Board -> Int
heuristic (Board rows) =
valueRows coordValues rows
coordValues : List (List Int)
coordValues =
[ [ 10, -5, 6, 5, 5, 6, -5, 10 ]
, [ -5, -8, -4, -4, -4, -4, -8, -5 ]
, [ 6, -4, 2, 3, 3, 2, -4, 6 ]
, [ 5, -4, 3, 4, 4, 3, -4, 6 ]
, [ 5, -4, 3, 4, 4, 3, -4, 6 ]
, [ 6, -4, 2, 3, 3, 2, -4, 6 ]
, [ -5, -8, -4, -4, -4, -4, -8, -5 ]
, [ 10, -5, 6, 5, 5, 6, -5, 10 ]
]
valueRows : List (List Int) -> Array (Array Cell) -> Int
valueRows factors rows =
List.map2 valueRow factors (Array.toList rows)
|> List.sum
valueRow : List Int -> Array Cell -> Int
valueRow factors cells =
List.map2 valueCell factors (Array.toList cells)
|> List.sum
valueCell : Int -> Cell -> Int
valueCell factor cell =
case cell of
Empty ->
0
Occupied Black ->
factor
Occupied White ->
-factor
toStone : PlayerType -> Stone
toStone playerType =
case playerType of
Maximizing ->
Black
Minimizing ->
White
toPlayerType : Stone -> PlayerType
toPlayerType stone =
case stone of
Black ->
Maximizing
White ->
Minimizing