cleaning in quartett.py

This commit is contained in:
Steinadler 2021-01-12 10:41:48 +01:00
parent b9a2767eeb
commit 6c1848c5ba
2 changed files with 24 additions and 17 deletions

View File

@ -81,8 +81,7 @@ def io_card_selection(message: str):
if user_input[0] in "0123" and user_input[1] in "abcdefgh": if user_input[0] in "0123" and user_input[1] in "abcdefgh":
for c in card_stack: for c in card_stack:
if c["number"] == user_input[0] and c["letter"] == user_input[1]: if c["number"] == user_input[0] and c["letter"] == user_input[1]:
card = c return c
return card
elif user_input == "q": elif user_input == "q":
end() end()

View File

@ -6,7 +6,7 @@ __email__ = ""
import random import random
import clean_input import clean_input
import itertools #import itertools
# Game # Game
@ -17,42 +17,46 @@ def central_function():
active = 0 # contains index of the active player active = 0 # contains index of the active player
card_stack, players_with_cards, players = initialize() card_stack, players_with_cards, players = initialize()
while deckP1 and deckP2 and deckP3 not empty: print(players)
round(card_stack, players_with_cards, players, active) # while deckP1 and deckP2 and deckP3 not empty:
for i in itertools.cycle(range(len(players))): # round(card_stack, players_with_cards, players, active)
active = i # for i in itertools.cycle(range(len(players))):
# active = i
def round(card_stack, players_with_cards, players,active): def round(card_stack, players_with_cards, players, active):
'''structures one round in the game '''structures one round in the game
active player chooses another player from whom to steal a card''' active player chooses another player from whom to steal a card'''
switch = True switch = True
while switch: while switch:
players_without_active = players players_without_active = players
players_without_active.remove(active) players_without_active.remove(active)
print('Folgende Spieler stehen zur Verfügung:') print('Folgende Spieler stehen zur Verfügung:')
print(players_without_active) print(players_without_active)
# needs clean.io, chosen_player enthält den index in der players liste # needs clean.io, chosen_player enthält den index in der players liste
chosen_player = players.clean_input.io_str('Welchen Spieler möchtest du befragen?', players_without_active)) chosen_player = clean_input.io_str('Welchen Spieler möchtest du befragen?',
players_without_active)
player_request = clean_input.io_card_selection('Welche Karte möchtest du haben?') player_request = clean_input.io_card_selection('Welche Karte möchtest du haben?')
if player_request in players_with_cards[chosen_player]['cards_on_hand']: if player_request in players_with_cards[chosen_player]['cards_on_hand']:
steal(active, chosen_player) steal(active, chosen_player, players_with_cards, player_request)
else: else:
# end of round # end of round
# except if stack is not empty, which is only the case with two players # except if stack is not empty, which is only the case with two players
if card_stack == True: if card_stack == True:
# last card from the stack gets added to active player's hand # last card from the stack gets added to active player's hand
players_with_cards[active]['cards_on_hand'].append(card_stack.pop()) players_with_cards[active]['cards_on_hand'].append(card_stack.pop())
break
switch = False switch = False
def steal(P1, P2): 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 """
players_with_cards[active].['cards_on_hand'].append(players_with_cards[chosen_player]['cards_on_hand'].pop(player_request)) players_with_cards[active]['cards_on_hand'].append(
players_with_cards[chosen_player]['cards_on_hand'].pop(player_request))
def initialize(): def initialize():
""" """
@ -71,7 +75,7 @@ def initialize():
card_stack.append(d) card_stack.append(d)
x += 1 x += 1
number_of_players = clean_input.io_int("Mit wie vielen Spielern wollen Sie spielen?") number_of_players = clean_input.io_int("Gegen wie viele Spieler wollen Sie spielen?")
number_of_players += 1 # Add also human player. number_of_players += 1 # Add also human player.
for i in range(number_of_players): for i in range(number_of_players):
@ -93,9 +97,13 @@ def initialize():
# Add list of dicts (cards) to players # Add list of dicts (cards) to players
players_with_cards.append({"player": players[i], "cards_on_hand": cards_of_player}) players_with_cards.append({"player": players[i], "cards_on_hand": cards_of_player})
return card_stack, players_with_cards, players return card_stack, players_with_cards, players
# Call central_function() only if quartett.py is the main module
if __name__ == "__main__":
central_function()
''' '''
players_with_cards = [ players_with_cards = [
{'player': 'player0', 'cards_on_hand': [{'id': 13, 'number': 1, 'letter': 'f'}, {'player': 'player0', 'cards_on_hand': [{'id': 13, 'number': 1, 'letter': 'f'},