Skip to content

Instantly share code, notes, and snippets.

@thquinn
Created April 13, 2020 00:00
Show Gist options
  • Save thquinn/00bc8fc0b10c99265c73feb475cce75e to your computer and use it in GitHub Desktop.
Save thquinn/00bc8fc0b10c99265c73feb475cce75e to your computer and use it in GitHub Desktop.
Drawing the patterns of N players' go stones placed in a spiral
import numpy as np, PIL
from PIL import Image
from queue import Queue
def within(board, coor):
return coor[0] >= 0 and coor[1] >= 0 and coor[0] < board.shape[0] and coor[1] < board.shape[1]
def get_neighbors(coor):
return [(coor[0] - 1, coor[1]), (coor[0] + 1, coor[1]), (coor[0], coor[1] - 1), (coor[0], coor[1] + 1)]
def next_diamond_coor(coor):
# Center
if coor == (0, 0):
return (0, 1)
# Skip to next layer
if coor[0] == -1 and coor[1] >= 0:
return (0, coor[1] + 2)
# right-down
if coor[0] >= 0 and coor[1] > 0:
return (coor[0] + 1, coor[1] - 1)
# left-down
if coor[0] > 0 and coor[1] <= 0:
return (coor[0] - 1, coor[1] - 1)
# left-up
if coor[0] <= 0 and coor[1] < 0:
return (coor[0] - 1, coor[1] + 1)
# right-up
return (coor[0] + 1, coor[1] + 1)
def next_square_coor(coor):
x = coor[0]
y = coor[1]
# up to next layer
if x == -y and x <= 0 and y >= 0:
return (x, y + 1)
# right
if y > 0 and x > -y and x < y:
return (x + 1, y)
# down
if x > 0 and y > -x and y <= x:
return (x, y - 1)
# left
if y < 0 and x > y and x <= -y:
return (x - 1, y)
# up
return (x, y + 1)
def capture_check(board, coor):
if not within(board, coor):
return set()
if board[coor] == -1:
return set()
queue = Queue()
queue.put(coor)
seen = set()
seen.add(coor)
while not queue.empty():
current = queue.get()
for neighbor in get_neighbors(current):
if not within(board, neighbor):
return set()
if board[neighbor] == -1:
return set()
if neighbor in seen:
continue
if board[neighbor] == board[current]:
queue.put(neighbor)
seen.add(neighbor)
return seen
# definitions
player_count = 7
bound = 200
i = 0
coor = (0, 0)
board = np.full((bound * 2 + 1, bound * 2 + 1), -1)
while abs(coor[0]) <= bound and abs(coor[1]) <= bound:
arr_coor = (bound + coor[0], bound + coor[1])
board[arr_coor] = i % player_count
captures = set()
neighbors = get_neighbors(arr_coor)
for neighbor in neighbors:
captures = captures.union(capture_check(board, neighbor))
for capture in captures:
board[capture] = -1
i += 1
coor = next_diamond_coor(coor)
im = Image.new('RGB', board.shape)
for index, x in np.ndenumerate(board):
if x != -1:
im.putpixel(index, (255, 255, 255))
im.save('output.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment