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 random
import clean_input import clean_input
import itertools
import copy import copy
# Game # Game
def central_function(): def central_function():
@ -20,25 +20,31 @@ def central_function():
switch = True switch = True
card_stack, players_with_cards, players = initialize() card_stack, players_with_cards, players, complete_card_stack = initialize()
print("players",players) # debug
print("card stack", card_stack) # debug # stores the dropped cards
for i in range(len(players)): # debug all_quartets = []
pretty_print_deck(players_with_cards, i) # debug # print("players",players) # debug
print("active", active) # debug # print("card stack", card_stack) # debug
drop_cards(players_with_cards) # 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: 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 for i in range(len(players)): # check if any card deck is empty
switch = bool(players_with_cards[i]['cards_on_hand']) 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() # 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 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
@ -48,29 +54,48 @@ def round(card_stack, players_with_cards, players, active):
switch = True switch = True
while switch: while switch:
print(active)
players_without_active = copy.copy(players) players_without_active = copy.copy(players)
players_without_active.pop(active) 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']: if players[active] == "player0": # human player
steal(active, chosen_player, players_with_cards, player_request) print('Folgende Spieler stehen zur Verfügung:')
drop_cards(players_with_cards, active) 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: if player_request in players_with_cards[chosen_player]['cards_on_hand']:
print("Diese Karte hat der Spieler nicht, Ende der Runde") steal(active, chosen_player, players_with_cards, player_request)
# end of round drop_cards(players_with_cards, active)
# except if stack is not empty, which is not only the case with two players
if card_stack == True: else:
# last card from the stack gets added to active player's hand print("Diese Karte hat der Spieler nicht. Der nächste Spieler ist dran...")
players_with_cards[active]['cards_on_hand'].append(card_stack.pop()) # 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): 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']) pretty_deck.append(card['number'] + card['letter'])
print("Dein Deck: ", pretty_deck) print("Dein Deck: ", pretty_deck)
def steal(active, chosen_player, players_with_cards, player_request): 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
:return: None :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) card_index = players_with_cards[chosen_player]['cards_on_hand'].index(player_request)
players_with_cards[active]['cards_on_hand'].append( players_with_cards[active]['cards_on_hand'].append(
players_with_cards[chosen_player]['cards_on_hand'].pop(card_index)) players_with_cards[chosen_player]['cards_on_hand'].pop(card_index))
pretty_print_deck(players_with_cards, active) 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 function that lets players drop cards
if they have a quartet in their deck if they have a quartet in their deck
@ -104,14 +131,16 @@ def drop_cards(players_with_cards, active=None):
# default, check all players # default, check all players
if active is None: if active is None:
for p in players_with_cards: for p in players_with_cards:
drop_cards_help(p) drop_cards_help(p, all_quartets)
else: else:
p = players_with_cards[active] 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 eh
""" """
@ -127,10 +156,11 @@ def drop_cards_help(p):
counter[counter.index(i)] = 0 counter[counter.index(i)] = 0
p["quartet"] += 1 p["quartet"] += 1
print(f"{p['player']} legt das {quartet_letter}-Quartett ab...") 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"]] p["cards_on_hand"] = [c for c in p["cards_on_hand"] if quartet_letter != c["letter"]]
# print(p) return p, all_quartets
return p
def initialize(): def initialize():
@ -151,8 +181,13 @@ def initialize():
card_stack.append(d) card_stack.append(d)
x += 1 x += 1
complete_card_stack = copy.copy(card_stack)
# determine number of players # determine number of players
number_of_players = clean_input.io_int("Gegen wie viele Spieler wollen Sie spielen?") 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. number_of_players += 1 # Add also human player.
for i in range(number_of_players): for i in range(number_of_players):
@ -177,7 +212,7 @@ def initialize():
players_with_cards.append( players_with_cards.append(
{"player": players[i], "cards_on_hand": cards_of_player, "quartet": 0}) {"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(): def the_winner_is():
@ -193,7 +228,7 @@ def the_winner_is():
# Call central_function() only if quartett.py is the main module # Call central_function() only if quartett.py is the main module
if __name__ == "__main__": if __name__ == "__main__":
central_function() central_function()
"""p = {'player': 'player1', 'cards_on_hand': [{'id': '27', 'number': '3', 'letter': 'd'}, """p = {'player': 'player1', 'cards_on_hand': [{'id': '27', 'number': '3', 'letter': 'd'},
{'id': '3', 'number': '0', 'letter': 'd'}, {'id': '3', 'number': '0', 'letter': 'd'},
{'id': '25', 'number': '3', 'letter': 'd'}, {'id': '25', 'number': '3', 'letter': 'd'},
@ -207,7 +242,6 @@ if __name__ == "__main__":
'quartet': 0} 'quartet': 0}
drop_cards_help(p)""" drop_cards_help(p)"""
''' '''
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'},