Ejemplo n.º 1
0
 def glory_possibility(self, card):
     '''Returns wether there is still a chance this card can produce glory'''
     color = card.color
     d1 = self.unknown_cards[0].filter_color(color)
     d2 = self.unknown_cards[1].filter_color(color)
     d3 = self.unknown_cards[2].filter_color(color)
     if not d1: d1 = [fake_card()]
     if not d2: d2 = [fake_card()]
     if not d3: d3 = [fake_card()]
     for a in d1:
         for b in d2:
             for c in d3:
                 if glory_calculation([a,b,c], self.trump): continue #disregard glory caused by other cards
                 g = glory_calculation([card, a, b, c], self.trump)
                 if g: return True
     return False
Ejemplo n.º 2
0
    def maxmin_glory_prime(self, cards, maximize = True, deck=None):
        '''Returns the card that maximizes glory, and gives the expected glory.
        If maximize is False then it minimizes glory'''
        mult = 1
        if maximize == False: mult = -1
        if not cards:
            color = deck[0].color
        else: color = cards[0].color
        if deck==None:
            poss = self.cards.filter_color(color)
        else:
            poss = Cards(deck).filter_color(color)

        glory = 0
        if not maximize: glory = 120
        best = poss[0]
        if len(cards) == 3:
            for c in poss:
                g = glory_calculation(cards + [c], self.trump)
                if mult*cmp(g,glory) == 1:
                    glory = g
                    best = c
                elif g == glory and c < best:
                    best = c
            return (best, glory)
        if len(cards) == 2:
            other = self.unknown_cards[0].filter_color(cards[0].color) # the cards of the next player
            for c in poss:
                if not other:
                    g = glory_calculation(cards + [c], self.trump)
                    if mult*cmp(g,glory) == 1:
                        glory = g
                        best = c
                    elif g == glory and c < best:
                        best = c
                else:
                    g = 0
                    for b in other:
                        g += glory_calculation(cards + [c,b], self.trump)
                    g = float(g)/len(other) # average glory
                    if mult*cmp(g,glory) == 1:
                        glory = g
                        best = c
                    elif g == glory and c < best:
                        best = c
            return (best, glory)
        if len(cards) == 1:
            other1 = self.unknown_cards[0].filter_color(cards[0].color)
            other2 = self.unknown_cards[0].filter_color(cards[0].color)
            if not other1:
                other1 = other2
                other2 = []
            if not other1 and not other2: #only possibility is stuk
                if maximize:
                    for c in poss:
                        g = glory_calculation(cards + [c], self.trump)
                        if g > glory: return (c, glory)
                else:
                    for c in poss:
                        g = glory_calculation(cards + [c], self.trump)
                        if g == 0: return (c,0)
                return (poss[0], 0)
            if not other2:
                for c in poss:
                    g = 0
                    for b in other1:
                        g += glory_calculation(cards + [c,b], self.trump)
                    g = float(g)/len(other1)
                    if mult*cmp(g,glory) == 1:
                        glory = g
                        best = c
                    elif g == glory and c < best:
                        best = c
                return (best, glory)
            for c in poss:
                g = 0
                for b in other1:
                    for a in other2:
                        g += glory_calculation(cards + [c,b,a], self.trump)
                g = float(g)/(len(other1)*len(other2))
                if mult*cmp(g,glory) == 1:
                    glory = g
                    best = c
                elif g == glory and c < best:
                    best = c
            return (best, glory)
Ejemplo n.º 3
0
    def show_trick(self,cards, round):
        '''Here we analyze the played cards, and try to conclude some information
        on the hands of the players'''
        color = cards[0].color
        p = cards[0].owner
        glory = glory_calculation(cards,self.trump)

        def remove_all_in_color(index, color, exceptions=[]):
            if index == self.index: return
            d = self.index_to_deck(index)
            for cc in  [a for a in d if (a.color == color and a.value not in exceptions)]:
                d.remove(cc)
        
        if p != self.index: #we did not start this round
            d = self.index_to_deck(p)
            if round == 1:
                if color != self.trump: #starting player didn't play a trump
                    if Card(JACK, self.trump) in d:
                        d.remove(Card(JACK, self.trump))
                else:
                    if cards[0].value == NINE:
                        if Card(JACK, self.trump) in d:
                            self.player_has_card(p, Card(JACK, self.trump))
                    if cards[0].value == KING:
                        if Card(QUEEN, self.trump) in d:
                            self.player_has_card(p, Card(QUEEN, self.trump))

            #if not self.player_is_playing(p): #player starting didn't play
            if cards[0].value == TEN and color != self.trump:
                if Card(ACE, color) in d:
                    self.player_has_card(p, Card(ACE, color))

        if color == self.trump and cards[0].value == JACK:
            for i in [1,3]:
                if cards[i] == Card(NINE, self.trump) and glory < 25:
                    remove_all_in_color(cards[i].owner, self.trump)
                if cards[i] == Card(QUEEN, self.trump):
                    if glory:
                        remove_all_in_color(cards[i].owner, self.trump, [NINE])
                        
                    
        if p == self.index and highest_card(cards).owner == self.index: #we are playing and have won
            m = cards[2] # the card of your mate
            if m.color != color and m.color != self.trump: #mate didn't confess color and didn't trump
                if m.value in (SEVEN, EIGHT, NINE, ACE): #mate is signing
                    if m.color not in self.mate_prefered_colors:
                        self.mate_prefered_colors.append(m.color)
                        self.pp("Mate signed card color")
                elif m.value in (QUEEN, KING, TEN): #mate is de-signing the color
                    if m.color not in self.mate_prefered_colors: #if it was already signed, ignore this
                        if len(self.mate_prefered_colors) == 0:
                            self.mate_prefered_colors = list(range(4))
                            self.mate_prefered_colors.remove(self.trump)
                            self.mate_prefered_colors.remove(m.color)
                    
        if color == self.trump: #trump asked
            highest = cards[0]
            for (i,c) in enumerate(cards[1:]):
                if c.color == self.trump:
                    if c > highest:
                        highest = c
                    else: #didn't overtrump, remove those possibilities
                        if c.owner == self.index: continue
                        d = self.index_to_deck(c.owner)
                        for cc in [a for a in d if (a.color == self.trump and a > c)]:
                            d.remove(cc)
                else: #couldn't confess color
                    if c.owner == self.index: continue
                    remove_all_in_color(c.owner, self.trump)
        else: #not a trump asked
            highest = cards[0]
            for (i, c) in enumerate(cards[1:]):
                if c.color == color:
                    if c > highest: highest = c
                else: #couldn't confess color
                    if c.owner == self.index: continue
                    d = self.index_to_deck(c.owner)
                    for cc in [a for a in d if (a.color == color)]:
                        d.remove(cc)
                    if highest.owner != self.index_to_mate(c.owner):
                        if c.color != self.trump: #he didn't trump in
                            if highest.color != self.trump:
                                for cc in [a for a in d if (a.color == self.trump)]:
                                    d.remove(cc)
                            else:
                                for cc in [a for a in d if (a.color == self.trump and a > highest)]:
                                    d.remove(cc)
        
        highest = highest_card(cards)
        if highest.owner != cards[3].owner: #last person didn't win the round
            if highest.owner != cards[1].owner: #his mate also didn't
                if cards[3].color == color: #he confessed color
                    if glory > 20: #at least 40 glory, it would always have been better to prevent it
                        remove_all_in_color(cards[3].owner, color)
                    if glory == 20 and color != self.trump: #he might have chosen not to play the TEN
                        remove_all_in_color(cards[3].owner, color, [TEN])

        if color != self.trump:
            a = Card(ACE, color)
            if Card(ACE, color) in self.mystery_cards: #we haven't seen the ACE yet
                if a not in cards and cards[0].value!=TEN: #it is not played in this round
                    if self.trump not in Cards(cards).colors(): #not trumped in anywhere
                        self.pp("No Ace played in this round. How weird")
                        if cards[0].owner != self.index:
                            d = self.index_to_deck(cards[0].owner)
                            if a in d: d.remove(a)
                        if cards[2].owner != self.index:
                            d = self.index_to_deck(cards[2].owner)
                            if a in d: d.remove(a)
            if cards[0] == a: #ACE has been played
                t = Card(TEN, color)
                if cards[1] == t: remove_all_in_color(cards[1].owner, color)
                if cards[3] == t: #last person played a TEN
                    values = Cards(cards).filter_color(color).values()
                    if QUEEN in values:
                        if KING in values: remove_all_in_color(cards[3].owner, color, [JACK])
                        else: remove_all_in_color(cards[3].owner, color, [KING])
                    elif KING in values: remove_all_in_color(cards[3].owner, color, [QUEEN])
                    elif SEVEN in values and EIGHT in values:
                        remove_all_in_color(cards[3].owner, color, [NINE])
                    elif SEVEN in values and NINE in values:
                        remove_all_in_color(cards[3].owner, color, [EIGHT])
                    else:
                        remove_all_in_color(cards[3].owner, color)
            
                                    
        self.remove_known_cards(cards)