-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures.py
225 lines (185 loc) · 6.23 KB
/
features.py
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
222
223
224
225
from infrastructure import *
# CRUCIAL METHOD
def featureDict(featureList, fstate, action, combo):
"""
fstate : Fstate -- the feature state
action : Card -- the card you are trying to play
combo : Combostate -- the combined state of rules and effects
given a list of features, returns a dictionary mapping:
feature -> featureActivity (given fstate, action, and combo)
"""
featureDict = {}
for feature in featureList:
featureDict[feature] = feature.f(fstate,action,combo)
return featureDict
class Feature(object):
def f(fstate, action, combostate):
pass
class SizeofHand(Feature):
def f(self, fstate, action, combostate):
return len(fstate.hand) # remove self -- just depends on arguments
# if basicValueRule.setting == True: 2, 3, 4, 5. Else, 11, 12 ,13 ,14
class HighCount(Feature):
def f(self, fstate, action, combostate):
#if greater is more powerful
highCards = 0
if (combostate.state.basicValueRule.setting == True):
for card in fstate.hand:
if card.value >= 11:
highCards += 1
else:
for card in fstate.hand:
if card.value <= 5:
highCards += 1
return highCards
class LowCount(Feature):
def f(self, fstate, action, combostate):
#if greater is more powerful
lowCards = 0
if (combostate.state.basicValueRule.setting == True):
for card in fstate.hand:
if card.value <= 5:
lowCards += 1
else:
for card in fstate.hand:
if card.value >= 11:
lowCards += 1
return lowCards
# dosen't depend on state
class MedCount(Feature):
def f(self, fstate, action, combostate):
medCards = 0
for card in fstate.hand:
if card.value >= 6:
if card.value <= 10:
medCards += 1
return medCards
# checker if it works with rule state
# check against last card and make sure it works
class Illegality(Feature):
def __init__ (self):
self.checker = Checker()
def f(self, fstate, action, combostate):
actionCard = action
lastCard = fstate.lastCard
constraintState = combostate.state
isLegalGivenState = self.checker.isConsistent(Notification(LEGAL, actionCard, lastCard), constraintState)
if not isLegalGivenState:
return 1
else:
return 0
## THESE NEED TO BE REDONE! -- remove init (unless it's absolutely necessary),
# and migrate stuff to f
class WildValue(Feature):
def f(self, fstate, action, combostate):
wildvals = 0
for card in fstate.hand:
if card.value == combostate.state.wildValueRule.setting:
wildvals += 1
return wildvals
class MajorityPercent(Feature):
def f(self, fstate, action, combostate):
c = 0
s = 0
h = 0
d = 0
maxsuit = 0
for card in fstate.hand:
if card.suit == "C":
c += 1
if card.suit == "D":
d += 1
if card.suit == "S":
s += 1
if card.suit == "H":
h += 1
maxsuit = max([c,s,h,d])
return (maxsuit/float(len(fstate.hand)))
class SkipCount(Feature):
def f(self, fstate, action, combostate):
skipCards = 0
for card in fstate.hand:
if card.value == combostate.effectState.skipPlayerRule.setting:
skipCards += 1
return skipCards
class ScrewCount(Feature):
def f(self, fstate, action, combostate):
screwCards = 0
for card in fstate.hand:
if card.value == combostate.effectState.screwOpponentRule.setting:
screwCards += 1
return screwCards
class PoisonCount(Feature):
def f(self, fstate, action, combostate):
poisonCards = 0
for card in fstate.hand:
if card.value == combostate.effectState.poisonCardRule.setting:
poisonCards += 1
return poisonCards
class WildSuit(Feature):
def f(self, fstate, action, combostate):
trumpSuit = 0
if combostate.state.wildSuitRule.setting != None:
for card in fstate.hand:
if card.suit == combostate.state.wildSuitRule.setting:
trumpSuit += 1
return trumpSuit
class AceCount(Feature):
def f(self, fstate, action, combostate):
aces = 0
for card in fstate.hand:
if card.value == 14:
aces += 1
return aces
class TwoCount(Feature):
def f(self, fstate, action, combostate):
twos = 0
for card in fstate.hand:
if card.value == 2:
twos += 1
return twos
class PlayScrew(Feature):
def f(self, fstate, action, combostate):
return action.value == combostate.effectState.screwOpponentRule.setting
class PlaySkip(Feature):
def f(self, fstate, action, combostate):
return action.value == combostate.effectState.skipPlayerRule.setting
class OpponentCards(Feature):
def f(self, fstate, action, combostate):
#num cards in opponent's hand
return len(fstate.opponentHand)
# \\\\\\\\\\\\\\\\
#
# Testing stuff
#
# ////////////////
# Fake rules gethhelelp
# bvRule = Rule('basicValueRule', 2)
# wvRule = Rule('wildValueRule', 8)
# wsRule = Rule('wildSuitRule', 'C')
# pdRule = Rule('poisonDistRule', 9)
#
# # fake effects (not inited)
# pcRule = Rule('poisonCardRule', 14)
# soRule = Rule('screwOpponentRule', 99)
# spRule = Rule('skipPlayerRule', 99)
#
# # fake state
# ruleState = State(bvRule, wvRule, wsRule, pdRule)
# effectState = EffectState(pcRule, soRule,spRule)
#
# # combined
# combostate = CombinedState(ruleState,effectState)
#
# # Cards
# card1 = Card(2, "C")
# card2 = Card(13, "H")
# card4 = Card(8, "H")
# card3 = Card(14, "C")
#
# testFstate = Fstate([card1, card3], card2, [card4])
# testAction = card2
#
# # print WildSuit(testFstate, testAction, combostate).f()
#
# print featureDict([MajorityPercent(), PoisonCount()], testFstate, testAction, combostate)