Commit 27fe4cac authored by Ralf's avatar Ralf
Browse files

Merge branch 'master' of https://git.hacksaar.de/hacksaar/haxotel

parents d5ae9894 76cccc78
#!/usr/bin/python3
import sys
import random
import itertools
NEIGHBORS = [(-1,0),(1,0),(0,-1),(0,1)]
def main ():
if len(sys.argv) != 4:
print ("Usage: %s width height walls" % sys.argv[0])
return
width = int(sys.argv[1])
height = int(sys.argv[2])
walls = int(sys.argv[3])
if walls >= width * height:
raise Exception("Too many walls!")
positions = random.sample(range(width*height), walls)
field = [[0]*height for i in range(width)]
actualwalls = 0
for i in positions:
y = i%height
x = i//height
field[x][y] = "W"
if testConnected(field):
actualwalls += 1
else:
field[x][y] = 0
print ("Actually placed walls:", actualwalls, file=sys.stderr)
printfield(field)
def printfield (field):
width = len(field)
height = len(field[0])
for y in range(height):
print(("".join(str(field[x][y]) for x in range(width))).replace('0',' '))
def neighbors(x,y):
return [(x+i,y+j) for i,j in NEIGHBORS]
def find (field,walter):
for x,column in enumerate(field):
for y,f in enumerate(column):
if f == walter:
return (x,y)
raise Exception("Not found: ",walter)
def paintConnected (field, x, y, checkfor, paint):
if x < 0 or y < 0 or x >= len(field) or y >= len(field[x]) or field[x][y] != checkfor:
return
pos = [(x,y)]
while len(pos) > 0:
x,y = pos.pop()
if field[x][y] == paint:
continue
assert field[x][y] == checkfor
field[x][y] = paint
for nx,ny in neighbors(x,y):
if nx < 0 or ny < 0 or nx >= len(field) or ny >= len(field[nx]) or field[nx][ny] != checkfor:
continue
pos.append((nx,ny))
def testConnected (field):
startx, starty = find (field,0)
paintConnected(field,startx,starty,0,1)
try:
find(field,0)
except Exception as e:
if e.args == ("Not found: ",0):
return True
else:
raise e
finally:
paintConnected(field,startx,starty,1,0)
return False
if __name__ == "__main__":
main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment