Last active
December 10, 2015 11:57
-
-
Save salty-horse/c3783d90c5731605c812 to your computer and use it in GitHub Desktop.
Generate a CSV of patterns for a card game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import random | |
from itertools import izip | |
def get_neighbors(coords): | |
neighbors = set() | |
for (x, y) in coords: | |
neighbors.update( | |
(X,Y) for (X,Y) in [ | |
(x-1, y), | |
(x+1, y), | |
(x, y-1), | |
(x, y+1), | |
] | |
if 0<=X<=3 and 0<=Y<=3 and (X,Y) not in coords) | |
return list(neighbors) | |
def pick_hexomino(): | |
"""Picks 6 contiguous coordinates in a 4x4 grid""" | |
coords = {(random.randrange(4), random.randrange(4))} | |
while len(coords) < 6: | |
neighbors = get_neighbors(coords) | |
new_coord = random.choice(neighbors) | |
coords.add(new_coord) | |
return sorted(4*x + y for (x,y) in coords) | |
def pick_outline(): | |
"""Picks 6 random coordinates in a 4x4 grid""" | |
return sorted(random.sample(xrange(16), 6)) | |
PATTERN_CODE = { | |
(0, 0): 'W', | |
(1, 0): 'B', | |
(0, 1): 'R', | |
(1, 1): 'BR', | |
} | |
def create_card(): | |
hexomino_full = [0]*16 | |
for dot in pick_hexomino(): | |
hexomino_full[dot] = 1 | |
outline_full = [0]*16 | |
for dot in pick_outline(): | |
outline_full[dot] = 1 | |
return tuple(PATTERN_CODE[t] for t in izip(hexomino_full, outline_full)) | |
def main(): | |
# Pick output file name | |
base_filename = 'puzzle_backs' | |
output_filename = base_filename | |
n = 1 | |
while os.path.exists(output_filename + '.csv'): | |
output_filename = base_filename + '_' + str(n) | |
n += 1 | |
generated_puzzles = set() | |
# Write to output filename | |
with open(output_filename + '.csv', 'w') as f: | |
header = ','.join('@'+l+str(n) for l in ['A','B','C','D'] for n in [1,2,3,4]) | |
f.write(header) | |
f.write('\n') | |
while len(generated_puzzles) < 50: | |
new_puzzle = create_card() | |
if new_puzzle in generated_puzzles: | |
continue | |
generated_puzzles.add(new_puzzle) | |
card_row = ','.join(new_puzzle) | |
f.write(card_row) | |
f.write('\n') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment