Example #1
0
    def getLegalActions(self, state):
        legalActions = []
        lastCard = state.getSaidLastCard()
        if lastCard == None:
            lastCardNum ,var = featureExtractors.centerMass(state.hands[self.index])
        else:
            lastCardNum = lastCard[1]
        currHand = state.hands[self.index]
        centerMassCard , var = featureExtractors.centerMass(currHand, lastCardNum)
        direction = featureExtractors.getSuggestion(lastCardNum, centerMassCard)
        if direction == 0: # if no direction preferred we will choose randomly
            direction = random.choice([1,-1])
        self.orderWorstCards(state , direction)
        
        #terminal state, no legal actions
        if currHand.getSize() == 0:
            return None 
        # add beginning state!!!
        
        

        legalNums = self.getLegalCardNums(lastCardNum)
        
        for i in range(min(currHand.getSize(),CARDS_PER_TURN) ): # so we don't attempt to play more than we have
            playSize = i+1
            for num in legalNums: # find moves for each legal num- if last card is 6, legal nums are 5 6 7
                realBL = []
                saidBL = []
                realGL = []
                saidGL = []
                realT = []
                saidT = []
                cards = currHand.getChoice(num , playSize)
                if cards != None:
                    for cardIndex in range(len(cards)):
                        realT.append(cards[cardIndex])
#                         saidT.append(cards[cardIndex])
                        if self.canLie: # True if we have cards that aren't in the legal cards group, meaning we can lie with them >:)
                            realGL.append(self.worstCards[cardIndex])
                            saidGL.append(cards[cardIndex])
                    legalActions.append( (realT , (len(realT) , num) ,self.index )  )
                    if self.canLie:
                        legalActions.append((realGL , (len(realGL),num) , self.index ) )
                else:
                    for cardIndex in range(playSize):
                        realBL.append(self.worstCards[cardIndex])
                        saidBL.append((1,num))
                        
                    legalActions.append((realBL , (len(realBL),num) , self.index))
                    
                    
            #################
            
        self.bbLogger.debug( "[getLegalActions] - returning legal action list "+str( legalActions))    
        return legalActions
Example #2
0
    def orderWorstCards(self,state, prefDirection): 
        self.worstCards = []
        
        lastCard = state.getSaidLastCard()
        if lastCard == None:
            lastCardNum ,var = featureExtractors.centerMass(state.hands[self.index])
        else:
            lastCardNum = lastCard[1]
        pivot = (lastCardNum+ (NUM_CARD_TYPES/2))% NUM_CARD_TYPES +1
#         print "pivot is ", pivot
        
        legalNums = self.getLegalCardNums(lastCardNum)
#         print"legal nums are",legalNums
            
        lieNumbers = deque([num+1 for num in range(NUM_CARD_TYPES) if (num+1) not in legalNums])
        for i in range(len(lieNumbers)-1):
            if (abs(lieNumbers[i+1]-lieNumbers[i])%NUM_CARD_TYPES) != 1:
                break
        
        midCard = lieNumbers[i]
#         print "midCard , i are ",midCard,i
#         print "[orderWorstCards] lie numbers are", lieNumbers
        if prefDirection == -1:
            lieNumbers = deque(reversed(lieNumbers))
            legalNums = reversed(legalNums)
        while lieNumbers[0] != pivot:
            lieNumbers.rotate(1)
#         print "[orderWorstCards] rotated lie numbers are", lieNumbers
        newMidIndex = list(lieNumbers).index(midCard)
        lowerHalf = list(lieNumbers)[:newMidIndex+prefDirection] # correction to index if we prefer up direction
        upperHalf = reversed(list(lieNumbers)[newMidIndex+prefDirection:])
        orderedLieNums = lowerHalf+list(upperHalf)+list(legalNums)
#         print "[orderWorstCards] orderedLieNums are", orderedLieNums
        
        self.canLie = False
        for cardNum in orderedLieNums:
#             print" card Num is ",cardNum
            cardsList = state.hands[self.index].getAllOfCardsNum(cardNum)
            if cardsList != None:
                self.worstCards += cardsList
                if cardNum not in legalNums: # we do have a card 
                    self.canLie = True
                    
         
         
        self.bbLogger.debug( "[orderWorstCards] worstCardList is" + str( self.worstCards))