modified drop_cards_help()

This commit is contained in:
Steinadler 2021-01-12 18:16:10 +01:00
parent 9b628e5917
commit 4484a60f26
1 changed files with 54 additions and 29 deletions

View File

@ -6,7 +6,7 @@ __email__ = ""
import random
import clean_input
#import itertools
import itertools
# Game
@ -16,18 +16,20 @@ def central_function():
active = 0 # contains index of the active player
switch = True
card_stack, players_with_cards, players = initialize()
print(players)
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
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
for i in itertools.cycle(range(len(players))): # rotate active player
active = i
#the_winner_is()
# the_winner_is()
def round(card_stack, players_with_cards, players, active):
@ -72,38 +74,45 @@ def steal(active, chosen_player, players_with_cards, player_request):
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):
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
# default, check all players
if active is None:
for p in players_with cards:
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):
p = players_with_cards[active]
drop_cards_help(p)
def drop_cards_help(p):
"""
eh
"""
reference = ['a','b','c','d','e','f','g','h']
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 card in p["cards_on_hand"]:
counter[reference.index(card["letter"])] += 1
for i in counter:
if i == 4:
quartet_letter = reference[counter.index(i)]
counter[counter.index(i)] = 0
p["quartet"] += 1
print(f"{p['player']} legt das {quartet_letter}-Quartett ab...")
p["cards_on_hand"] = [c for c in p["cards_on_hand"] if quartet_letter != c["letter"]]
# print(p)
return p
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():
"""
@ -137,30 +146,46 @@ def initialize():
cards_per_player = int(len(card_stack) // len(players))
# 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)
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]
del card_stack[x]
cards_of_player.append(selected_card)
# 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})
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():
def the_winner_is():
"""
counts the number of quartets
a player has made, and chooses the
winner, who made the most
"""
#TODO
pass
# TODO
# Call central_function() only if quartett.py is the main module
if __name__ == "__main__":
central_function()
p = {'player': 'player1', 'cards_on_hand': [{'id': '27', 'number': '3', 'letter': 'd'},
{'id': '3', 'number': '0', 'letter': 'd'},
{'id': '25', 'number': '3', 'letter': 'd'},
{'id': '19', 'number': '2', 'letter': 'd'},
{'id': '0', 'number': '0', 'letter': 'a'},
{'id': '28', 'number': '3', 'letter': 'a'},
{'id': '26', 'number': '3', 'letter': 'a'},
{'id': '5', 'number': '0', 'letter': 'a'},
{'id': '9', 'number': '1', 'letter': 'b'},
{'id': '7', 'number': '0', 'letter': 'h'}],
'quartet': 0}
drop_cards_help(p)
# central_function()
'''
players_with_cards = [