implemented check if any deck is empty, drop_cards()-function to drop quartets from hand, started the_winner_is()-function that determines the winner in the end.

This commit is contained in:
adamercat 2021-01-12 12:06:18 +01:00
parent 6c1848c5ba
commit 9b628e5917
1 changed files with 75 additions and 16 deletions

View File

@ -18,15 +18,24 @@ def central_function():
card_stack, players_with_cards, players = initialize()
print(players)
# while deckP1 and deckP2 and deckP3 not empty:
# round(card_stack, players_with_cards, players, active)
# for i in itertools.cycle(range(len(players))):
# active = i
drop_cards(players_with_cards)
while switch:
round(card_stack, players_with_cards, players, active)
for i in range(len(players)): # check if any card deck is empty
switch = bool(players_with_cards[i]['cards_on_hand'])
for i in itertools.cycle(range(len(players))): # rotate active player
active = i
#the_winner_is()
def round(card_stack, players_with_cards, players, active):
'''structures one round in the game
active player chooses another player from whom to steal a card'''
'''
structures one round in the game
active player chooses another player from whom to steal a card
:return: None
'''
switch = True
@ -42,6 +51,8 @@ def round(card_stack, players_with_cards, players, active):
if player_request in players_with_cards[chosen_player]['cards_on_hand']:
steal(active, chosen_player, players_with_cards, player_request)
drop_cards(players_with_cards, active)
else:
# end of round
# except if stack is not empty, which is only the case with two players
@ -53,21 +64,58 @@ def round(card_stack, players_with_cards, players, active):
def steal(active, chosen_player, players_with_cards, player_request):
"""defines how the active players steals cards from chosen player """
"""
defines how the active players steals cards from chosen player
:return: None
"""
players_with_cards[active]['cards_on_hand'].append(
players_with_cards[chosen_player]['cards_on_hand'].pop(player_request))
def drop_cards(players_with_cards, active = None):
"""
function that lets players drop cards
if they have a quartet in their deck
:return: None
"""
#default, check all players
if active is None:
for p in players_with cards:
drop_cards_help(p)
else:
p = players_with_cards[active]
drop_cards_help(p)
def drop_cards_help(players_with_cards,p):
"""
eh
"""
reference = ['a','b','c','d','e','f','g','h']
counter = [0, 0, 0, 0, 0, 0, 0, 0]
for card in p["cards_on_hand"]:
counter[reference.index(card["letter"])] += 1
for i in counter:
if i == 4:
quartet_letter = reference[index(i)]
for card in p["cards_on_hand"]:
if card["letter"] == quartet_letter:
del card
def initialize():
"""
initializes stuff
:return:
:return: list, list (of dicts), list
"""
card_stack = []
players = []
players_with_cards = []
# define card stack
x = 0
for i in range(4):
for l in "abcdefgh":
@ -75,30 +123,40 @@ def initialize():
card_stack.append(d)
x += 1
# determine number of players
number_of_players = clean_input.io_int("Gegen wie viele Spieler wollen Sie spielen?")
number_of_players += 1 # Add also human player.
for i in range(number_of_players):
players.append(f"player{i}")
# determine number of cards per player
if len(players) == 2:
cards_per_player = 10
else:
cards_per_player = int(len(card_stack) // len(players))
for i in range(number_of_players): # Select cards for x players
cards_of_player = [] # Player deck
for j in range(cards_per_player): # Select ten cards
x = random.randint(0, len(card_stack) - 1) # Select one random card
# distribute hand cards to players
for i in range(number_of_players):
cards_of_player = []
for j in range(cards_per_player):
x = random.randint(0, len(card_stack) - 1)
selected_card = card_stack[x]
del card_stack[x] # Remove card from main card deck.
del card_stack[x]
cards_of_player.append(selected_card)
# Add list of dicts (cards) to players
players_with_cards.append({"player": players[i], "cards_on_hand": cards_of_player})
# create a list that contains every player, their deck and successes
players_with_cards.append({"player": players[i], "cards_on_hand": cards_of_player, "quartet": 0})
return card_stack, players_with_cards, players
#def the_winner_is():
"""
counts the number of quartets
a player has made, and chooses the
winner, who made the most
"""
#TODO
# Call central_function() only if quartett.py is the main module
if __name__ == "__main__":
@ -115,6 +173,7 @@ players_with_cards = [
{'id': 6, 'number': 0, 'letter': 'g'},
{'id': 9, 'number': 1, 'letter': 'b'},
{'id': 5, 'number': 0, 'letter': 'f'},
{'id': 15, 'number': 1, 'letter': 'h'}]
{'id': 15, 'number': 1, 'letter': 'h'}],
'quartet': 0
},
...]'''