changed active player rotation/selection in central_function(), added AI branch to round(), started to implement all_quartets (a list of all dropped cards)

This commit is contained in:
Steinadler 2021-01-13 16:30:10 +01:00
parent 484de50a83
commit 7937adc402
1 changed files with 74 additions and 40 deletions

View File

@ -6,9 +6,9 @@ __email__ = ""
import random
import clean_input
import itertools
import copy
# Game
def central_function():
@ -20,25 +20,31 @@ def central_function():
switch = True
card_stack, players_with_cards, players = initialize()
print("players",players) # debug
print("card stack", card_stack) # 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)
card_stack, players_with_cards, players, complete_card_stack = initialize()
# stores the dropped cards
all_quartets = []
# print("players",players) # debug
# print("card stack", card_stack) # 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)
round(card_stack, players_with_cards, players, active, complete_card_stack)
for i in range(len(players)): # check if any card deck is empty
switch = bool(players_with_cards[i]['cards_on_hand'])
print(switch)
active += 1
if active >= len(players):
active = 0
#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):
def round(card_stack, players_with_cards, players, active, complete_card_stack):
'''
structures one round in the game
active player chooses another player from whom to steal a card
@ -48,29 +54,48 @@ def round(card_stack, players_with_cards, players, active):
switch = True
while switch:
print(active)
players_without_active = copy.copy(players)
players_without_active.pop(active)
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))
print("chosen player", chosen_player)
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)
drop_cards(players_with_cards, active)
if players[active] == "player0": # human player
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))
print("chosen player", chosen_player)
player_request = clean_input.io_card_selection('Welche Karte möchtest du haben? ')
else:
print("Diese Karte hat der Spieler nicht, Ende der Runde")
# end of round
# except if stack is not empty, which is not only the case with two players
if card_stack == True:
# last card from the stack gets added to active player's hand
players_with_cards[active]['cards_on_hand'].append(card_stack.pop())
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:
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
if card_stack == True:
# 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"]]
# TODO: check that all_quartets is consistent in the whole module
# cards_to_choose_from = [c for c in cards_to_choose_from if
# c not in all_quartets]
player_request = random.choice(cards_to_choose_from)
print("AI choose:", chosen_player)
switch = False
def pretty_print_deck(players_with_cards, player):
"""
@ -82,19 +107,21 @@ def pretty_print_deck(players_with_cards, player):
pretty_deck.append(card['number'] + card['letter'])
print("Dein Deck: ", pretty_deck)
def steal(active, chosen_player, players_with_cards, player_request):
"""
defines how the active players steals cards from chosen player
:return: None
"""
print("player", chosen_player, "hat die Karte, die du haben möchtest und sie wandert in dein Deck.")
print("player", chosen_player,
"hat die Karte, die du haben möchtest und sie wandert in dein 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, active=None):
def drop_cards(players_with_cards, all_quartets, active=None):
"""
function that lets players drop cards
if they have a quartet in their deck
@ -104,14 +131,16 @@ def drop_cards(players_with_cards, active=None):
# default, check all players
if active is None:
for p in players_with_cards:
drop_cards_help(p)
drop_cards_help(p, all_quartets)
else:
p = players_with_cards[active]
drop_cards_help(p)
drop_cards_help(p, all_quartets)
return all_quartets
def drop_cards_help(p):
def drop_cards_help(p, all_quartets):
"""
eh
"""
@ -127,10 +156,11 @@ def drop_cards_help(p):
counter[counter.index(i)] = 0
p["quartet"] += 1
print(f"{p['player']} legt das {quartet_letter}-Quartett ab...")
all_quartets.append(c for c in p["cards_on_hand"] if quartet_letter == c["letter"])
p["cards_on_hand"] = [c for c in p["cards_on_hand"] if quartet_letter != c["letter"]]
# print(p)
return p
return p, all_quartets
def initialize():
@ -151,8 +181,13 @@ def initialize():
card_stack.append(d)
x += 1
complete_card_stack = copy.copy(card_stack)
# determine number of players
number_of_players = clean_input.io_int("Gegen wie viele Spieler wollen Sie spielen?")
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?")
number_of_players += 1 # Add also human player.
for i in range(number_of_players):
@ -177,7 +212,7 @@ def initialize():
players_with_cards.append(
{"player": players[i], "cards_on_hand": cards_of_player, "quartet": 0})
return card_stack, players_with_cards, players
return card_stack, players_with_cards, players, complete_card_stack
def the_winner_is():
@ -193,7 +228,7 @@ def the_winner_is():
# 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'},
@ -207,7 +242,6 @@ if __name__ == "__main__":
'quartet': 0}
drop_cards_help(p)"""
'''
players_with_cards = [
{'player': 'player0', 'cards_on_hand': [{'id': 13, 'number': 1, 'letter': 'f'},