forked from rspeer/dominiate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicAI.coffee
221 lines (204 loc) · 5.98 KB
/
basicAI.coffee
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# utility functions
count = (list, elt) ->
count = 0
for member in list
if member == elt
count++
count
stringify = (obj) ->
if obj is null
return null
else
return obj.toString()
# This class defines a rule-based AI, the kind that is currently popular
# for evaluating Dominion strategies. Subclass it to define new strategies.
class BasicAI
name: 'BasicAI'
choosePriority: (state, choices, priorityfunc) ->
# Given a game state, a list of possible choices, and a function
# that returns a preference order, make the best choice in that
# preference order.
priority = priorityfunc(state)
bestChoice = null
bestIndex = null
for choice in choices
index = priority.indexOf(stringify(choice))
if index != -1 and (bestIndex is null or index < bestIndex)
bestIndex = index
bestChoice = choice
if bestChoice is null and null not in choices
# either no choices are available, or this AI is being forced
# to make a decision it's not prepared for
return choices[0] ? null
return bestChoice
chooseValue: (state, choices, valuefunc) ->
# Given a game state, a list of possible choices, and a function that
# returns a *value* for each choice, make the highest-valued choice.
#
# The null choice has value 0 when it is available, so negative-valued
# choices will be avoided.
bestChoice = null
bestValue = -Infinity
for choice in choices
if choice is null
value = 0
else
value = valuefunc(state, choice)
if value > bestValue
bestValue = value
bestChoice = choice
if bestChoice is null and null not in choices
# Either no choices are available, or this AI is being forced
# to make a decision it's not prepared for.
return choices[0] ? null
return bestChoice
# When an AI is asked to make a choice, it has two ways of doing so that
# we support: to rank the possible choices in a preference order, or to
# assign a numerical value to each choice.
chooseAction: (state, choices) ->
if this.actionValue?
this.chooseValue(state, choices, this.actionValue)
else
this.choosePriority(state, choices, this.actionPriority)
chooseTreasure: (state, choices) ->
if this.treasureValue?
this.chooseValue(state, choices, this.treasureValue)
else
this.choosePriority(state, choices, this.treasurePriority)
chooseGain: (state, choices) ->
if this.gainValue?
this.chooseValue(state, choices, this.gainValue)
else
this.choosePriority(state, choices, this.gainPriority)
chooseDiscard: (state, choices) ->
if this.discardValue?
this.chooseValue(state, choices, this.discardValue)
else
this.choosePriority(state, choices, this.discardPriority)
chooseTrash: (state, choices) ->
if this.trashValue?
this.chooseValue(state, choices, this.trashValue)
else
this.choosePriority(state, choices, this.trashPriority)
# The default buying strategy is Big Money Ultimate.
gainPriority: (state) -> [
"Colony" if state.current.countInDeck("Platinum") > 0
"Province" if state.countInSupply("Colony") <= 6
"Duchy" if 0 < state.gainsToEndGame() <= 5
"Estate" if 0 < state.gainsToEndGame() <= 2
"Platinum"
"Gold"
"Silver"
"Copper" if state.gainsToEndGame() <= 3
null
]
actionPriority: (state) -> [
"Menagerie" if state.current.menagerieDraws() == 3
"Shanty Town" if state.current.shantyTownDraws() == 2
"Festival"
"Bazaar"
"Worker's Village"
"Village"
"Grand Market"
"Alchemist"
"Laboratory"
"Caravan"
"Fishing Village"
"Market"
"Peddler"
"Great Hall"
"Smithy" if state.current.actions > 1
"Pawn"
"Warehouse"
"Menagerie"
"Shanty Town" if state.current.actions == 1
"Nobles"
"Witch"
"Wharf"
"Militia"
"Princess"
"Steward"
"Bridge"
"Horse Traders"
"Coppersmith" if state.current.countInHand("Copper") >= 3
"Smithy"
"Merchant Ship"
"Monument"
"Harvest"
"Woodcutter"
"Coppersmith" if state.current.countInHand("Copper") >= 2
"Moat"
"Chapel"
"Coppersmith"
"Shanty Town"
null
]
treasurePriority: (state) -> [
"Platinum"
"Diadem"
"Philosopher's Stone"
"Gold"
"Harem"
"Silver"
"Quarry"
"Copper"
"Potion"
"Bank"
]
discardPriority: (state) -> [
"Colony"
"Province"
"Duchy"
"Estate"
"Copper"
null # this is where discarding-for-benefit should stop
"Silver"
"Gold"
"Platinum"
]
trashPriority: (state) -> [
"Curse"
"Estate" if state.gainsToEndGame() > 4
"Copper" if state.current.getTotalMoney() > 4
"Potion" if state.current.turnsTaken >= 10
"Estate" if state.gainsToEndGame() > 2
null
"Copper"
"Potion"
"Estate"
"Silver"
]
chooseBenefit: (state, choices) ->
buyValue = 1
cardValue = 2
coinValue = 3
trashValue = 4 # if there are cards we want to trash
actionValue = 10 # if we need more actions
trashableCards = 0
actionBalance = state.current.actionBalance()
usableActions = Math.max(0, -actionBalance)
# Draw cards if we have a surplus of actions
if actionBalance >= 1
cardValue += actionBalance
# How many cards do we want to trash?
for card in state.current.hand
if this.chooseTrash(state, [card, null]) is card
trashableCards += 1
best = null
bestValue = -1000
for choice in choices
value = cardValue * (choice.cards ? 0)
value += coinValue * (choice.coins ? 0)
value += buyValue * (choice.buys ? 0)
trashes = (choice.trashes ? 0)
if trashes <= trashableCards
value += trashValue * trashes
else
value -= trashValue * trashes
value += actionValue * Math.min((choice.actions ? 0), usableActions)
if value > bestValue
best = choice
bestValue = value
best
toString: () -> this.name
this.BasicAI = BasicAI