def init_cards(): # 建立全局变量,包括一副完整的牌,双方手牌,明牌和花色变量 global deck, p_hand, c_hand, up_card, active_suit # 建立一副牌 deck = [] for suit in range(1, 5): for rank in range(1, 14): new_card = Card(suit, rank) if rank == 8: new_card.value = 50 # 当遇到8时将其得分改为50 deck.append(new_card) # 玩家手牌 p_hand = [] for cards in range(0, 5): card = random.choice(deck) p_hand.append(card) deck.remove(card) # 电脑手牌 c_hand = [] for cards in range(0, 5): card = random.choice(deck) c_hand.append(card) deck.remove(card) # 创建第一张明牌并使用其花色 card = random.choice(deck) deck.remove(card) up_card = card active_suit = up_card.suit
def get_starting_hand(input): if input == 'r': return PokerHand([deck.deal(), deck.deal()]) else: print(input) card1 = Card(input.split()[0][0], input.split()[0][1]) card2 = Card(input.split()[1][0], input.split()[1][1]) deck.remove(card1) deck.remove(card2) return PokerHand([card1, card2])
def update_dir(name,make): main='../../../' os.system("cp ./ident_mw_card.dat "+main+"/Cards/") os.system("cp transfer_card.dat "+main+"/Cards/") os.system("cp data/transfer_card_"+name+".dat "+main+"/Cards/transfer_card.dat") os.system("cp data/transfer_card_"+name+".dat "+main+"/Cards/transfer_card_default.dat") if make: os.chdir(main+"/Source/") os.system("make") os.chdir('..') else: os.chdir(main) print os.getcwd() #charge card ident=Card('./Cards/ident_mw_card.dat') madweight=Card('./Cards/MadWeight_card.dat') transfer=Card('./Cards/transfer_card.dat') #create output madweight.create_include_file(ident,'./Source/madweight_card.inc') transfer.create_include_file(ident,'./Source/MadWeight_File/Transfer_Fct/transfer_card.inc') os.chdir('./Source/MadWeight_File/Transfer_Fct')
def test_order(self): c1 = Card(1, 1) c2 = Card(1, 1) c3 = Card(2, 2) self.assertTrue(c1 == c1) self.assertTrue(c1 == c2) self.assertTrue(c1 < c3) self.assertTrue(c1 <= c2) self.assertFalse(c1 == c3) self.assertFalse(c1 > c3) self.assertFalse(c1 >= c3)
def playCards(self, player, msglist): if not self.playing: player.tell("Wait for the game to start...") elif player != self.currentPlayer: player.tell("Wait for your turn...") elif len(msglist) == 1: player.tell("You have to play a card.") else: cards = msglist[1:] playedCards = [] for card in cards: card = Card(str(card[0]), str(card[1:])) playedCards.append(card) try: self.isValidPlay(self, playedCards, self.bufferDeck) try: player.playFromHand(playedCards, self.bufferDeck) self.showGUIHand(self.currentPlayer) self.broadcast(player.name + " has played " + str(self.bufferDeck.size()) + " card(s).") self.broadcast("They currently hold " + str(player.hand.size()) + " cards.") self.playFlag = True except NotInStackException: player.tell( "You can only play cards that are in your hand.") except ValueError: player.tell("You can only play cards that are in your hand.")
def get_deck(): deck = [] for suit in get_suits(): for rank in get_ranks(): card = Card(suit, rank) deck.append(card) return deck
def GenerateFullDeck(): deck = Deck() for i in range(2,15): for k in suitCodes.values(): deck.Add(Card(i,k)) return deck
def set_cards(self): used_locations = [] for word in self.card_options: for i in range(2): available_locations = set(self.locations) - set(used_locations) random_location = random.choice(list(available_locations)) used_locations.append(random_location) card = Card(word, random_location) self.cards.append(card)
def _ParseCard(driver, isAssociated=False): time.sleep(MobalyticsScraper._DELAY_BETWEEN_PARSE) associatedCards = [] cardsNumber = 1 i = 0 while i < cardsNumber: # check associated cards associatedBox = MobalyticsScraper._FindReqElements(driver, MobalyticsScraper._CARD_ASSOCIATED_BOX) childs = associatedBox[0].find_elements_by_css_selector('*') # waiting and get images card and all associated elements imgElements = MobalyticsScraper._FindReqElements(driver, MobalyticsScraper._CARD_IMG_CLASS) cardsNumber = max(len(childs), 1) # open next associated card and wait loading if len(childs) > 0: childs[i].click() if (i != 0): time.sleep(MobalyticsScraper._DELAY_BETWEEN_PARSE) parsedFields = {'abilities': []} parsedFields['title'] = driver.find_element_by_class_name(MobalyticsScraper._CARD_TITLE_CLASS).text parsedFields['title'] = parsedFields['title'].replace('> ', '') # get main info elements: region, type, rarity mainEls = driver.find_elements_by_class_name(MobalyticsScraper._CARD_MAIN_CLASS) # get stats info elements: health, attack statsEls = driver.find_elements_by_class_name(MobalyticsScraper._CARD_STATS_CLASS) # get abilities info abilitiesEls = driver.find_elements_by_class_name(MobalyticsScraper._CARD_ABILITIES_CLASS) # and filter it by data-sel-id elements = mainEls + statsEls + abilitiesEls for el in elements: id = el.get_attribute('data-sel-id') if id == 'cardType': parsedFields['type'] = el.text elif id == 'cardRegion': parsedFields['region'] = el.text elif id == 'cardRarity': parsedFields['rarity'] = el.text elif id == 'cardHealth': parsedFields['health'] = el.text elif id == 'cardAttack': parsedFields['attack'] = el.text elif id == 'keyword': parsedFields['abilities'].append(el.text) # get img link by associated img number i link = imgElements[i].get_attribute('src') parsedFields['img link'] = link cardId = link.split('/')[-1].split('.webp')[0] # get content with full description (for mana cost) elements = driver.find_elements_by_name('description') description = elements[0].get_attribute('content') parsedFields['mana cost'] = description.split('Mana Cost: ')[1] # add to cards list card = Card(id=cardId, params=parsedFields) associatedCards.append(card) i += 1 return associatedCards
def GenerateRandomDeck(): fullList = [] for i in range(2,15): for k in suitCodes.values(): fullList.append(Card(i,k)) deck = Deck() for i in range(0,52): deck.Add(fullList.pop(random.randrange(len(fullList)))) return deck
def playCards(self, player, msglist): cards = msglist[1:] playedCards = [] for card in cards: card = Card(str(card[0]),str(card[1:])) playedCards.append(card) if not self.playing: player.tell("Wait for the game to start...") elif player != self.currentPlayer: player.tell("Wait for your turn...") elif (len(msglist) != 3): player.tell("You must drop one card and pick up one card.") elif (playedCards[1] not in self.played.cards): player.tell("You must pick up one of the played cards.") elif (playedCards[0] not in player.hand.cards): player.tell("You must own the card you want to drop.") else: self.played.addToDeck(playedCards[0]) player.hand.cards.remove(playedCards[0]) player.addToHand(self.played.remove(playedCards[1])) self.broadcast(str(self.currentPlayer) + " dropped " + str(playedCards[0]) + " and picked up " + str(playedCards[1])) self.showGUIHand(self.currentPlayer) self.playFlag = True
def playCards(self, player, msglist): if not self.playing: player.tell("Wait for the game to start...") elif player != self.currentPlayer: player.tell("Wait for your turn...") elif (len(msglist) != 2): player.tell("You must play one card.") else: cards = msglist[1:] playedCards = [] for card in cards: card = Card(str(card[0]), str(card[1:])) playedCards.append(card) try: if playedCards[0].rank == self.played.lastCard( ).rank or playedCards[0].suit == self.played.lastCard().suit: player.playFromHand(playedCards, self.played) self.broadcast( str(self.currentPlayer) + " played " + str(playedCards[0])) self.showGUIHand(self.currentPlayer) # if playedCards[0].rank == '2': # self.nextPlayer.addToHand(self.deck.draw()) # self.nextPlayer.addToHand(self.deck.draw()) # self.skipNextTurn = True self.playFlag = True else: player.tell( "You cannot play this card! Play a card from the same suit or rank." ) return self.broadcast("They currently hold " + str(player.hand.size()) + " cards.") except NotInStackException: player.tell("You can only play cards that are in your hand.")
def pass_in_pid(self,process_line,multi): """ convert information in pid information """ ParticlesFile=Card('./Source/MODEL/particles.dat') pid=ParticlesFile.give_pid_dict() # # Update information with multi_particle tag # for couple in multi.items(): text=couple[1] tag=couple[0] pid_list=[] len_max=3 key_list=pid.keys() while text: text,add=self.first_part_pid(text,pid) pid_list+=add pid.update({tag:pid_list}) # # pid list is now complete # convert line in decay pid information decay_rule=[] #1) take only the decay part: for letter in ['$','\\','@','#','\n']: if letter in process_line: process_line=process_line[:process_line.index(letter)] break # only one such symbol to signify the end of the decay part process_line=process_line[process_line.index('>')+1:] decay_diag=[] level_decay=0 while process_line: if process_line[0]==' ': process_line=process_line[1:] continue if process_line[0]=='>': process_line=process_line[1:] continue if process_line[0]=='(': process_line=process_line[1:] level_decay+=1 new_decay=1 continue if process_line[0]==')': level_decay-=1 current_part=current_part.mother process_line=process_line[1:] continue process_line,pid_content=self.first_part_pid(process_line,pid) if level_decay==0 or (level_decay==1 and new_decay): new_decay=0 part=Proc_decay(pid_content) decay_diag.append(part) current_part=part elif new_decay: new_decay=0 part=current_part.add_desint(pid_content) #return new part current_part=part else: current_part.add_desint(pid_content) return decay_diag
def create_deck(self): colors = ['red', 'yellow', 'green', 'blue'] for color in colors: for symbol in range(1, 10): # 1 - 9 new_card = Card(color, symbol, action=None) self.deck.append(new_card) new_card = Card(color, symbol, action=None) self.deck.append(new_card) # 0 new_card = Card(color, 0, action=None) self.deck.append(new_card) # Draw 2 new_card = Card(color, None, action="draw2") self.deck.append(new_card) new_card = Card(color, None, action="draw2") self.deck.append(new_card) # Reverse new_card = Card(color, None, action="reverse") self.deck.append(new_card) new_card = Card(color, None, action="reverse") self.deck.append(new_card) # Skip new_card = Card(color, None, action="skip") self.deck.append(new_card) new_card = Card(color, None, action="skip") self.deck.append(new_card) # Wildcards new_card = Card(None, None, action="wildcard") self.deck.append(new_card) new_card = Card(None, None, action="wildcard4") self.deck.append(new_card) return self.deck
def decode_action_discard(action): """ Return the cards to be discarded from the action Action is a integer ranging from 0 to 347 """ discard = [] # find the cards behind the action number # 52(single)+78(double)+52(triple)+13(quadruple)+44(staight3)+40(staight4)+36(staight5)+32(6) # card ranges from 1 to 13, suit ranges from CDHS if action <= 52: # single action -= 1 rank = action % 13 + 1 suit = suits[int(action / 13)] discard = [Card(rank, suit)] elif action <= 130: # double action -= 53 rank = action % 13 + 1 suit1 = double_combination[int(action / 13)][0] suit2 = double_combination[int(action / 13)][1] discard = [Card(rank, suit1), Card(rank, suit2)] elif action <= 182: # triple action -= 131 rank = action % 13 + 1 suit1 = triple_combination[int(action / 13)][0] suit2 = triple_combination[int(action / 13)][1] suit3 = triple_combination[int(action / 13)][2] discard = [Card(rank, suit1), Card(rank, suit2), Card(rank, suit3)] elif action <= 195: # quadruple action -= 183 rank = action + 1 discard = [ Card(rank, "Clubs"), Card(rank, "Diamonds"), Card(rank, "Hearts"), Card(rank, "Spades") ] elif action <= 239: # straight of 3 action -= 196 suit = suits[int(action / 11)] rank = action % 11 + 1 discard = [ Card(rank, suit), Card(rank + 1, suit), Card(rank + 2, suit) ] elif action <= 279: # straight of 4 action -= 240 suit = suits[int(action / 10)] rank = action % 10 + 1 discard = [ Card(rank, suit), Card(rank + 1, suit), Card(rank + 2, suit), Card(rank + 3, suit) ] elif action <= 315: # straight of 5 action -= 280 suit = suits[int(action / 9)] rank = action % 9 + 1 discard = [ Card(rank, suit), Card(rank + 1, suit), Card(rank + 2, suit), Card(rank + 3, suit), Card(rank + 4, suit) ] elif action <= 347: # straight of 6 action -= 316 suit = suits[int(action / 8)] rank = action % 8 + 1 discard = [ Card(rank, suit), Card(rank + 1, suit), Card(rank + 2, suit), Card(rank + 3, suit), Card(rank + 4, suit), Card(rank + 5, suit) ] return discard
def cardValue(self, i) : c = Card(i) return c.value
def cardClub(self, i) : c = Card(i) return c.club()
def test_to_rannum(self): self.assertEqual(15, Card(1, 1).to_rannum())
def __init__(self, player1, player2, player3): self.players = [player1, player2, player3] self.current_player = 0 #0 represents player1, 1 represents player2 and 2 represents player3 self.game_over_value = False self.field = [] self.origDeck = [Card(3,'diamonds'),Card(3,'clubs'),Card(3,'hearts'),Card(3,'spades'), Card(4,'diamonds'),Card(4,'clubs'),Card(4,'hearts'),Card(4,'spades'), Card(5,'diamonds'),Card(5,'clubs'),Card(5,'hearts'),Card(5,'spades'), Card(6,'diamonds'),Card(6,'clubs'),Card(6,'hearts'),Card(6,'spades'), Card(7,'diamonds'),Card(7,'clubs'),Card(7,'hearts'),Card(7,'spades'), Card(8,'diamonds'),Card(8,'clubs'),Card(8,'hearts'),Card(8,'spades'), Card(9,'diamonds'),Card(9,'clubs'),Card(9,'hearts'),Card(9,'spades'), Card(10,'diamonds'),Card(10,'clubs'),Card(10,'hearts'),Card(10,'spades'), Card(11,'diamonds'),Card(11,'clubs'),Card(11,'hearts'),Card(11,'spades'), Card(12,'diamonds'),Card(12,'clubs'),Card(12,'hearts'),Card(12,'spades'), Card(13,'diamonds'),Card(13,'clubs'),Card(13,'hearts'),Card(13,'spades'), Card(14,'diamonds'),Card(14,'clubs'),Card(14,'hearts'),Card(14,'spades'), Card(15,'diamonds'),Card(15,'clubs'),Card(15,'hearts'),Card(15,'spades'), Card(16,'joker'),Card(17,'joker')] #The original deck of 54 cards including the jokers self.landlord = 0 # The landlord represented as an integer self.currentPlay = [] #Current play on the board self.turn = 1 #Turn number self.prevPlay = [] #Shuffle cards np.random.shuffle(self.origDeck) self.deck1 = self.origDeck[:18] self.deck1.sort(key = lambda x: x.value) self.deck2 = self.origDeck[18:36] self.deck2.sort(key = lambda x: x.value) self.deck3 = self.origDeck[36:52] self.deck3.sort(key = lambda x: x.value) self.hands = [self.deck1,self.deck2,self.deck3] #Hands for all of the players in the order of p1,p2,p3 #the hidden cards in the center of the table self.hidden_cards = self.origDeck[52:] #Get from parsing the Game
Player2 = 'HillClimbAI' Player3 = 'SimulatedAnnealingAI' game = DDZ(create_player(Player1, 1), create_player(Player2, 2), create_player(Player3, 3)) game.landlord = game.updateLandlord() game.hands[game.landlord] = game.hands[game.landlord] + game.hidden_cards game.update_game_state(game.move()) if __name__== '__main__': main() #tests hand = [ Card(4, "hearts"), Card(5, "diamonds"), Card(6,"spades"), Card(7, "hearts"), Card(8, "diamonds"), Card(9,"spades")] """ hand = [ Card(4, "hearts"), Card(5, "diamonds"), Card(6,"spades"), Card(7, "hearts"), Card(8, "diamonds"), Card(9,"spades"), Card(11, "hearts"), Card(11, "diamonds"), Card(12,"spades"), Card(13, "hearts"), Card(14, "diamonds"), Card(15,"spades"), Card(15, "hearts"), Card(16, "joker"), Card(17,"joker")] """ current_play = [Card(3,"clubs"), Card(3, "spades")] current_play = [Card(3,"clubs")] current_play = [] eai = ExpectiMiniMaxAI(1)
import random from Cards import Card from BasicRules import possibleCard packOfCards = [Card(i) for i in range(1, 53)] def Shuffle(pack): # pack is an array of cards shuffledPack = [] numberOfCards = len(pack) unshuffledPack = [i for i in range(0, numberOfCards)] shuffledIDs = [] for i in range(0, numberOfCards): nextCard = random.randint(0, len(unshuffledPack) - 1) shuffledIDs.append(unshuffledPack[nextCard]) unshuffledPack.remove(unshuffledPack[nextCard]) for id in shuffledIDs: shuffledPack.append(pack[id]) return shuffledPack UnusedCards = Shuffle(packOfCards) # Cards in the pile to be taken # To add to a stack use append, to remove use pop # shuffledPack has the top card at the end of the list
def testDeckRemove(self): deck = Deck() card23 = Card(2, 3) deck.remove(card23)