This commit is contained in:
Jonas_Jones 2023-01-25 21:28:26 +01:00
parent 3801ab2697
commit 36d3a420a2

19
game.py
View file

@ -39,15 +39,21 @@ def deal_cards(card_deck:list, players:int, cards_per_player:int):
- player_cards: tuple - player_cards: tuple
tuple of lists, each list contains the cards of a player tuple of lists, each list contains the cards of a player
''' '''
# create a list of lists, each list contains the cards of a player in the
# order of their occurance in the players list in the game loop of main.py
temp_cards = [] temp_cards = []
for player in range(players): for player in range(players):
temp_cards.append([]) temp_cards.append([])
for _ in range(cards_per_player): for _ in range(cards_per_player):
# add a random card to the player's list and remove it from the
# deck
temp_cards[player].append(card_deck.pop(randint(0, temp_cards[player].append(card_deck.pop(randint(0,
len(card_deck)-1))) len(card_deck)-1)))
# if the number of players is not equal to 5, add empty lists to the list
# until it has 5 elements
while len(temp_cards) < 5: while len(temp_cards) < 5:
temp_cards.append([]) temp_cards.append([])
# return the list of lists as a tuple
return (temp_cards[0], temp_cards[1], temp_cards[2], temp_cards[3], return (temp_cards[0], temp_cards[1], temp_cards[2], temp_cards[3],
temp_cards[4]) temp_cards[4])
@ -63,22 +69,33 @@ def compare_cards(cards_to_compare:list, trumpf_color:str):
- winner: int - winner: int
index of the winning card index of the winning card
''' '''
# first player is defined as winner
winner = 0 winner = 0
# no possible trump card has been detected yet
# list contains all indices of trump cards
# index maps to index in cards_to_compare
trumpf = [] trumpf = []
# index of all trump cards are added to trumpf
for i in cards_to_compare: for i in cards_to_compare:
if i.get('suit') == trumpf_color: if i.get('suit') == trumpf_color:
trumpf.append(cards_to_compare.index(i)) trumpf.append(cards_to_compare.index(i))
if len(trumpf) == 1: if len(trumpf) == 1:
# if only one trump card is present, it is the winner
return trumpf[0] return trumpf[0]
elif len(trumpf) > 1: elif len(trumpf) > 1:
winner = 0 winner = 0
for j in trumpf: for j in trumpf:
# loop through all trump cards and compare them
# highest rank is the winner
if RANKS.index(cards_to_compare[j].get('rank')) > RANKS.index( if RANKS.index(cards_to_compare[j].get('rank')) > RANKS.index(
cards_to_compare[winner].get('rank')): cards_to_compare[winner].get('rank')):
winner = j winner = j
winner = j winner = j
else: else:
# no trump card is present
winner = 0 winner = 0
# loop through all cards and compare them
# highest rank is the winner
for j in cards_to_compare: for j in cards_to_compare:
if RANKS.index(j.get('rank')) > RANKS.index( if RANKS.index(j.get('rank')) > RANKS.index(
cards_to_compare[winner].get('rank')): cards_to_compare[winner].get('rank')):