Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. from catan import *
  2.  
  3.  
  4. def output(p, x, y):
  5. resources = [[0 for _ in range(3)] for _ in range(11)]
  6. for dx in [-1, 0]:
  7. for dy in [-1, 0]:
  8. xx = x + dx
  9. yy = y + dy
  10. if p.board.is_tile(xx, yy):
  11. die = p.board.dice[yy, xx]
  12. if die != 7:
  13. resource = p.board.resources[yy, xx]
  14. resources[die - 2][resource] += 1
  15. return resources
  16.  
  17.  
  18. def doAction(p, move):
  19. if move[0] == "buy": # Road
  20. p.buy(move[1], move[2], move[3])
  21. elif move[0] == "trade":
  22. p.trade(move[1], move[2])
  23.  
  24.  
  25. # Helper method to help us determine valid actions to take
  26. def actionSet(p):
  27. """
  28. Set of actions we can take:
  29. -nothing: do nothing
  30. -city: upgrade to a city
  31. -settlement: build a settlement
  32. -road: build a road
  33. -card: buy a development card
  34. -trade: trade for resources
  35. """
  36.  
  37. actions = []
  38.  
  39. if p.if_can_buy("settlement"):
  40. for x in range(p.board.height):
  41. for y in range(p.board.width):
  42. if p.board.if_can_build("settlement", x, y, p.player_id):
  43. actions.append(("buy", "settlement", x, y))
  44.  
  45. if p.if_can_buy("city"):
  46. for x in range(p.board.height):
  47. for y in range(p.board.width):
  48. if p.board.if_can_build("city", x, y, p.player_id):
  49. actions.append(("buy", "city", x, y))
  50.  
  51. if p.if_can_buy("road"):
  52. for x in range(p.board.height):
  53. for y in range(p.board.width):
  54. if x - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  55. p.board.get_vertex_number(x - 1, y), p.player_id):
  56. actions.append(("buy", "road", (x, y), (x - 1, y)))
  57. if x + 1 < p.board.height and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  58. p.board.get_vertex_number(x + 1, y),
  59. p.player_id):
  60. actions.append(("buy", "road", (x, y), (x + 1, y)))
  61. if y - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  62. p.board.get_vertex_number(x, y - 1), p.player_id):
  63. actions.append(("buy", "road", (x, y), (x, y - 1)))
  64. if y + 1 < p.board.width and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  65. p.board.get_vertex_number(x, y + 1),
  66. p.player_id):
  67. actions.append(("buy", "road", (x, y), (x, y + 1)))
  68.  
  69. if p.if_can_buy("card"):
  70. actions.append(("buy", "card", -1, -1))
  71.  
  72. for r_in in range(0, 3):
  73. required = 4
  74. ports = []
  75. for e in p.get_settlements():
  76. if p.board.is_port(e):
  77. ports.append(p.board.which_port(e))
  78. for e in p.get_cities():
  79. if p.board.is_port(e):
  80. ports.append(p.board.which_port(e))
  81. if r_in in ports:
  82. required = 2
  83. if 3 in ports:
  84. required = min(required, 3)
  85. if p.resources[r_in] >= required:
  86. for r_out in range(0, 3):
  87. if r_in == r_out:
  88. continue
  89. actions.append(("trade", r_in, r_out))
  90. return actions
  91.  
  92.  
  93. def purchaseSet(p):
  94. """
  95. returntype:
  96. {"settlement":([ LIST OF COSTS (r_in, r_out, frequency) ],[ LISIT OF POSITIONS ])
  97. "city":([ LIST OF COSTS ],[ LIST OF POSITIONS ])
  98. }
  99. """
  100. # For every type of thing that can be purchsed
  101.  
  102. # else:
  103. # Figure out what resources we are lacking (if all 3 lacking then gg)
  104. # find what combinations of surplus resources we can trade to fix shortages
  105. # Example:
  106. # possible trades (4 of any to any, 2 of #3 to any)
  107. # cost = 1, 2, 2
  108. # have = 5, 1, 4 (defecit of resource 2)
  109. # we need to trade resource 1 --> 2 or 3 --> 2
  110. # 2 possible prices we could pay are (5,1,2) or (1,1,4)
  111. # also condsider all locations that we can buy the thing in
  112.  
  113.  
  114. ### GLOBALS
  115. # SETTLEMENT = 0
  116. # CARD = 1
  117. # CITY = 2
  118. # ROAD = 3
  119. # costs = np.array([[2, 1, 1],
  120. # [1, 2, 2],
  121. # [0, 3, 3],
  122. # [1, 1, 0]])
  123.  
  124. result = {}
  125. purchase_types = ["settlement", "card", "city", "road"]
  126. for index in range(4):
  127. item = purchase_types[index]
  128. list_of_costs = []
  129. list_of_positions = []
  130.  
  131. if p.if_can_buy(item):
  132. list_of_costs.append(("No trade needed"))
  133. else:
  134. ports = []
  135. for e in p.get_settlements():
  136. if p.board.is_port(e):
  137. ports.append(p.board.which_port(e))
  138. for e in p.get_cities():
  139. if p.board.is_port(e):
  140. ports.append(p.board.which_port(e))
  141.  
  142. for r_out in range(3):
  143. for r_in in range(3):
  144. if r_in == r_out:
  145. continue
  146.  
  147. required = 4
  148. if r_in in ports:
  149. required = 2
  150. if 3 in ports:
  151. required = min(required, 3)
  152.  
  153. new_costs = [cost for cost in costs[index]]
  154. for i in range(costs[index][r_out]):
  155. new_costs[r_out] -= 1
  156. new_costs[r_in] += required
  157.  
  158. if sum(new_costs) <= ROBBER_MAX_RESOURCES:
  159. list_of_costs.append((r_in, r_out, i+1))
  160. else:
  161. break
  162.  
  163. #print(list_of_costs)
  164.  
  165. if item == "road":
  166. for x in range(p.board.height):
  167. for y in range(p.board.width):
  168. if x - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  169. p.board.get_vertex_number(x - 1, y), p.player_id):
  170. list_of_positions.append(((x, y), (x - 1, y)))
  171. if x + 1 < p.board.height and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  172. p.board.get_vertex_number(x + 1, y),
  173. p.player_id):
  174. list_of_positions.append(((x, y), (x + 1, y)))
  175. if y - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  176. p.board.get_vertex_number(x, y - 1), p.player_id):
  177. list_of_positions.append(((x, y), (x, y - 1)))
  178. if y + 1 < p.board.width and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
  179. p.board.get_vertex_number(x, y + 1),
  180. p.player_id):
  181. list_of_positions.append(((x, y), (x, y + 1)))
  182.  
  183. elif item == "city" or item == "settlement":
  184. for x in range(p.board.height):
  185. for y in range(p.board.width):
  186. if p.board.if_can_build(item, x, y, p.player_id):
  187. list_of_positions.append((x, y))
  188.  
  189. if item == "card" and len(list_of_costs) > 0:
  190. result["card"] = (list_of_costs, list_of_positions)
  191. if item != "card" and len(list_of_costs) > 0 and len(list_of_positions) > 0:
  192. result[item] = (list_of_costs, list_of_positions)
  193.  
  194. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement