diff --git a/.gitignore b/.gitignore index fd5106f..11d3e4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .DS_STORE +*.ipynb* +*__pycache__ diff --git a/gym_risk/attack.py b/gym_risk/attack.py new file mode 100644 index 0000000..5ac6c68 --- /dev/null +++ b/gym_risk/attack.py @@ -0,0 +1,17 @@ +import random +import numpy as np + +rng = np.random.default_rng() +sided_die = 6 +attack_max = 3 +defend_max = 2 +attack_until_threshold = 2 + +def single_roll(attack, defend): + attack_roll = np.sort(rng.integers(1,sided_die+1,min(attack_max,attack)))[::-1] + defend_roll = np.sort(rng.integers(1,sided_die+1,min(defend_max,defend)))[::-1] + max_loss = min(len(attack_roll), len(defend_roll)) + attack_wins = np.sum([i>j for i,j in zip(attack_roll,defend_roll)]) + new_attack = attack - max_loss - attack_wins + new_defend = defend - attack_wins + return new_attack, new_defend \ No newline at end of file diff --git a/gym_risk/map.py b/gym_risk/map.py index bf1b165..96dc78e 100644 --- a/gym_risk/map.py +++ b/gym_risk/map.py @@ -1,24 +1,38 @@ import random +import json +from pathlib import Path import networkx as nx -from maps.debug import debug - class Map(): metadata = {} - def __init__(self): - self.graph = None - pass + def __init__(self, board='us_cont'): + # read in graph + graph_path = Path(__file__).parent / f"assets/{board}_graph.json" + with graph_path.open() as json_file: + graph_data = json.load(json_file) - def initialize_player_assignment(self): - """Randomly assigns each player a country until there are none left""" + #Load in map + self.graph = nx.node_link_graph(graph_data) + + # randomize and assign player map + self.graph.nodes = self.initialize_player_assignment() + pass - pass + def initialize_player_assignment(self,players=['red','blue']): + """Randomly assigns each player a country until there are none left""" + graph_nodes = self.graph.nodes + random.shuffle(graph_nodes) + player_list = ['red', 'blue'] + for j,i in enumerate(graph_nodes): + graph_nodes[i]['player'] = player_list[j%len(player_list)] + graph_nodes[i]['units'] = 3 + return graph_nodes def change_ownership(self, payload): """Change ownership of a country Params: payload (dict) {node:player} """ - nx.set_node_attributes(self.graph, payload, "owner") + nx.set_node_attributes(self.graph, payload, "player") pass diff --git a/gym_risk/maps/__init__.py b/gym_risk/maps/__init__.py new file mode 100644 index 0000000..e69de29