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 test_deal_cards(): deck = cards.create_deck() inDeck = deck[:] (hum, cpu) = cards.deal_cards(inDeck) assert len(hum) == 26 assert len(cpu) == 26 assert deck == inDeck
def play(seed): result = '' random.seed(seed) deck = cards.create_deck(shuffle=True) names = "P1 P2 P3 P4".split() hands = {n: h for n, h in zip(names, cards.deal_hands(deck))} print(hands['P1']) row_points = {'P1': 0, 'P2': 0, 'P3': 0, 'P4': 0} start_player = cards.choose(names) turn_order = cards.player_order(names, start=start_player) final_pont = {'P1': 0, 'P2': 0, 'P3':0, 'P4': 0} while hands[start_player]: row_points = {'P1': 0, 'P2': 0, 'P3': 0, 'P4': 0} for name in turn_order: card = cards.choose(hands[name]) print(name) row_points[name] += card_value(card) hands[name].remove(card) print(row_points) points_sort = sorted(row_points.items(), key = lambda x: x[1], reverse=True) max_pont = points_sort[0][1] for k, v in row_points.items(): if v == max_pont: final_pont[k] += 1 final_sort = sorted(final_pont.items(), key = lambda x: x[1], reverse=True) max_pont = final_sort[0][1] for k, v in final_pont.items(): if v == max_pont: result += k + ' ' return result.strip()
def play(seed): random.seed(seed) deck = cards.create_deck(True) scores = {1: 0, 2: 0, 3: 0, 4: 0} hand = cards.deal_hands(deck) players = [1, 2, 3, 4] start_player = cards.choose(players) player_order = cards.player_order(players, start_player) hand = {x: hand[x - 1] for x in player_order} print(hand) print(player_order) for x in range(13): score = {1: 0, 2: 0, 3: 0, 4: 0} for player in hand: card = cards.choose(hand[player]) #print(card, card_value(card), x, f"P{idx+1}") hand[player].remove(card) score[player] += card_value(card) win = max(list(score.values())) for player in score: if score[player] == win: scores[player] += 1 print(score, scores) win = max(list(scores.values())) return " ".join(["P" + str(x) for x in scores if scores[x] == win])
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_create_deck(): deck = cards.create_deck() assert type(deck) is list assert len(deck) == 52 for card in deck: assert type(card) is tuple assert type(card[0]) is int assert type(card[1]) is str assert card[0] <= 13 assert card[0] >= 1
def play(seed): random.seed(seed) deck = cards.create_deck(shuffle=True) names = "P1 P2 P3 P4".split() hands = {n: h for n, h in zip(names, cards.deal_hands(deck))} start_player = cards.choose(names) turn_order = cards.player_order(names, start=start_player) players = {'P1':0,'P2':0,'P3':0,'P4':0} move = 0 game = [] while hands[start_player]: for name in turn_order: move += 1 card = cards.choose(hands[name]) value = card_value(card) game.append((name,value)) hands[name].remove(card) # when all 4 players had played (move == 4), check which one has played the valuest card if move == 4: game = sorted(game, key = lambda x: x[1]) maximum = game[-1][1] for i in game: # if that player's card has a value that matches the maximum played if i[1] == maximum: # then, that player receives a point players[i[0]] += 1 game = [] move = 0 # list ordered by pontuaction alist = [(value,item) for value,item in players.items()] alist = sorted(alist,key = lambda x: x[1]) # the first output will be the player known by having the maximum pontuaction max_points = alist[-1][1] output = alist[-1][0] for elem in alist: if elem[1] == max_points: # if another player has the same pontuaction, then he gets added to the output list if elem[0] not in output: output += ' ' + elem[0] # output is ordered by crescent order and converted to a string output = sorted(output.split()) return ' '.join(output)
def debug(self, args): (g, player) = self.getsession() if args == "win": g.winner = player return "Set winner" elif args == "koikoi": c = cards.Card("jan1.gif", 0) c.attrs['bright'] = True c.rank = 20 l = len(filter(lambda x: 'bright' in x.attrs, g.captures[player])) g.captures[player].extend([c] * (5 - l)) return "Next play will koikoi" elif args == "cardselect": g.field[0] = cards.Card("jan1.gif", 0) g.field[1] = cards.Card("jan2.gif", 0) g.cards.append(cards.Card("jan3.gif", 0)) return "Next play will require card select (maybe)" elif args == "take3": g.field[0] = cards.Card("jan1.gif", 0) g.field[1] = cards.Card("jan2.gif", 0) g.field[2] = cards.Card("jan3.gif", 0) g.hands[player][0] = cards.Card("jan4.gif", 0) return "Next play will take 3 cards from the field" elif args == "status": output = "Field: " def img(card): if card: return "<img src=\"/img/" + c.image + "\" />" else: return "<img src=\"/img/empty.gif\" />" for c in g.field: output += img(c) output += "<br />P1 hand: " for c in g.hands[0]: output += img(c) output += "<br />P2 hand: " for c in g.hands[1]: output += img(c) return output elif args == "showcards": deck = cards.create_deck() buf = "" for card in deck: buf += "<img src=\"/img/%s\" />Suit: %i Rank: %i<br />\n" % ( card.image, card.suit, card.rank) return buf else: return "Invalid argument"
def deal_cards(): global connected head = "INITCARD\n" player_num = len(connected) #1 deck for every 10 players decks = 1 + (player_num // 10) deck = cards.create_deck(decks) time.sleep(1) for i in range(player_num): c1 = deck[i * 2] c2 = deck[i * 2 + 1] c_s = connected[i] hands.append([c1, c2]) #head, number, card 1, card 2 payload = head + str(i) + "\n" + c1 + "\n" + c2 c_s.sendall(payload.encode()) #print(payload) deck = deck[player_num * 2:] #add the community cards table.extend([deck[0], deck[1], deck[2], deck[3], deck[4]])
def itest(): return Game(cards.create_deck(), 0)
#print("LOSE", end="") c_reserve.extend(pot) elif winner == 0: # A tie; a cause for WAR! print("There is a tie.") print("Declare War!") interface.wait_for_input() #print(" TIE", end="\n") (h_hand, h_reserve) = flip_if_needed((h_hand, h_reserve), n=3) (c_hand, c_reserve) = flip_if_needed((c_hand, c_reserve), n=3) pot.extend(cards.pull_three(h_hand)) pot.extend(cards.pull_three(c_hand)) # Weeeee, recursion! ((h_hand, h_reserve), (c_hand, c_reserve)) = play_turn( (h_hand, h_reserve), (c_hand, c_reserve), pot) return ((h_hand, h_reserve), (c_hand, c_reserve)) if __name__ == '__main__': deck = cards.create_deck() deck = cards.shuffle_cards(deck) (h_hand, c_hand) = cards.deal_cards(deck) interface.wait_for_input(DISABLE_WAIT) won = main((h_hand, []), (c_hand, []))
def test_shuffle(): deck = cards.create_deck() shuf = cards.shuffle_cards(deck) assert deck != shuf
def ask_user_to_hit_or_stand(): """ Ask a user if they want to hit or stand. If they give invalid input, continue asking. """ while True: user_input = input("Do you want to (h)it or (s)tand? ").lower() if user_input in ("h", "s"): return user_input print("I don't understand that input. Try again.") if __name__ == "__main__": deck = cards.create_deck(shuffle=True) hand = [] hand.append(cards.get_card(deck)) hand.append(cards.get_card(deck)) bust = cards.calculate_hand_score(hand) > 21 stand = False while not bust and not stand: print_hand(hand) hit_or_stand = ask_user_to_hit_or_stand() if hit_or_stand == "h": hand.append(cards.get_card(deck)) bust = cards.calculate_hand_score(hand) > 21 else:
print("You lose {} cards!".format(len(pot))) # print("LOSE", end="") c_reserve.extend(pot) elif winner == 0: # A tie; a cause for WAR! print("There is a tie.") print("Declare War!") interface.wait_for_input() # print(" TIE", end="\n") (h_hand, h_reserve) = flip_if_needed((h_hand, h_reserve), n=3) (c_hand, c_reserve) = flip_if_needed((c_hand, c_reserve), n=3) pot.extend(cards.pull_three(h_hand)) pot.extend(cards.pull_three(c_hand)) # Weeeee, recursion! ((h_hand, h_reserve), (c_hand, c_reserve)) = play_turn((h_hand, h_reserve), (c_hand, c_reserve), pot) return ((h_hand, h_reserve), (c_hand, c_reserve)) if __name__ == "__main__": deck = cards.create_deck() deck = cards.shuffle_cards(deck) (h_hand, c_hand) = cards.deal_cards(deck) interface.wait_for_input(DISABLE_WAIT) won = main((h_hand, []), (c_hand, []))