Files
  • main.py
main.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
import random
import time

class Item:
	def __init__(self, name, baseValue):
		self.name = name
		self.baseValue = baseValue
	def GetNormalPrice(self):
		return self.baseValue
	def GetPremiumPrice(self):
		return self.GetNormalPrice() * 2
	def GetDiscountPrice(self):
		return self.GetNormalPrice() / 2

# "items" is a dict with an item and a count
class Inventory:
	def __init__(self, items):
		self.items = items
	def RemoveItem(self, item):
		for i in self.items:
			if (item == i):
				self.items[i] -= 1
	def AddItem(self, item):
		for i in self.items:
			if (item == i):
				self.items[i] += 1
	def HasItem(self, item):
		for i in self.items:
			if (item == i):
				if self.items[i] > 0:
					return True
		return False
	def ToString(self):
		retVal = ""
		hasListedAnItem = False
		for i in self.items:
			if self.items[i] > 0:
				retVal += (i.name + " (" + str(self.items[i]) + "x)" + " ... " + str(i.baseValue) + "\n")
				hasListedAnItem = True
		if not hasListedAnItem:
			retVal += "*Nothing*"
		return retVal
		
class Person:
	def __init__(self, name, inventory, wallet, job, valuableItemList, desiredItemList, undesiredItemList):
		self.name = name
		self.inventory = inventory
		self.wallet = wallet
		self.job = job		# wizard, fighter
		self.valuableItemList = valuableItemList
		self.desiredItemList = desiredItemList
		self.undesiredItemList = undesiredItemList
	def GetMy_iWillSellThisFor_Price(self, item):
		if self.valuableItemList is not None and item in self.valuableItemList:
			return item.GetPremiumPrice()
		else:
			return item.GetNormalPrice()
	def GetMy_iWillBuyThisFor_Price(self, item):
		if item in self.desiredItemList:
			return self.GetPremiumPrice(item)
		elif item in self.undesiredItemList:
			return self.GetDiscountPrice(item)
		else:
			return self.GetNormalPrice(item)
	def InventoryToString(self, showPrices):
		retVal = ""
		hasListedAnItem = False
		for i in self.inventory.items:
			retVal += i.name
			if self.inventory.items[i] is not 0:
				retVal += " (" + str(self.inventory.items[i]) + "x) "
			else:
				retVal += " (--) "
			if (showPrices):
				retVal += " ... " + str(self.GetMy_iWillSellThisFor_Price(i))
			else:
				retVal += " ... (standard price) " + str(self.GetMy_iWillSellThisFor_Price(i))
				
			if self.desiredItemList is not None and self.undesiredItemList is not None:
				#if i in self.desiredItemList:
				#	retVal += "    (I WANT more of these)    "
				if i in self.undesiredItemList:
					retVal += "    (I DON'T want any more of these)    "
			retVal += "\n"
			hasListedAnItem = True
		if not hasListedAnItem:
			retVal += "*No Items*\n"
		retVal += "Wallet: $" + str(self.wallet)
		return retVal
	def HasItem(self, item):
		return self.inventory.HasItem(item)
	def UpdateWallet(self, amountChange):
		self.wallet += amountChange

# globals #########################################
currentDifficulty = 1
currentRentCost = -1
startingRentCost = -1
numDaysToRaiseRent = -1
dayCounter = 0
startingCash = -1
goalCash = -1
minCustomerCash = -1
maxCustomerCash = -1
chanceToNotWantToBuy = -1		# 1 over this number
maxNumOfEachItem = 4
helpText = "help:\n'buy [item]' or 'b [item]' \tbuy something\n'sell [item]' or 's [item]' \tsell something\n'next' or 'n' \t\t\tgo to the next day with a new customer\n'repeat' or 'r' \t\trepeat the last command\n'exit' \t\texit\n"

# methods ########################################		

def GetNewEmptyInventory(_MasterItemList):
	EmptyInventory_dict = {}
	for i in _MasterItemList:
		EmptyInventory_dict[_MasterItemList[i]] = 0
	return Inventory(EmptyInventory_dict)
	
def ScrollToBlankScreen():
	print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")

def MakeTransaction(seller, buyer, item, isUsingSellersPrice):

	transAmount = 0 
	if isUsingSellersPrice:
		transAmount = seller.GetMy_iWillSellThisFor_Price(item)
	else:
		transAmount = buyer.GetMy_iWillSellThisFor_Price(item)
	
	if buyer.undesiredItemList is not None and item in buyer.undesiredItemList:
		print ("I don't want any more of those.")
		return False
		
	if transAmount <= buyer.wallet:
		if seller.HasItem(item):
			# make a successful transaction
			buyer.UpdateWallet(-transAmount)
			seller.UpdateWallet(transAmount)
			seller.inventory.RemoveItem(item)
			buyer.inventory.AddItem(item)
			print ("Thanks! Anything else?")
			return True
		else:
			print ("All out of that.")
	else:
		print ("Not enough money.")
	return False
	
def ListInventories(player, customer):
	retVal = "\nItem \tYou Have \tCustomer Has \tCustomer Price \tStandard Price \tCustomer Comments\n"
	for i in player.inventory.items:
		retVal += "----------------------------------------------------------------------------------------------------------\n "
		retVal +=  i.name
		retVal += "\t"
		if player.inventory.items[i] is not 0:
				retVal += str(player.inventory.items[i]) + "x "
		else:
			retVal += "-- "
		retVal += "\t\t"
		if customer.inventory.items[i] is not 0:
				retVal += str(customer.inventory.items[i]) + "x "
		else:
			retVal += "-- "
		retVal += "\t\t"
		retVal += "$" + str(customer.GetMy_iWillSellThisFor_Price(i))
		
		if i in customer.valuableItemList:
			retVal += " ^"
		retVal += "\t\t"
		retVal += "$" + str(player.GetMy_iWillSellThisFor_Price(i))
		retVal += "\t\t"
		if i in customer.undesiredItemList:
			retVal += "I DON'T want any more of these"
		retVal += "\n"
	
	# list wallets
	retVal += "\nCustomer's Wallet: $" + str(customer.wallet)
	retVal += "\nYour Wallet: $" + str(player.wallet)
	retVal += "\n"
	
	# wallet bar
	for dollar in range(0, int(player.wallet / 5)):
		retVal += ">"
	for space in range(0, int((goalCash - player.wallet) / 5)):
		retVal += " "
	retVal += "||"
	retVal += " Goal: $" + str(goalCash)
	retVal += "\n"
	return retVal
	
# start main #######################################

# intro screen
ScrollToBlankScreen()
print("Welcome to the Trading Post!")
print("\n")
print("Instructions:")
print("* Customers will visit your store looking to trade.")
print("* Try to buy items at a standard price, and then sell items at a premium price.")
print("* Save up a certain amount to win!")
print("\n")
#print("'h' for help\n\n")

user_input = input("Choose difficulty (1 to 3): ")

if user_input is "2":
	currentDifficulty = 2
elif user_input is "3":
	currentDifficulty = 3
else:
	currentDifficulty = 1

# set difficulty settings
if currentDifficulty == 1:
	startingCash = 120
	startingRentCost = 1
	numDaysToRaiseRent = 5
	goalCash = 200
	minCustomerCash = 25
	maxCustomerCash = 70
	chanceToNotWantToBuy = 12
elif currentDifficulty == 2:
	startingCash = 100
	startingRentCost = 2
	numDaysToRaiseRent = 4
	goalCash = 300
	minCustomerCash = 30
	maxCustomerCash = 80
	chanceToNotWantToBuy = 9
elif currentDifficulty == 3:
	startingCash = 80
	startingRentCost = 3
	numDaysToRaiseRent = 3
	goalCash = 400
	minCustomerCash = 40
	maxCustomerCash = 100
	chanceToNotWantToBuy = 7

currentRentCost = startingRentCost

# create items
MasterItemList = {}
MasterItemList["shoes"] = Item("shoes", 2)
MasterItemList["potion"] = Item("potion", 8)
MasterItemList["sword"] = Item("sword", 15)
if currentDifficulty >= 2:
	MasterItemList["cart"] = Item("cart", 25)
if currentDifficulty >= 3:
	MasterItemList["crown"] = Item("crown", 60)
	
shopInventory = GetNewEmptyInventory(MasterItemList)

# create player person
player = Person("You", shopInventory, startingCash, None, None, None, None)

customerNumber = 0

historyOfWallet = []
lastCommand = ""

while True:

	historyOfWallet.append(player.wallet)
	dayCounter += 1
	if (dayCounter % numDaysToRaiseRent == 0):
		currentRentCost += 1
	minCustomerCash += 1
	maxCustomerCash += 2

	# create a new customer
	customerNumber += 1
	cust_inventory = GetNewEmptyInventory(MasterItemList)
	for i in MasterItemList:
		cust_inventory.items[MasterItemList[i]] = random.randrange(0, maxNumOfEachItem)
	myValuableItems = []
	myDesiredItems = []
	myUndesiredItems = []
	for i in cust_inventory.items:
		if random.randrange(0, 2) == 1:
			myValuableItems.append(i)
		valuableShuffle = random.randrange(0, chanceToNotWantToBuy)
		if valuableShuffle == 1:
			myDesiredItems.append(i)
		elif valuableShuffle == 2:
			myUndesiredItems.append(i)
	customer = Person("Customer" + str(customerNumber), cust_inventory, random.randrange(minCustomerCash, maxCustomerCash + 1), "fighter", myValuableItems, myDesiredItems, myUndesiredItems)
	
	print("Can I sell any of this stuff here?\n")
	while True:
		# list items
		print(ListInventories(player, customer))
		
		# ask user which item they want to buy
		user_input = input("You: ")
		
		if user_input == "repeat" or user_input == "r":
			user_input = lastCommand
		else:
			lastCommand = user_input
		
		ScrollToBlankScreen()
		user_input_tokens = user_input.split(' ')
		
		if user_input_tokens[0] == "buy" or user_input_tokens[0] == "b" or user_input_tokens[0] == "sell" or user_input_tokens[0] == "s":
			if len(user_input_tokens) > 1 and user_input_tokens[1] in MasterItemList:
				itemForSale = MasterItemList[user_input_tokens[1].lower()]
				if user_input_tokens[0] == "buy" or user_input_tokens[0] == "b":
					MakeTransaction (customer, player, itemForSale, True)
				elif user_input_tokens[0] == "sell" or user_input_tokens[0] == "s":
					MakeTransaction (player, customer, itemForSale, False)
			else:
				print("What? I'm not sure what that is.\n")
		elif user_input_tokens[0] == "next" or user_input_tokens[0] == "n":
			break
		elif user_input_tokens[0] == "exit":
			print("We'll call it a tie.\n\n")
			quit()
		elif user_input_tokens[0] == "h":
			print(helpText)
		else:
			print("What?\n")
	
	if player.wallet >= goalCash:
		print("You are the richest trader in the land!")
		break
	
	player.UpdateWallet(-currentRentCost)
	print("Day " + str(dayCounter) + ": You spent $" + str(currentRentCost) + " on rent today\n\n")
	if player.wallet < 0:
		print("You are bankrupt. Heroes will have to find a new shop.")
		break
	
		
# display game summary
print("\n\nBankStatement (" + str(len(historyOfWallet)) + " days):\n")
for historicValue in historyOfWallet:
	oneBar = ""
	for dollar in range(0, int(historicValue / 5)):
		oneBar += ">"
	print(oneBar)
		
time.sleep(1)