-
Notifications
You must be signed in to change notification settings - Fork 0
/
CookieClicker.py
284 lines (214 loc) · 7.84 KB
/
CookieClicker.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Cookie Clicker Simulator
June 24, 2014
Kaili Liu
"""
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._current_cookies = 0.0
self._total_cookies = 0.0
self._current_time = 0.0
self._current_cookies_per_second = 1.0
self._just_bought_upgrade = None
self._now_upgrade_cost = 0.0
self._orig_upgrade_cost = 0.0
self._history_list = [(0.0, None, 0.0, 0.0)]
def __str__(self):
"""
Return human readable state
"""
game_state = ""
game_state += '\n'
game_state += '\n'
game_state += "Time: "
game_state += str(self._current_time)
game_state += '\n'
game_state += "Current Cookies: "
game_state += str(self._current_cookies)
game_state += '\n'
game_state += "CPS: "
game_state += str(self._current_cookies_per_second)
game_state += '\n'
game_state += "Total Cookies: "
game_state += str(self._total_cookies)
game_state += '\n'
game_state += "History: "
game_state += str(self._history_list)
game_state += '\n'
game_state += str(len(self._history_list))
return game_state
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_cookies_per_second
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)
"""
return self._history_list
def time_until(self, cookies):
"""
Return time until you have the given number of cookies
(could be 0 if you already have enough cookies)
Should return a float with no fractional part
"""
if self._current_cookies > cookies:
return 0.0
else:
seconds_until = math.ceil((cookies - self._current_cookies)/ self._current_cookies_per_second)
return seconds_until
def wait(self, time):
"""
Wait for given amount of time and update state
Should do nothing if time <= 0
"""
if time <= 0:
return
else:
self._current_cookies += (self._current_cookies_per_second * time)
self._total_cookies += (self._current_cookies_per_second * 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 self._current_cookies < cost:
return
else:
self._current_cookies -= cost
self._history_list.append((self._current_time, item_name, cost, self._total_cookies))
self._current_cookies_per_second += 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 game.
"""
# Replace with your code
game = ClickerState()
the_clone = build_info.clone()
while game.get_time() <= duration:
item_name = strategy(game.get_cookies(), game.get_cps(), duration - game.get_time(), the_clone)
if (item_name == None):
break
item_cost = the_clone.get_cost(item_name)
item_cps = the_clone.get_cps(item_name)
item_wait_time = game.time_until(item_cost)
time_left = duration - game.get_time()
if item_wait_time > time_left:
break
game.wait(item_wait_time)
game.buy_item(item_name, item_cost, item_cps)
the_clone.update_item(item_name)
if game.get_time() < duration:
game.wait(duration - game.get_time())
if item_name != None:
while game.get_cookies() > the_clone.get_cost(item_name):
game.buy_item(item_name, the_clone.get_cost(item_name), the_clone.get_cps(item_name))
the_clone.update_item(item_name)
return game
def strategy_cursor(cookies, cps, time_left, build_info):
"""
Always pick Cursor!
Note that this simplistic strategy does not properly check whether
it can actually buy a Cursor in the time left. Your strategy
functions must do this and return None rather than an item you
can't buy in the time left.
"""
return "Cursor"
def strategy_none(cookies, cps, time_left, build_info):
"""
Always return None
This is a pointless strategy that you can use to help debug
your simulate_clicker function.
"""
return None
def strategy_cheap(cookies, cps, time_left, build_info):
"""
Always select the cheapest item that you can afford in the time left.
"""
cheapest_item = None
cheapest_item_cost = 99999999999999999999999.0
for item in build_info.build_items():
item_cost = build_info.get_cost(item)
if (item_cost <= cookies + cps * time_left) and (item_cost < cheapest_item_cost):
cheapest_item = item
cheapest_item_cost = item_cost
return cheapest_item
def strategy_expensive(cookies, cps, time_left, build_info):
"""
Always select the most expensive item you can afford in the time left.
"""
most_expensive_item = None
highest_price = 0.0
for item in build_info.build_items():
item_cost = build_info.get_cost(item)
if (item_cost <= cookies + cps * time_left) and (item_cost > highest_price):
most_expensive_item = item
highest_price = item_cost
return most_expensive_item
def strategy_best(cookies, cps, time_left, build_info):
"""
Best possible strategy that I can think of.
"""
best_item = None
best_cps = 0.0
for item in build_info.build_items():
item_cost = build_info.get_cost(item)
item_cps = build_info.get_cps(item)
if (item_cost <= cookies + cps * time_left) and (item_cps/item_cost > best_cps):
best_item = item
best_cps = item_cps/item_cost
return best_item
def run_strategy(strategy_name, time, strategy):
"""
Run a simulation with one strategy
"""
state = simulate_clicker(provided.BuildInfo(), time, strategy)
print strategy_name, ":", state
# Plot total cookies over time
# Uncomment out the lines below to see a plot of total cookies vs. time
# Be sure to allow popups, if you do want to see it
# 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)
# 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()