Skip to content

Commit

Permalink
Merge pull request #1 from kedean/master
Browse files Browse the repository at this point in the history
Configurability
  • Loading branch information
livibetter authored Mar 9, 2018
2 parents 86fe589 + a3707f8 commit b1c225f
Showing 1 changed file with 76 additions and 51 deletions.
127 changes: 76 additions & 51 deletions maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

from curses import *
from locale import setlocale, LC_ALL
from random import choice, randint
from random import choice, randint, shuffle
from math import ceil
from sys import argv, exit
from time import sleep
from argparse import ArgumentParser

setlocale(LC_ALL, '')

# Symbols
ARCHIVE = [
ARCHIVE = {
# ASCII lines
{
'ASCII' : {
'0000': ' ',
'0001': '-',
'0010': '|',
Expand All @@ -32,7 +34,7 @@
},

# Thick lines
{
'THICK' : {
'0000': ' ',
'0001': '╸',
'0010': '╻',
Expand All @@ -52,7 +54,7 @@
},

# Thin lines
{
'THIN' : {
'0000': ' ',
'0001': '╴',
'0010': '╷',
Expand All @@ -72,7 +74,7 @@
},

# Double lines
{
'DOUBLE' : {
'0000': ' ',
'0001': '═',
'0010': '║',
Expand All @@ -90,15 +92,35 @@
'1110': '╠',
'1111': '╬',
},
]
SYMBOLS = [ARCHIVE[3]]
}

COLOR_SETS = ['basic', 'rainbow', 'drab'] #possible kinds of color sets, user can also use 'random' to pick a different one each iteration

# Create a randomized color palette based on the given color set with at least 'colors_needed' values
def get_color_palette(color_set, colors_needed):
if color_set == 'random' or color_set not in COLOR_SETS:
color_set = choice(COLOR_SETS)

base_palette = None

if color_set == 'rainbow':
base_palette = [1, 2, 3, 4, 5, 9, 10, 11, 12, 13]
elif color_set == 'drab':
base_palette = [0, 7, 8, 15]
elif color_set == 'basic':
base_palette = [1, 2, 4]

full_palette = base_palette * ceil(colors_needed / len(base_palette))
shuffle(full_palette)

return full_palette

# Init
def init(screen):
def init(screen, all_symbols, color_set, min_pipes, max_pipes):
rows, cols = screen.getmaxyx()

matrix = {
'symbols':all_symbols,
'grid': [[None for _ in range(rows)] for _ in range(cols)],
'cols': cols,
'rows': rows
Expand All @@ -111,34 +133,19 @@ def init(screen):

screen.clear()

def redraw(matrix, seq):
def redraw(all_symbols, matrix, seq):
for point in seq:
render(screen, matrix, point)
render(screen, all_symbols, matrix, point)

mazes = []
# for color in [1,2,4]:
type = randint(0, 2)
if type == 0:
for _ in range(25):
maze = Maze(matrix,
color=choice([1, 2, 3, 4, 5, 9, 10, 11, 12, 13]))
maze.redraw(redraw)
mazes.append(maze)

elif type == 1:
for _ in range(25):
maze = Maze(matrix, color=choice([0, 7, 8, 15]))
maze.redraw(redraw)
mazes.append(maze)

elif type == 2:
maze = Maze(matrix, color=1)
maze.redraw(redraw)
mazes.append(maze)
maze = Maze(matrix, color=2)
maze.redraw(redraw)
mazes.append(maze)
maze = Maze(matrix, color=4)

seed_count = randint(min_pipes, max_pipes)

palette = get_color_palette(color_set, seed_count)

for i in range(seed_count):
maze = Maze(matrix,
color=palette[i])
maze.redraw(redraw)
mazes.append(maze)

Expand Down Expand Up @@ -167,7 +174,7 @@ def get_color(n):


# Render matrix
def render(screen, matrix, loc):
def render(screen, all_symbols, matrix, loc):

def in_bounds(matrix, col, row):
cols = len(matrix)
Expand All @@ -193,13 +200,13 @@ def in_bounds(matrix, col, row):
try:
if matrix[ncol][nrow] is None:
screen.addstr(nrow, ncol,
SYMBOLS[matrix[ncol][nrow][2]]['0000'])
all_symbols[matrix[ncol][nrow][2]]['0000'])
else:
key = ''
for val in matrix[ncol][nrow][0]:
key += str(val)
screen.addstr(nrow, ncol,
SYMBOLS[matrix[ncol][nrow][2]][key],
all_symbols[matrix[ncol][nrow][2]][key],
get_color(matrix[ncol][nrow][1]))
except:
"""
Expand All @@ -217,13 +224,14 @@ def __init__(self, matrix, color=None, symbol=None):
self._matrix = matrix['grid']
self._cols = matrix['cols']
self._rows = matrix['rows']
self._all_symbols = matrix['symbols']
col = randint(0, self._cols-1)
row = randint(0, self._rows-1)
self._color = 0 if color is None else color
if symbol is None:
self._symbol = randint(0, len(SYMBOLS)-1)
self._symbol = randint(0, len(self._all_symbols)-1)
else:
self._symbol = symbol % len(SYMBOLS)
self._symbol = symbol % len(self._all_symbols)
if self._matrix[col][row] is None:
self._matrix[col][row] = [[0, 0, 0, 0], self._color, self._symbol]
self._stack = [(col, row)]
Expand Down Expand Up @@ -296,14 +304,14 @@ def _step(self):
self._color, self._symbol]
self._matrix[next['col']][next['row']][0][(next['dir']+2) % 4] = 1
self._stack.append((next['col'], next['row']))
self._redraw(self._matrix, [self._stack[-1], self._stack[-2]])
self._redraw(self._all_symbols, self._matrix, [self._stack[-1], self._stack[-2]])
return True
# fill(matrix, next['col'], next['row'])
# break


# Run
def run():
def run(max_framerate, all_symbols, color_set, min_pipes, max_pipes):
initscr()
start_color()
use_default_colors()
Expand All @@ -316,7 +324,9 @@ def run():

init_colors()

matrix, mazes = init(screen)
frame_sleep_time = 1.0 / max_framerate

matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes)

(running, paused, quitting) = range(0, 3)
state = running
Expand All @@ -327,9 +337,9 @@ def run():
mazes = set([maze for maze in mazes if maze.step()])

if len(mazes) < 1:
matrix, mazes = init(screen)
matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes)

sleep(0.01)
sleep(frame_sleep_time)

event = screen.getch()

Expand All @@ -340,24 +350,39 @@ def run():
elif event == ord('p'):
state = running if state == paused else paused
elif event == ord(' '):
matrix, mazes = init(screen)
matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes)
state = running
elif event == KEY_RESIZE:
matrix, mazes = init(screen)
matrix, mazes = init(screen, all_symbols, color_set, min_pipes, max_pipes)
state = running

screen.clear()
screen.keypad(0)
screen.nodelay(0)
echo()
endwin()

return 0


# Main
def main(argv=None):
if argv is None:
argv = ""
argc = len(argv)
run()
def main():
argparser = ArgumentParser(description='Simple curses pipes')
argparser.add_argument('--symbol_set', nargs='*', choices=list(ARCHIVE.keys()), default=['ASCII'], help='One or more allowable symbol sets to use, defaults to ASCII symbols')
argparser.add_argument('--max_framerate', type=int, choices=[1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], default=100, help='Maximum allowable drawing rate')
argparser.add_argument('--color_set', type=str, choices=COLOR_SETS + ['random'], default='random', help='Change the color set pipes are drawn with, defaults to random')
argparser.add_argument('--min_pipes', type=int, default=5, help="Minimum number of pipes to draw each round, defaults to 5")
argparser.add_argument('--max_pipes', type=int, default=25, help="Maximum number of pipes to draw each round, defaults to 25")

args = argparser.parse_args()

run(
max_framerate=args.max_framerate,
all_symbols=[ARCHIVE.get(syms) for syms in args.symbol_set],
color_set=args.color_set,
min_pipes=args.min_pipes,
max_pipes=args.max_pipes
)
return 0


Expand Down

0 comments on commit b1c225f

Please sign in to comment.