- i, j = torch.randint(self.height, (1,)), torch.randint(self.width, (1,))
- if (
- i == 0
- or i == self.height - 1
- or j == 0
- or j == self.width - 1
- or X[i, j] == 1
- ):
- break
- while True:
- di, dj = torch.randint(3, (2,)) - 1
- if abs(di) + abs(dj) > 0:
- break
- X[i, j] = 1
- while True:
- i, j = i + di, j + dj
- if i < 0 or i >= self.height or j < 0 or j >= self.width:
- break
- b = (
- i == 0
- or i == self.height - 1
- or j == 0
- or j == self.width - 1
- or X[i, j] == 1
- )
- X[i, j] = 1
- if b:
+ nb_fixes = 0
+ for i in range(1, self.height - 1):
+ for j in range(1, self.width - 1):
+ if (
+ X[i, j] == 1
+ and X[i - 1, j] + X[i + 1, j] + X[i, j - 1] + X[i, j + 1]
+ == 1
+ ):
+ X[i, j] = 0
+ nb_fixes += 1
+
+ if nb_fixes == 0: