sloast commited on
Commit
5fa9e4b
1 Parent(s): adbf004

equal distribution of sources and sinks

Browse files
Files changed (1) hide show
  1. gen.py +8 -7
gen.py CHANGED
@@ -40,8 +40,8 @@ def gettile():
40
  while True:
41
  tile = (WIRE, [i for i in range(4) if random.random() < 0.4])
42
  if len(tile[1]) == 1:
43
- if random.random() < 0.2:
44
- return (SOURCE, tile[1])
45
  elif len(tile[1]) == 0:
46
  if random.random() < 0.2:
47
  return tile
@@ -65,7 +65,8 @@ def joined(puzzle,x1,y1,x2,y2):
65
  _,ds = puzzle[y1][x1]
66
  _,ds2 = puzzle[y2][x2]
67
  diff = (x2-x1,y2-y1)
68
- if diff not in [(0,1),(1,0),(0,-1),(-1,0)]: return False
 
69
  d = directions.index(diff)
70
  return (d+2)%4 in ds2 and d in ds
71
 
@@ -111,13 +112,13 @@ def genpuzzle(w,h=None,seed=0):
111
  while not consistent(puzzle,x,y):
112
  puzzle[y][x] = gettile()
113
 
114
- numsinks = (w*h//10) + 1
115
- # add linked sink to each source
116
  for y in range(h):
117
  for x in range(w):
118
- if puzzle[y][x][0] == SOURCE:
119
  x2,y2 = reachable(puzzle,x,y)
120
- puzzle[y2][x2] = (SINK,puzzle[y2][x2][1])
121
 
122
  for _ in range(numsinks):
123
  x = random.randint(0,w-1)
 
40
  while True:
41
  tile = (WIRE, [i for i in range(4) if random.random() < 0.4])
42
  if len(tile[1]) == 1:
43
+ if random.random() < 0.1:
44
+ return (random.choice([SINK,SOURCE]), tile[1])
45
  elif len(tile[1]) == 0:
46
  if random.random() < 0.2:
47
  return tile
 
65
  _,ds = puzzle[y1][x1]
66
  _,ds2 = puzzle[y2][x2]
67
  diff = (x2-x1,y2-y1)
68
+ if diff not in [(0,1),(1,0),(0,-1),(-1,0)]:
69
+ return False
70
  d = directions.index(diff)
71
  return (d+2)%4 in ds2 and d in ds
72
 
 
112
  while not consistent(puzzle,x,y):
113
  puzzle[y][x] = gettile()
114
 
115
+ numsinks = (w+h)//5 + 1
116
+ # add linked source to each sink and vice versa
117
  for y in range(h):
118
  for x in range(w):
119
+ if puzzle[y][x][0] in [SOURCE,SINK]:
120
  x2,y2 = reachable(puzzle,x,y)
121
+ puzzle[y2][x2] = (3-puzzle[y][x][0],puzzle[y2][x2][1])
122
 
123
  for _ in range(numsinks):
124
  x = random.randint(0,w-1)