def set_each_point(seed: int, width: int, height: int, region_centers: List[Tuple[int, int]], image: List[List[int]], d_limit: int, f: List[Callable[[int, int, int, int], float]]): """Calculate the image regions (up to a distance) using the provided metric.""" randomseed(seed) region_distance_functions = [ f if not isinstance(f, list) else choice(f) for _ in range(len(region_centers)) ] for x in range(width): for y in range(height): d_min = float('inf') for i, region in enumerate(region_centers): xn, yn = region d = region_distance_functions[i](x, y, xn, yn) if d < d_min: d_min = d if d <= d_limit: image[x][y] = id(region)
def main(): """ Main function that calls all functions. """ # Paul Lu. Set the seed to get deterministic behaviour for each run. # Makes it easier for testing and tracing for understanding. randomseed(274 + 2020) clean() # Create an object for first choice choice = choices() # Set patterns for human and computer choices choice.choose_pattern() c_choice = choice.get_c_choice() h_choice = choice.get_h_choice() clean() # Set the order of who plays first choice.play_order() first = choice.get_first() # Create a board for one turn of game board1 = board() # Main loop of this game while board1.board_depth() > 0 and not board1.game_over(): if first == 'N': board1.ai_turn(c_choice, h_choice) first = '' board1.human_turn(c_choice, h_choice) board1.ai_turn(c_choice, h_choice) # Get the value of human and the value of computer HUMAN, COMP = board1.get_HUMAN(), board1.get_COMP() # Game over message if board1.wins(HUMAN): clean() print(f'Human turn [{h_choice}]') board1.render(c_choice, h_choice) print('YOU WIN!') elif board1.wins(COMP): clean() print(f'Computer turn [{c_choice}]') board1.render(c_choice, h_choice) print('YOU LOSE!') else: clean() board1.render(c_choice, h_choice) print('DRAW!') exit()
def main(): """ Main function that calls all functions """ # Paul Lu. Set the seed to get deterministic behaviour for each run. # Makes it easier for testing and tracing for understanding. randomseed(274 + 2020) clean() h_choice = '' # X or O c_choice = '' # X or O first = '' # if human is the first # Human chooses X or O to play while h_choice != 'O' and h_choice != 'X': try: print('') h_choice = input('Choose X or O\nChosen: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Setting computer's choice if h_choice == 'X': c_choice = 'O' else: c_choice = 'X' # Human may starts first clean() while first != 'Y' and first != 'N': try: first = input('First to start?[y/n]: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Main loop of this game while len(empty_cells(board)) > 0 and not game_over(board): if first == 'N': ai_turn(c_choice, h_choice) first = '' human_turn(c_choice, h_choice) ai_turn(c_choice, h_choice) # Game over message if wins(board, HUMAN): clean() print(f'Human turn [{h_choice}]') render(board, c_choice, h_choice) print('YOU WIN!') elif wins(board, COMP): clean() print(f'Computer turn [{c_choice}]') render(board, c_choice, h_choice) print('YOU LOSE!') else: clean() render(board, c_choice, h_choice) print('DRAW!') exit()
def main(): """ Main function that calls all functions """ # game (object): Instance of the class Game. game = Game() # term (object): Instance of the class Console. term = Console() # Paul Lu. Set the seed to get deterministic behaviour for each run. # Makes it easier for testing and tracing for understanding. randomseed(274 + 2020) term.clean() h_choice = '' # X or O c_choice = '' # X or O first = '' # if human is the first # Human chooses X or O to play while h_choice != 'O' and h_choice != 'X': try: print('') h_choice = input('Choose X or O\nChosen: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Setting computer's choice if h_choice == 'X': c_choice = 'O' else: c_choice = 'X' # Human may starts first term.clean() while first != 'Y' and first != 'N': try: first = input('First to start?[y/n]: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Main loop of this game while len(game.empty_cells()) > 0 and not game.game_over(): if first == 'N': game.ai_turn(c_choice, h_choice) first = '' game.human_turn(c_choice, h_choice) game.ai_turn(c_choice, h_choice) # Game over message if game.wins(game.get_HUMAN()): term.clean() print(f'Human turn [{h_choice}]') term.render(game.get_state(), c_choice, h_choice) print('YOU WIN!') elif game.wins(game.get_COMP()): term.clean() print(f'Computer turn [{c_choice}]') term.render(game.get_state(), c_choice, h_choice) print('YOU LOSE!') else: term.clean() term.render(game.get_state(), c_choice, h_choice) print('DRAW!') exit()
def play(self): # Paul Lu. Set the seed to get deterministic behaviour for each run. # Makes it easier for testing and tracing for understanding. randomseed(274 + 2020) clean() h_choice = '' # X or O c_choice = '' # X or O first = '' # if human is the first # Human chooses X or O to play while h_choice != 'O' and h_choice != 'X': try: print('') h_choice = input('Choose X or O\nChosen: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Setting computer's choice if h_choice == 'X': c_choice = 'O' else: c_choice = 'X' # Human may starts first clean() while first != 'Y' and first != 'N': try: first = input('First to start?[y/n]: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Main loop of this game while len(self.board.empty_cells( self.board.get_board())) > 0 and not self.board.game_over( self.board.get_board()): if first == 'N': self.ai_turn(c_choice, h_choice) first = '' self.human_turn(c_choice, h_choice) self.ai_turn(c_choice, h_choice) # Game over message clean() if self.board.wins(self.board.get_board(), self.HUMAN): print(f'Human turn [{h_choice}]') self.board.render(self.board.get_board(), c_choice, h_choice) print('YOU WIN!') elif self.board.wins(self.board.get_board(), self.COMP): print(f'Computer turn [{c_choice}]') self.board.render(self.board.get_board(), c_choice, h_choice) print('YOU LOSE!') else: self.board.render(self.board.get_board(), c_choice, h_choice) print('DRAW!')
def main(): randomseed(274 + 2020) clean() # clean the board. h_choice = '' # X or O c_choice = '' # X or O first = '' # if human is the first # Human chooses X or O to play while h_choice != 'O' and h_choice != 'X': try: print('') h_choice = input('Choose X or O\nChosen: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Setting computer's choice if h_choice == 'X': c_choice = 'O' else: c_choice = 'X' # Human may starts first clean() while first != 'Y' and first != 'N': try: first = input('First to start?[y/n]: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Instantiate State class object state = State() # Instantiate Turns class object turns_object = Turns() # Main loop of this game while len(state.empty_cells()) > 0 and \ not state.game_over(turns_object.COMP, turns_object.HUMAN): if first == 'N': turns_object.ai_turn(c_choice, h_choice, state) first = '' turns_object.human_turn(c_choice, h_choice, state) turns_object.ai_turn(c_choice, h_choice, state) # Game over message if state.wins(turns_object.HUMAN): clean() print(f'Human turn [{h_choice}]') state.render(c_choice, h_choice) print('YOU WIN!') elif state.wins(turns_object.COMP): clean() print(f'Computer turn [{c_choice}]') state.render(c_choice, h_choice) print('YOU LOSE!') else: clean() state.render(c_choice, h_choice) print('DRAW!') exit()
except (KeyError, ValueError): print('Bad choice') # Human may starts first def get_choice(self): return [self.h_choice, self.c_choice, self.first] def set_human(self, choice): self.h_choice = choice def set_comp(self, choice): self.c_choice = choice if __name__ == '__main__': randomseed(274 + 2020) board = Board() state = State() """ Main function that calls all functions """ state.clean() state.init_choice() choices = state.get_choice() human = choices[0] comp = choices[1] first = choices[2] # Main loop of this game
def main_function(self): """ Main method that calls all other methods. Adapted from main(). I converted this to a method, as it would help with encapsulation. """ # Paul Lu. Set the seed to get deterministic behaviour for each run. # Makes it easier for testing and tracing for understanding. randomseed(274 + 2020) HUMAN = self.get_human() COMP = self.get_comp() self.clean() h_choice = '' # X or O c_choice = '' # X or O first = '' # if human is the first # Human chooses X or O to play while h_choice != 'O' and h_choice != 'X': try: print('') h_choice = input('Choose X or O\nChosen: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Setting computer's choice if h_choice == 'X': c_choice = 'O' else: c_choice = 'X' # Human may starts first self.clean() while first != 'Y' and first != 'N': try: first = input('First to start?[y/n]: ').upper() except (EOFError, KeyboardInterrupt): print('Bye') exit() except (KeyError, ValueError): print('Bad choice') # Main loop of this game while len(self.empty_cells()) > 0 and not self.game_over(): if first == 'N': self.ai_turn(c_choice, h_choice) first = '' self.human_turn(c_choice, h_choice) self.ai_turn(c_choice, h_choice) # Game over message if self.wins(HUMAN): self.clean() print(f'Human turn [{h_choice}]') self.render(c_choice, h_choice) print('YOU WIN!') elif self.wins(COMP): self.clean() print(f'Computer turn [{c_choice}]') self.render(c_choice, h_choice) print('YOU LOSE!') else: self.clean() self.render(c_choice, h_choice) print('DRAW!')
parser.add_argument('it_doesn\'t_have_to_be_correct') args = parser.parse_args() len_open = args.file sys_exit = args.redundant_file args.more_redundant_files.split("/") if not hash(args.code) == code_hash: print( 'To find the code, complete the last Zahada bonus level at http://www.mcgov.co.uk/zahada.html' ) sys.exit() max = len(open(args.answer_key).read().split('\n')) for i in range(len(args.responses)): score = random.randomseed(0, max) print('Student #{} gets {}/{}'.format(i, score, max)) sys.exit() import random import time listSize = 3000 comparisons = 0 toSort = [] def main(): for i in range(listSize):
def generate( path: str, regions: int, colors: List[Union[Tuple[int, int, int], str]], width: int = 1920, height: int = 1080, region_algorithm=RegionAlgorithm.uniform, distance_algorithm=DistanceAlgorithm.euclidean, color_algorithm=ColorAlgorithm.random, seed: Optional[int] = None, border_size: int = 0, border_color="#FFFFFF", animate=False, animation_background="#FFFFFF", ): # possibly seed the random algorithm if seed is None: seed = random() randomseed(seed) if type(regions) == list: Utilities.info("Region centers provided, skipping generation.") # flip vertically! region_centers = [(int(center[0] * width), int(height - center[1] * height)) for center in regions] else: # check for correct region count if width * height < regions: Utilities.error("Not enough pixels for the number of regions.") Utilities.info("Calculating region centers.") region_centers = region_algorithm(width, height, regions) image = [[None] * height for _ in range(width)] Utilities.info("Calculating region areas.") DistanceAlgorithm.set_each_point(seed, width, height, region_centers, image, float("inf"), distance_algorithm) # possibly convert string colors to tuples i = 0 while i < len(colors): if type(colors[i]) == str: colors[i] = Utilities.hex_to_tuple(colors[i]) i += 1 # either assign colors randomly, or calculate the chromatic number and assign them then if color_algorithm == ColorAlgorithm.random: Utilities.info("Assigning region colors.") region_colors = { id(region): choice(colors) for region in region_centers } else: Utilities.info( "Assigning region colors such that no two adjacent regions have the same color." ) region_colors = Utilities.get_different_adjacent_colors( width, height, image, colors, color_algorithm) # the original, full image (without borders) pil_image = Image.new("RGB", (width, height)) for x in range(width): for y in range(height): pil_image.putpixel((x, y), region_colors[image[x][y]]) if border_size != 0: Utilities.add_border(border_color, border_size, image, pil_image, width, height) if animate: if not os.path.exists(path): os.makedirs(path) d = 1 if type(animation_background) == str: animation_background = Utilities.hex_to_tuple(animation_background) while True: animation_image = [[None] * height for _ in range(width)] DistanceAlgorithm.set_each_point(seed, width, height, region_centers, animation_image, d, distance_algorithm) animation_pil_image = Image.new("RGB", (width, height)) for x in range(width): for y in range(height): animation_pil_image.putpixel( (x, y), animation_background if animation_image[x][y] is None else region_colors[image[x][y]]) if border_size != 0: Utilities.add_border(border_color, border_size, animation_image, animation_pil_image, width, height) animation_path = os.path.join(path, f"{d}.png") animation_pil_image.save(animation_path, "PNG") Utilities.success(f"Animation image saved to {animation_path}") d += 1 if image == animation_image: Utilities.success(f"Done!") break else: pil_image.save(path, resolution=300) Utilities.success(f"Image saved to {path}!")