Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.95 KB | None | 0 0
  1. import time
  2. #import threading
  3. import random
  4.  
  5. # Description -> type:(probability, value)
  6. APPLE_TYPES = {"GREEN":(0.40, 10), "RED":(0.40, 10), "GOLD":(0.15, 50), "FADED":(0.05, 100)}
  7. CHEST_TYPES = {"BROKEN":(0.05, 10), "NORMAL":(0.70, 1500), "RARE":(0.20, 5000), "LEGENDARY":(0.05, 50000)}
  8.  
  9. # upgrade costs
  10. BASE_COST = {"basket":250, "machine":1500, "x_machine":15000}
  11. DELTA_COST = {"basket": 250, "machine":2500, "x_machine":25000}
  12. CHEST_COST = 1500
  13.  
  14. def weighted_choice(iter_obj,
  15.                     values = lambda iter_obj: iter_obj.keys(),
  16.                     weight = lambda iter_obj, val: iter_obj[val][0]):
  17.    
  18.     total = sum(weight(iter_obj, val) for val in values(iter_obj))
  19.     r = random.uniform(0, total)
  20.     upto = 0
  21.     for val in values(iter_obj):
  22.         if upto + weight(iter_obj, val) >= r:
  23.             return val
  24.         upto += weight(iter_obj, val)
  25.  
  26. class Apple(object):
  27.     def __init__(self):
  28.         self.color = weighted_choice(APPLE_TYPES)
  29.         self.value = APPLE_TYPES[self.color][1]
  30.  
  31. class Chest(object):
  32.     def __init__(self):
  33.         self.type = weighted_choice(CHEST_TYPES)
  34.         self.value = int(max(0.1, random.random()) * CHEST_TYPES[self.type][1])
  35.  
  36. class ApplePicker(object):
  37.  
  38.     def __init__(self):
  39.         self.apples = []
  40.         self.upgrades = {"basket":0, "machine":0, "x_machine":0}
  41.         self.coins = 0
  42.         self.chests = []
  43.  
  44.     def open_chest(self, x = 1):
  45.         if len(self.chests) < x:
  46.             print "Error, you don't own any chests."
  47.         else:
  48.             for i in xrange(x):
  49.                 # Get last chest
  50.                 chest = self.chests.pop()
  51.                 self.coins += chest.value
  52.                 print "Your %s chest contained: %d coins" % (chest.type, chest.value)
  53.            
  54.     def buy_chest(self):
  55.         if self.coins < CHEST_COST:
  56.             print "You don't have enough coins"
  57.         else:
  58.             self.coins -= CHEST_COST
  59.             chest = Chest()
  60.             self.chests.append(chest)
  61.             print "You bought a %s chest!" % chest.type
  62.             print "press c to open it!"
  63.            
  64.     def check_balance(self):
  65.         print "You have %d apples." % len(self.apples)
  66.         print "You have %d coins." % self.coins
  67.         print "You have %d chests." % len(self.chests)
  68.    
  69.     def robber_check(self):
  70.         delta = time.time() - self.robber_timer
  71.        
  72.         # For every second passed 30 second the risk of getting robbed increases
  73.         if random.random()*delta > 30:
  74.             amount = int(self.coins * max(0.3, random.random()))
  75.             self.coins -= amount
  76.             self.robber_timer = time.time()
  77.             print ("\nA robber just stole %d coins from you!\n" % amount).upper()
  78.        
  79.         # Sometimes by random reset timer
  80.         elif random.random() > 0.95:
  81.             #print "timer war reset"
  82.             self.robber_timer = time.time()
  83.        
  84.     def main(self):
  85.         self.machine_timer = time.time()
  86.         self.x_machine_timer = time.time()
  87.         self.robber_timer = time.time()
  88.         while True:
  89.             user_in = raw_input()
  90.             self.robber_check()
  91.  
  92.             if user_in == "c":
  93.                 self.open_chest()
  94.  
  95.             elif user_in == "u":
  96.                 self.upgrade()
  97.            
  98.             elif user_in == "p":
  99.                 self.pick()
  100.  
  101.             elif user_in == "m":
  102.                 self.machine_pick()
  103.            
  104.             elif user_in.startswith("s"):
  105.                 val = user_in.split()
  106.                 if len(val) > 1:
  107.                     self.sell_apples(x = val[-1])
  108.  
  109.                 else:        
  110.                     self.sell_apples()
  111.  
  112.             elif user_in == "b":
  113.                 self.check_balance()
  114.                
  115.             elif user_in == "q":
  116.                 break
  117.            
  118.             elif user_in == "help":
  119.                 apple_picker.print_welcome()
  120.                
  121.             elif user_in == "prices":
  122.                 apple_picker.print_prices()
  123.  
  124.             elif user_in == "g":
  125.                 self.buy_chest()
  126.  
  127.     def machine_pick(self):
  128.  
  129.         delta_time = int(round(time.time() - self.machine_timer))
  130.         x_delta_time = int(round(time.time() - self.x_machine_timer))
  131.  
  132.         new_apples = delta_time * self.upgrades["machine"]
  133.         new_x_apples = x_delta_time * self.upgrades["x_machine"]**2
  134.        
  135.         self.machine_timer += delta_time
  136.         self.x_machine_timer += x_delta_time
  137.        
  138.         print "Machine picked %d apples" % new_apples
  139.        
  140.         if new_x_apples:
  141.             print "X_machine picked %d apples" % new_x_apples
  142.        
  143.         for _ in xrange(new_apples):
  144.             self.apples.append(Apple())
  145.            
  146.         for _ in xrange(new_x_apples):
  147.             self.apples.append(Apple())
  148.  
  149.     def pick(self):
  150.         for i in xrange(self.upgrades["basket"]+1):
  151.             self.apples.append(Apple())
  152.             print "You picked a %s apple!" % self.apples[-1].color
  153.          
  154.     def sell_apples(self, x = "1"):
  155.  
  156.         if x.isdigit():
  157.             # Convert to integear is possible
  158.             x = int(x)
  159.  
  160.         elif isinstance(x, str):
  161.             # If x is a string check if equal to "all"
  162.             if x == "all":
  163.                 x = len(self.apples)
  164.  
  165.             # If string not valid, remove one apple anyway
  166.             else:
  167.                 x = 1
  168.        
  169.         # If more than 5 apples are sold, don't print for every apple
  170.         printout = True
  171.         if x > 5:
  172.             printout = False
  173.            
  174.         if len(self.apples) < x:
  175.             print "You don't have %d apples to sell" % x
  176.            
  177.         else:
  178.             pre_sell_coins = self.coins
  179.             for i in xrange(x):
  180.                 apple = self.apples[0]
  181.                    
  182.                 self.coins += apple.value
  183.                 self.apples.remove(apple)
  184.                
  185.                 if printout:
  186.                     print "You sold a %s apple and got %d coins." % (apple.color, apple.value)
  187.                    
  188.             print "You sold %d apples worth %d coins" % (x, self.coins-pre_sell_coins)
  189.                
  190.     def upgrade(self):
  191.         machine_cost = BASE_COST["machine"] + DELTA_COST["machine"] * self.upgrades["machine"]
  192.         basket_cost = BASE_COST["basket"] + DELTA_COST["basket"] * self.upgrades["basket"]
  193.         x_machine_cost = BASE_COST["x_machine"] + DELTA_COST["x_machine"] * self.upgrades["x_machine"]
  194.         print ("Current upgrades are:\n"
  195.                "Basket: %d\n"
  196.                "Machine: %d\n"
  197.                "X_machine: %d\n" % (self.upgrades["basket"],
  198.                                     self.upgrades["machine"],
  199.                                     self.upgrades["x_machine"]))
  200.         print ("Would you like to upgrade:\n"
  201.                "[b] Basket: %d Coins\n"
  202.                "[m] Machine: %d Coins\n"
  203.                "[x] X_machine: %d Coins." % (basket_cost,
  204.                                              machine_cost,
  205.                                              x_machine_cost))
  206.         while True:
  207.             user_in = raw_input()
  208.             if user_in == "m":
  209.                 if self.coins >= machine_cost:
  210.                    
  211.                     if self.upgrades["machine"] >= 5:
  212.                         print "You can't upgrade your machine anymore."
  213.                         break
  214.                    
  215.                     self.upgrades["machine"] += 1
  216.                     self.coins -= machine_cost
  217.  
  218.                     # reset machine timer when upgraded
  219.                     self.machine_timer = time.time()
  220.                     print "You upgraded your machine [-%d coins]" % machine_cost
  221.                    
  222.                 else:
  223.                     print "You dont have enough coins [%d needed]" % machine_cost
  224.                    
  225.                 break
  226.            
  227.             elif user_in == "b":
  228.                 if self.upgrades["basket"] >= 5:
  229.                         print "You can't upgrade your basket anymore."
  230.                         break
  231.                
  232.                 if self.coins >= basket_cost:
  233.                     self.upgrades["basket"] += 1
  234.                     self.coins-=basket_cost
  235.                     print "You upgraded your basket! [-%d Coins]" % basket_cost
  236.                 else:
  237.                     print "You dont have enough coins [%d needed]" % basket_cost
  238.                 break
  239.            
  240.             elif user_in == "x":
  241.                 if self.upgrades["machine"] < 5:
  242.                     print "You must upgrade your machine to level 5 before you can upgrade x_machine"
  243.                
  244.                 elif self.coins < x_machine_cost:
  245.                     print "You dont have enough coins [%d needed]" % x_machine_cost
  246.                
  247.                 else:
  248.                     self.upgrades["x_machine"] += 1
  249.                     self.coins -= x_machine_cost
  250.                     self.x_machine_timer = time.time()
  251.                     print "You upgraded your x_machine [-%d coins]" % x_machine_cost
  252.                
  253.                 break
  254.            
  255.             else:
  256.                 print "Invalid input, try again!"
  257.                
  258.     def print_welcome(self):
  259.         print "==================================================================================="
  260.         print "The apolcolypse is near,\nWe got to do whatever we can to prepare for it!"
  261.         print ""
  262.         print "Hey YOU!\nGo to those trees and pick some apples you later can sell for other special stuff!"
  263.         print "===================================================================================\n"
  264.         print ("Controlls:\n[c] Opens your chests.\n"
  265.                "[g] Buy a chest [%d coins]\n"
  266.                "[p] Picks an apple for you.\n"
  267.                "[b] Is your balance.\n"
  268.                "[s] [amount] sells the amount apples that you wish!\n"
  269.                "[u] to upgrade your tools\n"
  270.                "[m] Collect apples from machines.\n"
  271.                "And more controlls are comming!\n" % CHEST_COST)
  272.         print ("Commands:\n"
  273.                "[help] Brings this text back if you're lost in the forest.\n"
  274.                "[prices] Will google for you how much an apple is worth.\n")
  275.         print "Just remember, these fade apples are the most valuable! but at the same time they are the most rare."
  276.  
  277.     def print_prices(self):
  278.         print "==================================================================================="
  279.         print "Oh hello, this is the website of [BuyingApples.org]\nHere you can see what we are offering for YOUR apples."
  280.         print "==================================================================================="
  281.         print ""
  282.         print "[Faded Apples] = 100 Coins\n[Golden Apples] = 50 Coins\n[Red Apples] = 10 Coins\n[Green Apples] = 10 Coins"
  283.        
  284. if __name__ == "__main__":
  285.     apple_picker = ApplePicker()
  286.     apple_picker.print_welcome()
  287.     apple_picker.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement