2021-01-06 16:57:51 +01:00
|
|
|
"""main module"""
|
|
|
|
|
2021-01-11 18:04:09 +01:00
|
|
|
__author__ = ""
|
|
|
|
__credits__ = ""
|
|
|
|
__email__ = ""
|
|
|
|
|
2021-01-06 16:57:51 +01:00
|
|
|
import random
|
|
|
|
import clean_input
|
2021-01-12 20:35:03 +01:00
|
|
|
import copy
|
2021-01-06 16:57:51 +01:00
|
|
|
|
2021-01-13 16:30:10 +01:00
|
|
|
|
2021-01-06 16:57:51 +01:00
|
|
|
# Game
|
|
|
|
|
|
|
|
def central_function():
|
2021-01-12 20:35:03 +01:00
|
|
|
'''
|
|
|
|
does everything, keeps order
|
|
|
|
'''
|
2021-01-06 16:57:51 +01:00
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
switch = True
|
|
|
|
|
2021-01-13 18:36:39 +01:00
|
|
|
card_stack, players_with_cards, players, complete_card_stack = initialize()
|
|
|
|
active = random.randint(0, len(players) - 1) # contains index of the active player
|
2021-01-13 16:30:10 +01:00
|
|
|
|
|
|
|
# stores the dropped cards
|
|
|
|
all_quartets = []
|
2021-01-13 18:36:39 +01:00
|
|
|
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
|
2021-01-13 16:30:10 +01:00
|
|
|
drop_cards(players_with_cards, all_quartets)
|
2021-01-12 12:06:18 +01:00
|
|
|
while switch:
|
2021-01-13 16:53:07 +01:00
|
|
|
round(card_stack, players_with_cards, players, active, complete_card_stack, all_quartets)
|
2021-01-12 12:06:18 +01:00
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
for i in range(len(players)): # check if any card deck is empty
|
2021-01-12 12:06:18 +01:00
|
|
|
switch = bool(players_with_cards[i]['cards_on_hand'])
|
2021-01-13 16:30:10 +01:00
|
|
|
|
|
|
|
active += 1
|
|
|
|
if active >= len(players):
|
|
|
|
active = 0
|
2021-01-12 12:06:18 +01:00
|
|
|
|
2021-01-13 17:50:01 +01:00
|
|
|
the_winner_is(players_with_cards, players)
|
2021-01-06 16:57:51 +01:00
|
|
|
|
|
|
|
|
2021-01-13 16:53:07 +01:00
|
|
|
def round(card_stack, players_with_cards, players, active, complete_card_stack, all_quartets):
|
2021-01-12 12:06:18 +01:00
|
|
|
'''
|
|
|
|
structures one round in the game
|
|
|
|
active player chooses another player from whom to steal a card
|
|
|
|
:return: None
|
|
|
|
'''
|
2021-01-12 10:41:48 +01:00
|
|
|
|
2021-01-11 18:40:12 +01:00
|
|
|
switch = True
|
|
|
|
|
2021-01-12 10:41:48 +01:00
|
|
|
while switch:
|
2021-01-13 18:07:36 +01:00
|
|
|
# print(active)
|
2021-01-12 20:35:03 +01:00
|
|
|
players_without_active = copy.copy(players)
|
|
|
|
players_without_active.pop(active)
|
2021-01-13 16:30:10 +01:00
|
|
|
|
|
|
|
if players[active] == "player0": # human player
|
2021-01-13 18:07:36 +01:00
|
|
|
pretty_print_deck(players_with_cards, active)
|
2021-01-13 16:30:10 +01:00
|
|
|
print('Folgende Spieler stehen zur Verfügung:')
|
|
|
|
print(players_without_active)
|
|
|
|
# chosen_player enthält den index in der players liste
|
|
|
|
chosen_player = players.index(clean_input.io_str(
|
|
|
|
'Welchen Spieler möchtest du befragen? ', players_without_active))
|
2021-01-13 18:07:36 +01:00
|
|
|
# print("chosen player", chosen_player)
|
2021-01-13 16:30:10 +01:00
|
|
|
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']:
|
|
|
|
steal(active, chosen_player, players_with_cards, player_request)
|
2021-01-13 18:07:36 +01:00
|
|
|
drop_cards(players_with_cards, all_quartets, active)
|
2021-01-13 16:30:10 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
print("Diese Karte hat der Spieler nicht. Der nächste Spieler ist dran...")
|
|
|
|
# end of round
|
|
|
|
# except if stack is not empty, which is not only the case with two players
|
2021-01-13 18:07:36 +01:00
|
|
|
if card_stack:
|
2021-01-13 16:30:10 +01:00
|
|
|
# last card from the stack gets added to active player's hand
|
|
|
|
players_with_cards[active]['cards_on_hand'].append(card_stack.pop())
|
|
|
|
|
|
|
|
switch = False
|
|
|
|
|
|
|
|
else: # AI players
|
|
|
|
# select random player
|
|
|
|
chosen_player = players.index(random.choice(players_without_active))
|
|
|
|
|
|
|
|
# get list of cards that are not on the hand of active AI player
|
|
|
|
cards_to_choose_from = [c for c in complete_card_stack if
|
|
|
|
c not in players_with_cards[active]["cards_on_hand"]]
|
|
|
|
|
2021-01-13 16:53:07 +01:00
|
|
|
# get list of cards that are not on the hand of active AI player AND not in quartets
|
|
|
|
cards_to_choose_from = [c for c in cards_to_choose_from if
|
|
|
|
c not in all_quartets]
|
|
|
|
|
|
|
|
# select card
|
2021-01-13 16:30:10 +01:00
|
|
|
player_request = random.choice(cards_to_choose_from)
|
|
|
|
|
2021-01-13 18:07:36 +01:00
|
|
|
print(
|
|
|
|
f"{players[active]} fragt {players[chosen_player]} nach 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)
|
|
|
|
drop_cards(players_with_cards, all_quartets, active)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print(
|
|
|
|
f"{players[chosen_player]} hat die Karte {player_request} nicht. 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:
|
|
|
|
# last card from the stack gets added to active player's hand
|
|
|
|
players_with_cards[active]['cards_on_hand'].append(card_stack.pop())
|
|
|
|
|
|
|
|
switch = False
|
|
|
|
|
|
|
|
# print("AI deck:", players_with_cards[active])
|
|
|
|
# print("AI choose:", chosen_player)
|
|
|
|
# print("AI card wish", player_request)
|
2021-01-13 16:30:10 +01:00
|
|
|
|
2021-01-06 16:57:51 +01:00
|
|
|
|
2021-01-12 20:35:03 +01:00
|
|
|
def pretty_print_deck(players_with_cards, player):
|
|
|
|
"""
|
|
|
|
prints the cards in a players deck in a readable format
|
|
|
|
"""
|
|
|
|
|
|
|
|
pretty_deck = []
|
|
|
|
for card in players_with_cards[player]['cards_on_hand']:
|
|
|
|
pretty_deck.append(card['number'] + card['letter'])
|
2021-01-13 18:07:36 +01:00
|
|
|
|
|
|
|
if player == 0:
|
|
|
|
print("Dein Deck: ", pretty_deck)
|
2021-01-06 16:57:51 +01:00
|
|
|
|
2021-01-13 16:30:10 +01:00
|
|
|
|
2021-01-12 10:41:48 +01:00
|
|
|
def steal(active, chosen_player, players_with_cards, player_request):
|
2021-01-12 12:06:18 +01:00
|
|
|
"""
|
|
|
|
defines how the active players steals cards from chosen player
|
|
|
|
:return: None
|
|
|
|
"""
|
2021-01-13 18:07:36 +01:00
|
|
|
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.")
|
2021-01-12 20:35:03 +01:00
|
|
|
card_index = players_with_cards[chosen_player]['cards_on_hand'].index(player_request)
|
2021-01-12 10:41:48 +01:00
|
|
|
players_with_cards[active]['cards_on_hand'].append(
|
2021-01-12 20:35:03 +01:00
|
|
|
players_with_cards[chosen_player]['cards_on_hand'].pop(card_index))
|
2021-01-13 18:07:36 +01:00
|
|
|
# pretty_print_deck(players_with_cards, active)
|
2021-01-12 10:41:48 +01:00
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
|
2021-01-13 16:30:10 +01:00
|
|
|
def drop_cards(players_with_cards, all_quartets, active=None):
|
2021-01-12 12:06:18 +01:00
|
|
|
"""
|
|
|
|
function that lets players drop cards
|
|
|
|
if they have a quartet in their deck
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
# default, check all players
|
2021-01-12 12:06:18 +01:00
|
|
|
if active is None:
|
2021-01-12 18:16:10 +01:00
|
|
|
for p in players_with_cards:
|
2021-01-13 16:30:10 +01:00
|
|
|
drop_cards_help(p, all_quartets)
|
2021-01-12 12:06:18 +01:00
|
|
|
|
|
|
|
else:
|
2021-01-12 18:16:10 +01:00
|
|
|
p = players_with_cards[active]
|
2021-01-13 16:30:10 +01:00
|
|
|
drop_cards_help(p, all_quartets)
|
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
|
2021-01-13 16:30:10 +01:00
|
|
|
def drop_cards_help(p, all_quartets):
|
2021-01-12 12:06:18 +01:00
|
|
|
"""
|
|
|
|
eh
|
|
|
|
"""
|
2021-01-12 18:16:10 +01:00
|
|
|
reference = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
|
2021-01-12 12:06:18 +01:00
|
|
|
counter = [0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
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...")
|
2021-01-13 16:30:10 +01:00
|
|
|
|
2021-01-13 18:07:36 +01:00
|
|
|
# build list of cards that are now dropped
|
|
|
|
q = [c for c in p["cards_on_hand"] if quartet_letter == c["letter"]]
|
|
|
|
|
|
|
|
# add quartet cards to list
|
|
|
|
for c in q:
|
|
|
|
all_quartets.append(c)
|
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
p["cards_on_hand"] = [c for c in p["cards_on_hand"] if quartet_letter != c["letter"]]
|
|
|
|
|
2021-01-13 16:53:07 +01:00
|
|
|
return p
|
2021-01-12 12:06:18 +01:00
|
|
|
|
2021-01-06 16:57:51 +01:00
|
|
|
|
|
|
|
def initialize():
|
|
|
|
"""
|
|
|
|
initializes stuff
|
2021-01-12 12:06:18 +01:00
|
|
|
:return: list, list (of dicts), list
|
2021-01-06 16:57:51 +01:00
|
|
|
"""
|
|
|
|
|
2021-01-11 16:49:05 +01:00
|
|
|
card_stack = []
|
2021-01-06 16:57:51 +01:00
|
|
|
players = []
|
|
|
|
players_with_cards = []
|
|
|
|
|
2021-01-12 12:06:18 +01:00
|
|
|
# define card stack
|
2021-01-06 16:57:51 +01:00
|
|
|
x = 0
|
|
|
|
for i in range(4):
|
|
|
|
for l in "abcdefgh":
|
2021-01-11 18:04:09 +01:00
|
|
|
d = {"id": str(x), "number": str(i), "letter": l}
|
2021-01-11 16:49:05 +01:00
|
|
|
card_stack.append(d)
|
2021-01-06 16:57:51 +01:00
|
|
|
x += 1
|
|
|
|
|
2021-01-13 16:30:10 +01:00
|
|
|
complete_card_stack = copy.copy(card_stack)
|
|
|
|
|
2021-01-12 12:06:18 +01:00
|
|
|
# determine number of players
|
2021-01-12 10:41:48 +01:00
|
|
|
number_of_players = clean_input.io_int("Gegen wie viele Spieler wollen Sie spielen?")
|
2021-01-13 16:30:10 +01:00
|
|
|
while number_of_players < 1 or number_of_players > 7:
|
|
|
|
print("Fehler! Sie dürfen lediglich gegen 1-7 Spieler spielen.")
|
|
|
|
number_of_players = clean_input.io_int("Gegen wie viele Spieler wollen Sie spielen?")
|
2021-01-06 16:57:51 +01:00
|
|
|
number_of_players += 1 # Add also human player.
|
|
|
|
|
|
|
|
for i in range(number_of_players):
|
|
|
|
players.append(f"player{i}")
|
|
|
|
|
2021-01-12 12:06:18 +01:00
|
|
|
# determine number of cards per player
|
2021-01-06 16:57:51 +01:00
|
|
|
if len(players) == 2:
|
|
|
|
cards_per_player = 10
|
|
|
|
else:
|
2021-01-11 16:49:05 +01:00
|
|
|
cards_per_player = int(len(card_stack) // len(players))
|
2021-01-06 16:57:51 +01:00
|
|
|
|
2021-01-12 12:06:18 +01:00
|
|
|
# distribute hand cards to players
|
2021-01-12 18:16:10 +01:00
|
|
|
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)
|
2021-01-11 16:49:05 +01:00
|
|
|
selected_card = card_stack[x]
|
2021-01-12 18:16:10 +01:00
|
|
|
del card_stack[x]
|
2021-01-06 16:57:51 +01:00
|
|
|
cards_of_player.append(selected_card)
|
|
|
|
|
2021-01-12 12:06:18 +01:00
|
|
|
# create a list that contains every player, their deck and successes
|
2021-01-12 18:16:10 +01:00
|
|
|
players_with_cards.append(
|
|
|
|
{"player": players[i], "cards_on_hand": cards_of_player, "quartet": 0})
|
2021-01-06 16:57:51 +01:00
|
|
|
|
2021-01-13 16:30:10 +01:00
|
|
|
return card_stack, players_with_cards, players, complete_card_stack
|
2021-01-06 17:12:36 +01:00
|
|
|
|
2021-01-12 18:16:10 +01:00
|
|
|
|
2021-01-13 17:50:01 +01:00
|
|
|
def the_winner_is(players_with_cards, players):
|
2021-01-12 12:06:18 +01:00
|
|
|
"""
|
|
|
|
counts the number of quartets
|
|
|
|
a player has made, and chooses the
|
|
|
|
winner, who made the most
|
|
|
|
"""
|
2021-01-13 18:05:05 +01:00
|
|
|
|
2021-01-13 17:50:01 +01:00
|
|
|
temp = 1
|
|
|
|
winners = []
|
|
|
|
|
|
|
|
# all potential winners are saved in the winners-list
|
|
|
|
# (in case there is more than one)
|
|
|
|
for i in range(len(players)):
|
|
|
|
winners.append(0)
|
|
|
|
dropped_quartets = players_with_cards[i]['quartet']
|
|
|
|
|
2021-01-13 18:36:39 +01:00
|
|
|
if temp < dropped_quartets:
|
|
|
|
temp = dropped_quartets
|
|
|
|
winners.insert(i, dropped_quartets)
|
2021-01-13 17:50:01 +01:00
|
|
|
|
2021-01-13 18:36:39 +01:00
|
|
|
if temp == dropped_quartets:
|
2021-01-13 17:50:01 +01:00
|
|
|
winners[i] = dropped_quartets
|
2021-01-13 18:05:05 +01:00
|
|
|
|
|
|
|
# winners then functions as a template
|
|
|
|
# to print all the players' names who have won
|
2021-01-13 17:50:01 +01:00
|
|
|
for i in range(len(winners)):
|
|
|
|
if winners[i] == temp:
|
|
|
|
print(players[i], "has won the game!")
|
2021-01-12 10:41:48 +01:00
|
|
|
|
2021-01-13 18:36:39 +01:00
|
|
|
|
2021-01-12 10:41:48 +01:00
|
|
|
# Call central_function() only if quartett.py is the main module
|
|
|
|
if __name__ == "__main__":
|
2021-01-12 20:35:03 +01:00
|
|
|
central_function()
|