示例#1
0
    async def _exchange(self, extra_cards: CardList) -> CardList:
        await asyncio.sleep(get_time_for_move())
        cards = self._cards + extra_cards
        random.shuffle(cards)
        return_cards = [cards.pop(), cards.pop()]
        self._cards = CardList(cards)

        return return_cards
示例#2
0
 def decorate(g, p):
     from cards import CardList
     p.cards          = CardList(p, 'cards')       # Cards in hand
     p.showncards     = CardList(p, 'showncards')  # Cards which are shown to the others, treated as 'Cards in hand'
     p.equips         = CardList(p, 'equips')      # Equipments
     p.fatetell       = CardList(p, 'fatetell')    # Cards in the Fatetell Zone
     p.special        = CardList(p, 'special')     # used on special purpose
     p.showncardlists = [p.showncards, p.fatetell]
     p.tags           = defaultdict(int)
 def __init__(self):
     self.root = Tk()
     self.screen = None
     self.player = None
     self.computer = None
     self.current_screen = None
     self.card_list = CardList("cards")
示例#4
0
 def __init__(self):
     """Create a new 15 cards deck."""
     cards = [Duke(i) for i in range(3)]
     cards += [Assassin(i) for i in range(3)]
     cards += [Ambassador(i) for i in range(3)]
     cards += [Captain(i) for i in range(3)]
     cards += [Contessa(i) for i in range(3)]
     self.cards = CardList(cards)
示例#5
0
    async def exchange(self, deck: Deck):
        card_1 = deck.draw_card()
        card_2 = deck.draw_card()
        current_num_cards = len(self._cards)
        return_cards = await self._exchange(CardList([card_1, card_2]))

        deck.return_cards(return_cards)
        deck.shuffle()

        if current_num_cards != len(self._cards):
            raise RuntimeError("Number of cards returned from _exchange " +
                               f"is inequivalent to {current_num_cards}")
    def __init__(self, master, call_on_next, player1, player2):
        super(Screen_war, self).__init__(master)

        # Save the method reference to which we return control after this page Exits.
        self.call_on_selected = call_on_next
        self.player1 = player1
        self.player2 = player2
        self.deck = CardList("cards")
        self.create_widgets()
        self.grid()
        self.ppoints = 0
        self.cpoints = 0
        self.deck.shuffle()
        self.p1list = []
        self.p2list = []
        self.deck.shuffle()
        int = 0
        for c in self.deck.card_list:
            if int % 2 == 0:
                self.p1list.append(c)
            elif int % 2 == 1:
                self.p2list.append(c)
            self.deck.card_list.remove(c)
            int += 1
示例#7
0
def uniform_counter_action(cards: CardList, action: Action,
                           source: Player) -> CounterAction:
    prob_BLOCKFOREIGNAID = 1
    prob_BLOCKSTEAL = 1
    prob_BLOCKASSASS = 1

    if action == Action.FOREIGNAID:
        if cards.has("Duke"):
            prob_BLOCKFOREIGNAID = 0.6
        else:
            prob_BLOCKFOREIGNAID = 0.2

        actions = [CounterAction.BLOCKFOREIGNAID, None]
        probs = [prob_BLOCKFOREIGNAID, 1 - prob_BLOCKFOREIGNAID]

    if action == Action.STEAL:
        if cards.has("Captain") or cards.has("Ambassador"):
            prob_BLOCKSTEAL = 0.9
        else:
            prob_BLOCKSTEAL = 0.3

        actions = [CounterAction.BLOCKSTEAL, None]
        probs = [prob_BLOCKSTEAL, 1 - prob_BLOCKSTEAL]

    if action == Action.ASSASS:
        if cards.has("Contessa") and len(cards) == 1:
            prob_BLOCKASSASS = 1.0
        elif cards.has("Contessa") and len(cards) == 2:
            prob_BLOCKASSASS = 0.9
        else:
            prob_BLOCKASSASS = 0.3

        actions = [CounterAction.BLOCKASSASS, None]
        probs = [prob_BLOCKASSASS, 1 - prob_BLOCKASSASS]

    return random.choices(actions, weights=probs)[0]
示例#8
0
    async def __call__(self):
        logger.info("Game starting.")
        self.deck.shuffle()

        for player in self.players:
            player.coins = 2
            player.cards = CardList(
                [self.deck.draw_card(),
                 self.deck.draw_card()])

        while True:
            self.n += 1
            await self.turn()
            print(str(game))

            # Finalize game
            if len(self.players) == 1:
                logger.info(f"Player {self.players[0]} has won the game!")
                return
示例#9
0
    def decorate(self, p):
        from cards import CardList
        p.cards = CardList(p, 'cards')  # Cards in hand
        p.showncards = CardList(
            p, 'showncards'
        )  # Cards which are shown to the others, treated as 'Cards in hand'
        p.equips = CardList(p, 'equips')  # Equipments
        p.fatetell = CardList(p, 'fatetell')  # Cards in the Fatetell Zone
        p.faiths = CardList(p, 'faiths')  # 'faith' cards
        p.special = CardList(p, 'special')  # used on special purpose

        p.showncardlists = [p.showncards, p.faiths,
                            p.fatetell]  # cardlists should shown to others

        p.tags = defaultdict(int)
        p.tags['faithcounter'] = True  # for ui
示例#10
0
def binary_search(request):
    wp = jp.QuasarPage()

    size = int(request.query_params.get('size', 50))

    consigne = jp.parse_html(html_consigne(), a=wp)
    container = jp.Div(classes="container q-ma-md", a=wp)

    if size > MAX_CARDS_NUM:
        error_msg = f"Indiquez un nombre de cartes inférieur à {MAX_CARDS_NUM}"
        error_message = jp.QBanner(a=wp,
                                   text=error_msg,
                                   classes="bg-red-4 text-white")
        return wp

    a = randint(0, 3000)
    nb_groups = randint(5, 10)
    distribution = [randint(0, 800) for _ in range(nb_groups)]
    # distribution = [5, 30, 60, 4, 15, 70]
    values = generate_random_numbers(a, size, distribution)
    CardList(values=values, a=wp, classes="q-ma-xs q-pa-sm")

    return wp
class Screen_war(Frame):
    def __init__(self, master, call_on_next, player1, player2):
        super(Screen_war, self).__init__(master)

        # Save the method reference to which we return control after this page Exits.
        self.call_on_selected = call_on_next
        self.player1 = player1
        self.player2 = player2
        self.deck = CardList("cards")
        self.create_widgets()
        self.grid()
        self.ppoints = 0
        self.cpoints = 0
        self.deck.shuffle()
        self.p1list = []
        self.p2list = []
        self.deck.shuffle()
        int = 0
        for c in self.deck.card_list:
            if int % 2 == 0:
                self.p1list.append(c)
            elif int % 2 == 1:
                self.p2list.append(c)
            self.deck.card_list.remove(c)
            int += 1

    def create_widgets(self):
        ''' This method creates all of the widgets for the battle page. '''
        Label(self, text="You", font="COMIC 10").grid(row=1, column=0)
        Label(self, text="Your Card", font="COMIC 10").grid(row=1, column=1)

        Label(self, text="Computer", font="COMIC 10").grid(row=1, column=3)
        Label(self, text="Computer's Card", font="COMIC 10").grid(row=1,
                                                                  column=2)

        self.next_button = Button(self,
                                  text="Click To Draw",
                                  font="Helvetica 20",
                                  fg="black",
                                  bg="red",
                                  command=self.round)

        self.next_button.grid(row=3, column=4, columnspan=4, sticky=N)

        self.pLabel = Label(self, text="Points:", font="COMIC 14")
        self.pLabel.grid(row=0, column=4)

        self.ppoint_label = Label(self, text="You:", font="COMIC 15")
        self.ppoint_label.grid(row=1, column=4, sticky=S)

        self.cpoint_label = Label(self, text="Computer:", font="COMIC 15")
        self.cpoint_label.grid(row=2, column=4)

    def round(self):
        image = PhotoImage(file='cardBack.png')
        self.back1 = Label(self, image=image)
        self.back1.photo = image
        self.back1.grid(row=2, column=0, sticky=W)

        image = PhotoImage(file="cardBack.png")
        self.back2 = Label(self, image=image)
        self.back2.photo = image
        self.back2.grid(row=2, column=3, sticky=E)

        self.p1card = self.p1list[0]
        image = PhotoImage(file=('cardImages/' + self.p1card.image))
        p1 = Label(self, image=image)
        p1.photo = image
        p1.grid(row=2, column=1)

        self.p2card = self.p2list[0]
        image = PhotoImage(file=('cardImages/' + self.p2card.image))
        p2 = Label(self, image=image)
        p2.photo = image
        p2.grid(row=2, column=2)
        if self.p1card.value == 14 and self.p2card.value == 2:
            self.cpoints += 1
            self.cpoint_label['text'] = "Computer:", self.cpoints
            self.p2list.remove(self.p2card)
            self.p2list.append(self.p2card)
            self.p1list.remove(self.p1card)
            self.p2list.append(self.p1card)
        elif self.p2card.value == 14 and self.p1card == 2:
            self.ppoints += 1
            self.ppoint_label['text'] = "You:", self.ppoints
            self.p1list.remove(self.p1card)
            self.p1list.append(self.p1card)
            self.p2list.remove(self.p2card)
            self.p1list.append(self.p2card)
        else:
            if self.p1card.value > self.p2card.value:
                self.ppoints += 1
                self.ppoint_label['text'] = "You:", self.ppoints
                self.p1list.remove(self.p1card)
                self.p1list.append(self.p1card)
                self.p2list.remove(self.p2card)
                self.p1list.append(self.p2card)
            elif self.p1card.value < self.p2card.value:
                self.cpoints += 1
                self.cpoint_label['text'] = "Computer:", self.cpoints
                self.p2list.remove(self.p2card)
                self.p2list.append(self.p2card)
                self.p1list.remove(self.p1card)
                self.p2list.append(self.p1card)
            else:
                self.master.update()
                time.sleep(2)
                self.tie()
                self.master.update()
                time.sleep(1.5)
                self.p1.destroy()
                self.p2.destroy()
                self.p3.destroy()
                self.p4.destroy()
                self.p5.destroy()
                self.p6.destroy()
                self.p7.destroy()
                self.p8.destroy()
        if self.ppoints >= 12:
            self.master.update()
            time.sleep(1.25)
            self.call_on_selected("p")
        if self.cpoints >= 12:
            self.master.update()
            time.sleep(1.25)
            self.call_on_selected("c")

    def tie(self):

        self.back1.destroy()
        self.back2.destroy()

        p1c1 = self.p1list[0]
        self.p1list.remove(p1c1)
        image = PhotoImage(file=('cardBack.png'))
        self.p1 = Label(self, image=image)
        self.p1.photo = image
        self.p1.grid(row=2, column=0)

        p1c2 = self.p1list[1]
        self.p1list.remove(p1c2)
        image = PhotoImage(file=('cardBack.png'))
        self.p2 = Label(self, image=image)
        self.p2.photo = image
        self.p2.grid(row=2, column=1)

        p1c3 = self.p1list[2]
        self.p1list.remove(p1c3)
        image = PhotoImage(file=('cardBack.png'))
        self.p3 = Label(self, image=image)
        self.p3.photo = image
        self.p3.grid(row=2, column=2)

        p2c1 = self.p2list[0]
        self.p2list.remove(p2c1)
        image = PhotoImage(file=('cardBack.png'))
        self.p4 = Label(self, image=image)
        self.p4.photo = image
        self.p4.grid(row=3, column=0)

        p2c2 = self.p2list[1]
        self.p2list.remove(p2c2)
        image = PhotoImage(file=('cardBack.png'))
        self.p5 = Label(self, image=image)
        self.p5.photo = image
        self.p5.grid(row=3, column=1)

        p2c3 = self.p2list[2]
        self.p2list.remove(p2c3)
        image = PhotoImage(file=('cardBack.png'))
        self.p6 = Label(self, image=image)
        self.p6.photo = image
        self.p6.grid(row=3, column=2)

        p1cM = self.p1list[3]
        self.p1list.remove(p1cM)
        image = PhotoImage(file=('cardImages/' + p1cM.image))
        self.p7 = Label(self, image=image)
        self.p7.photo = image
        self.p7.grid(row=2, column=3)

        p2cM = self.p2list[3]
        self.p2list.remove(p2cM)
        image = PhotoImage(file=('cardImages/' + p2cM.image))
        self.p8 = Label(self, image=image)
        self.p8.photo = image
        self.p8.grid(row=3, column=3)

        if p1cM.value > p2cM.value:
            self.ppoints += 4
            self.master.update()
            time.sleep(1)
            self.ppoint_label['text'] = "You:", self.ppoints
            self.p1list.append(p1c1)
            self.p1list.append(p1c2)
            self.p1list.append(p1c3)
            self.p1list.append(p1cM)
            self.p1list.append(p2c1)
            self.p1list.append(p2c2)
            self.p1list.append(p2c3)
            self.p1list.append(p2cM)
        elif p1cM.value < p2cM.value:
            self.cpoints += 4
            self.master.update()
            time.sleep(1)
            self.cpoint_label['text'] = "Computer:", self.cpoints
            self.p2list.append(p1c1)
            self.p2list.append(p1c2)
            self.p2list.append(p1c3)
            self.p2list.append(p1cM)
            self.p2list.append(p2c1)
            self.p2list.append(p2c2)
            self.p2list.append(p2c3)
            self.p2list.append(p2cM)
        else:
            self.tie()
示例#12
0
    def game_start(self):
        # game started, init state

        from cards import Card, Deck, CardList

        self.deck = Deck()

        self.ehclasses = []

        for i, p in enumerate(self.players):
            p.cards = CardList(p, 'handcard')  # Cards in hand
            p.showncards = CardList(
                p, 'showncard'
            )  # Cards which are shown to the others, treated as 'Cards in hand'
            p.equips = CardList(p, 'equips')  # Equipments
            p.fatetell = CardList(p, 'fatetell')  # Cards in the Fatetell Zone
            p.special = CardList(p, 'special')  # used on special purpose

            p.showncardlists = [p.showncards, p.fatetell]

            p.tags = defaultdict(int)

            p.dead = False
            p.identity = Identity()
            p.identity.type = (Identity.TYPE.HAKUREI,
                               Identity.TYPE.MORIYA)[i % 2]

        # choose girls -->
        from characters import characters as chars
        from characters.akari import Akari

        _chars = self.random.sample(chars, 10)
        if Game.SERVER_SIDE:
            choice = [
                CharChoice(cls, cid)
                for cls, cid in zip(_chars[-10:], xrange(10))
            ]

            for c in self.random.sample(choice, 4):
                c.real_cls = c.char_cls
                c.char_cls = Akari

        elif Game.CLIENT_SIDE:
            choice = [CharChoice(None, i) for i in xrange(10)]

        # -----------

        self.players.reveal(choice)

        # roll
        roll = range(len(self.players))
        self.random.shuffle(roll)
        pl = self.players
        roll = sync_primitive(roll, pl)

        roll = [pl[i] for i in roll]

        self.emit_event('game_roll', roll)

        first = roll[0]
        second = roll[1]

        self.emit_event('game_roll_result', first)
        # ----

        # akaris = {}  # DO NOT USE DICT! THEY ARE UNORDERED!
        akaris = []
        self.emit_event('choose_girl_begin', (self.players, choice))

        A, B = first, second
        order = [A, B, B, A, A, B, B, A, A, B]
        A.choices = []
        B.choices = []
        del A, B

        for i, p in enumerate(order):
            cid = p.user_input('choose_girl', choice, timeout=10)
            try:
                check(isinstance(cid, int))
                check(0 <= cid < len(choice))
                c = choice[cid]
                check(not c.chosen)
                c.chosen = p
            except CheckFailed:
                # first non-chosen char
                for c in choice:
                    if not c.chosen:
                        c.chosen = p
                        break

            if issubclass(c.char_cls, Akari):
                akaris.append((p, c))

            p.choices.append(c)

            self.emit_event('girl_chosen', c)

        self.emit_event('choose_girl_end', None)

        # reveal akaris for themselves
        for p, c in akaris:
            c.char_cls = c.real_cls
            p.reveal(c)

        for p in self.players:
            perm = range(5)
            self.random.shuffle(perm)
            perm = sync_primitive(perm, p)
            p._perm = perm

        def process(p, input):
            try:
                check(input)
                check_type([int, Ellipsis], input)
                check(len(set(input)) == len(input))
                check(all(0 <= i < 5 for i in input))
                p._perm_input = input
            except CheckFailed:
                p._perm_input = range(5)

        self.players.user_input_all('kof_sort_characters', process, None, 30)

        for p in self.players:
            perm = p.choices
            perm = [perm[i] for i in p._perm]
            perm = [perm[i] for i in p._perm_input[:3]]
            p.characters = [c.char_cls for c in perm]
            del p.choices

        self.next_character(first)
        self.next_character(second)

        self.update_event_handlers()

        try:
            pl = self.players
            for p in pl:
                self.process_action(RevealIdentity(p, pl))

            self.emit_event('game_begin', self)

            for p in pl:
                self.process_action(
                    DrawCards(p, amount=3 if p is second else 4))

            for i, p in enumerate(cycle([second, first])):
                if i >= 6000: break
                if p.dead:
                    assert p.characters  # if not holds true, DeathHandler should end game.
                    KOFCharacterSwitchHandler.do_switch()

                assert not p.dead

                try:
                    self.emit_event('player_turn', p)
                    self.process_action(PlayerTurn(p))
                except InterruptActionFlow:
                    pass

        except GameEnded:
            pass
示例#13
0
    def game_start(self):
        # game started, init state
        from cards import Card, Deck, CardList

        self.deck = Deck()

        ehclasses = list(action_eventhandlers) + self.game_ehs.values()

        for i, p in enumerate(self.players):
            p.cards = CardList(p, 'handcard')  # Cards in hand
            p.showncards = CardList(
                p, 'showncard'
            )  # Cards which are shown to the others, treated as 'Cards in hand'
            p.equips = CardList(p, 'equips')  # Equipments
            p.fatetell = CardList(p, 'fatetell')  # Cards in the Fatetell Zone
            p.special = CardList(p, 'special')  # used on special purpose

            p.showncardlists = [p.showncards, p.fatetell]

            p.tags = defaultdict(int)

            p.dead = False
            p.identity = Identity()
            p.identity.type = (Identity.TYPE.HAKUREI,
                               Identity.TYPE.MORIYA)[i % 2]

        self.forces = forces = BatchList([PlayerList(), PlayerList()])
        for i, p in enumerate(self.players):
            f = i % 2
            p.force = f
            forces[f].append(p)

        # choose girls -->
        from characters import characters as chars
        from characters.akari import Akari

        if Game.SERVER_SIDE:
            choice = [
                CharChoice(cls, cid)
                for cls, cid in zip(self.random.sample(chars, 16), xrange(16))
            ]

            for c in self.random.sample(choice, 4):
                c.real_cls = c.char_cls
                c.char_cls = Akari

        elif Game.CLIENT_SIDE:
            choice = [CharChoice(None, i) for i in xrange(16)]

        # -----------

        self.players.reveal(choice)

        # roll
        roll = range(len(self.players))
        self.random.shuffle(roll)
        pl = self.players
        roll = sync_primitive(roll, pl)

        roll = [pl[i] for i in roll]

        self.emit_event('game_roll', roll)

        first = roll[0]

        self.emit_event('game_roll_result', first)
        # ----

        first_index = self.players.index(first)
        n = len(self.order_list)
        order = [self.players[(first_index + i) % n] for i in self.order_list]

        def mix(p, c):
            # mix char class with player -->
            mixin_character(p, c.char_cls)
            p.skills = list(p.skills)  # make it instance variable
            p.life = p.maxlife
            ehclasses.extend(p.eventhandlers_required)

        # akaris = {}  # DO NOT USE DICT! THEY ARE UNORDERED!
        akaris = []
        self.emit_event('choose_girl_begin', (self.players, choice))
        for i, p in enumerate(order):
            cid = p.user_input('choose_girl', choice, timeout=(n - i + 1) * 5)
            try:
                check(isinstance(cid, int))
                check(0 <= cid < len(choice))
                c = choice[cid]
                check(not c.chosen)
                c.chosen = p
            except CheckFailed:
                # first non-chosen char
                for c in choice:
                    if not c.chosen:
                        c.chosen = p
                        break

            if issubclass(c.char_cls, Akari):
                akaris.append((p, c))
            else:
                mix(p, c)

            self.emit_event('girl_chosen', c)

        self.emit_event('choose_girl_end', None)

        # reveal akaris
        if akaris:
            for p, c in akaris:
                c.char_cls = c.real_cls

            self.players.reveal([i[1] for i in akaris])

            for p, c in akaris:
                mix(p, c)

        first_actor = first

        self.event_handlers = EventHandler.make_list(ehclasses)

        # -------
        log.info(u'>> Game info: ')
        log.info(u'>> First: %s:%s ', first.char_cls.__name__,
                 Identity.TYPE.rlookup(first.identity.type))
        for p in self.players:
            log.info(u'>> Player: %s:%s %s', p.char_cls.__name__,
                     Identity.TYPE.rlookup(p.identity.type),
                     p.account.username)

        # -------

        try:
            pl = self.players
            for p in pl:
                self.process_action(RevealIdentity(p, pl))

            self.emit_event('game_begin', self)

            for p in self.players:
                self.process_action(
                    DrawCards(p, amount=3 if p is first_actor else 4))

            pl = self.players.rotate_to(first_actor)

            for i, p in enumerate(cycle(pl)):
                if i >= 6000: break
                if not p.dead:
                    self.emit_event('player_turn', p)
                    try:
                        self.process_action(PlayerTurn(p))
                    except InterruptActionFlow:
                        pass

        except GameEnded:
            pass

        log.info(u'>> Winner: %s',
                 Identity.TYPE.rlookup(self.winners[0].identity.type))
示例#14
0
 def replace(self, card_name: str, deck: Deck):
     deck.return_cards(CardList([self.get(card_name)]))
     deck.shuffle()
     self._cards.append(deck.draw_card())
示例#15
0
 async def lose_influence(self, discard_pile: CardList) -> int:
     card = await self._lose_influence()
     self.logger.info(f"Lost a {card.name} influence")
     discard_pile.append(card)
     return len(self._cards)
示例#16
0
    def game_start(g):
        # game started, init state
        from cards import Card, CardList

        g.action_types[LaunchCard] = RaidLaunchCard
        g.action_types[ActionStageLaunchCard] = RaidActionStageLaunchCard

        ehclasses = g.ehclasses = []

        for p in g.players:
            p.cards = CardList(p, 'handcard')  # Cards in hand
            p.showncards = CardList(p, 'showncard')  # Cards which are shown to the others, treated as 'Cards in hand'
            p.equips = CardList(p, 'equips')  # Equipments
            p.fatetell = CardList(p, 'fatetell')  # Cards in the Fatetell Zone
            p.faiths = CardList(p, 'faiths')  # 'faith' cards
            p.special = CardList(p, 'special')  # used on special purpose

            p.showncardlists = [p.showncards, p.faiths, p.fatetell]  # cardlists should shown to others

            p.tags = defaultdict(int)
            p.tags['faithcounter'] = True

            p.dead = False

        # reveal identities
        mutant = g.mutant = g.players[0]
        attackers = g.attackers = PlayerList(g.players[1:])

        mutant.identity = Identity()
        mutant.identity.type = Identity.TYPE.MUTANT

        g.process_action(RevealIdentity(mutant, g.players))

        for p in attackers:
            p.identity = Identity()
            p.identity.type = Identity.TYPE.ATTACKER

            g.process_action(RevealIdentity(p, g.players))

        # choose girl init
        chosen_girls = []
        pl = PlayerList(g.players)
        def process(p, cid):
            try:
                check(isinstance(cid, int))
                check(0 <= cid < len(choice))
                c = choice[cid]
                if c.chosen:
                    raise ValueError

                c.chosen = p
                chosen_girls.append(c)
                g.emit_event('girl_chosen', c)
                pl.remove(p)
                return c

            except CheckFailed:
                raise ValueError

        # mutant's choose
        from characters import ex_characters as ex_chars

        choice = [CharChoice(cls, cid) for cid, cls in enumerate(ex_chars)]

        g.emit_event('choose_girl_begin', ([mutant], choice))
        PlayerList([mutant]).user_input_all('choose_girl', process, choice, timeout=5)

        if not chosen_girls:
            # didn't choose
            c = choice[0]
            c.chosen = mutant
            g.emit_event('girl_chosen', c)
            pl.remove(mutant)
        else:
            c = chosen_girls.pop()

        assert c.chosen is mutant

        # mix it in advance
        # so the others could see it

        mixin_character(mutant, c.char_cls)
        mutant.skills = list(mutant.skills)  # make it instance variable
        ehclasses.extend(mutant.eventhandlers_required)

        mutant.life = mutant.maxlife
        mutant.morphed = False

        g.emit_event('choose_girl_end', None)

        # init deck & mutant's initial equip
        # (SinsackCard, SPADE, 1)
        # (SinsackCard, HEART, Q)
        from cards import Deck, SinsackCard, card_definition
        raid_carddef = [
            carddef for carddef in card_definition
            if carddef[0] is not SinsackCard
        ]

        g.deck = Deck(raid_carddef)

        # attackers' choose
        from characters import characters as chars

        if Game.SERVER_SIDE:
            choice = [
                CharChoice(cls, cid) for cid, cls in
                enumerate(g.random.sample(chars, 16))
            ]

        elif Game.CLIENT_SIDE:
            choice = [
                CharChoice(None, i)
                for i in xrange(16)
            ]

        # -----------

        g.players.reveal(choice)
        g.emit_event('choose_girl_begin', (attackers, choice))
        attackers.user_input_all('choose_girl', process, choice, timeout=30)
        g.emit_event('choose_girl_end', None)

        # if there's any person didn't make a choice -->
        if pl:
            choice = [c for c in choice if not c.chosen]
            sample = sync_primitive(
                g.random.sample(xrange(len(choice)), len(pl)), g.players
            )
            for p, i in zip(pl, sample):
                c = choice[i]
                c.chosen = p
                chosen_girls.append(c)
                g.emit_event('girl_chosen', c)

        # mix char class with player -->
        for c in chosen_girls:
            p = c.chosen
            mixin_character(p, c.char_cls)
            p.skills = list(p.skills)  # make it instance variable
            p.skills.extend([
                Cooperation, Protection,
                Parry, OneUp,
            ])
            p.life = p.maxlife
            ehclasses.extend(p.eventhandlers_required)

        g.update_event_handlers()

        g.emit_event('game_begin', g)

        # -------
        log.info(u'>> Game info: ')
        log.info(u'>> Mutant: %s', mutant.char_cls.__name__)
        for p in attackers:
            log.info(u'>> Attacker: %s', p.char_cls.__name__)

        # -------

        try:
            g.process_action(DrawCards(mutant, amount=6))
            for p in attackers:
                g.process_action(DrawCards(p, amount=4))

            # stage 1
            try:
                for i in xrange(500):
                    g.process_action(CollectFaith(mutant, mutant, 1))

                    g.emit_event('round_start', False)
                    for p in attackers:
                        p.tags['action'] = True

                    while True:
                        avail = PlayerList([p for p in attackers if p.tags['action'] and not p.dead])
                        if not avail:
                            break

                        p, _ = avail.user_input_any(
                            tag='choose_option',
                            expects=lambda p, data: bool(data),
                            attachment=RequestAction,
                            timeout=15,
                        )

                        p = p or avail[0]

                        p.tags['action'] = False
                        try:
                            g.process_action(PlayerTurn(p))
                        except InterruptActionFlow:
                            pass

                        try:
                            g.process_action(PlayerTurn(mutant))
                        except InterruptActionFlow:
                            pass

            except MutantMorph:
                pass

            # morphing
            stage1 = mutant.__class__
            stage2 = stage1.stage2

            for s in stage1.skills:
                try:
                    mutant.skills.remove(s)
                except ValueError:
                    pass

            mutant.skills.extend(stage2.skills)

            ehclasses = g.ehclasses
            for s in stage1.eventhandlers_required:
                try:
                    ehclasses.remove(s)
                except ValueError:
                    pass

            ehclasses.extend(stage2.eventhandlers_required)

            g.process_action(
                MaxLifeChange(mutant, mutant, -(stage1.maxlife // 2))
            )
            mutant.morphed = True

            mixin_character(mutant, stage2)

            g.update_event_handlers()

            for p in attackers:
                g.process_action(CollectFaith(p, p, 1))

            g.process_action(DropCards(mutant, mutant.fatetell))

            g.emit_event('mutant_morph', mutant)

            g.pause(4)

            # stage 2
            for i in xrange(500):
                g.process_action(CollectFaith(mutant, mutant, 1))

                g.emit_event('round_start', False)
                for p in attackers:
                    p.tags['action'] = True

                try:
                    g.process_action(PlayerTurn(mutant))
                except InterruptActionFlow:
                    pass

                while True:
                    avail = PlayerList([p for p in attackers if p.tags['action'] and not p.dead])
                    if not avail:
                        break

                    p, _ = avail.user_input_any(
                        tag='choose_option',
                        expects=lambda p, data: bool(data),
                        attachment=RequestAction,
                        timeout=15,
                    )

                    p = p or avail[0]

                    p.tags['action'] = False
                    try:
                        g.process_action(PlayerTurn(p))
                    except InterruptActionFlow:
                        pass

        except GameEnded:
            pass

        winner_force = 'Mutant' if g.winners == [mutant] else 'Attackers'
        log.info(u'>> Winner: %s', winner_force)
示例#17
0
    def game_start(g):
        # game started, init state
        from cards import Card, Deck, CardList

        g.deck = Deck()

        ehclasses = list(action_eventhandlers) + _game_ehs.values()

        np = g.n_persons

        for p in g.players:
            p.cards = CardList(p, 'handcard') # Cards in hand
            p.showncards = CardList(p, 'showncard') # Cards which are shown to the others, treated as 'Cards in hand'
            p.equips = CardList(p, 'equips') # Equipments
            p.fatetell = CardList(p, 'fatetell') # Cards in the Fatetell Zone
            p.special = CardList(p, 'special') # used on special purpose

            p.showncardlists = [p.showncards, p.fatetell] # cardlists should shown to others

            p.tags = defaultdict(int)

            p.dead = False

        # choose girls init -->
        from characters import characters as chars

        if Game.SERVER_SIDE:
            choice = [
                CharChoice(cls, cid)
                for cls, cid in zip(g.random.sample(chars, 3*np+2), xrange(3*np+2))
            ]
        elif Game.CLIENT_SIDE:
            choice = [
                CharChoice(None, i)
                for i in xrange(3*np+2)
            ]

        chosen_girls = []
        pl = PlayerList(g.players)
        def process(p, cid):
            try:
                retry = p._retry
            except AttributeError:
                retry = 3

            retry -= 1
            try:
                check(isinstance(cid, int))
                i = p._choose_tag
                if p is boss:
                    assert p is pl[-1]
                    check(i*3 <= cid< (i+1)*3+2)
                else:
                    check(i*3 <= cid < (i+1)*3)
                c = choice[cid]
                if c.chosen and retry > 0:
                    p._retry = retry
                    raise ValueError
                c.chosen = p
                chosen_girls.append(c)
                g.emit_event('girl_chosen', c)
                pl.remove(p)
                return c

            except CheckFailed as e:
                return None

            finally:
                try:
                    del p._retry
                    del p._choose_tag
                except AttributeError:
                    pass

        # choose boss
        idx = sync_primitive(g.random.randrange(len(g.players)), g.players)
        pl[-1], pl[idx] = pl[idx], pl[-1]
        boss = g.boss = pl[-1]

        boss.identity = Identity()
        boss.identity.type = Identity.TYPE.BOSS

        g.process_action(RevealIdentity(boss, g.players))

        for i, p in enumerate(pl):
            p._choose_tag = i
            p.reveal(choice[i*3:(i+1)*3])

        boss.reveal(choice[-2:])

        g.emit_event('choose_girl_begin', ([boss], choice))
        PlayerList([boss]).user_input_all('choose_girl', process, choice, timeout=30)

        if not chosen_girls:
            # didn't choose
            offs = sync_primitive(g.random.randrange(5), g.players)
            c = choice[(len(pl)-1)*3+offs]
            c.chosen = boss
            g.emit_event('girl_chosen', c)
            pl.remove(boss)
        else:
            c = chosen_girls.pop()

        assert c.chosen is boss

        g.players.reveal(c)

        # mix it in advance
        # so the others could see it

        mixin_character(boss, c.char_cls)
        boss.skills = list(boss.skills) # make it instance variable
        ehclasses.extend(boss.eventhandlers_required)

        # boss's hp bonus
        if len(g.players) > 5:
            boss.maxlife += 1

        boss.life = boss.maxlife

        g.emit_event('choose_girl_end', None)

        # reseat
        opl = g.players
        loc = range(len(opl))
        g.random.shuffle(loc)
        loc = sync_primitive(loc, opl)
        npl = opl[:]
        for i, l in zip(range(len(opl)), loc):
            npl[i] = opl[l]
        g.players[:] = npl

        g.emit_event('reseat', None)

        # tell the others their own identity
        il = list(g.identities)
        g.random.shuffle(il)
        for p in g.players.exclude(boss):
            p.identity = Identity()
            id = il.pop()
            if Game.SERVER_SIDE:
                p.identity.type = id
            g.process_action(RevealIdentity(p, p))

        pl = g.players.exclude(boss)
        g.emit_event('choose_girl_begin', (pl, choice))
        pl.user_input_all('choose_girl', process, choice, timeout=30)
        g.emit_event('choose_girl_end', None)

        # now you can have them all.
        g.players.reveal(choice)

        # if there's any person didn't make a choice -->
        # FIXME: this can choose girl from the other force!
        if pl:
            choice = [c for c in choice if not c.chosen]
            sample = sync_primitive(
                g.random.sample(xrange(len(choice)), len(pl)), g.players
            )
            for p, i in zip(pl, sample):
                c = choice[i]
                c.chosen = p
                chosen_girls.append(c)
                g.emit_event('girl_chosen', c)

        # mix char class with player -->
        for c in chosen_girls:
            p = c.chosen
            mixin_character(p, c.char_cls)
            p.skills = list(p.skills)  # make it instance variable
            p.life = p.maxlife
            ehclasses.extend(p.eventhandlers_required)

        g.event_handlers = EventHandler.make_list(ehclasses)

        g.emit_event('game_begin', g)

        try:
            for p in g.players:
                g.process_action(DrawCards(p, amount=4))

            pl = g.players.rotate_to(boss)

            for i, p in enumerate(cycle(pl)):
                if i >= 6000: break
                if not p.dead:
                    try:
                        g.process_action(PlayerTurn(p))
                    except InterruptActionFlow:
                        pass

        except GameEnded:
            pass
示例#18
0
 def __init__(self, players: Sequence[Player]):
     logger.info(f"Game is set up with {players}")
     self.players = players
     self.deck = Deck()
     self.discard_pile = CardList()
     self.n = 0
示例#19
0
class RandomPlayer(Player):
    async def _lose_influence(self) -> Card:
        await asyncio.sleep(get_time_for_move())
        random.shuffle(self._cards)
        return self._cards.pop()

    async def _exchange(self, extra_cards: CardList) -> CardList:
        await asyncio.sleep(get_time_for_move())
        cards = self._cards + extra_cards
        random.shuffle(cards)
        return_cards = [cards.pop(), cards.pop()]
        self._cards = CardList(cards)

        return return_cards

    async def _counter_action(self, action: Action,
                              source: Player) -> CounterAction:
        await asyncio.sleep(get_time_for_move())
        return uniform_counter_action(self._cards, action, source)

    async def _proactive_action(self,
                                players: Sequence[Player]) -> (Action, Player):
        await asyncio.sleep(get_time_for_move())
        return self.uniform_proactive_action(players)

    async def _maybe_challenge(self, source, action: Action) -> bool:
        await asyncio.sleep(get_time_for_challange())
        return random.choices([True, False], [0.1, 0.9])[0]

    def uniform_proactive_action(self, players: Sequence[Player]) -> Action:
        VALID_FACTOR = 2

        prob_INCOME = 1
        prob_FOREIGNAID = 1
        prob_COUP = 1
        prob_TAX = 1
        prob_ASSASS = 1
        prob_EX = 1
        prob_STEAL = 1

        if self._cards.has("Duke"):
            prob_TAX *= VALID_FACTOR
        if self._cards.has("Assassin"):
            prob_ASSASS *= VALID_FACTOR
        if self._cards.has("Ambassador"):
            prob_EX *= VALID_FACTOR
        if self._cards.has("Captain"):
            prob_STEAL *= VALID_FACTOR

        probs = [
            prob_INCOME,
            prob_FOREIGNAID,
            prob_COUP,
            prob_TAX,
            prob_ASSASS,
            prob_EX,
            prob_STEAL,
        ]

        actions = [
            Action.INCOME,
            Action.FOREIGNAID,
            Action.COUP,
            Action.TAX,
            Action.ASSASS,
            Action.EXCHANGE,
            Action.STEAL,
        ]

        while True:
            action = random.choices(actions, weights=probs)[0]
            target = (random.choice(players) if action
                      in [Action.COUP, Action.ASSASS, Action.STEAL] else None)
            try:
                check_legal_action(action, self, target)
            except IllegalActionError:
                continue
            except StopIteration:
                break

        return action, target