From 81b1cd3cd896027e5ea656cefadaab40a1bf2528 Mon Sep 17 00:00:00 2001 From: Steinadler Date: Thu, 14 Jan 2021 10:07:39 +0100 Subject: [PATCH] removed debug prints and added docstrings in quartett.py and clean_input.py --- clean_input.py | 20 +++++++---- quartett.py | 98 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 78 insertions(+), 40 deletions(-) diff --git a/clean_input.py b/clean_input.py index e24d10c..45027c4 100644 --- a/clean_input.py +++ b/clean_input.py @@ -69,9 +69,12 @@ def io_int(message: str) -> int: def io_card_selection(message: str): """ - check card selection - :param message: Select something... - :return: + Takes an input message to be printed. + Checks if user input fits the form 0a - 3h. + Construct a card dict to be returned. + + :param message: message needs to be a string that is printed for user instruction + :return: card dict """ card_stack = define_card_stack() @@ -95,8 +98,11 @@ def io_card_selection(message: str): def define_card_stack(): """ + Defines a card stack for comparing values. + The card stack has the same structure as in + quartett.initialize() - :return: + :return: list of dicts """ card_stack = [] x = 0 @@ -111,8 +117,9 @@ def define_card_stack(): def end(): """ + Prints exit screen and quits. - :return: + :return: None """ print( "---------------------\nSpiel wird beendet...\n---------------------") @@ -121,8 +128,9 @@ def end(): def restart(): """ + Prints restart screen and restarts the game. - :return: + :return: None """ print( "---------------------\nSpiel wird neu gestartet...\n---------------------") diff --git a/quartett.py b/quartett.py index 305b935..606d1aa 100644 --- a/quartett.py +++ b/quartett.py @@ -1,20 +1,22 @@ -"""main module""" +"""This module handles everything for the game but the cleaning of user input.""" __author__ = "" __credits__ = "" __email__ = "" import random -import clean_input import copy +import clean_input -# Game - def central_function(): - ''' - does everything, keeps order - ''' + """ + Calls the other function. + Keeps track of played rounds and who is the active player. + Game continues until one player has no cards left. + + :return: None + """ switch = True @@ -23,13 +25,9 @@ def central_function(): # stores the dropped cards all_quartets = [] - print("players", players) # debug - print("card stack", card_stack) # debug - print(players_with_cards) # debug - for i in range(len(players)): # debug - pretty_print_deck(players_with_cards, i) # debug - print("active", active) # debug + drop_cards(players_with_cards, all_quartets) + while switch: round(card_stack, players_with_cards, players, active, complete_card_stack, all_quartets) @@ -44,11 +42,20 @@ def central_function(): def round(card_stack, players_with_cards, players, active, complete_card_stack, all_quartets): - ''' - 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. + If no card is stolen a card has to be drawn from card_stack. + Also handles AI decisions. + + :param card_stack: list of not distributed cards + :param players_with_cards: list of dicts with player name, cards and number of quartets + :param players: list of player names + :param active: index + :param complete_card_stack: list of 32 cards for comparison + :param all_quartets: list of dropped cards :return: None - ''' + """ switch = True @@ -97,7 +104,8 @@ def round(card_stack, players_with_cards, players, active, complete_card_stack, player_request = random.choice(cards_to_choose_from) print( - f"{players[active]} fragt {players[chosen_player]} nach der Karte {player_request}.") + f"{players[active]} fragt {players[chosen_player]} nach " + f"der Karte {player_request}.") if player_request in players_with_cards[chosen_player]['cards_on_hand']: steal(active, chosen_player, players_with_cards, player_request) @@ -105,7 +113,8 @@ def round(card_stack, players_with_cards, players, active, complete_card_stack, else: print( - f"{players[chosen_player]} hat die Karte {player_request} nicht. Der nächste Spieler ist dran...") + f"{players[chosen_player]} hat die Karte {player_request} nicht. " + f"Der nächste Spieler ist dran...") # end of round # except if stack is not empty, which is not only the case with two players if card_stack: @@ -121,7 +130,11 @@ def round(card_stack, players_with_cards, players, active, complete_card_stack, def pretty_print_deck(players_with_cards, player): """ - prints the cards in a players deck in a readable format + Prints the cards in a players deck in a readable format. + + :param players_with_cards: list of dicts with player name, cards and number of quartets + :param player: index + :return: None """ pretty_deck = [] @@ -134,25 +147,32 @@ def pretty_print_deck(players_with_cards, player): def steal(active, chosen_player, players_with_cards, player_request): """ - defines how the active players steals cards from chosen player + Transfers cards between player decks. + + :param active: index + :param chosen_player: index of player to be asked + :param players_with_cards: list of dicts with player name, cards and number of quartets + :param player_request: wanted card :return: None """ if active == 0: print(f"player{chosen_player} hat die Karte, die du " "haben möchtest und sie wandert in dein Deck.") else: - print(f"player{chosen_player} hat die Karte, die player{active}" - "haben möchte nicht und sie wandert in sein Deck.") + print(f"player{chosen_player} hat die Karte, die player{active} " + "haben möchte und sie wandert in sein Deck.") card_index = players_with_cards[chosen_player]['cards_on_hand'].index(player_request) players_with_cards[active]['cards_on_hand'].append( players_with_cards[chosen_player]['cards_on_hand'].pop(card_index)) - # pretty_print_deck(players_with_cards, active) def drop_cards(players_with_cards, all_quartets, active=None): """ - function that lets players drop cards - if they have a quartet in their deck + Checks whether human or AI player is supposed to drop cards. + + :param players_with_cards: list of dicts with player name, cards and number of quartets + :param all_quartets: list of cards + :param active: index :return: None """ @@ -168,7 +188,13 @@ def drop_cards(players_with_cards, all_quartets, active=None): def drop_cards_help(p, all_quartets): """ - eh + Counts the how often a letter is in the players hand. + If there is quartet it will be dropped. + The cards a then added to all_quartets. + + :param p: dict with player name, cards and number of quartets + :param all_quartets: list of cards + :return: None """ reference = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] counter = [0, 0, 0, 0, 0, 0, 0, 0] @@ -192,13 +218,13 @@ def drop_cards_help(p, all_quartets): p["cards_on_hand"] = [c for c in p["cards_on_hand"] if quartet_letter != c["letter"]] - return p - def initialize(): """ - initializes stuff - :return: list, list (of dicts), list + Constructs two card stacks. Distributes one of them between the players. + Player number is set here, player names are also constructed here. + + :return: list, list (of dicts), list, list (of dicts) """ card_stack = [] @@ -240,7 +266,7 @@ def initialize(): del card_stack[x] cards_of_player.append(selected_card) - # create a list that contains every player, their deck and successes + # create a list that contains every player, their deck and number of made quartets players_with_cards.append( {"player": players[i], "cards_on_hand": cards_of_player, "quartet": 0}) @@ -249,9 +275,13 @@ def initialize(): def the_winner_is(players_with_cards, players): """ - counts the number of quartets + Counts the number of quartets a player has made, and chooses the - winner, who made the most + winner, who got the most. + + :param players_with_cards: Main list of dicts with players and their 'hand' + :param players: list of names + :return: None """ temp = 1