Exemplo n.º 1
0
	def naiveMinAIPlay(self):
		#get list of valid cards
		gameState = self.gameState
		validClubs = []
		validDiamonds = []
		validSpades = []
		validHearts = []

		validHand = [validClubs, validDiamonds, validSpades, validHearts]
		for suit in range(0,4):
			handSuit = self.hand.hand[suit]
			for card in handSuit:
				if gameState.isValidCard(card,self):
 					validHand[suit].append(card)

 		# self.print_hand(validHand)

		#if first, play lowest card in a random suit
		if gameState.currentTrick.isUnset():
			# print("Going first!")
			#include hearts if hearts not broken or only has hearts
			if gameState.heartsBroken == True or self.hasOnlyHearts():
			  suitRange = 3
			else:
				suitRange = 2
			randomSuit = randint(0,suitRange)
			#return lowest card
			# print("Current trick suit is: ", gameState.currentTrick.suit.iden)
			# print("Going first and playing lowest card")
			return Hand.lowestCard(validHand[randomSuit])
		#if not first:
		else:
			# print("Not going first!")
			trickSuit = gameState.currentTrick.suit.iden
			#if there are cards in the trick suit play lowest card in trick suit
			if(len(validHand[trickSuit]) > 0):
				# print("Still cards in trick suit")
				return Hand.lowestCard(validHand[trickSuit])
			else:
				# print("No cards in trick suit")

				#play cards by points, followed by rank
				maxPoints = -sys.maxsize
				maxCard = None
				for suit in range(0,4):
					for card in validHand[suit]:
						cardPoints = card.rank.rank
						if card.suit == Suit(hearts):
							cardPoints += 15 #Greater than rank of all non-point cards
						if card.suit == Suit(spades) and card.rank == Rank(queen):
							cardPoints += 13
						if cardPoints > maxPoints:
							maxPoints = cardPoints
							maxCard = card
				return maxCard
Exemplo n.º 2
0
    def addCard(self, card):
        if card.suit == Suit(clubs):
            self.clubs.append(card)
        elif card.suit == Suit(diamonds):
            self.diamonds.append(card)
        elif card.suit == Suit(spades):
            self.spades.append(card)
        elif card.suit == Suit(hearts):
            self.hearts.append(card)
        else:
            print 'Invalid card'

        if self.size() == 13:
            for suit in self.hand:
                suit.sort()
Exemplo n.º 3
0
 def __init__(self):
     self.trick = [0, 0, 0, 0]
     self.suit = Suit(-1)
     self.cardsInTrick = 0
     self.points = 0
     self.highest = 0  # rank of the high trump suit card in hand
     self.winner = -1
Exemplo n.º 4
0
    def isValidCard(self, card, player):
        if card is None:
            return False

        # if it is not the first trick and no cards have been played:
        if self.trickNum != 0 and self.currentTrick.cardsInTrick == 0:
            if card.suit == Suit(hearts) and not self.heartsBroken:
                if not player.hasOnlyHearts():
                    return False
        #Can't play hearts or queen of spades on first hand
        if self.trickNum == 0:
            if card.suit == Suit(hearts):
                return False
            elif card.suit == Suit(spades) and card.rank == Rank(queen):
                return False
        return True
Exemplo n.º 5
0
	def naiveMaxAIPlay(self): 
		gameState = self.gameState
		validClubs = []
		validDiamonds = []
		validSpades = []
		validHearts = []

		validHand = [validClubs, validDiamonds, validSpades, validHearts]
		for suit in range(0,4):
			handSuit = self.hand.hand[suit]
			for card in handSuit:
				if gameState.isValidCard(card,self):
 					validHand[suit].append(card)

		#if first, play highest card in a random suit
		if gameState.currentTrick.isUnset():
			if gameState.heartsBroken == True or self.hasOnlyHearts():
			  suitRange = 3
			else:
				suitRange = 2
			randomSuit = randint(0,suitRange)
			return Hand.highestCard(validHand[randomSuit])
		#if not first:
		else:
			# print("Not going first!")
			trickSuit = gameState.currentTrick.suit.iden
			#if there are cards in the trick suit play highest card in trick suit
			if(len(validHand[trickSuit]) > 0):
				# print("Still cards in trick suit")
				return Hand.highestCard(validHand[trickSuit])
			else:
				# print("No cards in trick suit")

				#play cards by points, followed by rank
				minPoints = sys.maxsize
				minCard = None
				for suit in range(0,4):
					for card in validHand[suit]:
						cardPoints = -card.rank.rank
						if card.suit == Suit(hearts):
							cardPoints -= 15 #Greater than rank of all non-point cards
						if card.suit == Suit(spades) and card.rank == Rank(queen):
							cardPoints -= 13
						if cardPoints < minPoints:
							minPoints = cardPoints
							minCard = card
				return minCard
Exemplo n.º 6
0
	def addCard(self, card):
		if card.suit == Suit(clubs):
			if card.rank.rank == 2:
				self.contains2ofclubs = True
			self.clubs.append(card)
		elif card.suit == Suit(diamonds):
			self.diamonds.append(card)
		elif card.suit == Suit(spades):
			self.spades.append(card)
		elif card.suit == Suit(hearts):
			self.hearts.append(card)
		else:
			print( 'Invalid card')

		if self.size() == 13:
			for suit in self.hand:
				suit.sort()
Exemplo n.º 7
0
 def new_deck(self):
     STD_DECK = [
         self.card_type(j, Suit(i)) for i in range(4) for j in range(13)
     ]
     total_deck = STD_DECK * self.numofDeck
     random.shuffle(total_deck)
     # NOTICE when you return a list
     return total_deck[:]
Exemplo n.º 8
0
    def addCard(self, card, index):  # 出了一张牌或者初始化的时候,更新状态

        if self.cardsInTrick == 0:  # if this is the first card added, set the trick suit
            self.setTrickSuit(card)

        self.trick[index] = card
        self.cardsInTrick += 1

        if card.suit == Suit(hearts):
            self.points += 1
        elif card == Card(queen, spades):
            self.points += 13

        if card.suit == self.suit:
            if card.rank.rank > self.highest:
                self.highest = card.rank.rank
                self.winner = index
Exemplo n.º 9
0
	def addCard(self, card, index):
		if self.cardsInTrick == 0: # if this is the first card added, set the trick suit
			self.setTrickSuit(card)
			print 'Current trick suit:', self.suit
		
		self.trick[index] = card
		self.cardsInTrick += 1

		if card.suit == Suit(hearts):
			self.points += 1
		elif card == Card(queen, spades):
			self.points += 13

		if card.suit == self.suit:
			if card.rank.rank > self.highest:
				self.highest = card.rank.rank
				self.winner = index
				print "Highest:",self.highest
Exemplo n.º 10
0
    def playTrick(self, start):
        shift = 0
        if self.trickNum == 0:
            startPlayer = self.players[start]
            addCard = startPlayer.play(self, option="play", c='2c')
            startPlayer.removeCard(addCard)

            self.currentTrick.addCard(addCard, start)

            shift = 1  # alert game that first player has already played

        # have each player take their turn
        for i in range(start + shift, start + len(self.players)):
            self.printCurrentTrick()
            curPlayerIndex = i % len(self.players)
            self.printPlayer(curPlayerIndex)
            curPlayer = self.players[curPlayerIndex]
            addCard = None

            while addCard is None:  # wait until a valid card is passed

                addCard = curPlayer.play(
                    self)  # change auto to False to play manually
                # print('addCard', addCard)

                # the rules for what cards can be played
                # card set to None if it is found to be invalid
                if addCard is not None:

                    # if it is not the first trick and no cards have been played,
                    # set the first card played as the trick suit if it is not a heart
                    # or if hearts have been broken
                    if self.trickNum != 0 and self.currentTrick.cardsInTrick == 0:
                        if addCard.suit == Suit(
                                hearts) and not self.heartsBroken:
                            # if player only has hearts but hearts have not been broken,
                            # player can play hearts
                            if not curPlayer.hasOnlyHearts():
                                print(curPlayer.hasOnlyHearts())
                                print(curPlayer.hand.__str__())
                                print("Hearts have not been broken.")
                                addCard = None
                            else:
                                self.currentTrick.setTrickSuit(addCard)
                        else:
                            self.currentTrick.setTrickSuit(addCard)

                    # player tries to play off suit but has trick suit
                    if addCard is not None and addCard.suit != self.currentTrick.suit:
                        if curPlayer.hasSuit(self.currentTrick.suit):
                            print("Must play the suit of the current trick.")
                            addCard = None
                        elif addCard.suit == Suit(hearts):
                            self.heartsBroken = True

                    if self.trickNum == 0:
                        if addCard is not None:
                            if addCard.suit == Suit(hearts):
                                print(
                                    "Hearts cannot be broken on the first hand."
                                )
                                self.heartsBroken = False
                                addCard = None
                            elif addCard.suit == Suit(
                                    spades) and addCard.rank == Rank(queen):
                                print(
                                    "The queen of spades cannot be played on the first hand."
                                )
                                addCard = None

                    if addCard is not None and self.currentTrick.suit == Suit(
                            noSuit):
                        if addCard.suit == Suit(
                                hearts) and not self.heartsBroken:
                            print("Hearts not yet broken.")
                            addCard = None

                    if addCard is not None:
                        if addCard == Card(queen, spades):
                            self.heartsBroken = True
                        curPlayer.removeCard(addCard)

            self.currentTrick.addCard(addCard, curPlayerIndex)

        self.evaluateTrick()
        self.trickNum += 1
Exemplo n.º 11
0
    def finishTrick(self, start, action):
        print("finishTrick start")
        if self.trickNum >= totalTricks:
            print('end game')
            return
        playersLeft = len(self.players) - self.currentTrick.cardsInTrick
        # have each player take their turn
        for i in range(start, start + playersLeft):
            # print("playerNum: ", i)
            # self.printCurrentTrick()
            curPlayerIndex = i % len(self.players)
            # self.printPlayer(curPlayerIndex)
            curPlayer = self.players[curPlayerIndex]
            addCard = None

            while addCard is None:  # wait until a valid card is passed
                if i == 0:
                    addCard = action
                else:
                    addCard = curPlayer.play(
                        self)  # change auto to False to play manually

                # the rules for what cards can be played
                # card set to None if it is found to be invalid
                if addCard is not None:

                    # if it is not the first trick and no cards have been played,
                    # set the first card played as the trick suit if it is not a heart
                    # or if hearts have been broken
                    if self.trickNum != 0 and self.currentTrick.cardsInTrick == 0:
                        if addCard.suit == Suit(
                                hearts) and not self.heartsBroken:
                            # if player only has hearts but hearts have not been broken,
                            # player can play hearts
                            if not curPlayer.hasOnlyHearts():
                                # print(curPlayer.hasOnlyHearts())
                                # print(curPlayer.hand.__str__())
                                print("Hearts have not been broken.")
                                addCard = None
                            else:
                                self.currentTrick.setTrickSuit(addCard)
                        else:
                            self.currentTrick.setTrickSuit(addCard)

                    # player tries to play off suit but has trick suit
                    if addCard is not None and addCard.suit != self.currentTrick.suit:
                        if curPlayer.hasSuit(self.currentTrick.suit):
                            print("Must play the suit of the current trick.")
                            addCard = None
                        elif addCard.suit == Suit(hearts):
                            self.heartsBroken = True

                    if self.trickNum == 0:
                        if addCard is not None:
                            if addCard.suit == Suit(hearts):
                                print(
                                    "Hearts cannot be broken on the first hand."
                                )
                                self.heartsBroken = False
                                addCard = None
                            elif addCard.suit == Suit(
                                    spades) and addCard.rank == Rank(queen):
                                print(
                                    "The queen of spades cannot be played on the first hand."
                                )
                                addCard = None

                    if addCard is not None and self.currentTrick.suit == Suit(
                            noSuit):
                        if addCard.suit == Suit(
                                hearts) and not self.heartsBroken:
                            print("Hearts not yet broken.")
                            addCard = None

                    if addCard is not None:
                        if addCard == Card(queen, spades):
                            self.heartsBroken = True
                        curPlayer.removeCard(addCard)

            self.currentTrick.addCard(addCard, curPlayerIndex)
            print('all players played')
        self.evaluateTrick(printOut=False)
        self.trickNum += 1
Exemplo n.º 12
0
	def isValid(self, addCard, state, game):
		spades = 2
		noSuit = 0
		hearts = 3
		queen = 12
		# print("curr suit", game.currentTrick.suit)
		curPlayer = self
		if addCard is not None:		
			# if it is not the first trick and no cards have been played,
			# set the first card played as the trick suit if it is not a heart
			# or if hearts have been broken
			if game.trickNum != 0 and game.currentTrick.cardsInTrick == 0:
				if addCard.suit == Suit(hearts) and not game.heartsBroken:
					# if player only has hearts but hearts have not been broken,
					# player can play hearts
					if not curPlayer.hasOnlyHearts():
						# print(curPlayer.hasOnlyHearts())
						# print(curPlayer.hand.__str__())
						# print("Hearts have not been broken.")
						addCard = None
						return False
					# else:
						# game.currentTrick.setTrickSuit(addCard)
				# else:
					# game.currentTrick.setTrickSuit(addCard)
			if game.currentTrick.suit == Suit(-1):
				return True
			# player tries to play off suit but has trick suit
			
			if addCard is not None and addCard.suit != game.currentTrick.suit:
				if curPlayer.hasSuit(game.currentTrick.suit):
					# print('invalid 2', addCard)
					# print("Must play the suit of the current trick.")
					addCard = None
					return False
				elif addCard.suit == Suit(hearts):
					game.heartsBroken = True

			if game.trickNum == 0:
				if addCard is not None:
					if addCard.suit == Suit(hearts):
						# print("Hearts cannot be broken on the first hand.")
						game.heartsBroken = False
						addCard = None
						return False
					elif addCard.suit == Suit(spades) and addCard.rank == Rank(queen):
						# print("The queen of spades cannot be played on the first hand.")
						addCard = None
						return False

			if addCard is not None and game.currentTrick.suit == Suit(noSuit):
				if addCard.suit == Suit(hearts) and not game.heartsBroken:
					# print("Hearts not yet broken.")
					addCard = None
					return False


			if addCard is not None:
				return True
			else:
				return False
Exemplo n.º 13
0
    def playSimTrick(self, start, rollout):
        shift = 0
        if rollout.trickNum == 0:
            startPlayer = rollout.players[start]
            # only try to play the 2 of clubs if it hasn't actually been played yet
            if not startPlayer.hand.containsCard(2, 0) is None:
                addCard = startPlayer.play(option="play",
                                           c='2c',
                                           state=rollout)
                startPlayer.removeCard(addCard)
                rollout.currentTrick.addCard(addCard, start)

            shift = 1  # alert game that first player has already played

        # have each player take their turn
        for i in range(start + shift, start + len(rollout.players)):
            curPlayerIndex = i % len(rollout.players)
            curPlayer = rollout.players[curPlayerIndex]

            # if this is the first move of the simulated game, we must play
            # the card associated with the node we're testing
            if (self.firstMove and curPlayerIndex == 0):
                addCard = self.node.card
                #print("FIRST MOVE playing " + str(addCard) + " and I am " + curPlayer.name)
                curPlayer.removeCard(addCard)
                self.firstMove = False
                rollout.currentTrick.addCard(addCard, curPlayerIndex)
            elif curPlayer.hand.size() > 0:
                addCard = None

                while addCard is None:  # wait until a valid card is passed

                    addCard = curPlayer.play(
                        auto=auto,
                        state=rollout)  # change auto to False to play manually

                    # the rules for what cards can be played
                    # card set to None if it is found to be invalid
                    if addCard is not None:

                        # if it is not the first trick and no cards have been played,
                        # set the first card played as the trick suit if it is not a heart
                        # or if hearts have been broken
                        if rollout.trickNum != 0 and rollout.currentTrick.cardsInTrick == 0:
                            if addCard.suit == Suit(
                                    hearts) and not rollout.heartsBroken:
                                # if player only has hearts but hearts have not been broken,
                                # player can play hearts
                                if not curPlayer.hasOnlyHearts():
                                    addCard = None
                                else:
                                    rollout.currentTrick.setTrickSuit(addCard)
                            else:
                                rollout.currentTrick.setTrickSuit(addCard)

                        # player tries to play off suit but has trick suit
                        if addCard is not None and addCard.suit != rollout.currentTrick.suit:
                            if curPlayer.hasSuit(rollout.currentTrick.suit):
                                addCard = None
                            elif addCard.suit == Suit(hearts):
                                rollout.heartsBroken = True

                        if rollout.trickNum == 0:
                            if addCard is not None:
                                if addCard.suit == Suit(hearts):
                                    rollout.heartsBroken = False
                                    addCard = None
                                elif addCard.suit == Suit(
                                        spades) and addCard.rank == Rank(
                                            queen):
                                    addCard = None

                        if addCard is not None and rollout.currentTrick.suit == Suit(
                                noSuit):
                            if addCard.suit == Suit(
                                    hearts) and not rollout.heartsBroken:
                                addCard = None

                        if addCard is not None:
                            if addCard == Card(queen, spades):
                                rollout.heartsBroken = True
                            curPlayer.removeCard(addCard)

                rollout.currentTrick.addCard(addCard, curPlayerIndex)

        rollout.evaluateTrick(rollout)
        rollout.trickNum += 1