From 1999a58cee723c5f2d8e10ead22676a3e6d2300a Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Wed, 24 Feb 2016 10:12:08 -0800 Subject: make extra hard mode fast, but not uniformly random --- TODO | 2 -- oo.py | 48 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 24b5fa6..e696b98 100644 --- a/TODO +++ b/TODO @@ -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: diff --git a/oo.py b/oo.py index ae50189..e1f2e4a 100644 --- a/oo.py +++ b/oo.py @@ -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 = \ [" ", "╸╵╺╷", -- cgit v1.2.3-54-g00ecf