Skip to content

Instantly share code, notes, and snippets.

@Nircek
Last active September 24, 2018 15:12
Show Gist options
  • Select an option

  • Save Nircek/7e1ee37e0bbc30f7ab554c633209a8d4 to your computer and use it in GitHub Desktop.

Select an option

Save Nircek/7e1ee37e0bbc30f7ab554c633209a8d4 to your computer and use it in GitHub Desktop.
A script for generating mazes
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# from https://gist.github.com/Nircek/7e1ee37e0bbc30f7ab554c633209a8d4/
from random import random
from os import system, name
# ----| code from https://github.com/Nircek/minesweeper-solver/blob/master/getch.py
# inspired by http://code.activestate.com/recipes/134892/
try:
import msvcrt
getch = msvcrt.getch
except ImportError:
import sys, tty, termios
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
# /----|
# ----/ from https://github.com/Nircek/minesweeper-solver/blob/886a7ffde222c6960e61822f2059d59c88e06631/solver.py#L32
def clear():
# src: https://www.geeksforgeeks.org/clear-screen-python/
if name == 'nt':
system('cls')
else:
system('clear')
# ----/
class Map:
def __init__(self,W,H):
self.W=W
self.H=H
self.X = -1
self.Y = -1
self.m = []
for i in range(self.H):
self.m += [[]]
for j in range(self.W):
self.m[i] += ['█']
def view(self):
for i in range(self.W+2):
print('█',end='')
print()
for i in range(self.H):
print('█',end='')
for j in range(self.W):
print(self.m[i][j],end='')
print('█')
for i in range(self.W+2):
print('█',end='')
print()
def get(self,x,y):
if x in range(self.W) and y in range(self.H):
return self.m[x][y]
else:
return '█'
def set(self,x,y,z):
if x in range(self.W) and y in range(self.H):
self.m[x][y] = z
def randp(self):
x = int(random()*self.H*self.W)
y = x // self.W
x %= self.W
return x, y
def gen(self):
self.X, self.Y = m.randp()
self.set(self.X,self.Y,' ')
it = 0
while 1:
x, y = self.randp()
n = 0
ni = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
if self.get(x+dx,y+dy) == ' ':
n += 1
if dx == 0 or dy == 0:
ni += 1
if ni == 1 and n < 3:
if self.get(x,y) != ' ':
it = 0
self.set(x,y,' ')
else:
it += 1
if it == self.W*self.H*3:
break
s = int(input())
while 1:
m = Map(s, s)
m.gen()
x, y = m.randp()
m.set(x,y,'O')
win = False
while not win:
clear()
m.set(m.X,m.Y,'@')
m.view()
m.set(m.X,m.Y,' ')
g = str(getch())
if g == 'w' or g == 'W':
dy = 0
dx = -1
elif g == 'a' or g == 'A':
dy = -1
dx = 0
elif g == 's' or g == 'S':
dy = 0
dx = 1
elif g == 'd' or g == 'D':
dy = 1
dx = 0
elif g == 'q' or g == 'Q':
exit()
else:
continue
if m.get(m.X+dx,m.Y+dy) == '█':
continue
elif m.get(m.X+dx,m.Y+dy) == 'O':
print('You win!')
win = True
input()
m.X += dx
m.Y += dy
@Nircek
Copy link
Copy Markdown
Author

Nircek commented Aug 5, 2018

obraz
Example of generated maze

@Nircek
Copy link
Copy Markdown
Author

Nircek commented Aug 5, 2018

That "@" is your player. You can move him by W, A, S and D keys. You must achieve 'O' by him.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment