def process_user_command(game_tree, target_tree, curr, pic): '''Read and process one command from the user, modifying BTNode game_tree and current BTNode curr as appropriate and redrawing the new game_tree and BTNode target_tree on Picture pic. Return the new value of curr.''' d = {'Left': 'l', 'Right': 'r', 'Up': 'u', 'a': 'L', 's': 'R', 'q': 'q'} cmd = d.get(KD.moving_by_keys().key, 'm') # Only listen to valid commands. if len(cmd) != 1 or cmd[0] not in 'qulrLR': return curr # Erase the old tree and redraw target_tree halfway across the window. media.add_rect_filled(pic, 0, 0, WIDTH, HEIGHT, media.white) draw(pic, target_tree.root, 0, WIDTH / 2, curr) # Process user commands. if cmd == 'q': media.close(pic) sys.exit(0) elif cmd == 'u' and curr != None and curr.parent != None: curr = curr.parent elif cmd == 'l' and curr.left != None: curr = curr.left elif cmd == 'r' and curr.right != None: curr = curr.right elif cmd == 'L' and curr.right != None: curr = bst.rotate_left(game_tree, curr) elif cmd == 'R' and curr.left != None: curr = bst.rotate_right(game_tree, curr) # The parent attribute of the nodes of the new tree must be corrected. # If curr is at the top, a rotation may have moved it there. Set the # game_tree root to curr if that happened. if curr.parent == None: game_tree.root = curr # Draw the new game tree. draw(pic, game_tree.root, 0, 0, curr) media.update(pic) return curr
def process_user_command(game_tree, target_tree, curr, pic): '''Read and process one command from the user, modifying BTNode game_tree and current BTNode curr as appropriate and redrawing the new game_tree and BTNode target_tree on Picture pic. Return the new value of curr.''' cmd = raw_input(prompt) # Only listen to valid commands. while cmd not in 'shqulrLR': cmd = raw_input(prompt) # Erase the old tree and redraw target_tree halfway across the window. media.add_rect_filled(pic, 0, 0, WIDTH, HEIGHT, media.white) draw(pic, target_tree.root, 0, WIDTH / 2, curr) # Process user commands. if cmd == 'q': media.close(pic) return None elif cmd == 'h': solver.help_solve(game_tree, target_tree) elif cmd== 's': while game_tree != target_tree: solver.help_solve(game_tree, target_tree) elif cmd == 'u' and curr != None and curr.parent != None: curr = curr.parent elif cmd == 'l' and curr.left != None: curr = curr.left elif cmd == 'r' and curr.right != None: curr = curr.right elif cmd == 'L' and curr.right != None: curr = bst.rotate_left(game_tree, curr) elif cmd == 'R' and curr.left != None: curr = bst.rotate_right(game_tree, curr) # Draw the new game tree. draw(pic, game_tree.root, 0, 0, curr) media.update(pic) return curr
def updatePicture(pic): media.update(pic)