Last active
September 17, 2018 11:32
-
-
Save Nircek/a45d5df49bf03c21d6febebc3d465a01 to your computer and use it in GitHub Desktop.
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/python3 | |
| from random import random | |
| from sys import argv | |
| 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 | |
| if len(argv) > 1: | |
| i = 0 | |
| for s in argv[1:]: | |
| s = int(s) | |
| m = Map(s, s) | |
| m.gen() | |
| if i != 0: | |
| print() | |
| else: | |
| i += 1 | |
| m.view() | |
| else: | |
| while 1: | |
| s = int(input()) | |
| m = Map(s, s) | |
| m.gen() | |
| m.view() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment