Skip to content

Commit

Permalink
Updated map and viz, began work on dice mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcarthur committed Aug 18, 2020
1 parent 85b10eb commit 52ad143
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_STORE
*.ipynb*
*__pycache__
17 changes: 17 additions & 0 deletions gym_risk/attack.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 23 additions & 9 deletions gym_risk/map.py
Original file line number Diff line number Diff line change
@@ -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
Empty file added gym_risk/maps/__init__.py
Empty file.

0 comments on commit 52ad143

Please sign in to comment.