Mountebank

roamer.py

Sep 11th, 2021
1,475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. import random
  2.  
  3. RAIKOU_START = 42
  4. ENTEI_START = 37
  5.  
  6. # Burned tower cannot match any of the routes
  7. PLAYER_START = 0
  8.  
  9. NEXT_ROUTES = {
  10.   29: [30, 46],
  11.   30: [29, 31],
  12.   31: [30, 32, 36],
  13.   32: [36, 31, 33],
  14.   33: [32, 34],
  15.   34: [33, 35],
  16.   35: [34, 36],
  17.   36: [35, 31, 32, 37],
  18.   37: [36, 38, 42],
  19.   38: [37, 39, 42],
  20.   39: [38],
  21.   42: [43, 44, 37, 38],
  22.   43: [42, 44],
  23.   44: [42, 43, 45],
  24.   45: [44, 46],
  25.   46: [45, 29],
  26. }
  27. ALL_ROUTES = list(NEXT_ROUTES)
  28.  
  29. def newRandomRoute(playerRoute):
  30.   newRoute = random.sample(ALL_ROUTES, 1)[0]
  31.   while newRoute == playerRoute:
  32.     newRoute = random.sample(ALL_ROUTES, 1)[0]
  33.   return newRoute
  34.  
  35. def chooseNextRoute(route, playerRoute, lastPlayerRoute):
  36.   possibleRoutes = NEXT_ROUTES[route]
  37.   while True:
  38.     a = random.randrange(256)
  39.     a &= 0b00011111
  40.     if a == 0:
  41.       return newRandomRoute(playerRoute)
  42.     a &= 0b11
  43.     if a >= len(possibleRoutes):
  44.       continue
  45.     newRoute = possibleRoutes[a]
  46.     if newRoute == lastPlayerRoute:
  47.       continue
  48.     return newRoute
  49.  
  50. def odds(path, nSamples):
  51.   successes = [0, 0]
  52.  
  53.   for i in range(nSamples):
  54.     routes = [RAIKOU_START, ENTEI_START]
  55.     lastPlayerRoute = PLAYER_START
  56.     for x in path:
  57.       routes = [chooseNextRoute(route, x, lastPlayerRoute) for route in routes]
  58.       lastPlayerRoute = x
  59.     if path[-1] == routes[0]:
  60.       successes[0] += 1
  61.     if path[-1] == routes[1]:
  62.       successes[1] += 1
  63.   return [float(x) / nSamples for x in successes]
  64.  
  65. def main():
  66.   random.seed()
  67.   print('Burned tower B1 -> Ekruteak -> Route 37')
  68.   print(odds([0, 37], 100000))
  69.  
  70.   print('Burned tower B1 -> Ekruteak -> Gate -> Route 38')
  71.   print(odds([0, 0, 38], 100000))
  72.  
  73.   print('Burned tower B1 -> Burned tower F1 -> Ekruteak -> Route 37')
  74.   print(odds([0, 0, 37], 100000))
  75.   print('Burned tower B1 -> Burned tower F1 -> Ekruteak -> Gate -> Route 38')
  76.   print(odds([0, 0, 0, 38], 100000))
  77.  
  78. if __name__ == '__main__':
  79.   main()
  80.  
Advertisement
Add Comment
Please, Sign In to add comment