def reverse_pawn(situation, position, player): """ Reverse the pawn of a catch play :param situation: a game situation :type situation: a situation :param position: a game position :type position: a tuple :param player: a game player :type player: a player :return: None :Side effect: reverse the color of the pawns catch """ x = get_pos_x(position) y = get_pos_y(position) neighbors = [(x + 1, y), (x + 1, y + 1), (x + 1, y - 1), (x, y + 1), (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] for neighbor in neighbors: x_neigh = get_pos_x(neighbor) y_neigh = get_pos_y(neighbor) delta_x = x_neigh - x delta_y = y_neigh - y list_pawn = [(x_neigh, y_neigh)] while is_in_grid((x_neigh, y_neigh)) and get_color(situation, x_neigh, y_neigh) != Player.get_color( player) and get_color(situation, x_neigh, y_neigh) is not None: x_neigh += delta_x y_neigh += delta_y list_pawn.append((x_neigh, y_neigh)) if is_in_grid((x_neigh, y_neigh)): if get_color(situation, x_neigh, y_neigh) == Player.get_color(player): list_pawn.pop() for pos in list_pawn: set_color(situation, get_pos_x(pos), get_pos_y(pos), Player.get_color(player))
def humanPlayerPlays(game, player, situation): """ makes the human player plays for given situation in the game :param game: the game :type game: game :param player: the human player :type player: player :param situation: the current situation :type situation: a game situation :returns: *(game situation)* -- the game situation reached afte the human player play """ coord = input("Where would you play? x, y ") try: x, y = coord.split(',') if not get_color(situation, int(x), int(y)) is None: print("Cell already used") humanPlayerPlays(game, player, situation) else: set_color(situation, int(x), int(y), Player.get_color(player)) return situation except KeyboardInterrupt: raise KeyboardInterrupt except: print("input must be 2 separated with a coma x,y . (x = width , y = height) and values must be in [0,2]") humanPlayerPlays(game, player, situation) return situation
def evalFunction(situation, player): """ the evaluation function for the min-max algorithm. It evaluates the given situation, the evaluation function increases with the quality of the situation for the player :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(number)* -- the score of the given situation for the given player. The better the situation for the minmax player, the higher the score. The opposite for human player. """ cells_pts = 0 dic_pts = {(0, 0): 2, (0, 1): 0.75, (0, 2): 0.75, (0, 3): 0.75, (0, 4): 0.75, (0, 5): 0.75, (0, 6): 0.75, (0, 7): 2, (1, 0): 0.75, (1, 1): 0,2 (1, 2): 0,2 (1, 3): 0,2 (1, 4): 0,2 (1, 5): 0,2 (1, 6): 0,2 (1, 7): 0.75, (2, 0): 0.75, (2, 1): 0,2 (2, 2): 0,2 (2, 3): 0,2 (2, 4): 0,2 (2, 5): 0,2 (2, 6): 0,2 (2, 7): 0.75, (3, 0): 0.75, (3, 1): 0,2 (3, 2): 0,2 (3, 3): 0,2 (3, 4): 0,2 (3, 5): 0,2 (3, 6): 0,2 (3, 7): 0.75, (4, 0): 0.75, (4, 1): 0,2 (4, 2): 0,2 (4, 3): 0,2 (4, 4): 0,2 (4, 5): 0,2 (4, 6): 0,2 (4, 7): 0.75, (5, 0): 0.75, (5, 1): 0,2 (5, 2): 0,2 (5, 3): 0,2 (5, 4): 0,2 (5, 5): 0,2 (5, 6): 0,2 (5, 7): 0.75, (6, 0): 0.75, (6, 1): 0,2 (6, 2): 0,2 (6, 3): 0,2 (6, 4): 0,2 (6, 5): 0,2 (6, 6): 0,2 (6, 7): 0.75, (7, 0): 2, (7, 1): 0.75, (7, 2): 0.75, (7, 3): 0.75, (7, 4): 0.75, (7, 5): 0.75, (7, 6): 0.75, (7, 7): 2, } for x_list in situation: for cell in x_list: if get_color(situation, get_cell_x(cell), get_cell_y(cell)) == Player.get_color(player): cells_pts += dic_pts[get_position(situation, get_cell_x(cell), get_cell_y(cell))] if isFinished(situation): return 5 * cells_pts * coef(player) else: return 1 * cells_pts * coef(player)
def catch_play(situation, position, player): """ Get True if a position is a catch play for a player :param situation: a situation :type situation: list of list :param position: a position :type position: a tuple :param player: a game player :type player: a player :return: *(Boolean)* --True if the position is a catch play """ x = get_pos_x(position) y = get_pos_y(position) neighbors = [(x + 1, y), (x + 1, y + 1), (x + 1, y - 1), (x, y + 1), (x, y - 1), (x - 1, y - 1), (x - 1, y), (x - 1, y + 1)] for neighbor in neighbors: x_neigh = get_pos_x(neighbor) y_neigh = get_pos_y(neighbor) delta_x = x_neigh - x delta_y = y_neigh - y while is_in_grid((x_neigh, y_neigh)) and get_color(situation, x_neigh, y_neigh) != Player.get_color(player) \ and get_color(situation, x_neigh, y_neigh) is not None: x_neigh += delta_x y_neigh += delta_y if is_in_grid((x_neigh, y_neigh)): if get_color(situation, x_neigh, y_neigh) == Player.get_color(player): return True return False
def nextSituations(situation, player): """ returns the list of situations that can be reached from given situation by the player in the game :param situation: the current situation :type situation: a game situation :param player: the current player :type player: player :returns: *(list<situation>)* -- the list of situations that can be reached from given situation when player plays one round in the game """ l_situation = [] for position in available_position(situation, player): copy_situation = copy.deepcopy(situation) set_color(copy_situation, position[0], position[1], Player.get_color(player)) l_situation.append(copy_situation) return l_situation