def parse_parameter(self): """Set the parameter if any.""" # # Check what parameters this command takes # parameterType = None for menuItem in self.menuItems: if self.command == menuItem[Menu.command]: parameterType = menuItem[Menu.parameterType] parameter = None if parameterType == 'string1': parameter = self.userInput[1:].strip() elif parameterType == 'integer1': number = self.userInput[1:].strip() if is_integer(number): parameter = int(number) elif parameterType == 'integer2': numbers = self.userInput[1:].strip() for separator in 'x,-/ ': if separator in numbers: numbers = numbers.split(separator) if is_integer(numbers[0]) and is_integer(numbers[1]): parameter = [int(numbers[0]), int(numbers[1])] elif parameterType is None: pass else: raise TypeError(f'parameterType "{parameterType}" is not defined.') return parameter
def change_size(self, parameter): """ changes the size of the current grid world. :param parameter: :return: None """ if parameter: rows, columns = parameter.split('x',2) if toolbox.is_integer(rows) and toolbox.is_integer(columns): rows = int(rows) columns = int(columns) else: print("You cannot choose 1 for rows or columns.") prompt = 'How many rows of cells?' rows = toolbox.get_integer_between(2,40,prompt) prompt = 'How many cells in each row?' columns = toolbox.get_integer_between(2,120,prompt) self.__world = World(rows, columns) self.random() print(self.__world, end='') self.__generation = 0 print() print(f'Your world size is now {rows} by {columns}.') print() print(self.status() + '\n' + self.menu(), end=' ')
def change_size(self, parameter): if parameter: rows, columns = parameter.split('x', 2) if toolbox.is_integer(rows) and toolbox.is_integer(columns): rows = int(rows) columns = int(columns) else: prompt = 'How many rows of cells?' rows = toolbox.get_integer_between(1, 40, prompt) prompt = 'How many cells in each row?' columns = toolbox.get_integer_between(1, 120, prompt) self.__world = World(rows, columns) self.random()
def skip_generations(self, generations): """ Displays the next generation of the world :param generations: :return: next generation """ if toolbox.is_integer(generations) and int(generations) > 0: generations = int(generations) else: prompt = 'How many generations do you wanna skip?' generations = toolbox.get_integer_between(1, 10000, prompt) print(f'Skipping {generations} generations.', end='') for generation in range(generations): self.__world.next_generation() if self.__world.is_stable() == True: print('It is stable now.') break else: pass if generation % 100 == 0: print('.', end='') print(' done!') time.sleep(2) self.display()
def change_rules(self, whichNumbers): """ Changes the rules of life, from how many neighbors it takes for a cell to remain alive or become alive :param whichNumbers: :return: """ if whichNumbers: if toolbox.is_integer(whichNumbers) and \ 1 <= int(whichNumbers) <= len(Rules.ruleSets.keys()): whichNumbers = int(whichNumbers) else: print('**************************************') for number, set in enumerate(Rules.ruleSets): stayAlive = Rules.ruleSets[set]['stayAlive'] stayAlive1 = stayAlive[0] stayAlive2 = stayAlive[1] becomeAlive = Rules.ruleSets[set]['becomeAlive'] string = f'{number + 1}: Neighbors needed for cell to stay alive: {stayAlive1}, {stayAlive2} ' string += f'Neighbors needed for cell to become alive: {becomeAlive}' print(string) print(f'{number + 2}: pick your own characters') print('**************************************') prompt = 'What character set would you like to use?' whichNumbers = toolbox.get_integer_between(1, number + 2, prompt) if whichNumbers == number + 2: print() string = 'You can pick 2 number amounts of neighbors so that an alive cell stays alive. ' string += '\nFor example, typing "23" makes it so if a cell has 2 or 3 neighbors, it stays alive. ' string += '\nHow many neighbors do you want? Please type a 2 digit number: ' stay = toolbox.get_string(string) become = toolbox.get_string('Which number of neighbors would you like to change a cell from dead to alive? (One digit number) ') Rules.set_rules_user_values(stay, become) setString = list(Rules.ruleSets.keys())[whichNumbers - 1] Rules.set_rules(setString) self.display()
def change_size(self, parameter): if parameter and ('x' in parameter): rows, columns = parameter.split('x', 2) if toolbox.is_integer(rows) and toolbox.is_integer(columns): rows = int(rows) columns = int(columns) else: # # Can not have 1x1, 1x2, etc because it would out of range; at least is 2x2 world. # prompt = 'How many rows of cells?' rows = toolbox.get_integer_between(2, 40, prompt) prompt = 'How many cells in each row?' columns = toolbox.get_integer_between(2, 120, prompt) self.__world = self.__worldType(rows, columns) self.random()
def change_display(self, parameter): """ Change the live and dead characters for the cells. :param parameter: :return: None """ if toolbox.is_integer(parameter) and \ 1 <= int(parameter) <= len(Cell.displaySets.keys()): setNumber = int(parameter) else: print('**************************************') for number, set in enumerate(Cell.displaySets): liveChar = Cell.displaySets[set]['liveChar'] deadChar = Cell.displaySets[set]['deadChar'] print( f'{number + 1}: living cells: {liveChar} dead cells: {deadChar}' ) print('**************************************') prompt = 'What character set would you like to use?' setNumber = toolbox.get_integer_between(1, number + 1, prompt) setString = list(Cell.displaySets.keys())[setNumber - 1] Cell.set_display(setString) print(self.__world, end='') print() print( f'Your living cell is now {liveChar} and a dead cell is {deadChar}.' ) print() print(self.status() + '\n' + self.menu(), end=' ')
def change_graphics(self, whichCharacters): """ Changes the alive and dead cells to characters of the users choice :param whichCharacters: the characters that the user wants to change the alive and dead cells to. :return: """ if toolbox.is_integer(whichCharacters) and \ 1 <= int(whichCharacters) <= len(Cell.displaySets.keys()): whichCharacters = int(whichCharacters) else: print('**************************************') for number, set in enumerate(Cell.displaySets): liveChar = Cell.displaySets[set]['liveChar'] deadChar = Cell.displaySets[set]['deadChar'] print(f'{number + 1}: living cells: {liveChar} dead cells: {deadChar}') print(f'{number + 2}: pick your own characters') print('**************************************') prompt = 'What character set would you like to use?' whichCharacters = toolbox.get_integer_between(1, number + 2, prompt) if whichCharacters == number + 2: alive = toolbox.get_string('Which character should represent alive cells?') dead = toolbox.get_string('Which character should represent dead cells?') Cell.set_display_user_values(alive, dead) setString = list(Cell.displaySets.keys())[whichCharacters - 1] Cell.set_display(setString) self.display()
def change_rule(self, parameter): """ Change the number of neighbors a cell needs to live or die. :param parameter: :return: None """ if toolbox.is_integer(parameter) and \ 1 <= int(parameter) <= len(Rule.ruleSets.keys()): setNumber = int(parameter) else: print('**************************************') for number, set in enumerate(Rule.ruleSets): neighbor1 = Rule.ruleSets[set]['neighbor1'] neighbor2 = Rule.ruleSets[set]['neighbor2'] neighbor3 = Rule.ruleSets[set]['neighbor3'] print( f'{number + 1}: neighbor1: {neighbor1} neighbor2: {neighbor2} neighbor3: {neighbor3}' ) print('**************************************') prompt = 'What rule set would you like to use?' setNumber = toolbox.get_integer_between(1, number + 1, prompt) setString = list(Rule.ruleSets.keys())[setNumber - 1] Rule.set_rule(setString) print(self.__world, end='') print() print( f'Your rule set is now neighbor1:{neighbor1}, neighbor2:{neighbor2}, neighbor3:{neighbor3}.' ) print() print(self.status() + '\n' + self.menu(), end=' ')
def run_simulation(self, parameter): """ Displays the next generation of the world. :param parameter: :return: None """ if toolbox.is_integer(parameter) and int(parameter) > 0: generations = int(parameter) else: prompt = 'How many generations would you like to simulate?' generations = toolbox.get_integer_between(1, 10000, prompt) for generation in range(generations): self.__world.next_generation() if self.__world.is_stable() == True: "It is stable now." break else: pass #self.__world.__lastWorld = self.__world.__secondWorld string = self.__world.__str__() string += self.status() string += f'left: {generations - generation}' print(string) time.sleep(self.__delay) print() print(f'You have now been through {generations} generations. ') print() print(self.menu(), end=' ') self.__generation += generations
def skip_generations(self, parameter): """ Displays the next generation of the world. :param parameter: :return: None """ if toolbox.is_integer(parameter) and int(parameter) > 0: generations = int(parameter) else: prompt = 'How many generations would you like to skip?' generations = toolbox.get_integer_between(1, 10000, prompt) print(f'Skipping {generations} generations.', end='') for generation in range(generations): self.__world.next_generation(self.__world) if self.__world.is_stable() == True: "It is stable now." break else: pass if generation % 100 == 0: print('.', end='') print(' done!') self.__generation += generations time.sleep(2) string = self.__world.__str__() string += self.status() print(string) print() print(f'You now have skipped forward {generations} generations.') print() print(self.menu(), end=' ')
def change_display(self, parameter): """ Print possible display changes for the user. :param parameter: :return: none """ if toolbox.is_integer(parameter) and \ 1 <= int(parameter) <= len(Cell.displaySets.keys()): setNumber = int(parameter) else: print('**************************************') for number, set in enumerate(Cell.displaySets): liveChar = Cell.displaySets[set]['liveChar'] deadChar = Cell.displaySets[set]['deadChar'] print( f'{number+1}: living cells: {liveChar} dead cells: {deadChar}' ) print(f'{number+2}: Choose your own characters! ') print('**************************************') prompt = 'What character set would you like to use?' setNumber = toolbox.get_integer_between(1, number + 2, prompt) numberOfSets = number + 2 if setNumber == numberOfSets: setString = 'choice' else: setString = list(Cell.displaySets.keys())[setNumber - 1] Cell.set_display(setString) print(self.__currentWorld, end='')
def change_rules(self, parameter): """ Print possible display changes for the user. :param parameter: which rule display user wants :return: none """ if toolbox.is_integer(parameter) and \ 1 <= int(parameter) <= len(Rules.ruleSets.keys()): setNumber = int(parameter) else: print('**************************************') for number, ruleSet in enumerate(Rules.ruleSets): bornNum = Rules.ruleSets[ruleSet]['bornNum'] surviveNum = Rules.ruleSets[ruleSet]['surviveNum'] print( f'{number+1}: Born Number: {bornNum} Survive Number: {surviveNum}' ) print(f'{number+2}: Choose your own characters! ') print('**************************************') prompt = 'What character set would you like to use?' setNumber = toolbox.get_integer_between(1, number + 2, prompt) numberOfSets = number + 2 if setNumber == numberOfSets: setString = 'choice' else: setString = list(Rules.ruleSets.keys())[setNumber - 1] Rules.set_rules(setString) print(self.__currentWorld, end='')
def __init__(self, decks=6): super().__init__() if not is_integer(decks): raise TypeError('Number of decks must be an integer.') self.__decks = decks self.populate_shoe() self.set_cut_card()
def run_simulation(self, parameter): """Displays the next generation of the world""" if toolbox.is_integer(parameter) and int(parameter) > 0: generations = int(parameter) else: prompt = 'How many generations would you like to simulate?' generations = toolbox.get_integer_between(1, 10000, prompt) for generation in range(generations): self.__world.next_generation() string = self.__world.__str__() string += self.status() string += f'left: {generations - generation}' print(string) time.sleep(self.__delay) print(self.menu(), end=' ')
def skip_generations(self, generations): """Displays the next generation of the world""" if toolbox.is_integer(generations) and int(generations) > 0: generations = int(generations) else: prompt = 'How many generations would you like to skip?' generations = toolbox.get_integer_between(1, 10000, prompt) print(f'Skipping {generations} generations.', end='') for generation in range(generations): self.__world.next_generation() if generation % 100 == 0: print('.', end='') print(' done!') self.__generations = generations time.sleep(2) self.display()
def skip_generations(self, parameter): """Displays the next generation of the world""" if toolbox.is_integer(parameter) and int(parameter) > 0: generations = int(parameter) else: prompt = 'How many generations would you like to skip?' generations = toolbox.get_integer_between(1, 10000, prompt) print(f'Skipping {generations} generations.', end='') for generation in range(generations): self.__world.next_generation() if generation % 100 == 0: print('.', end='') print(' done!') time.sleep(2) string = self.__world.__str__() string += self.status() print(string) print(self.menu(), end=' ')
def run_simulation(self, generations): """Displays the next generation of the world""" if toolbox.is_integer(generations) and int(generations) > 0: generations = int(generations) else: prompt = 'How many generations would you like to simulate?' generations = toolbox.get_integer_between(1, 10000, prompt) for generation in range(generations): if self.__world.stop_simulation(): print('simulation is steady') break else: self.__world.next_generation() string = self.__world.__str__() string += self.status() string += f'left: {generations - generation}' print(string) # self.__generations += 1 time.sleep(self.__delay) print(self.menu())
def run_simulation(self, generations): """ Displays the next generation of the world :param generations: :return: next generation """ if toolbox.is_integer(generations) and int(generations) > 0: generations = int(generations) else: prompt = 'How many generations do you want to do?' generations = toolbox.get_integer_between(1, 10000, prompt) for generation in range(generations): self.__world.next_generation() if self.__world.is_stable() == True: break string = self.__world.__str__() string += self.status() string += f'left: {generations - generation}' print(string) time.sleep(self.__delay) print(self.menu())