create config class, read from config file

This commit is contained in:
Bela 2019-08-26 17:03:22 +02:00
parent 31e45074f3
commit 80dbd1a8dd

View file

@ -1,45 +1,59 @@
import random as r
import os
import configparser
class Config:
def __init__(self, path):
"""Create config object.
Args:
path (path/string): Path to the config file.
"""
config = configparser.ConfigParser()
config.read(path)
class playcard:
def __init__(self, name, cost, expansion):
self.name = name
self.cost = cost
self.expansion = expansion
(_, _, filenames) = next(os.walk("cards"))
cards = {}
for filename in filenames:
with open(os.path.join("cards", filename)) as file:
expansion = file.readline().strip()
cardset = [[] for i in range(4)]
for line in file.readlines():
cost, card = line.split()
cost = min(int(cost), 5)
cardset[cost-2].append(playcard(card, cost, expansion))
cards[expansion] = cardset
print("Found expansions:")
for exp in cards.keys():
print("\t" + exp)
print("Type the expansions you want to use seperated by space.")
chosen = input().strip().split()
chosen_cards = [[] for i in range(4)]
for exp in chosen:
if __name__ == "__main__":
(_, _, filenames) = next(os.walk("cards"))
cards = {}
for filename in filenames:
with open(os.path.join("cards", filename)) as file:
expansion = file.readline().strip()
cardset = [[] for i in range(4)]
for line in file.readlines():
cost, card = line.split()
cost = min(int(cost), 5)
cardset[cost-2].append(playcard(card, cost, expansion))
cards[expansion] = cardset
print("Found expansions:")
for exp in cards.keys():
print("\t" + exp)
print("Type the expansions you want to use seperated by space.")
chosen = input().strip().split()
chosen_cards = [[] for i in range(4)]
for exp in chosen:
for i in range(4):
chosen_cards[i].extend(cards[exp][i])
print("How many cards of costs 2, 3, 4, >=5 do you want (seperated by space)?")
cardcounts = list(map(int, input().split()))
playcards = []
for i, count in enumerate(cardcounts):
playcards.extend(r.sample(chosen_cards[i], count))
remaining_cards = []
for i in range(4):
chosen_cards[i].extend(cards[exp][i])
print("How many cards of costs 2, 3, 4, >=5 do you want (seperated by space)?")
cardcounts = list(map(int, input().split()))
playcards = []
for i, count in enumerate(cardcounts):
playcards.extend(r.sample(chosen_cards[i], count))
remaining_cards = []
for i in range(4):
for card in chosen_cards[i]:
if not card in playcards:
remaining_cards.append(card)
if sum(cardcounts) < 10:
playcards.extend(r.sample(remaining_cards, 10 - sum(cardcounts)))
print("Use the following cards for your game:")
for card in playcards:
print("\t" + str(card.cost) + "\t" + card.name + " (" + card.expansion + ")")
for card in chosen_cards[i]:
if not card in playcards:
remaining_cards.append(card)
if sum(cardcounts) < 10:
playcards.extend(r.sample(remaining_cards, 10 - sum(cardcounts)))
print("Use the following cards for your game:")
for card in playcards:
print("\t" + str(card.cost) + "\t" + card.name + " (" + card.expansion + ")")