-
Notifications
You must be signed in to change notification settings - Fork 3
/
drugwars.py
351 lines (312 loc) · 12.7 KB
/
drugwars.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from tkinter import *
import random
import os
import time
import pickle
class Drugs:
def __init__(self, name, price_range, amount):
self.name = name
self.price_range = price_range
self.amount = amount
self.price_check()
def price_check(self):
# the * is to unpack the touple of values that the random goes between
self.price = random.randint(*self.price_range)
# print("the price of " + self.name + " is " + str(self.price))
return self.price
class Events:
def __init__(self, name, text, price_range):
self.name = name
self.price_range = price_range
self.text = text
self.price_mod()
def price_mod(self):
self.price = random.randint(*self.price_range)
return self.price
my_drugs = [
Drugs("Cocaine", (15000, 28000), 0),
Drugs("Heroin", (2000, 10000), 0),
Drugs("Weed", (300, 1000), 0),
Drugs("Hash", (200, 1200), 0),
Drugs("Opium", (400, 1800), 0),
Drugs("Acid", (1000, 4200), 0),
Drugs("Ludes", (18, 75), 0),
]
event_list = [
Events("Cocaine", 'El Chapo Arrested! Coke price thru the roof!', (40000, 110000)),
Events("Heroin", 'Trump cracks down on opiates! Heroin in high demand by addicts', (9000, 25000)),
Events("Weed", 'The Mexicans have flooded the market with cheap grass! Weed prices bottom out', (50, 400)),
Events("Hash", 'Ricky\'s hash driveway burned down! Jesus Murphy look at the price boys!', (800, 2000)),
Events("Opium", 'Shenzhen 深圳 Opium 鸦片 Den 塔 was raided! Street price is popping off!', (1800, 6000))
]
def generatelocations():
locs = {'Canada': ('Red Deer', 'Edmonton', 'Calgary', 'Toronto', 'Vancouver', 'St. Johns'),
'USA': ('L.A.', 'NYC', 'Chicago', 'Miami', 'Houston', 'Phoenix')}
country = list(locs.keys())
country = country[random.randint(0, len(country)-1)]
location = []
for i in range(len(locs[country])):
location.append(locs[country][i])
return location
def generate_event():
event_choice = random.randint(0, len(event_list)-1)
if random.randint(0, 100) > 35:
return event_choice
else:
return -1
def officer():
global cash
global inventory
# rolls to see if the officer takes drugs from you
# odds are (1 - event chance) * (officer chance) * (confiscation chance)
# currently (1 - 0.35) * (0.20) * (0.35) = 4.55%
# chance is approximate, not sure how randint handles endpoints, close enough for my purposes
if random.randint(0, 100) > 65: # confiscation chance
k = 0
j = 0
# removes all drugs from inventory tally and individual class attirbute
for i in range(0, len(my_drugs)):
j = my_drugs[i].amount
my_drugs[i].amount = 0
k += j
inventory -= k
# sends 'conf' for confiscated. sending a string is better than a number here
cash_taken = 'conf'
return cash_taken
# rolls to see if the officer takes cash from you
# odds are (1 - event chance) * (officer chance) * (1 - confiscation chance)
# currently (1 - 0.35) * (0.20) * (0.65) = 8.45%
# chance is approximate, not sure how randint handles endpoints, close enough for my purposes
cash_taken = random.randint(1, cash-1)
cash -= cash_taken
return cash_taken
def price_change(event_number):
price_list = []
# again, fuck range() and its stupid handling of endpoints
for i in range(0, len(my_drugs)):
j = my_drugs[i]
k = j.price_check()
price_list.append(k)
if event_number != -1:
price_list[event_number] = event_list[event_number].price_mod()
return price_list
return price_list
def validate_numeric(value_string, numeric_type=int):
"""Validate a string as being a numeric_type"""
try:
if numeric_type(value_string) > 0:
return numeric_type(value_string)
else:
raise ValueError
except ValueError:
raise
def validate_alpha(value_string):
"""Validate a string as being a single alpha type"""
try:
if value_string.isalpha() and len(value_string) == 1:
return value_string
except ValueError:
raise
def check_inv():
k = 0
for i in range(0, len(my_drugs)-1):
k += my_drugs[i].amount
return k
def buy_func(price_list):
global cash
global inventory
menu_iteration = 0
while True:
if menu_iteration < 1:
drug_choice = input("Which drug?:\n")
menu_iteration += 1
else:
drug_choice = input("Buy anything Else? (press / to go back)\n")
try:
if drug_choice == '/':
break
drug_choice = validate_numeric(drug_choice, int)
if drug_choice in range(1, len(my_drugs) + 1):
drug_choice = drug_choice - 1
print(my_drugs[drug_choice].name + ": you have " + str(my_drugs[drug_choice].amount))
print("The going price is: $" + str(price_list[drug_choice]))
# need to restructure this
while True:
buy_amount = input('How much you want to buy? (m for max)\n')
if buy_amount == 'm':
buy_amount = cash // price_list[drug_choice]
if buy_amount > 100 - inventory:
buy_amount = 100 - inventory
if buy_amount == '/':
break
try:
buy_amount = validate_numeric(buy_amount, int)
if buy_amount > 100 - inventory:
print("You don\'t have enough space for all that.")
continue
if buy_amount * price_list[drug_choice] <= cash:
my_drugs[drug_choice].amount += buy_amount
cash -= buy_amount * price_list[drug_choice]
inventory += buy_amount
print("Cash: $" + str(cash))
print("You bought " + str(buy_amount) + " " + my_drugs[drug_choice].name)
break
else:
print("You don\'t have enough cash!")
raise ValueError
except ValueError:
continue
except ValueError:
continue
def sell_func(price_list):
global cash
global inventory
menu_iteration = 0
while True:
if menu_iteration < 1:
drug_choice = input("Which drug?:\n")
menu_iteration += 1
# can nuke this and menu_iteration if I dont want another option in the future
else:
drug_choice = input("Sell anything Else? (press / to go back)\n")
try:
if drug_choice == '/':
break
drug_choice = validate_numeric(drug_choice, int)
if drug_choice in range(1, len(my_drugs) + 1) and my_drugs[drug_choice-1].amount > 0:
drug_choice = drug_choice - 1
print(my_drugs[drug_choice].name + ": you have " + str(my_drugs[drug_choice].amount))
print("The going price is: $" + str(price_list[drug_choice]))
# need to restructure this
while True:
sell_amount = input('How much you want? (m for max)\n')
if sell_amount == 'm':
sell_amount = my_drugs[drug_choice].amount
if sell_amount == '/':
break
try:
sell_amount = validate_numeric(sell_amount, int)
if sell_amount <= my_drugs[drug_choice].amount:
my_drugs[drug_choice].amount -= sell_amount
cash += sell_amount * price_list[drug_choice]
inventory -= sell_amount
print("Cash: $" + str(cash))
print("You sold " + str(sell_amount) + " " + my_drugs[drug_choice].name + ' for $' + str(
sell_amount * price_list[drug_choice]))
break
else:
print("You don\'t have that much")
raise ValueError
except ValueError:
continue
else:
print('You don\'t have any')
continue
except ValueError:
continue
def location_change():
print("Locations:")
for i in range(len(loc)):
print(str(i + 1) + '. ' + loc[i])
while True:
loc_choice = input('Where do you want to go?:\n')
try:
# set this as 1-6 for an index of 0-5, then set from 1-7 because range isnt inclusive on the last number
# could be fed locs variable for better autism, but manual based on # of locs is ok for now
if int(loc_choice) in range(1, 7):
return loc_choice
# this should filter out all bad responses
except ValueError:
continue
def print_list(day_play, total_day, loc_choice, event_number, price_list, cash_stolen):
os.system('cls' if os.name == 'nt' else 'clear')
news_header = "=" * 36 + "N E W S" + "=" * 37
news_footer = "=" * 79
if event_number != -1:
print(news_header)
print(event_list[event_number].text)
print(news_footer)
if event_number == -1 and cash_stolen != 0 and cash_stolen != 'conf':
print(news_header)
print("Officer Leroy stopped you and took $" + str(cash_stolen) + " from you.")
print(news_footer)
if event_number == -1 and cash_stolen == 'conf':
print(news_header)
print("Officer Leroy stopped you and took all of your drugs.")
print(news_footer)
print("Location: " + loc[int(loc_choice) - 1])
print("Day: " + str(day_play) + '/' + str(total_day))
print("Cash: $" + str(cash))
print("Inventory: " + str(inventory) + "/100")
print("No. Inv: Drug: Price:")
print("----------------------------")
for i, drug in enumerate(my_drugs, 1):
print(str(i) + ". " + " (" + str(drug.amount) + ") " + ' '.rjust(6-len(str(drug.amount))) + drug.name + ' '.rjust(8-len(str(drug.name))) + " $" + str(price_list[i-1]))
def gameloop(day_play, total_day):
global cash
loc_choice = location_change()
event_number = generate_event()
cash_stolen = 0
# rolls to see if the officer event happens
# odds are (1 - event chance) * (officer chance)
# currently (1 - 0.35) * (0.20) = 13%
# chance is approximate, not sure how randint handles endpoints, close enough for my purposes
if event_number == -1 and random.randint(0, 100) > 80:
cash_stolen = officer()
price_list = price_change(event_number)
check_inv()
print_list(day_play, total_day, loc_choice, event_number, price_list, cash_stolen)
while True:
menu_choice = input("Press b to buy, s to sell, or m to move.\n")
try:
menu_choice = validate_alpha(menu_choice)
if menu_choice == 'b':
buy_func(price_list)
continue
if menu_choice == 's':
sell_func(price_list)
continue
if menu_choice == 'm':
os.system('cls' if os.name == 'nt' else 'clear')
break
# this should filter out all bad responses
except ValueError:
continue
# Pregame setup
loc = generatelocations()
global cash
global inventory
inventory = 0
starting_cash = 2000
cash = starting_cash
# filters player's input for the game duration
file = open('high_score.pickle', 'rb')
high_score = pickle.load(file)
file.close()
print('The Local high score is $' + str(high_score))
while True:
day = input("How many days do you want to play?\n")
try:
day = validate_numeric(day, int)
break
except ValueError:
continue
# Calls gameloop the number of times inputted by the player
for game_day in range(1, day+1):
gameloop(game_day, day)
# Loading highscore to variable, comparing against cash and ammending if new high score is achieved.
file = open('high_score.pickle', 'rb')
high_score = pickle.load(file)
file.close()
if cash > int(high_score):
file = open('high_score.pickle', 'wb')
pickle.dump(cash, file)
file.close()
print("You finished with $" + str(cash))
if cash > starting_cash:
print('You made money! Up ' + str((cash/starting_cash).__round__()) + 'x! Well done.')
if cash == starting_cash:
print('You broke even... hope you at least had fun')
if cash < starting_cash:
print("You lost money, better go get a real job.")
time.sleep(8)