Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from catan import *
- def output(p, x, y):
- resources = [[0 for _ in range(3)] for _ in range(11)]
- for dx in [-1, 0]:
- for dy in [-1, 0]:
- xx = x + dx
- yy = y + dy
- if p.board.is_tile(xx, yy):
- die = p.board.dice[yy, xx]
- if die != 7:
- resource = p.board.resources[yy, xx]
- resources[die - 2][resource] += 1
- return resources
- def doAction(p, move):
- if move[0] == "buy": # Road
- p.buy(move[1], move[2], move[3])
- elif move[0] == "trade":
- p.trade(move[1], move[2])
- # Helper method to help us determine valid actions to take
- def actionSet(p):
- """
- Set of actions we can take:
- -nothing: do nothing
- -city: upgrade to a city
- -settlement: build a settlement
- -road: build a road
- -card: buy a development card
- -trade: trade for resources
- """
- actions = []
- if p.if_can_buy("settlement"):
- for x in range(p.board.height):
- for y in range(p.board.width):
- if p.board.if_can_build("settlement", x, y, p.player_id):
- actions.append(("buy", "settlement", x, y))
- if p.if_can_buy("city"):
- for x in range(p.board.height):
- for y in range(p.board.width):
- if p.board.if_can_build("city", x, y, p.player_id):
- actions.append(("buy", "city", x, y))
- if p.if_can_buy("road"):
- for x in range(p.board.height):
- for y in range(p.board.width):
- if x - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x - 1, y), p.player_id):
- actions.append(("buy", "road", (x, y), (x - 1, y)))
- if x + 1 < p.board.height and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x + 1, y),
- p.player_id):
- actions.append(("buy", "road", (x, y), (x + 1, y)))
- if y - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x, y - 1), p.player_id):
- actions.append(("buy", "road", (x, y), (x, y - 1)))
- if y + 1 < p.board.width and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x, y + 1),
- p.player_id):
- actions.append(("buy", "road", (x, y), (x, y + 1)))
- if p.if_can_buy("card"):
- actions.append(("buy", "card", -1, -1))
- for r_in in range(0, 3):
- required = 4
- ports = []
- for e in p.get_settlements():
- if p.board.is_port(e):
- ports.append(p.board.which_port(e))
- for e in p.get_cities():
- if p.board.is_port(e):
- ports.append(p.board.which_port(e))
- if r_in in ports:
- required = 2
- if 3 in ports:
- required = min(required, 3)
- if p.resources[r_in] >= required:
- for r_out in range(0, 3):
- if r_in == r_out:
- continue
- actions.append(("trade", r_in, r_out))
- return actions
- def purchaseSet(p):
- """
- returntype:
- {"settlement":([ LIST OF COSTS (r_in, r_out, frequency) ],[ LISIT OF POSITIONS ])
- "city":([ LIST OF COSTS ],[ LIST OF POSITIONS ])
- }
- """
- # For every type of thing that can be purchsed
- # else:
- # Figure out what resources we are lacking (if all 3 lacking then gg)
- # find what combinations of surplus resources we can trade to fix shortages
- # Example:
- # possible trades (4 of any to any, 2 of #3 to any)
- # cost = 1, 2, 2
- # have = 5, 1, 4 (defecit of resource 2)
- # we need to trade resource 1 --> 2 or 3 --> 2
- # 2 possible prices we could pay are (5,1,2) or (1,1,4)
- # also condsider all locations that we can buy the thing in
- ### GLOBALS
- # SETTLEMENT = 0
- # CARD = 1
- # CITY = 2
- # ROAD = 3
- # costs = np.array([[2, 1, 1],
- # [1, 2, 2],
- # [0, 3, 3],
- # [1, 1, 0]])
- result = {}
- purchase_types = ["settlement", "card", "city", "road"]
- for index in range(4):
- item = purchase_types[index]
- list_of_costs = []
- list_of_positions = []
- if p.if_can_buy(item):
- list_of_costs.append(("No trade needed"))
- else:
- ports = []
- for e in p.get_settlements():
- if p.board.is_port(e):
- ports.append(p.board.which_port(e))
- for e in p.get_cities():
- if p.board.is_port(e):
- ports.append(p.board.which_port(e))
- for r_out in range(3):
- for r_in in range(3):
- if r_in == r_out:
- continue
- required = 4
- if r_in in ports:
- required = 2
- if 3 in ports:
- required = min(required, 3)
- new_costs = [cost for cost in costs[index]]
- for i in range(costs[index][r_out]):
- new_costs[r_out] -= 1
- new_costs[r_in] += required
- if sum(new_costs) <= ROBBER_MAX_RESOURCES:
- list_of_costs.append((r_in, r_out, i+1))
- else:
- break
- #print(list_of_costs)
- if item == "road":
- for x in range(p.board.height):
- for y in range(p.board.width):
- if x - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x - 1, y), p.player_id):
- list_of_positions.append(((x, y), (x - 1, y)))
- if x + 1 < p.board.height and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x + 1, y),
- p.player_id):
- list_of_positions.append(((x, y), (x + 1, y)))
- if y - 1 >= 0 and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x, y - 1), p.player_id):
- list_of_positions.append(((x, y), (x, y - 1)))
- if y + 1 < p.board.width and p.board.if_can_build_road(p.board.get_vertex_number(x, y),
- p.board.get_vertex_number(x, y + 1),
- p.player_id):
- list_of_positions.append(((x, y), (x, y + 1)))
- elif item == "city" or item == "settlement":
- for x in range(p.board.height):
- for y in range(p.board.width):
- if p.board.if_can_build(item, x, y, p.player_id):
- list_of_positions.append((x, y))
- if item == "card" and len(list_of_costs) > 0:
- result["card"] = (list_of_costs, list_of_positions)
- if item != "card" and len(list_of_costs) > 0 and len(list_of_positions) > 0:
- result[item] = (list_of_costs, list_of_positions)
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement