def init_ai(ai_class): my_map = Map(size=MAPSIZE) fleet = assignailoc() my_map.generate_player_map(fleet_to_array(fleet)) enemy_map = Map(size=MAPSIZE) enemy_map.generate_ai_enemy_map() return ai_class( my_map=my_map, enemy_map=enemy_map, fleet=fleet, enemy_fleet=copy.deepcopy(ENEMY_FLEET), target_cache=[] )
def init_ai(ai_class): my_map = Map(size=MAPSIZE) fleet = assignailoc() my_map.generate_player_map(fleet_to_array(fleet)) enemy_map = Map(size=MAPSIZE) enemy_map.generate_ai_enemy_map() return ai_class(my_map=my_map, enemy_map=enemy_map, fleet=fleet, enemy_fleet=copy.deepcopy(ENEMY_FLEET), target_cache=[])
def reset_ai(ai): my_map = Map(size=MAPSIZE) fleet = assignailoc() my_map.generate_player_map(fleet_to_array(fleet)) enemy_map = Map(size=MAPSIZE) enemy_map.generate_ai_enemy_map() ai.reset() ai.my_map = my_map ai.enemy_map = enemy_map ai.fleet = fleet ai.enemy_fleet = copy.deepcopy(ENEMY_FLEET)
def game(self): human_turn = True alloc = assignailoc() self.human = init_human(BOARDSIZE, grid_to_array(self.board.grid), self.board.fleet) self.agent = init_agent(BOARDSIZE, fleet_to_array(alloc), alloc) x = 0 y = 0 finished = False while True: # determine if anyone won if self.human.lose(): self.info = 'AI won' finished = True if self.agent.lose(): self.info = 'You won' finished = True self.draw_info(self.human.getstringifiedfleet(), self.agent.getstringifiedfleet(), human_turn) pygame.display.update() if finished: break if human_turn: for event in pygame.event.get(): if termination_detected(event): terminate() elif event.type == MOUSEBUTTONDOWN: x,y = pos_to_coord(event.pos) if x >= BOARDSIZE: resp = self.human.attack(self.agent, (x - BOARDSIZE,y)) result, sunk_ship = resp['result'], resp['sunk_ship'] self.board.grid[x][y] = result if result == 'M': human_turn = not human_turn play_miss_sound() else: play_hit_sound() if sunk_ship: self.info = 'Enemy\'s ' + sunk_ship + ' was destroyed!' self.counter = self.counter + 1 self.board.draw(self.surface) pygame.display.update() x = 0 y = 0 else: x, y = self.agent.find_target() resp = self.agent.attack(self.human, (x, y)) result, sunk_ship = resp['result'], resp['sunk_ship'] self.board.grid[x][y] = result if result == 'M': human_turn = not human_turn if sunk_ship: self.info = 'Your ' + sunk_ship + ' was destroyed!' self.counter = self.counter + 1 self.board.draw(self.surface) pygame.display.update() x = 0 y = 0 # FPSCLOCK.tick(FPS) # reset game self.finish()
def game(self): human_turn = True alloc = assignailoc() self.human = init_human(BOARDSIZE, grid_to_array(self.board.grid), self.board.fleet) self.agent = init_agent(BOARDSIZE, fleet_to_array(alloc), alloc) x = 0 y = 0 finished = False while True: # determine if anyone won if self.human.lose(): self.info = 'AI won' finished = True if self.agent.lose(): self.info = 'You won' finished = True self.draw_info(self.human.getstringifiedfleet(), self.agent.getstringifiedfleet(), human_turn) pygame.display.update() if finished: break if human_turn: for event in pygame.event.get(): if termination_detected(event): terminate() elif event.type == MOUSEBUTTONDOWN: x, y = pos_to_coord(event.pos) if x >= BOARDSIZE: resp = self.human.attack(self.agent, (x - BOARDSIZE, y)) result, sunk_ship = resp['result'], resp['sunk_ship'] self.board.grid[x][y] = result if result == 'M': human_turn = not human_turn play_miss_sound() else: play_hit_sound() if sunk_ship: self.info = 'Enemy\'s ' + sunk_ship + ' was destroyed!' self.counter = self.counter + 1 self.board.draw(self.surface) pygame.display.update() x = 0 y = 0 else: x, y = self.agent.find_target() resp = self.agent.attack(self.human, (x, y)) result, sunk_ship = resp['result'], resp['sunk_ship'] self.board.grid[x][y] = result if result == 'M': human_turn = not human_turn if sunk_ship: self.info = 'Your ' + sunk_ship + ' was destroyed!' self.counter = self.counter + 1 self.board.draw(self.surface) pygame.display.update() x = 0 y = 0 # FPSCLOCK.tick(FPS) # reset game self.finish()