diff options
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | oo.py | 48 |
2 files changed, 39 insertions, 11 deletions
@@ -6,8 +6,6 @@ * Make a way to save and load games. -* Make extra hard puzzle generation fast. - * After porting ooStudy into Go, collect data through python. Particularly: @@ -567,16 +567,9 @@ class ooPlay: else: X = self.X Y = self.Y - 2 + self.puzzle = ooPuzzle(X, Y, toroidal = self.toroidal) - #TODO: this implementation of extra_hard can be really slow - if self.extra_hard: - while True: - for piece in self.puzzle.pieces.values(): - if piece in [0, 5]: - break - else: - break - self.puzzle.set_pieces_from_game_id(random_game_id=True) + if self.extra_hard: self.make_extra_hard() self.puzzle.random_orients() # reset some flags @@ -594,6 +587,43 @@ class ooPlay: self.max_xpos = self.puzzle.X * (1 + self.toroidal) - 1 self.max_ypos = self.puzzle.Y * (1 + self.toroidal) - 1 + def make_extra_hard(self): + """Returns True unless it successfully makes self.puzzle extra hard. + + TODO: This implementation of extra_hard is not uniformly random. + TODO: This implementation is for toroidal mode only. + """ + + try_again = False + X = self.puzzle.X + Y = self.puzzle.Y + + for x in range(X): + for y in range(Y): + + piece = self.puzzle.get_piece(x, y) + if piece not in [0, 5]: continue + + bit = random.randrange(0, 2) + + if bit: + x += 1 + self.puzzle.horiz_edges[x, y] = (piece == 0) + if x == X: + self.puzzle.horiz_edges[0, y] = (piece == 0) + try_again = True + + else: + y += 1 + self.puzzle.vert_edges[x, y] = (piece == 0) + if y == Y: + self.puzzle.vert_edges[x, 0] = (piece == 0) + try_again = True + + self.puzzle.set_data_from_edges() + + if try_again: self.make_extra_hard() + PIECE_ORIENT_TO_STRING = \ [" ", "╸╵╺╷", |