Skip to content

Instantly share code, notes, and snippets.

@ggb
Created October 14, 2019 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggb/8c70ffeee6d4ab663f104ff33a5f5f67 to your computer and use it in GitHub Desktop.
Save ggb/8c70ffeee6d4ab663f104ff33a5f5f67 to your computer and use it in GitHub Desktop.
Script to create a game graph for 3x3 Nim
import networkx as nx
import matplotlib.pyplot as plt
import itertools
l = list(range(4))
lists = [l, l, l]
result = []
for element in itertools.product(*lists):
l = list(element)
l.sort()
result.append(tuple(l))
nodes = set(result)
edges = {}
def helper(x, y, z):
el = []
while x > 0:
x = x - 1
ele = [x, y, z]
ele.sort()
el.append(tuple(ele))
return el
for a, b, c in nodes:
edge_list = set(helper(a, b, c) + helper(b, a, c) + helper(c, a, b))
edges[(a, b, c)] = edge_list
node_sum = 0
for n, es in edges.items():
count = len(es)
print(str(n) + ": " + str(count))
node_sum += count
print(node_sum)
g = nx.DiGraph()
for n, es in edges.items():
for e in es:
g.add_edge(n, e)
nx.draw_spring(g, with_labels=True, scale=5, font_size=4)
plt.savefig("dea.png", dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment