-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_5.py
245 lines (211 loc) · 8.03 KB
/
2_5.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
Cookie Clicker Simulator
"""
import simpleplot
import math
# Used to increase the timeout, if necessary
import codeskulptor
codeskulptor.set_timeout(20)
import poc_clicker_provided as provided
# Constants
SIM_TIME = 10000000000.0
class ClickerState:
"""
Simple class to keep track of the game state.
"""
def __init__(self):
self._total_cookies = 0.0
self._current_cookies = 0.0
self._current_time = 0.0
self._current_cps = 1.0
self._history = [(0.0, None, 0.0, 0.0)]
def __str__(self):
"""
Return human readable state
"""
return " ".join(["\ntotal cookies:", str(self._total_cookies),
"\ncurrent cookies:", str(self._current_cookies),
"\ncurrent time:", str(self._current_time),
"\nCurrent CPS:", str(self._current_cps)])
def get_cookies(self):
"""
Return current number of cookies
(not total number of cookies)
Should return a float
"""
return self._current_cookies
def get_cps(self):
"""
Get current CPS
Should return a float
"""
return self._current_cps
def get_time(self):
"""
Get current time
Should return a float
"""
return self._current_time
def get_history(self):
"""
Return history list
History list should be a list of tuples of the form:
(time, item, cost of item, total cookies)
For example: [(0.0, None, 0.0, 0.0)]
Should return a copy of any internal data structures,
so that they will not be modified outside of the class.
"""
return self._history
def time_until(self, cookies):
"""
Return time until you have the given number of cookies
(could be 0.0 if you already have enough cookies)
Should return a float with no fractional part
"""
waittime = math.ceil((cookies - self._current_cookies) / self._current_cps)
if waittime > 0.0:
return waittime
else:
return 0.0
def wait(self, time):
"""
Wait for given amount of time and update state
Should do nothing if time <= 0.0
"""
if time > 0:
self._total_cookies += self._current_cps * time
self._current_cookies += self._current_cps * time
self._current_time += time
def buy_item(self, item_name, cost, additional_cps):
"""
Buy an item and update state
Should do nothing if you cannot afford the item
"""
if cost <= self._current_cookies:
self._history.append((self._current_time,
item_name,
cost,
self._total_cookies))
self._current_cookies -= cost
self._current_cps += additional_cps
def simulate_clicker(build_info, duration, strategy):
"""
Function to run a Cookie Clicker game for the given
duration with the given strategy. Returns a ClickerState
object corresponding to the final state of the game.
"""
build_info_clone = build_info.clone()
clickerstate = ClickerState()
while clickerstate.get_time() < duration:
choice = strategy(clickerstate.get_cookies(),
clickerstate.get_cps(),
clickerstate.get_history(),
duration - clickerstate.get_time(),
build_info_clone)
if choice == None:
clickerstate.wait(duration - clickerstate.get_time())
else:
if clickerstate.get_time() + clickerstate.time_until(build_info_clone.get_cost(choice)) <= duration:
clickerstate.wait(clickerstate.time_until(build_info_clone.get_cost(choice)))
clickerstate.buy_item(choice, build_info_clone.get_cost(choice), build_info_clone.get_cps(choice))
build_info_clone.update_item(choice)
else:
clickerstate.wait(duration - clickerstate.get_time())
while clickerstate.get_time() == duration:
choice = strategy(clickerstate.get_cookies(),
clickerstate.get_cps(),
clickerstate.get_history(),
0,
build_info_clone)
if choice == None:
break
else:
if build_info_clone.get_cost(choice) <= clickerstate.get_cookies():
clickerstate.buy_item(choice, build_info_clone.get_cost(choice), build_info_clone.get_cps(choice))
build_info_clone.update_item(choice)
else:
break
return clickerstate
def strategy_cursor_broken(cookies, cps, history, time_left, build_info):
"""
Always pick Cursor!
Note that this simplistic (and broken) strategy does not properly
check whether it can actually buy a Cursor in the time left. Your
simulate_clicker function must be able to deal with such broken
strategies. Further, your strategy functions must correctly check
if you can buy the item in the time left and return None if you
can't.
"""
return "Cursor"
def strategy_none(cookies, cps, history, time_left, build_info):
"""
Always return None
This is a pointless strategy that will never buy anything, but
that you can use to help debug your simulate_clicker function.
"""
return None
def strategy_cheap(cookies, cps, history, time_left, build_info):
"""
Always buy the cheapest item you can afford in the time left.
"""
pricelist = {}
fund = cookies + cps * time_left
for item in build_info.build_items():
pricelist[build_info.get_cost(item)] = item
if build_info.get_cost(pricelist[min(pricelist)]) <= cookies + cps * time_left:
return pricelist[min(pricelist)]
else:
return None
def strategy_expensive(cookies, cps, history, time_left, build_info):
"""
Always buy the most expensive item you can afford in the time left.
"""
pricelist = {}
fund = cookies + cps * time_left
for item in build_info.build_items():
if build_info.get_cost(item) <= fund:
pricelist[build_info.get_cost(item)] = item
if len(pricelist) > 0:
return pricelist[max(pricelist)]
else:
return None
def strategy_best(cookies, cps, history, time_left, build_info):
"""
The best strategy that you are able to implement.
"""
pricelist = {}
cpslist = {}
for item in build_info.build_items():
pricelist[build_info.get_cost(item)] = item
cpslist[build_info.get_cps(item) / build_info.get_cost(item)] = item
"""
In order to get as many cookies as possible, at the beginning 1.5% and
at the end 30% of simulation time, choose the item with lowest price,
other time choose the item with highest cps/cost
"""
if build_info.get_cost(pricelist[min(pricelist)]) <= cookies + cps * time_left:
if time_left > SIM_TIME / 100 * 98.5 or time_left < SIM_TIME / 100 * 30:
return pricelist[min(pricelist)]
else:
return cpslist[max(cpslist)]
else:
return None
def run_strategy(strategy_name, time, strategy):
"""
Run a simulation for the given time with one strategy.
"""
state = simulate_clicker(provided.BuildInfo(), time, strategy)
print strategy_name, ":", state
# history = state.get_history()
# history = [(item[0], item[3]) for item in history]
# simpleplot.plot_lines(strategy_name, 1000, 400, 'Time', 'Total Cookies', [history], True)
def run():
"""
Run the simulator.
"""
run_strategy("Cursor", SIM_TIME, strategy_cursor_broken)
# Add calls to run_strategy to run additional strategies
run_strategy("Cheap", SIM_TIME, strategy_cheap)
run_strategy("Expensive", SIM_TIME, strategy_expensive)
run_strategy("Best", SIM_TIME, strategy_best)
run()