예제 #1
0
    def select(self, active_cards, game_state):
        """
            Choose the character to activate whithin
            the given choices.
        """
        available_characters = [
            character.display() for character in active_cards
        ]
        question = {
            "question type": "select character",
            "data": available_characters,
            "game state": game_state
        }
        selected_character = ask_question_json(self, question)

        if selected_character not in range(len(active_cards)):
            warning_message = (
                ' !  : selected character not in '
                'available characters. Choosing random character.')
            logger.warning(warning_message)
            selected_character = randint(0, len(active_cards) - 1)

        perso = active_cards[selected_character]

        # log
        logger.info(f"question : {question['question type']}")
        logger.info(f"answer : {perso}")

        del active_cards[selected_character]
        return perso
예제 #2
0
    def actions(self):
        """
        phase = tour
        phase 1 : IFFI
        phase 2 : FIIF
        first phase : initially num_tour = 1, then 3, 5, etc.
        so the first player to play is (1+1)%2=0 (inspector)
        second phase : num_tour = 2, 4, 6, 8, etc.
        so the first player to play is (2+1)%2=1 (fantom)
        """
        first_player_in_phase = (self.num_tour + 1) % 2
        if first_player_in_phase == 0:
            logger.info(
                f"-\nshuffle {len(self.character_cards)} character_cards\n-")
            shuffle(self.character_cards)
            self.active_cards = self.character_cards[:4]
        else:
            self.active_cards = self.character_cards[4:]

        # the characters should be able to use their power at each new round
        for card in self.active_cards:
            card.power_activated = False

        for i in [
                first_player_in_phase, 1 - first_player_in_phase,
                1 - first_player_in_phase, first_player_in_phase
        ]:
            self.players[i].play(self)
예제 #3
0
def init_connexion():
    while len(clients) != 2:
        link.listen(2)
        (clientsocket, addr) = link.accept()
        logger.info("Received client !")
        clients.append(clientsocket)
        clientsocket.settimeout(10)
예제 #4
0
    def tour(self):
        # log
        logger.info("\n------------------")
        logger.info(self)
        logger.debug(json.dumps(self.update_game_state(""), indent=4))

        # work
        self.actions()
        self.fantom_scream()
        for p in self.characters:
            p.power = True
        self.num_tour += 1
예제 #5
0
    def play(self, game):
        logger.info("--\n" + self.role + " plays\n--")

        logger.debug(json.dumps(game.update_game_state(""), indent=4))
        charact = self.select(game.active_cards,
                              game.update_game_state(self.role))

        # red character can choose to activate its power
        # before OR after moving
        if charact.color == "red":
            activation_possibilities = ["before", "after"]
            question = {
                "question type": "red character power activation time",
                "data": activation_possibilities,
                "game state": game.update_game_state(self.role)
            }

            power_activation_time = ask_question_json(self, question)
            if power_activation_time not in [0, 1]:
                power_activation_time = random.choice(activation_possibilities)
            else:
                power_activation_time = activation_possibilities[
                    power_activation_time]

            # now play red character
            if power_activation_time == "before":
                moved_characters = self.activate_power(
                    charact, game, before | both,
                    game.update_game_state(self.role))

                self.move(charact, [charact], game.blocked,
                          game.update_game_state(self.role))
            else:
                self.move(charact, [charact], game.blocked,
                          game.update_game_state(self.role))

                self.activate_power(charact, game, after | both,
                                    game.update_game_state(self.role))

        # character is not red
        else:
            moved_characters = self.activate_power(
                charact, game, before | both,
                game.update_game_state(self.role))

            self.move(charact, moved_characters, game.blocked,
                      game.update_game_state(self.role))

            self.activate_power(charact, game, after | both,
                                game.update_game_state(self.role))
예제 #6
0
    def play(self, game):
        logger.info("--\n" + self.role + " plays\n--")

        logger.debug(json.dumps(game.update_game_state(""), indent=4))
        charact = self.select(game.active_cards,
                              game.update_game_state(self.role))

        # purple and brown power choose to activate or not before moving
        moved_character = self.activate_power(
            charact, game, before, game.update_game_state(self.role))

        self.move(charact, moved_character, game.blocked,
                  game.update_game_state(self.role), game)

        self.activate_power(charact, game, after,
                            game.update_game_state(self.role))
예제 #7
0
 def fantom_scream(self):
     partition: List[Set[Character]] = [{
         p
         for p in self.characters if p.position == i
     } for i in range(10)]
     if len(partition[self.fantom.position]) == 1 \
             or self.fantom.position == self.shadow:
         logger.info("The fantom screams.")
         self.position_carlotta += 1
         for room, chars in enumerate(partition):
             if len(chars) > 1 and room != self.shadow:
                 for p in chars:
                     p.suspect = False
     else:
         logger.info("the fantom does not scream.")
         for room, chars in enumerate(partition):
             if len(chars) == 1 or room == self.shadow:
                 for p in chars:
                     p.suspect = False
     self.position_carlotta += len(
         [p for p in self.characters if p.suspect])
예제 #8
0
    def play(self, game):
        logger.info("--\n" + self.role + " plays\n--")

        logger.debug(json.dumps(game.update_game_state(""), indent=4))
        charact = self.select(game.active_tiles,
                              game.update_game_state(self.role))

        moved_characters = self.activate_power(charact,
                                               game,
                                               before | two,
                                               game.update_game_state(self.role))

        self.move(charact,
                  moved_characters,
                  game.blocked,
                  game.update_game_state(self.role))

        self.activate_power(charact,
                            game,
                            after | two,
                            game.update_game_state(self.role))
예제 #9
0
    def move(self, charact, moved_characters, blocked, game_state):
        """
            Select a new position for the character.
        """
        pass_act = pink_passages if charact.color == 'pink' else passages
        if charact.color != 'purple' or charact.power:
            disp = {x for x in pass_act[charact.position]
                    if charact.position not in blocked or x not in blocked}

            available_positions = list(disp)
            question = {"question type": "select position",
                        "data": available_positions,
                        "game state": game_state}
            selected_index = ask_question_json(self, question)

            # test
            if selected_index not in range(len(disp)):
                warning_message = (
                    ' !  : selected position not available '
                    'Choosing random position.'
                )
                logger.warning(warning_message)
                selected_position = disp.pop()

            else:
                selected_position = available_positions[selected_index]

            logger.info(f"question : {question['question type']}")
            logger.info("answer : " + str(selected_position))

            if len(moved_characters) > 1:
                logger.debug("more than one character moves")
            for q in moved_characters:
                q.position = selected_position
                logger.info("new position : " + str(q))
예제 #10
0
 def actions(self):
     """
     phase = tour
     phase 1 : IFFI
     phase 2 : FIIF
     first phase : initially num_tour = 1, then 3, 5, etc.
     so the first player to play is (1+1)%2=0 (inspector)
     second phase : num_tour = 2, 4, 6, 8, etc.
     so the first player to play is (2+1)%2=1 (fantom)
     """
     first_player_in_phase = (self.num_tour + 1) % 2
     if first_player_in_phase == 0:
         logger.info(f"-\nshuffle {len(self.tiles)} tiles\n-")
         shuffle(self.tiles)
         self.active_tiles = self.tiles[:4]
     else:
         self.active_tiles = self.tiles[4:]
     for i in [
             first_player_in_phase, 1 - first_player_in_phase,
             1 - first_player_in_phase, first_player_in_phase
     ]:
         self.players[i].play(self)
예제 #11
0
 def lancer(self):
     """
         Run a game until either the fantom is discovered,
         or the singer leaves the opera.
     """
     # work
     while self.position_carlotta < self.exit and len(
         [p for p in self.characters if p.suspect]) > 1:
         self.tour()
     # game ends
     if self.position_carlotta < self.exit:
         logger.info("----------\n---- inspector wins : fantom is " +
                     str(self.fantom))
     else:
         logger.info("----------\n---- fantom wins")
     # log
     logger.info(
         f"---- final position of Carlotta : {self.position_carlotta}")
     logger.info(f"---- exit : {self.exit}")
     logger.info(
         f"---- final score : {self.exit - self.position_carlotta}\n----------"
     )
     return self.exit - self.position_carlotta
예제 #12
0
    def __init__(self, players: List[Player]):
        # Todo: Should be self.players: Tuple[Player] = (player_1, player_2)
        self.players = players
        self.position_carlotta = 6  # position on the exit path
        # Todo: Should be removed and make the game ends when carlotta reach 0.
        self.exit = 22
        self.num_tour = 1
        # Todo: Should be a Dict[enum, Character]
        self.characters = set({Character(color) for color in colors})
        # character_cards are used to draw 4 characters at the beginning
        # of each round
        self.character_cards = list(self.characters)
        self.active_cards = list()
        self.alibi_cards = self.character_cards.copy()
        self.fantom = choice(self.alibi_cards)
        # Todo: Should be placed in a logger section of the __init__()
        logger.info("the fantom is " + self.fantom.color)
        self.alibi_cards.remove(self.fantom)
        self.alibi_cards.extend(['fantom'] * 3)

        # log
        logger.info("\n=======\nnew game\n=======")
        # Todo: 1 Should be removed
        logger.info(f"shuffle {len(self.character_cards)} character_cards")
        # Todo: 2 Should be removed
        logger.info(f"shuffle {len(self.alibi_cards)} alibi cards")
        # work
        # Todo: 1 Should be removed
        shuffle(self.character_cards)
        # Todo: 2 Should be removed
        shuffle(self.alibi_cards)

        # Initialise character positions
        # Rooms at the center of the game are not available
        rooms_number = list(range(10))
        start_rooms = rooms_number[:5] + rooms_number[7:]
        for character in self.characters:
            character.position = choice(start_rooms)

        for character in self.characters:
            # get position of grey character
            if character.color == "grey":
                grey_character_position = character.display()["position"]
                self.shadow = grey_character_position
            if character.color == "blue":
                blue_character_position = character.display()["position"]
                # initially the blocked passage is
                # next to blue character clockwise
                if blue_character_position == 0:
                    self.blocked = (0, 1)
                elif blue_character_position == 1:
                    self.blocked = (1, 2)
                elif blue_character_position == 2:
                    self.blocked = (2, 3)
                elif blue_character_position == 3:
                    self.blocked = (3, 4)
                elif blue_character_position == 4:
                    self.blocked = (4, 5)
                elif blue_character_position == 7:
                    self.blocked = (7, 9)
                elif blue_character_position == 9:
                    self.blocked = (8, 9)
                elif blue_character_position == 8:
                    self.blocked = (4, 8)
                else:
                    print(blue_character_position)
                    raise ValueError(
                        "Wrong initial position of blue character")

        self.characters_display = [
            character.display() for character in self.characters
        ]

        # Todo: should be removed
        self.character_cards_display = [
            tile.display() for tile in self.character_cards
        ]
        self.active_cards_display = [
            tile.display() for tile in self.active_cards
        ]

        self.game_state = {
            "position_carlotta": self.position_carlotta,
            "exit": self.exit,
            "num_tour": self.num_tour,
            "shadow": self.shadow,
            "blocked": self.blocked,
            "characters": self.characters_display,
            # Todo: should be removed
            "character_cards": self.character_cards_display,
            "active character_cards": self.active_cards_display,
        }
예제 #13
0
    def activate_power(self, charact, game, activables, game_state):
        """
            Use the special power of the character.
        """
        # check if the power should be used before of after moving
        # this depends on the "activables" variable, which is a set.
        if not charact.power_activated and charact.color in activables:

            # check if special power is mandatory
            if charact.color in mandatory_powers:
                power_activation = 1

            # special power is not mandatory
            else:
                question = {
                    "question type": f"activate {charact.color} power",
                    "data": [0, 1],
                    "game state": game_state
                }
                power_activation = ask_question_json(self, question)

                # log
                logger.info(f"question : {question['question type']}")
                if power_activation == 1:
                    power_answer = "yes"
                else:
                    power_answer = "no"
                logger.info(f"answer  : {power_answer}")

            # the power will be used
            # charact.power represents the fact that
            # the power is still available
            if power_activation:
                logger.info(charact.color + " power activated")
                charact.power_activated = True

                # red character
                if charact.color == "red":
                    draw = choice(game.alibi_cards)
                    game.alibi_cards.remove(draw)
                    logger.info(str(draw) + " was drawn")
                    if draw == "fantom":
                        game.position_carlotta += -1 if self.num == 0 else 1
                    elif self.num == 0:
                        draw.suspect = False

                # black character
                if charact.color == "black":
                    for q in game.characters:
                        if q.position in self.get_adjacent_positions(
                                charact, game):
                            q.position = charact.position
                            logger.info("new position : " + str(q))

                # white character
                if charact.color == "white":
                    for moved_character in game.characters:
                        if moved_character.position == charact.position and charact != moved_character:
                            available_positions = self.get_adjacent_positions(
                                charact, game)

                            # format the name of the moved character to string
                            character_to_move = str(moved_character).split(
                                "-")[0]
                            question = {
                                "question type":
                                "white character power move " +
                                character_to_move,
                                "data": available_positions,
                                "game state": game_state
                            }
                            selected_index = ask_question_json(self, question)

                            # test
                            if selected_index not in range(
                                    len(available_positions)):
                                warning_message = (
                                    ' !  : selected position not available '
                                    'Choosing random position.')
                                logger.warning(warning_message)
                                selected_position = choice(available_positions)

                            else:
                                selected_position = available_positions[
                                    selected_index]

                            logger.info(
                                f"question : {question['question type']}")
                            logger.info("answer : " + str(selected_position))
                            moved_character.position = selected_position
                            logger.info("new position : " +
                                        str(moved_character))

                # purple character
                if charact.color == "purple":
                    # logger.debug("Rappel des positions :\n" + str(game))

                    available_characters = [
                        q for q in game.characters if q.color != "purple"
                    ]

                    # the socket can not take an object
                    available_colors = [q.color for q in available_characters]

                    question = {
                        "question type": "purple character power",
                        "data": available_colors,
                        "game state": game_state
                    }
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(colors)):
                        warning_message = (
                            ' !  : selected character not available '
                            'Choosing random character.')
                        logger.warning(warning_message)
                        selected_character = choice(colors)

                    else:
                        selected_character = available_characters[
                            selected_index]

                    logger.info(f"question : {question['question type']}")
                    logger.info(f"answer : {selected_character}")

                    # swap positions
                    charact.position, selected_character.position = selected_character.position, charact.position

                    logger.info(f"new position : {charact}")
                    logger.info(f"new position : {selected_character}")

                    return selected_character

                # brown character
                if charact.color == "brown":
                    # the brown character can take one other character with him
                    # when moving.
                    available_characters = [
                        q for q in game.characters
                        if charact.position == q.position if q.color != "brown"
                    ]

                    # the socket can not take an object
                    available_colors = [q.color for q in available_characters]
                    if len(available_colors) > 0:
                        question = {
                            "question type": "brown character power",
                            "data": available_colors,
                            "game state": game_state
                        }
                        selected_index = ask_question_json(self, question)

                        # test
                        if selected_index not in range(len(colors)):
                            warning_message = (
                                ' !  : selected character not available '
                                'Choosing random character.')
                            logger.warning(warning_message)
                            selected_character = choice(colors)
                        else:
                            selected_character = available_characters[
                                selected_index]

                        logger.info(f"question : {question['question type']}")
                        logger.info(f"answer : {selected_character}")
                        return selected_character
                    else:
                        return None

                # grey character
                if charact.color == "grey":

                    available_rooms = [
                        room for room in range(10) if room is not game.shadow
                    ]
                    question = {
                        "question type": "grey character power",
                        "data": available_rooms,
                        "game state": game_state
                    }
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(available_rooms)):
                        warning_message = (' !  : selected room not available '
                                           'Choosing random room.')
                        logger.warning(warning_message)
                        selected_index = randint(0, len(available_rooms) - 1)
                        selected_room = available_rooms[selected_index]

                    else:
                        selected_room = available_rooms[selected_index]

                    game.shadow = selected_room
                    logger.info(f"question : {question['question type']}")
                    logger.info("answer : " + str(game.shadow))

                # blue character
                if charact.color == "blue":

                    # choose room
                    available_rooms = [room for room in range(10)]
                    question = {
                        "question type": "blue character power room",
                        "data": available_rooms,
                        "game state": game_state
                    }
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(available_rooms)):
                        warning_message = (' !  : selected room not available '
                                           'Choosing random room.')
                        logger.warning(warning_message)
                        selected_index = randint(0, len(available_rooms) - 1)
                        selected_room = available_rooms[selected_index]

                    else:
                        selected_room = available_rooms[selected_index]

                    # choose exit
                    passages_work = passages[selected_room].copy()
                    available_exits = list(passages_work)
                    question = {
                        "question type": "blue character power exit",
                        "data": available_exits,
                        "game state": game_state
                    }
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(available_exits)):
                        warning_message = (' !  : selected exit not available '
                                           'Choosing random exit.')
                        logger.warning(warning_message)
                        __import__('ipdb').set_trace()
                        selected_exit = choice(passages_work)

                    else:
                        selected_exit = available_exits[selected_index]

                    logger.info(f"question : {question['question type']}")
                    logger.info("answer : " +
                                str({selected_room, selected_exit}))
                    game.blocked = tuple((selected_room, selected_exit))
            else:
                # if the power was not used
                return None
예제 #14
0
    def activate_power(self, charact, game, activables, game_state):
        """
            Use the special power of the character.
        """
        # check if the power should be used before of after moving
        # this depends on the "activables" variable, which is a set.
        if charact.power and charact.color in activables:
            character_color = charact.display()["color"]
            question = {"question type": f"activate {character_color} power",
                        "data": [0, 1],
                        "game state": game_state}
            power_activation = ask_question_json(self, question)

            # log
            logger.info(f"question : {question['question type']}")
            if power_activation == 1:
                power_answer = "yes"
            else:
                power_answer = "no"
            logger.info("answer  : " + power_answer)

            # work
            if power_activation:
                logger.info(charact.color + " power activated")
                charact.power = False

                # red character
                if charact.color == "red":
                    # Todo: 2.Origin Should be replaced by
                    #  draw = random.choice(game.cards)
                    #  game.cards.remove(draw)
                    draw = game.cards[0]
                    logger.info(str(draw) + " was drawn")
                    if draw == "fantom":
                        game.position_carlotta += -1 if self.num == 0 else 1
                    elif self.num == 0:
                        draw.suspect = False
                    # Todo: 2 Should be removed
                    del game.cards[0]

                # black character
                if charact.color == "black":
                    for q in game.characters:
                        if q.position in {x for x in passages[charact.position] if
                                          x not in game.blocked or q.position not in game.blocked}:
                            q.position = charact.position
                            logger.info("new position : " + str(q))

                # white character
                if charact.color == "white":
                    for moved_character in game.characters:
                        if moved_character.position == charact.position and charact != moved_character:
                            disp = {
                                x for x in passages[charact.position]
                                if x not in game.blocked or moved_character.position not in game.blocked}

                            # edit
                            available_positions = list(disp)
                            # format the name of the moved character to string
                            character_to_move = str(
                                moved_character).split("-")[0]
                            question = {"question type": "white character power move " + character_to_move,
                                        "data": available_positions,
                                        "game state": game_state}
                            selected_index = ask_question_json(self, question)

                            # test
                            if selected_index not in range(len(disp)):
                                warning_message = (
                                    ' !  : selected position not available '
                                    'Choosing random position.'
                                )
                                logger.warning(warning_message)
                                selected_position = disp.pop()

                            else:
                                selected_position = available_positions[selected_index]

                            logger.info(
                                f"question : {question['question type']}")
                            logger.info("answer : " +
                                        str(selected_position))
                            moved_character.position = selected_position
                            logger.info("new position : " + str(moved_character))

                # purple character
                if charact.color == "purple":
                    # logger.debug("Rappel des positions :\n" + str(game))

                    available_characters = list(colors)
                    available_characters.remove("purple")
                    question = {"question type": "purple character power",
                                "data": available_characters,
                                "game state": game_state}
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(colors)):
                        warning_message = (
                            ' !  : selected character not available '
                            'Choosing random character.'
                        )
                        logger.warning(warning_message)
                        selected_character = colors.pop()

                    else:
                        selected_character = available_characters[selected_index]

                    logger.info(f"question : {question['question type']}")
                    logger.info("answer : " + selected_character)

                    # y a pas plus simple ?
                    selected_crctr = [x for x in game.characters if x.color
                                      == selected_character][0]
                    charact.position, selected_crctr.position = selected_crctr.position, charact.position
                    logger.info("new position : " + str(charact))
                    logger.info("new position : " + str(selected_crctr))

                # brown character
                if charact.color == "brown":
                    # the brown character can take other characters with him
                    # when moving.
                    return [q for q in game.characters if charact.position == q.position]

                # grey character
                if charact.color == "grey":

                    available_rooms = [room for room in range(10)]
                    question = {"question type": "grey character power",
                                "data": available_rooms,
                                "game state": game_state}
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(available_rooms)):
                        warning_message = (
                            ' !  : selected room not available '
                            'Choosing random room.'
                        )
                        logger.warning(warning_message)
                        selected_index = random.randint(
                            0, len(available_rooms) - 1)
                        selected_room = available_rooms[selected_index]

                    else:
                        selected_room = available_rooms[selected_index]

                    game.shadow = selected_room
                    logger.info(f"question : {question['question type']}")
                    logger.info("answer : " + str(game.shadow))

                # blue character
                if charact.color == "blue":

                    # choose room
                    available_rooms = [room for room in range(10)]
                    question = {"question type": "blue character power room",
                                "data": available_rooms,
                                "game state": game_state}
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(available_rooms)):
                        warning_message = (
                            ' !  : selected room not available '
                            'Choosing random room.'
                        )
                        logger.warning(warning_message)
                        selected_index = random.randint(
                            0, len(available_rooms) - 1)
                        selected_room = available_rooms[selected_index]

                    else:
                        selected_room = available_rooms[selected_index]

                    # choose exit
                    passages_work = passages[selected_room].copy()
                    available_exits = list(passages_work)
                    question = {"question type": "blue character power exit",
                                "data": available_exits,
                                "game state": game_state}
                    selected_index = ask_question_json(self, question)

                    # test
                    if selected_index not in range(len(available_exits)):
                        warning_message = (
                            ' !  : selected exit not available '
                            'Choosing random exit.'
                        )
                        logger.warning(warning_message)
                        selected_exit = passages_work.pop()

                    else:
                        selected_exit = available_exits[selected_index]

                    logger.info(f"question : {question['question type']}")
                    logger.info("answer : " +
                                str({selected_room, selected_exit}))
                    game.blocked = {selected_room, selected_exit}
                    game.blocked_list = list(game.blocked)
        return [charact]
예제 #15
0
    def move(self, charact, moved_character, blocked, game_state, game):
        """
            Select a new position for the character.
        """

        # get the number of characters in the same room
        characters_in_room = [
            q for q in game.characters if q.position == charact.position
        ]
        number_of_characters_in_room = len(characters_in_room)

        # get the available rooms from a given position
        available_rooms = list()
        available_rooms.append(self.get_adjacent_positions(charact, game))
        for step in range(1, number_of_characters_in_room):
            # build rooms that are a distance equal to step+1
            next_rooms = list()
            for room in available_rooms[step - 1]:
                next_rooms += self.get_adjacent_positions_from_position(
                    room, charact, game)
            available_rooms.append(next_rooms)

        # flatten the obtained list
        temp = list()
        for sublist in available_rooms:
            for room in sublist:
                temp.append(room)

        # filter the list in order to keep an unique occurrence of each room
        temp = set(temp)
        available_positions = list(temp)

        # ensure the character changes room
        if charact.position in available_positions:
            available_positions.remove(charact.position)

        # if the character is purple and the power has
        # already been used, we pass since it was already moved
        # (the positions were swapped)
        if charact.color == "purple" and charact.power_activated:
            pass
        else:
            question = {
                "question type": "select position",
                "data": available_positions,
                "game state": game_state
            }
            selected_index = ask_question_json(self, question)

            # test
            if selected_index not in range(len(available_positions)):
                warning_message = (' !  : selected position not available '
                                   'Choosing random position.')
                logger.warning(warning_message)
                selected_position = choice(available_positions)

            else:
                selected_position = available_positions[selected_index]

            logger.info(f"question : {question['question type']}")
            logger.info(f"answer : {selected_position}")
            logger.info(f"new position : {selected_position}")

            # it the character is brown and the power has been activated
            # we move several characters with him
            if charact.color == "brown" and charact.power_activated:
                charact.position = selected_position
                if moved_character:
                    moved_character.position = selected_position
            else:
                charact.position = selected_position
예제 #16
0
    def __init__(self, players: List[Player]):
        # Todo: Should be self.players: Tuple[Player] = (player_1, player_2)
        self.players = players
        self.position_carlotta = 4  # position on the exit path
        # Todo: Should be removed and make the game ends when carlotta reach 0.
        self.exit = 22
        self.num_tour = 1
        # Todo: Lock should always block the hallway between the room
        #  occupied by the blue character pawn Madame Giry and the adjacent
        #  room clockwise.
        x: int = randrange(10)
        self.blocked = tuple((x, passages[x].copy().pop()))
        # Todo: Should be a Dict[enum, Character]
        self.characters = set({Character(color) for color in colors})
        # character_cards are used to draw 4 characters at the beginning
        # of each round
        self.character_cards = list(self.characters)
        self.active_cards = list()
        self.alibi_cards = self.character_cards.copy()
        self.fantom = choice(self.alibi_cards)
        # Todo: Should be placed in a logger section of the __init__()
        logger.info("the fantom is " + self.fantom.color)
        self.alibi_cards.remove(self.fantom)
        self.alibi_cards.extend(['fantom'] * 3)

        # log
        logger.info("\n=======\nnew game\n=======")
        # Todo: 1 Should be removed
        logger.info(f"shuffle {len(self.character_cards)} character_cards")
        # Todo: 2 Should be removed
        logger.info(f"shuffle {len(self.alibi_cards)} alibi cards")
        # work
        # Todo: 1 Should be removed
        shuffle(self.character_cards)
        # Todo: 2 Should be removed
        shuffle(self.alibi_cards)
        # Todo:
        #   rooms_number = list(range(10))
        #   start_rooms = rooms_number[:5] + rooms_number[7:]
        #   for c in self.characters:
        #       c.position = random.choice(start_rooms)
        #       start_rooms.remove(c.position)
        for i, p in enumerate(self.character_cards):
            p.position = i

        for character in self.characters:
            # get position of grey character
            if character.color == "grey":
                grey_character_position = character.display()["position"]
                self.shadow = grey_character_position

        self.characters_display = [
            character.display() for character in self.characters
        ]

        # Todo: should be removed
        self.character_cards_display = [
            tile.display() for tile in self.character_cards
        ]
        self.active_cards_display = [
            tile.display() for tile in self.active_cards
        ]

        self.game_state = {
            "position_carlotta": self.position_carlotta,
            "exit": self.exit,
            "num_tour": self.num_tour,
            "shadow": self.shadow,
            "blocked": self.blocked,
            "characters": self.characters_display,
            # Todo: should be removed
            "character_cards": self.character_cards_display,
            "active character_cards": self.active_cards_display,
        }
예제 #17
0

def init_connexion():
    while len(clients) != 2:
        link.listen(2)
        (clientsocket, addr) = link.accept()
        logger.info("Received client !")
        clients.append(clientsocket)
        clientsocket.settimeout(10)


if __name__ == '__main__':
    players = [Player(0), Player(1)]
    scores = []

    logger.info("no client yet")
    init_connexion()
    logger.info("received all clients")

    # profiling
    pr = cProfile.Profile()
    pr.enable()

    game = Game(players)
    game.lancer()

    link.close()

    # profiling
    pr.disable()
    # stats_file = open("{}.txt".format(os.path.basename(__file__)), 'w')
예제 #18
0
    def __init__(self, players: List[Player]):
        # Todo: Should be self.players: Tuple[Player] = (player_1, player_2)
        self.players = players
        self.position_carlotta = 4  # position on the exit path
        # Todo: Should be removed and make the game ends when carlotta reach 0.
        self.exit = 22
        self.num_tour = 1
        # Todo: Shadow should always be placed on Joseph Buquet at the
        #  beginning of a game.
        self.shadow = randrange(10)
        # Todo: Lock should always block the hallway between the room
        #  occupied by the blue character pawn Madame Giry and the adjacent
        #  room clockwise.
        x: int = randrange(10)
        # Todo: Should be a Tuple[int]
        self.blocked = {x, passages[x].copy().pop()}
        # Todo: Unused variable, should be removed
        self.blocked_list = list(self.blocked)
        # Todo: Should be a Dict[enum, Character]
        self.characters = set({Character(color) for color in colors})
        # tiles are used to draw 4 characters at the beginning
        # of each round
        # tile means 'tuile'
        # Todo: 1 Should be rename character_cards
        self.tiles = list(self.characters)
        # Todo: Should be rename active_cards
        self.active_tiles = list()
        # Todo: Should be rename alibi_cards, declared as a List[Character]
        #  and instanciated with self.tiles.copy()
        self.cards = self.tiles[:]
        # Todo: 2 Should use random.choice() and simplified as
        #  self.fantom = random.choice(self.cards)
        self.fantom = self.cards[randrange(8)]
        # Todo: Should be placed in a logger section of the __init__()
        logger.info("the fantom is " + self.fantom.color)
        self.cards.remove(self.fantom)
        # Todo: Should be replaced by self.cards.extend(['fantom'] * 3)
        self.cards += ["fantom"] * 3

        # log
        logger.info("\n=======\nnew game\n=======")
        # Todo: 1 Should be removed
        logger.info(f"shuffle {len(self.tiles)} tiles")
        # Todo: 2 Should be removed
        logger.info(f"shuffle {len(self.cards)} cards")
        # work
        # Todo: 1 Should be removed
        shuffle(self.tiles)
        # Todo: 2 Should be removed
        shuffle(self.cards)
        # Todo:
        #   rooms_number = list(range(10))
        #   start_rooms = rooms_number[:5] + rooms_number[7:]
        #   for c in self.characters:
        #       c.position = random.choice(start_rooms)
        #       start_rooms.remove(c.position)
        for i, p in enumerate(self.tiles):
            p.position = i

        self.characters_display = [
            character.display() for character in self.characters
        ]
        # Todo: should be removed
        self.tiles_display = [tile.display() for tile in self.tiles]
        self.active_tiles_display = [
            tile.display() for tile in self.active_tiles
        ]

        self.game_state = {
            "position_carlotta": self.position_carlotta,
            "exit": self.exit,
            "num_tour": self.num_tour,
            "shadow": self.shadow,
            "blocked": self.blocked_list,
            "characters": self.characters_display,
            # Todo: should be removed
            "tiles": self.tiles_display,
            "active tiles": self.active_tiles_display,
        }