def publish(self): self.context.dice = Dice() self.context.chest = Cards(communityChestCards) self.context.chance = Cards(chanceCards) if self.context.INITIAL_STATE == "DEFAULT": self.context.state = State(self.PLAY_ORDER) elif self.context.INITIAL_STATE == "TEST_BUY_HOUSES": properties = [Property(0,False,False,0,i) for i in range(NUMBER_OF_PROPERTIES)] agentOne = self.PLAY_ORDER[0] properties[6].owned = True properties[6].ownerId = agentOne properties[8].owned = True properties[8].ownerId = agentOne properties[9].owned = True properties[9].ownerId = agentOne agentTwo = self.PLAY_ORDER[1] properties[11].owned = True properties[11].ownerId = agentTwo properties[13].owned = True properties[13].ownerId = agentTwo properties[14].owned = True properties[14].ownerId = agentTwo self.context.state = State(self.PLAY_ORDER,properties) self.context.winner = None log("game","Game #"+str(self.context.gamesCompleted+1)+" started.") #Allow the agent to initialize state for a new game self.agentsYetToRespond = list(self.PLAY_ORDER) self.publishAction(None,"START_GAME_IN")
def publish(context): #reinitialize state for the new game context.winner = None context.dice = Dice() context.chest = Cards(communityChestCards) context.chance = Cards(chanceCards) if context.INITIAL_STATE == "DEFAULT": context.state = State(context.PLAY_ORDER) elif context.INITIAL_STATE == "TEST_BUY_HOUSES": properties = [Property(0,False,False,0,i) for i in range(NUMBER_OF_PROPERTIES)] agentOne = context.PLAY_ORDER[0] properties[6].ownerId = agentOne properties[8].ownerId = agentOne properties[9].ownerId = agentOne agentTwo = context.PLAY_ORDER[1] properties[11].ownerId = agentTwo properties[13].ownerId = agentTwo properties[14].ownerId = agentTwo context.state = State(context.PLAY_ORDER,properties) log("game","Game #"+str(context.gamesCompleted+1)+" started.") # all the agents provided can access the startGame API return context.PLAY_ORDER
def test_sub(self): cards = [Card(name='2', suit='Pagoda'), Card(name='2', suit='Sword')] cards = Cards(cards_list=cards) cards_to_remove = Cards(cards_list=[Card(name='2', suit='Sword')]) cards = cards - cards_to_remove self.assertEqual(cards.size, 1)
def test_deck_setup(): """ RED GREEN REFACTOR a (failing) sanity test layout """ deck = Deck() assert len(deck) == 52 assert Cards("A", "s") in deck.cards assert Cards("6", "d") in deck.cards assert Cards("J", "h") in deck.cards assert Cards("T", "c") in deck.cards
def find_multiple(cards: Cards, level_to_beat: int, cards_to_find: int): """ :param cards: :param level_to_beat: :param cards_to_find: :rtype: Combination """ if cards_to_find <= 0 or cards_to_find > 4: raise ValueError('Illegal combination_type %s' % cards_to_find) if cards_to_find == 1: for card in (cards - Phoenix()).cards: if card.power > level_to_beat: return Combination(cards_list=[card]) # if no card could have been player, try to take the lead with your Phoenix # Phoenix can not be played on a Dragon if cards.phoenix_flag and level_to_beat < Dragon().power: return Combination(cards_list=[Phoenix()]) # TODO - TO REFACTOR WITH LOGIC if cards_to_find == 2: for i in range(len(cards.cards) - 1): card = cards.cards[i] if card.power > level_to_beat and card.power == cards.cards[ i + 1].power: return Cards(cards_list=[card, cards.cards[i + 1]]) if cards_to_find == 3: for i in range(len(cards.cards) - 2): card = cards.cards[i] if card.power > level_to_beat and card.power == cards.cards[ i + 2].power: return Cards(cards_list=[ card, cards.cards[i + 1], cards.cards[i + 2] ]) if cards_to_find == 4: for i in range(len(cards.cards) - 3): card = cards.cards[i] if card.power > level_to_beat and card.power == cards.cards[ i + 3].power: return Cards(cards_list=[ card, cards.cards[i + 1], cards.cards[i + 2], cards.cards[i + 3] ]) # If no combination found, try to use Phoenix to play if cards.phoenix_flag and 1 <= cards_to_find < 4 and cards.size > 1: return Hand.find_multiple(cards - Phoenix(), level_to_beat, cards_to_find - 1) + Phoenix()
def find_straight(cards, level_to_beat, length=None, bomb=False): """ 1/ Start at level_to_beat - length +2: if level_to_beat is 10, length 6, their straight starts at 5, your straight has to start at 6 2/ see if you can find a straight that beats that 3/ see if you can find a length-1 straight at level_to_beat -1 """ # Get all possible start points for the straight if length is None: length = 5 elif length <= 0: return None start_point = max(1, level_to_beat - length + 2) max_strength_power = Dragon().power - 1 max_start_point = max_strength_power - length + 2 start_points = range(start_point, max_start_point + 1) for card in cards.cards: for start in start_points: if card.power == start: if length == 1: return Cards(cards_list=[card]) else: # TODO - jump in straights # Issue constantly increasing threshold rest = Hand.find_straight(cards - card, card.power + length - 2, length - 1, bomb=bomb) # TODO BOMB if rest: if not rest.phoenix_flag: if min(rest.cards).power == card.power + 1: return rest + card elif (len(rest.cards) == 1 and Phoenix() in rest.cards) or min( (rest - Phoenix() ).cards).power <= card.power + 2: return rest + card if cards.phoenix_flag: if length == 1: return Cards(cards_list=[Phoenix()]) rest = Hand.find_straight(cards - Phoenix(), level_to_beat, length - 1, bomb=bomb) if rest: return rest + Phoenix()
def calculate_best_choose(self): """!@brief Calculation best choose in deck """ self.sortByValue(self.hand_cards) resource_hand = [Cards(0, 0) for x in range(0, CONST.get_size_of_hand)] for i in range(0, CONST.get_size_of_hand): desk_card = Cards(self.desk_cards[i].getCardValue(), self.desk_cards[i].getCardType()) resource_hand[i] = desk_card self.calculateHandValues(resource_hand, i + 1, 0)
def reset(self): self.partner = None self.cards = Cards() self.discarded = Cards() self.trump = None self.is_playing = False #We wan't to record what the possible cards are for every player self.unknown_cards = [create_deck(), create_deck(), create_deck()] #possible_cards[0] is the player next, [1] is our mate, and [2] is the player before us index = self.index self.unknown_cards[0].owner = (1 + index) % 4 self.unknown_cards[1].owner = (2 + index) % 4 self.unknown_cards[2].owner = (3 + index) % 4 self.mystery_cards = create_deck() self.mate_prefered_colors = []
def stack_won(game): """Have the stack be won by the previous player. Nonmutating. Additionally, change the next player to the winning player. """ (player_cards, stack, next_player, winner) = game player_cards = player_cards.copy() winning_player = dec_player(next_player) # NOTE The stack is effectively turned upside down and put underneath player_cards[winning_player] = Cards(player_cards[winning_player] + stack) stack = Cards() next_player = winning_player return Game(player_cards, stack, next_player)
def setup_table(): """ Fixture that sets up a table with a single player. """ r = HouseRules( shoe_size=4, bet_limits=[10, 500], s17=True, blackjack_payout=1.5, max_hands=4, double_down=True, split_unlike_tens=True, double_after_split=True, resplit_aces=False, insurance=True, late_surrender=True, dealer_shows_hole_card=True ) c = Cards(rules=r) t = Table() p = Player( name='Player 1', rules=r, bankroll=100, min_bet=10 ) t.add_player(player=p) p.set_hand() p.stats.create_count_key(count_key=0) return c, t, r, p
def test_dealer_plays_hand(s17, dealer_hand, expected): """ Tests the dealer_plays_hand function when a dealer hits or stands on soft 17. """ r = HouseRules( shoe_size=4, bet_limits=[10, 500], s17=s17 ) c = Cards(rules=r) t = Table() p = Player( name='Player 1', rules=r, bankroll=100, min_bet=10 ) t.add_player(player=p) p.stats.create_count_key(count_key=0) dealer_hand = dealer_plays_hand( rules=r, cards=c, dealer_hole_card=dealer_hand[1], dealer_hand=dealer_hand ) assert dealer_hand == expected
def test_players_play_hands_double_down(double_down, expected): """ Tests the double down option within the players_play_hands function. """ r = HouseRules( shoe_size=4, bet_limits=[10, 500], double_down=double_down ) c = Cards(rules=r) t = Table() p = Player( name='Player 1', rules=r, bankroll=100, min_bet=10 ) t.add_player(player=p) p.stats.create_count_key(count_key=0) p.set_hand() p.hit(key=1, new_card=6) p.hit(key=1, new_card=4) players_play_hands( table=t, rules=r, cards=c, dealer_hand=[11, 2], dealer_up_card=2 ) assert p.get_double_down(key=1) == expected
def newround(round_number,thisgame): thisgame.cards = Cards() round_yuhuha = [] round_info = [] # player:winsets for player in thisgame.player_list: round_info.append(0) for player in thisgame.player_list: dast = thisgame.cards.dast_bede(round_number) player.mycards=dast showInPV( player,dast) yuhuha_showingp = [] for player in thisgame.player_list: name = player.name yuhuha = min(round_number,5) yuhuha_list = [i for i in range(0,yuhuha+1)] yuhuha_answer = (yuhuhaAskInPV(player, yuhuha_list))[1] player._yuhuha = yuhuha_answer round_yuhuha.append(yuhuha_answer) yuhuha_showingp.append({name:yuhuha_answer}) showInGroup(thisgame,yuhuha_showingp,"yuhuha") setstarter = (round_number-1) % len(thisgame.player_list) for i in range(round_number): #print("new set +++++") setstarter = newset(setstarter,thisgame) showInGroup(thisgame,thisgame.player_list[setstarter].name,"winner") round_info[setstarter]+=1 judgment_round(round_number,round_info,round_yuhuha,thisgame) scoreupdate(thisgame)
def deal(self): # copy list of players self.alivePlayers = list(self.players) # create new deck cards = Cards(len(self.players)) # store the chosen community cards self.communityCards = cards.communityCards # give each hand to a player for index, player in enumerate(self.players): player.hand = cards.hands[index] # move the dealer button self.dealer = self.players[(self.players.index(self.dealer) + 1) % len(self.players)] # take blinds l = self.players[(self.players.index(self.dealer) + 1) % len(self.players)] b = self.players[(self.players.index(self.dealer) + 2) % len(self.players)] lBet = Bet(l, self.littleBlind) bBet = Bet(b, self.littleBlind * 2) if lBet.valid(0): lBet.moveFunds(self) if bBet.valid(0): bBet.moveFunds(self) # set the acting player self.actingPlayer = self.players[(self.players.index(self.dealer) + 3) % len(self.players)] self.actingPlayer.act = True for p in self.players: p.hadTurn = False self.bettingRound = 0
def test_1(self): data = open("test.txt", "r").read().split("\n\n") game = Cards() game.deal(data) game.play() self.assertEqual([3, 2, 10, 6, 8, 5, 9, 4, 7, 1], game.winner) self.assertEqual(306, game.score())
def select_subject_category(self): pass # ask of what subject and catagory to compile cards for. study_card = Cards() subject_select = '' cat_select = '' subject_list = study_card.display_subject_list('mainsubject') print(f'Subject list: {subject_list}\n') subject = input('Please select which Subject to review: ') if subject.lower() == 'q': print('Good Bye') return exit() for i in subject_list: if i != subject.lower(): pass else: subject_select = i print("\nSubject Selected: " + subject_select + '\n') cat_list = study_card.display_subject_list(subject_select, 'category') print(f'Category: {cat_list}\n') category = input('Please select which category to review: ') if category.lower() == 'q': print('Good Bye') return exit() for i in cat_list: if i != category.lower(): pass else: cat_select = i print("\nCategory Selected: " + cat_select + '\n') return (subject_select, cat_select)
def test_bito(self): cards_game = Cards(25) cards_game.set_players_cards() cards_game.turn_cards() assert len(cards_game.list_bito) + len( cards_game.player_1_cards) + len(cards_game.player_2_cards) == 36 assert len(cards_game.list_coloda) == 0
def setup_counting_strategy(): r = HouseRules(shoe_size=4, bet_limits=[10, 500]) c = Cards(rules=r) c.burn_card() c.add_to_seen_cards(card=1) cs = CountingStrategy(rules=r, cards=c) return c, cs
def setup_cards(): cards_list = [] for shoe_size in [4, 6, 8]: r = HouseRules(shoe_size=shoe_size, bet_limits=[10, 500]) cards_list.append(Cards(rules=r)) return cards_list
def trow_away_card(self): '''Trow away a card, if you can't confess a card''' self.pp("We want to trow away a card") colors = self.cards.colors() if self.trump in colors: colors.remove(self.trump) if len(colors) == 1: self.pp( "Trow away the lowest card we have of the only possible color") return sorted(self.cards.filter_color(colors[0]))[0] if len(colors) == 0: raise NotImplementedError( "We only have trumps, shouldn't call this function") poss = Cards() for color in colors: filt = self.cards.filter_color(color) if len(filt) >= 4: self.pp( "We trow away the lowest card of the color we have the most of" ) return sorted(filt)[0] if filt.has(TEN) and not self.is_high(filt.has(TEN)): continue filt = filt.filter(lambda c: c.value not in (TEN, ACE)) poss.extend(filt) if poss: self.pp( "Trowing away a low card in a colour that doesn't have a low TEN" ) return sorted(poss)[0] self.pp( "We have a Ten in every playable color. Play a card in one of them" ) return sorted(self.cards.filter(self.is_not_trump))[0]
def main(): cards = Cards() coins = Coins() x = int(input("Indique la cantidad de jugadores (3-4): ")) players = Players(x) players.add_players() cards.shuffle_deck() cards.distribute_cards(x) cont = 0 rounds = 0 while len(players.deadplayers) != (x - 1): if cont == (x): cont = 0 rounds += 1 print("\nRonda " + str(rounds) + " terminada") if players.jugadores[cont] in players.deadplayers: cont += 1 else: print("\nEs el turno de " + players.jugadores[cont] + "\n") action = interface(cont, players, cards, coins) action_played(cont, action, players, cards, coins) cont += 1 for i in players.jugadores: if i not in players.deadplayers: print("\n" + i + " ha ganado el juego")
def legal_cards(self, played_cards): if not played_cards: return self.cards #first to play, everything is legal played_cards = Cards(played_cards) color = played_cards[0].color played_trumps = played_cards.filter_color(self.trump) highest = highest_card(played_cards) if highest.owner == None: raise KeyError("Played card doesn't have owner") winning = (highest.owner == self.partner) cards = self.cards.filter_color(color) if cards: #we can confess colour if color != self.trump: return cards #no trumps so no restrictions #must not undertrump higher = [t for t in cards if t>highest] if higher: return higher # We must overtrump return cards trumps = self.cards.get_trumps() if not trumps: return self.cards # Don't have any trumps so everything is legal if not played_trumps and not winning: return trumps # We aren't winning and we have trumps, so we must play one of those higher = [t for t in trumps if t>highest] if higher and not winning: return higher # We must overtrump c = self.cards.filter(lambda c: c.color!=self.trump) #Any card except trumps c.extend(higher) if c: return c return self.cards #we can't overtrump, but we only have trumps so we need to play them.
def __create_deck(self): roles = ["Duke", "Assassin", "Captain", "Ambassador", "Countess"] deck = [] for element in roles: for i in range(3): deck.append(Cards(element)) shuffle(deck) return deck
def __init__(self): self.name = 'mongoaudit' self.version = __version__ check_version(self.version) urwid.set_encoding("UTF-8") self.cards = Cards(self) self.setup_view() self.main()
def deals(num): cards = Cards() for i in range(num): cards.add(deck.pop()) cards.sort() return cards
def __init__(self): self.cards = Cards() self.playerCounter = 0 self.player = [None, None, None, None] self.started = 0 self.now_player = 0 self.pass_cnt = 0 self.pre_cards = [] self.game_args = [0, 0, 0]
def total_hand(self): c = Cards() sum = 0 faces = [face for (face, suit) in self.hand] for face in faces: sum += c.values[face] if 'Ace' in faces and sum < 12: sum += 10 return sum
def __create_deck(self): deck =[ ] characters = ["Duke","Assassin","Ambassador", "Captain","Contessa"] for i in range(3): for character in characters: deck.append(Cards(character)) shuffle(deck) return deck
def reset(self): self.partner = None # Index of player on your team self.cards = Cards() # Current cards in your hand self.discarded = Cards() # Cards you have trown out of your hand self.trump = None # The suite of Trump self.is_playing = False # Wether we are the 'attacking' team. #We wan't to record what the possible cards are for every player self.unknown_cards = [create_deck(), create_deck(), create_deck()] #possible_cards[0] is the player next, [1] is our mate, and [2] is the player before us index = self.index self.unknown_cards[0].owner = (1 + index) % 4 self.unknown_cards[1].owner = (2 + index) % 4 self.unknown_cards[2].owner = (3 + index) % 4 self.unknown_colours = [list( range(4))] * 3 # The possible colours every player might have self.mystery_cards = create_deck( ) # The cards still in play that we don't have self.mate_prefered_colors = []
def test_next_move(): """ Starting from position 222?/000?/111? with player 0 to play, and second choice 2, 0, 1, what is the best move? What is the best following move for player 2? """ h0 = Hand() h0.known_cards = Counter({2: 3}) h0.number_of_unknown_cards = 1 h1 = Hand() h1.known_cards = Counter({0: 3}) h1.number_of_unknown_cards = 1 h2 = Hand() h2.known_cards = Counter({1: 3}) h2.number_of_unknown_cards = 1 cards = Cards(3) cards.hands = [h0, h1, h2] cards.show(0) player = CleverPlayer(1000, 1000, [[2], [0], [1]]) history = set() depth = 1000 other, suit, result, _ = player._evaluate_move(0, cards, history, depth) print(f"player 0 asks {other} for {suit} (result={result})") assert result == 1 assert suit == 1 assert other == 2 # player 1 must say yes, as he has this card has = player.has_card(2, 0, 1, cards, history) assert has cards.transfer(suit, other, 0, False) winner = cards.test_winner(0) assert winner == -1 # nobody has won yet print() cards.show(1) # Now player 1 to move other, suit, result, _ = player._evaluate_move(1, cards, history, depth) print(f"player 1 asks {other} for {suit} (result={result})") assert result == 1 assert suit == 0 assert other == 2 # player 2 must say no, otherwise 1 wins immediately has = player.has_card(2, 1, 0, cards, history) assert not has cards.no_transfer(suit, other, 1, False) winner = cards.test_winner(1) cards.show(2) assert winner == 1, "test_next_move: expecting a win for player 1" print("----------------") print()