def __init__(self, game): self.game = game self.matrix_coordinates_2 = MatrixCoordinates() self.transform_coords = self.matrix_coordinates_2.get_matrix_cord_trans( ) self.normalize_coords_2 = self.matrix_coordinates_2.get_matrix_cord_norm( ) self.color = PlayerColor.WHITE
class Stone: def __init__(self, x_norm, y_norm, color, normalize_coord_stones_dict): self.normalize_coord_stones_dict = normalize_coord_stones_dict self.x_norm = x_norm self.y_norm = y_norm self.color = color self.validate = Validate() self.matrix_coordinates = MatrixCoordinates() self.list_dames = [] self.neighbor_stones = [] self.neighbor_coordinates = [] self.enemy_coordinates = [] def coordinates_generator(self): for new_x_norm, new_y_norm in [(self.x_norm, self.y_norm + 1), (self.x_norm, self.y_norm - 1), (self.x_norm + 1, self.y_norm), (self.x_norm - 1, self.y_norm)]: yield new_x_norm, new_y_norm def update_list_dames(self): self.list_dames.clear() for new_x_norm, new_y_norm in self.coordinates_generator(): (new_x_trans, new_y_trans) = self.matrix_coordinates.get_transformed_coord_norm( (new_x_norm, new_y_norm)) if self.validate.validate_gamer_zone(new_x_trans, new_y_trans): if self.validate.validate_free_place( self.normalize_coord_stones_dict, new_x_norm, new_y_norm): self.list_dames.append((new_x_norm, new_y_norm)) def check_neighbor_stone_and_coordinates(self): self.neighbor_stones.clear() self.neighbor_coordinates.clear() self.enemy_coordinates.clear() for new_x_norm, new_y_norm in self.coordinates_generator(): (new_x_trans, new_y_trans) = self.matrix_coordinates.get_transformed_coord_norm( (new_x_norm, new_y_norm)) if self.validate.validate_gamer_zone(new_x_trans, new_y_trans): if not self.validate.validate_free_place( self.normalize_coord_stones_dict, new_x_norm, new_y_norm): if self.normalize_coord_stones_dict[( new_x_norm, new_y_norm)].color == self.color: self.neighbor_stones.append( self.normalize_coord_stones_dict[(new_x_norm, new_y_norm)]) self.neighbor_coordinates.append( (new_x_norm, new_y_norm)) else: self.enemy_coordinates.append((new_x_norm, new_y_norm)) def i_am_dead(self): if len(self.list_dames) == 0: print(self.x_norm, self.y_norm, "died")
def __init__(self, x_norm, y_norm, color, normalize_coord_stones_dict): self.normalize_coord_stones_dict = normalize_coord_stones_dict self.x_norm = x_norm self.y_norm = y_norm self.color = color self.validate = Validate() self.matrix_coordinates = MatrixCoordinates() self.list_dames = [] self.neighbor_stones = [] self.neighbor_coordinates = [] self.enemy_coordinates = []
def __init__(self): self.validate = Validate() self.game_move_log = GameMoveLog() self._free_points = MatrixCoordinates().get_matrix_cord_norm() self.normalize_coord_stones_dict = {} self._move_number_engine = 0 self.black_groups = [] self.white_groups = [] self.removed_black_group = [] self.removed_white_group = []
def __init__(self, is_move_time=False, is_game_time=False, all_time=None, random_bot=False, smart_bot=False): super(ResponseInterface, self).__init__() self.col_pass_black = False self.col_pass_white = False self.is_random_bot = random_bot self.is_smart_bot = smart_bot self.is_move_time = is_move_time self.is_game_time = is_game_time self.all_time = all_time self.copy_time = all_time self.black_points = 0 self.white_points = 0 self.start_timing() self.normalize_coord_stones_dict = {} self.game = Game() self.signal = Signals() self.count_points = CountPoints() self.draw_who_run(self.game.get_move_number_engine()) self.matrix_coordinates = MatrixCoordinates() self.set_signals() self.check_bot()
class RandomBot: def __init__(self, game): self.game = game self.matrix_coordinates_2 = MatrixCoordinates() self.transform_coords = self.matrix_coordinates_2.get_matrix_cord_trans( ) self.normalize_coords_2 = self.matrix_coordinates_2.get_matrix_cord_norm( ) self.color = PlayerColor.WHITE def diff_set(self): return list( set(self.normalize_coords_2) - set(self.game.normalize_coord_stones_dict.keys())) def action(self): transform_coord_bot = random.choice(self.transform_coords) normalize_coord_bot = self.matrix_coordinates_2.get_normalize_coord( transform_coord_bot) if not self.game.move_is_valid(transform_coord_bot, normalize_coord_bot, self.color): try: return self.action() except Exception: return self.pass_gambit() else: time.sleep(0.1) return { "type": "move", "trans_cord": transform_coord_bot, "norm_cord": normalize_coord_bot } def pass_gambit(self): print("passss") return {"type": "pass", "trans_cord": (0, 0), "norm_cord": (0, 0)}
class ResponseInterface(BaseGameInterface): def __init__(self, is_move_time=False, is_game_time=False, all_time=None, random_bot=False, smart_bot=False): super(ResponseInterface, self).__init__() self.col_pass_black = False self.col_pass_white = False self.is_random_bot = random_bot self.is_smart_bot = smart_bot self.is_move_time = is_move_time self.is_game_time = is_game_time self.all_time = all_time self.copy_time = all_time self.black_points = 0 self.white_points = 0 self.start_timing() self.normalize_coord_stones_dict = {} self.game = Game() self.signal = Signals() self.count_points = CountPoints() self.draw_who_run(self.game.get_move_number_engine()) self.matrix_coordinates = MatrixCoordinates() self.set_signals() self.check_bot() def check_bot(self): if self.is_random_bot or self.is_smart_bot: self.set_bots() def set_bots(self): if self.is_random_bot: self.bot = RandomBot(self.game) elif self.is_smart_bot: self.bot = SmartBot(self.game) self.timer_bot = QTimer(self) self.timer_bot.setInterval(600) self.timer_bot.timeout.connect(self.action_bot) self.timer_bot.start() def action_bot(self): if self.game.get_player_color() == PlayerColor.WHITE: response = self.bot.action() # print(response) type_move = response["type"] cord_trans = response["trans_cord"] cord_norm = response["norm_cord"] if type_move == "pass": self.get_pass_white() elif type_move == "move": self.set_new_stone(cord_trans, cord_norm) def start_timing(self): if self.is_move_time or self.is_game_time: self.start_timer() def start_timer(self): self.all_time = self.copy_time self.timer = QTimer(self) self.timer.setInterval(1000) self.timer.timeout.connect(self.update_counter) self.timer.start() def validate_time(self): if self.all_time == -1: if self.is_move_time: self.time_transition() else: self.timer.stop() self.draw_menu() def update_counter(self): hours = int(self.all_time / 3600) mins = int((self.all_time - hours * 3600) / 60) secs = int(self.all_time % 60) self.draw_time("%d:%02d:%02d" % (hours, mins, secs)) self.all_time -= 1 self.validate_time() def set_signals(self): self.signal.restart_signal.connect(self.restart_game) self.signal.closed_signal.connect(self.close_game) def mousePressEvent(self, event): if self.game.validate.validate_gamer_zone(event.x(), event.y()): x_trans, y_trans = self.matrix_coordinates.transformed_coord_mouse(event.x(), event.y()) x_norm, y_norm = self.matrix_coordinates.get_normalize_coord((x_trans, y_trans)) transformed_coord = (x_trans, y_trans) normalized_coord = (x_norm, y_norm) color = self.game.get_player_color() if self.game.move_is_valid(transformed_coord, normalized_coord, color): self.set_new_stone(transformed_coord, normalized_coord) def set_new_gambit(self): self.game.set_new_move() hide_point = self.game.get_hide_stones() if len(hide_point) != 0: self.hide_stones(hide_point) def set_new_stone(self, transformed_coord, normalized_coord): self.check_timer() self.set_new_gambit() self.draw_new_stone(transformed_coord, normalized_coord) self.game.update_move_number_engine() self.draw_who_run(self.game.get_move_number_engine()) self.set_not_pass_gambit() self.draw_points() self.game.print_log_game() # print("-" * 30) # print("ХОД ВАЛИДНЫЙ") def set_not_pass_gambit(self): if self.game.get_player_color() == PlayerColor.BLACK: self.col_pass_black = False else: self.col_pass_white = False def draw_points(self): black_groups, white_groups = self.game.get_black_white_groups() self.black_points = self.count_points.get_count_points_black(black_groups) self.white_points = self.count_points.get_count_points_white(white_groups) self.redraw_points_black(self.black_points) self.redraw_points_white(self.white_points) def draw_new_stone(self, transformed_coord, normalized_coord): if self.game.get_player_color() == PlayerColor.BLACK: self.draw_stone_b(transformed_coord, normalized_coord) self.enabled_buttons(False, True) else: self.draw_stone_w(transformed_coord, normalized_coord) self.enabled_buttons(True, False) def hide_stones(self, remove_group): for group in remove_group: for norm_coord in group: self.del_chip(norm_coord) def check_timer(self): if self.is_move_time: self.timer.stop() self.start_timer() def time_transition(self): self.check_timer() self.game.update_move_number_engine() self.draw_who_run(self.game.get_move_number_engine()) def get_pass_black(self): self.check_timer() self.game.update_move_number_engine() self.draw_who_run(self.game.get_move_number_engine()) self.col_pass_black = True self.pass_black.setEnabled(False) self.pass_white.setEnabled(True) if self.col_pass_white: # print("конец") self.draw_menu() def get_pass_white(self): self.check_timer() self.game.update_move_number_engine() self.draw_who_run(self.game.get_move_number_engine()) self.col_pass_white = True self.pass_white.setEnabled(False) self.pass_black.setEnabled(True) if self.col_pass_black: # print("конец") self.draw_menu() def _close_time(self): if self.is_move_time or self.is_game_time: if self.timer.isActive(): self.timer.stop() if self.is_smart_bot or self.is_random_bot: if self.timer_bot.isActive(): self.timer_bot.stop() def draw_menu(self): self._close_time() self.close() new_win = EndGameInterFace(self, self.signal, self.black_points, self.white_points) new_win.show() def close_game(self): self._close_time() self.close() def closeEvent(self, event): self.close_game() def restart_game(self): self.close_game() from platform import system import os system = system() if system == "Windows": start = "python {}".format(ConstantsName.point_program) else: start = "python3 {}".format(ConstantsName.point_program) os.system(start)
class SmartBot: def __init__(self, game): self.matrix_coordinates_2 = MatrixCoordinates() self.game = game self.transform_coords = self.matrix_coordinates_2.get_matrix_cord_trans( ) self.normalize_coords_2 = self.matrix_coordinates_2.get_matrix_cord_norm( ) self.color = PlayerColor.WHITE def check_defender(self): kol_enemy = 0 helped_stone = False for groups in self.game.white_groups: for norm_coord in groups: stone = self.game.normalize_coord_stones_dict[norm_coord] stone_enemy = len(stone.enemy_coordinates) if stone_enemy > 1 and stone_enemy != 4: if stone_enemy > kol_enemy: kol_enemy = stone_enemy helped_stone = stone if helped_stone: new_moves_list_norm = self.get_list_moves(helped_stone) return self.is_valid_new_moves_list(new_moves_list_norm) return False def queens_gambit(self): if self.check_defender(): # print("defender") return True if self.check_attack(): # print("attack") return True if self.smart_attack(): # print("smart_attack") return True if self.check_create(): # print("create") return True return False def action(self): if self.queens_gambit(): return { "type": "move", "trans_cord": self.transform_coord_bot, "norm_cord": self.norm_cord } else: return self.standard_move() def is_valid_new_moves_list(self, new_moves_list_norm): is_valid_new_move = False k = len(new_moves_list_norm) for norm_cord in new_moves_list_norm: trans_cord = self.matrix_coordinates_2.get_transformed_coord_norm( norm_cord) if self.game.move_is_valid(trans_cord, norm_cord, self.color): is_valid_new_move = True self.norm_cord = norm_cord self.transform_coord_bot = trans_cord break k += 1 return is_valid_new_move def is_valid_specific_point(self, norm_cord): trans_cord = self.matrix_coordinates_2.get_transformed_coord_norm( norm_cord) if self.game.move_is_valid(trans_cord, norm_cord, self.color): self.norm_cord = norm_cord self.transform_coord_bot = trans_cord return True return False def check_attack(self): empty_stone = False best_attack_stone = False kol_enemy = 0 for groups in self.game.black_groups: for norm_coord in groups: stone = self.game.normalize_coord_stones_dict[norm_coord] neighbors = len(stone.neighbor_coordinates) enemies = len(stone.enemy_coordinates) if neighbors == 0: empty_stone = stone if enemies > 0 and enemies > kol_enemy and neighbors == 0: kol_enemy = enemies best_attack_stone = stone if best_attack_stone: new_moves_list_norm = self.get_list_moves(best_attack_stone) return self.is_valid_new_moves_list(new_moves_list_norm) elif empty_stone: new_moves_list_norm = self.get_list_moves(empty_stone) return self.is_valid_new_moves_list(new_moves_list_norm) return False def check_create(self): creation_stone = False for groups in self.game.white_groups: for norm_coord in groups: stone = self.game.normalize_coord_stones_dict[norm_coord] stone_enemy = len(stone.enemy_coordinates) if stone_enemy == 0: creation_stone = stone break if creation_stone: new_moves_list_norm = self.get_list_moves(creation_stone) return self.is_valid_new_moves_list(new_moves_list_norm) return False def what_is_color(self, cord_1): color_1 = False if cord_1 in self.game.normalize_coord_stones_dict: stone_1 = self.game.normalize_coord_stones_dict[cord_1] color_1 = stone_1.color return color_1 def smart_attack(self): list_norm_cord = self.matrix_coordinates_2.get_matrix_cord_norm() for norm_cord in list_norm_cord: x, y = norm_cord[0], norm_cord[1] variant_horizontal = [(x - 1, y), (x + 1, y)] variant_vertical = [(x, y + 1), (x, y - 1)] horizontal_color = False vertical_color = False for norm_cord_hor in variant_horizontal: horizontal_color = self.what_is_color(norm_cord_hor) break for norm_cord_ver in variant_vertical: vertical_color = self.what_is_color(norm_cord_ver) break if horizontal_color != vertical_color and horizontal_color and vertical_color: return self.is_valid_specific_point(norm_cord) return False def standard_move(self): transform_coord_bot = random.choice(self.transform_coords) normalize_coord_bot = self.matrix_coordinates_2.get_normalize_coord( transform_coord_bot) if not self.game.move_is_valid(transform_coord_bot, normalize_coord_bot, self.color): try: return self.standard_move() except Exception: return self.pass_gambit() else: # print("стандартный мув") return { "type": "move", "trans_cord": transform_coord_bot, "norm_cord": normalize_coord_bot } def get_list_moves(self, stone): return [(stone.x_norm + 1, stone.y_norm), (stone.x_norm - 1, stone.y_norm), (stone.x_norm, stone.y_norm + 1), (stone.x_norm, stone.y_norm - 1)] def pass_gambit(self): return {"type": "pass", "trans_cord": (0, 0), "norm_cord": (0, 0)}