def create_robot(self): """ Create a new robot based on user input for name and robot type. """ clear_console() print('*** Entering robot creation room ***\n') robot_name = input("What is you robot's name?\n") # Some name validation, we want unique names so other robots are not lost/overwritten by new bots with the same name while not robot_name or robot_name in self.robots: clear_console() print("*** Sorry that name is not valid or is already taken, please try another. ***\n") robot_name = input("What is you robot's name?\n") clear_console() _, robot_class = get_user_choice(ROBOT_TYPE_CHOICES, 'What type of robot is {0}'.format(robot_name)) try: robot = ROBOT_TYPES[robot_class](robot_name) # <- instantiate on the fly except KeyError: print('\n*** Leaving Robot Creation room ***\n') else: # Store new robots in class-level dict self.robots[robot_name] = robot finally: press_enter_to_continue() clear_console()
def destroy_a_robot(self): """ Destroys a robot based on it's name. """ clear_console() print('*** Entering robot destruction room ***\n') # Nothing to do if no robots have been created yet if not self.robots: print("No robots to destroy, please create one first!") press_enter_to_continue() clear_console() return # Get user's robot-to-destroy choice _, robot_name = get_user_choice(self.robot_name_choices, 'Choose a robot to destroy.') try: robot = self.robots[robot_name] except KeyError: # Only occurs when 0: exit is selected or no robots exist yet print("\n*** Leaving robot destruction room ***\n") else: # Confirrm that the user actually wants to destroy the bot clear_console() if input('Type DESTROY if you are sre you want to destroy {0} (not case sensitive)\n'.format(robot)).upper() == 'DESTROY': del self.robots[robot_name] print('\n*** {0} has been destroyed! ***\n'.format(robot_name)) else: print('\n*** {0} was NOT destroyed! ***\n'.format(robot_name)) finally: press_enter_to_continue() clear_console()
def interact_with_robot(self): """ Allows for interaction with a robot.""" clear_console() print('*** Entering robot interaction room ***\n') # Nothing to do if no robots have been created yet if not self.robots: print("No robots to interact with, please create one first!") press_enter_to_continue() clear_console() return # Get user's robot-to-interact-with choice _, robot_name = get_user_choice(self.robot_name_choices, 'Choose a robot to interact with.') try: bot = self.robots[robot_name] except KeyError: # Only occurs when 0: exit is selected or no robots exist yet print('\n*** Leaving Robot Interaction room ***\n') else: bot.interact_with() finally: press_enter_to_continue() clear_console()
def run_game(): os.system('clear') board = Board() message_to_user = "" # Player 0 = WHITE # Player 1 = BLACK player = 0 while True: print( f"{bcolors.OKBLUE}Press Ctrl+C or to exit the program{bcolors.ENDC}" ) print("Columns: a -- h\t\tRows: 1 -- 8") print("{}".format(message_to_user)) board.show_board() valid_piece, old_row, old_col = get_user_choice(player, board, new_pos=False) valid_new_pos, new_row, new_col = get_user_choice(player, board, new_pos=True) if valid_piece and valid_new_pos: tile = board[old_col, old_row] legal_move = tile.check_legal_move(board, new_col, new_row) print("Checked valid pieces") if legal_move: tile.move(board, new_col, new_row) print(valid_piece) print(valid_new_pos) time.sleep(3) os.system('clear') # Change player from W to B and vice versa player = not player
def interact_with(self): """ Allows the user to interact with the robot by listing out interaction options. """ while True: # Break when users chooses to '0: Leave' clear_console() print("*** Interacting with {0} ***\n".format(self)) # Get the user's interaction choice _, action_choice = get_user_choice( self.interaction_choices, 'What would you like to do with {0}'.format(self)) try: self.interactions[action_choice]() except KeyError: pass # Could break here instead as only hits when '0: Leave' is chosen if action_choice == 'Leave': break
def get_task_to_perform(self): """ Allows the user to choose a task to perform""" clear_console() # Get the user's task choice _, task_choice = get_user_choice( self.all_task_choices, 'Which task would you like {0} to perform?'.format(self)) # Rest self.msg as it is very likely it was previously changed self.msg = '' # Actually perform the chosen task self.perform_task(task_choice) print("\n*** Task finished! ***\n") press_enter_to_continue() clear_console()
def run_session(): clear_console( ) # <- clears the console, makes following prompts and information easier # Factory used as a context manager to ensure load and save methods are run with RobotFactory() as rf: # Choices from ACTION_CHOICES above mapped to actual methods actions_map = { 'Create a new robot': rf.create_robot, 'Interact with a robot': rf.interact_with_robot, 'Destroy a robot': rf.destroy_a_robot, 'Destroy all robots': rf.destroy_all_robots, 'View robot task leaderboard': rf.view_robot_leaderboard, # Exit' is not mapped so that it passes choice validation and still exits } # Primary user interaction loop runs until a user chooses to exit while True: # Below function gets user input based on dict of choices _, choice = get_user_choice(ACTION_CHOICES, 'What would you like to do?') try: actions_map[choice]() # <- Call the method on the fly except KeyError: clear_console() pass # Only hits KeyError if '0' : Exit is chosen if choice == 'Exit': # Have user confirm exit with case insensitve typed input if input( 'Type EXIT to quit if you are sure you want to leave.\n' ).upper() == 'EXIT': clear_console() break print('\n*** You are now leaving the robot factory! ***\n') print('\n*** Your robots have been saved, goodbye! ***\n')