From b4afcf7c3367a52ccf1d38a710d4b491c40abbf5 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Wed, 10 Feb 2016 21:19:52 -0800 Subject: basic ooPlay implemented --- oo.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/oo.py b/oo.py index 4f5bbc6..73aa622 100644 --- a/oo.py +++ b/oo.py @@ -168,11 +168,17 @@ class ooPuzzle: self.set_pieces_from_edges(horiz_edges, vert_edges) - def random_state(self): + def rotate_cw(self, x, y, turns=1): + """Rotates the piece at (x, y) by clockwise turns.""" + o = self.orients[x, y] + o = (o + turns) % 4 + self.orients[x, y] = o + + def random_orients(self): """Randomly rotate the current pieces.""" for x in range(self.X): for y in range(self.Y): - self.pieces[x, y] = random.randint(4) + self.pieces[x, y] = random.randrange(4) def is_solved(self): """Checks whether the puzzle is in a solved state.""" @@ -235,23 +241,27 @@ class ooPlay: Renders and interacts with an ooPuzzle instance. """ - def __init__(self, scr): + def __init__(self, screen): """Create a new ooPlay instance. Arguments. scr : curses screen object used for display """ - self.scr = scr - self.Y, self.X = self.scr.getmaxyx() + self.screen = screen + self.Y, self.X = self.screen.getmaxyx() self.puzzle = ooPuzzle(self.X, self.Y-2) - self.puzzle.random_state() - self.scr.clear() + self.puzzle.random_orients() + self.screen.clear() # draw the help area and board state border_line = self.X * "═" - self.scr.addstr(self.Y - 2, 0, border_line) + self.screen.addstr(self.Y - 2, 0, border_line) self.display() + # start the main loop + self.xpos, self.ypos = 0, 0 + self.keyloop() + PIECE_ORIENT_TO_STRING = \ [" ", "╸╵╺╷", @@ -262,11 +272,48 @@ class ooPlay: def display(self): """Display the state of the board, refresh screen.""" - pass + for x in range(self.puzzle.X): + for y in range(self.puzzle.Y): + piece = self.puzzle.pieces [x, y] + orient = self.puzzle.orients[x, y] + string = self.PIECE_ORIENT_TO_STRING[piece][orient] + self.screen.addstr(y, x, string) + self.screen.refresh() + + def success(self): + """Display the win screen.""" + curses.flash() def keyloop(self): """Wait for and parse keypress.""" - pass + while True: + self.screen.move(self.ypos, self.xpos) + inp = self.screen.getch() + + # parse character input + if 0 < inp < 256: + inp = chr(inp) + if inp in " \n": + self.puzzle.rotate_cw(self.xpos, self.ypos) + self.display() + if self.puzzle.is_solved(): + self.success() + return + elif inp in "Qq": + return + + # parse arrow key input + elif inp == curses.KEY_UP and self.ypos > 0: + self.ypos -= 1 + elif inp == curses.KEY_DOWN and self.ypos < self.puzzle.Y - 1: + self.ypos += 1 + elif inp == curses.KEY_LEFT and self.xpos > 0: + self.xpos -= 1 + elif inp == curses.KEY_RIGHT and self.xpos < self.puzzle.X - 1: + self.xpos += 1 def main(): curses.wrapper(ooPlay) + +if __name__ == "__main__": + main() -- cgit v1.2.3-70-g09d2