def user_display(self): living = toolbox.get_string('What would you like your living cell to be?') dead = toolbox.get_string('What would you like your dead cell to be?') Cell.set_display_user_values(living, dead) print() print(f"You now have a choice of a '{living}' as a living cell and a dead cell as '{dead}'.") print()
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_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 guess(self): letter = toolbox.get_string('What is your letter guess?') while len(letter) != 1: print('Your guess has to be one character.') letter = toolbox.get_string('What is your letter guess?') while letter in [ '1', '2', '3', '4', '5', '6', '7', '8', '8', '9', '0' ]: print('Your guess has to be a letter.') letter = toolbox.get_string('What is your letter guess?') while letter in game.get_lettersGuesses(): print('You have already guessed that.') letter = toolbox.get_string('What is your letter guess?') return letter
def load(self, filename, myPath='./'): """ loads a file that user already haves and makes it the current world :param filename: name the user calls the world :param myPath: location of file it comes from :return: """ allFiles = os.listdir(myPath) if filename == None: print("Here are your options to load:\n") for file in allFiles: print(file) print() filename = toolbox.get_string("Which file do you want to load? ") if filename[-5:] != '.life': filename += '.life' if filename not in allFiles: print('404: File not found...') else: if filename[0:len(myPath)] != myPath: filename = myPath + filename sleep(1) print(f"...loading {filename}...") sleep(2) self.__world = self.__worldType.from_file(filename, self.__worldType) print(self.__world)
def open(self, filename, myPath='./'): """ open a text file and use it to populate a new world. :param filename: name of the file, may be None at this point. :param myPath: Where the file is located. :return: None """ if filename == None: filename = toolbox.get_string('Which file do you wanna open?') # # Check for and add the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' allFiles = os.listdir(myPath) if filename not in allFiles: print( '404: File not found...thanks for breaking the program, idiot.' ) else: # # Add on the correct path for saving files if the user didn't # include it in the filename. # if filename[0:len(myPath)] != myPath: filename = myPath + filename self.__world = World.from_file(filename)
def main(): print('---Blackjack 0.5---\n') table = Table(doubleOn=[9, 10, 11]) dealer = Dealer('Dealer', 1000000) dealer.sit(table) # # Deck Stacking: # dealer._shoe.append(Card('ace', 'spades')) dealer._shoe.append(Card('ace', 'hearts')) # # # numberOfPlayers = get_integer_between(1, 7, 'How many players?') for number in range(1, numberOfPlayers + 1): name = get_string(f"What is player {number}'s name?") player = HumanPlayer(name, 100) player.sit(table) dealer.take_bets() while table.has_players(): dealer.deal() dealer.offer_insurance() dealer.play_hands() dealer.play_own_hand() dealer.payout_hands() dealer.take_bets() print('\n---There are no more players. Game Over.---')
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 get_filename_for_saving(self, filename, myPath='./'): """ Make sure that the filename exists, that it has the right extension and that it goes into the correct directory. :param filename: name of the file, may be None at this point. :param myPath: Where the file should be saved. :return: The corrected filename with the path prepended. """ if filename == '': filename = get_string('What do you want to call the file? ') # # Make sure the file has the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' # # Check if the file already exists. # if not path.isdir(myPath): mkdir(myPath) if filename in listdir(myPath): prompt = f"A file named '{filename}' already exists! Do you want to replace it?" replaceFile = get_boolean(prompt) if not replaceFile: filename = self.get_filename_for_saving('') # # Add on the correct path for saving files. # if filename[0:len(myPath)] != myPath: filename = path.join(myPath, filename) return filename
def open(self, filename, myPath='./'): """ open a text file and use it to populate a new world. :param filename: name of the file, may be None at this point. :param myPath: Where the file is located. :return: None """ Cell.set_display('basic') if filename == None: filename = toolbox.get_string('Which file do you want to open?') # # Check for and add the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' allFiles = os.listdir(myPath) if filename not in allFiles: print('404: File not found...') for filenames in allFiles: print(filenames) print('Try one of these next time.') print() else: # # Add on the correct path for saving files if the user didn't # include it in the filename. # if filename[0:len(myPath)] != myPath: filename = myPath + filename self.__world = World.from_file(filename) print(self.__world, end='') print() print(f'You now opened {filename} world.') print() print(self.status() + '\n' + self.menu(), end=' ')
def save(self, filename, myPath='./'): """ Save the current generation of the current world as a text file. :param filename: name of the file, may be None at this point. :param myPath: Where the file should be saved. :return: None """ if filename == None: filename = toolbox.get_string('What do you wanna call the file? ') # # Make sure the file has the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' # # if the path doesn't already exist, create it. # if not os.path.isdir(myPath): os.mkdir(myPath) # # Add on the correct path for saving files if the user didn't # include it in the filename. # if filename[0:len(myPath)] != myPath: filename = myPath + filename self.__world.save(filename)
def get_filename_for_opening(self, filename, myPath='./'): """Make sure the filename has the right extension, goes in the right directory and, if no name is given, provides a list of worlds.""" # # Find the files that are available. # filesAvailable = False files = os.listdir(myPath) availableFiles = 'Available files: ' for file in files: splitFile = file.split('.') if splitFile[-1] == 'life': availableFiles += splitFile[0] + ', ' filesAvailable = True availableFiles = availableFiles[:-2] if filesAvailable: # # Make sure the file has the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' while filename not in files: print(availableFiles) filename = get_string('Which file do you want to open?') if filename[-5:] != '.life': filename = filename + '.life' if filename not in files: print('404: File not found...') # # Add on the correct path for saving files. # if filename[0:9] != myPath: filename = os.path.join(myPath, filename) else: filename = '' return filename
def get_filename_for_saving(self, filename, myPath='./'): """Make sure that the filename exists, that it has the right extension and that it goes into the correct directory.""" if filename == '': filename = get_string('What do you want to call the file? ') # # Make sure the file has the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' # # Check if the file already exists. # if not os.path.isdir(myPath): mkdir(myPath) if filename in os.listdir(myPath): prompt = f"A file named '{filename}' already exists! Do you want to replace it?" replaceFile = get_boolean(prompt) if replaceFile == False: filename = self.get_filename_for_saving('') # # Add on the correct path for saving files. # if filename[0:len(myPath)] != myPath: filename = os.path.join(myPath, filename) return filename
def from_library(self, filename, myPath='./'): allFiles = os.listdir(myPath) if filename not in allFiles: for file in allFiles: print(file) if filename == None: filename = toolbox.get_string('Which world do you wanna use?') if filename[-5:] != '.life': filename = filename + '.life' else: if filename[0:len(myPath)] != myPath: filename = myPath + filename self.__world = World.from_file(filename)
def play(self, hand, dealerShowing): """ Returns the player's action for this hand. The dealer calls this method repeatedly for each of the player's hands until all hands are completed. """ allPlays = { 's': '[S]tand', 'h': '[H]it', 'd': '[D]ouble down', 'p': 's[P]lit', 'u': 's[U]rrender' } # # Some plays will not be legal for a given hand. Remove those choices. # if not hand.can_hit(): del allPlays['h'] if not hand.can_split() or (self._chips < hand.bet): del allPlays['p'] if not hand.can_double() or (self._chips == 0): del allPlays['d'] if hand.isStanding or hand.isBusted: del allPlays['s'] del allPlays['u'] assert len( allPlays ) > 0, "player.play() shouldn't be called if there are no legal plays." validPlays = allPlays.keys() print(f'\n{self.name} has {hand}.') print(f'The dealer has a {dealerShowing} showing.') validMenu = f'{self.name} can: ' + ' '.join(allPlays.values()) choice = 'getInMyWhileLoop' while choice not in validPlays: choice = get_string(validMenu).lower() # # If the player doubles down, they can say by how much. # additionalBet = None if choice == 'd': maxBet = hand.bet if self._chips < maxBet: maxBet = self._chips prompt = f'{self.player}, how much do you want to bet? [max = ${maxBet:0.2f}]' additionalBet = get_number_between(0, maxBet, prompt) return choice, additionalBet
def save(self, filename, myPath='./'): """ Save the current generation of the current world as a text file. :param filename: name of the file, may be None at this point. :param myPath: Where the file should be saved. :return: None """ replace = False Cell.set_display('basic') if filename == None: filename = toolbox.get_string( 'What do you want to call the file? ') # # Make sure the file has the correct file extension. # if filename[-5:] != '.life': filename = filename + '.life' allFiles = os.listdir(myPath) if filename in allFiles: replace = toolbox.get_boolean( 'There is already a file with this name. Would like to replace it?' ) if replace == True: # # if the path doesn't already exist, create it. # if not os.path.isdir(myPath): os.mkdir(myPath) # # Add on the correct path for saving files if the user didn't # include it in the filename. # if filename[0:len(myPath)] != myPath: filename = myPath + filename self.__world.save(filename) print() print('You now saved this world.') print() else: print("Okay I won't save it.") print(self.__world, end='') print() print(self.status() + '\n' + self.menu(), end=' ')
def name_file(self, filename): """ Name file based on user input :param filename: what the user would like as filename :return: filename """ if filename == None: filename = toolbox.get_string( 'What do you want to call the file? ') if filename[-5:] != '.life': filename = filename + '.life' if filename in self.__worldFiles: replace = toolbox.get_boolean( 'There is already with this file. Would you like to replace it with current world? ' ) if replace == False: self.name_file(filename) return filename