def save(self, filename, myPath='./'): """ saves the file to a name and location which user chooses :param filename: name the user wants the world to be called :param myPath: location of file it will save to :return: string telling user it was saved """ allFiles = os.listdir(myPath) if filename == None: filename = toolbox.get_string("What would you like to call the file? ") if filename[-5:] != '.life': filename += '.life' if not os.path.isdir(myPath): os.mkdir(myPath) if filename[0:len(myPath)] != myPath: filename = myPath + filename if filename in allFiles: answer = toolbox.yes_or_no("Are you sure you want to save?") if answer: self.__world.save(filename) print(f'Saved {filename}') else: print(f"{filename} not saved") else: self.__world.save(filename) print(f'Saved {filename}')
def play(self): """Main Event Loop. Calls former simulation methods for user to play Life.""" mainMenu = """[h]elp [n]ext [p]revious [e]dit world [o]pen [s]ave [q]uit""" self.display_status_bar() print(mainMenu) command = '' try: command = self.get_command('Enter command: ') except: # #if command == '', next code # if self.world.steady_state() == True: print(""" ======================================================================================================================== World is currently in a steady state. ======================================================================================================================== """) self.enterCommand = "p" self.enter(self.enterCommand) try: # #selects action based on command from Main Menu #placed in try/except due to an error with the 'q' method. # if command[0][0] == 'h': print("""=======================================""") myPath = './' filename = os.path.join(myPath, 'Help.txt') myFile = open(filename, 'r') text = myFile.read() myFile.close() print(text) print("""=======================================""") print(self.world) self.play() elif command[0][0] == 'n': if self.world.steady_state() == True: # #prohibits player from progressing forwards with Progress Forwards Menu. # print(""" ======================================================================================================================== World is in a steady state. ======================================================================================================================== """) self.play() else: # #if not prohibited... # self.enterCommand = 'n' command = self.get_command_forward('Enter command: ') if len(command) == 0: command = 'n' if command[0][0] == 'n': self.multiple_generations(1) self.play() elif command[0][0] == 'm': if len(command) == 1: generations = get_integer( input("How many generations? ")) else: generations = command[1] self.multiple_generations(generations) self.play() elif command[0][0] == 's': if len(command) == 1: generations = get_integer( input( "How many generations do you want to skip? " )) else: generations = command[1] self.skip_x_generations(generations) self.play() elif command[0][0] == 'p': # #Determines action based on Progress Backwards Menu # self.enterCommand = 'p' command = self.get_command_back('Enter command: ') if command[0][0] == 'p': self.back_x_generations(1) self.play() elif command[0][0] == 'b': if len(command) == 1: generations = get_integer( input("How many generations? ")) else: generations = command[1] self.back_x_generations(generations) self.play() elif command[0][0] == 's': if len(command) == 1: generations = get_integer( input( "How many generations do you want to skip? ")) else: generations = command[1] self.skip_back(generations) self.play() elif command[0][0] == 'r': self.reset_timeline() self.enterCommand = 'n' self.play() elif command[0][0] == 'e': # #Determines action based on Edit World Menu # command = self.get_command_edit_world('Enter command: ') if command[0][0] == 's': if len(command) == 1: newSpeed = get_positive_number(input("New speed: ")) else: newSpeed = command[1:] self.change_speed(newSpeed) self.play() elif command[0][0] == 'p': if len(command) == 1: newRate = get_integer(input("New rate: ")) else: newRate = command[1] self.change_population_rate(newRate) self.play() elif command[0][0] == 'w': self.change_world_size() self.play() elif command[0][0] == 't': self.toggle_geometry() self.play() elif command[0][0] == 'r': toLetLive = [] anotherNum = True while anotherNum != False and len(toLetLive) <= 8: num = get_integer( "Enter a number of neighbors that cells can live with: " ) if num > 8 or num < 0: anotherNum = True else: toLetLive.append(num) anotherNum = yes_or_no("Enter another number? ") toMakeAlive = -1 while toMakeAlive < 0 or toMakeAlive > 8: toMakeAlive = get_integer( "With what number of neighbors should cells become alive? " ) self.change_rules(toLetLive, toMakeAlive) self.play() elif command[0][0] == 'o': self.open() self.play() elif command[0][0] == 's': self.save() self.play() elif command[0][0] == 'q': print( """========================================================================================================================""" ) print('End of Life Simulation. Thanks for playing!') print( """========================================================================================================================""" ) pass except: pass