Last active
March 17, 2016 14:19
-
-
Save dekoza/2193216b0f827759db8a to your computer and use it in GitHub Desktop.
Przykładowe zadanie z kartami
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Karta(object): | |
def __init__(self, kolor, figura): | |
self.kolor = kolor | |
self.figura = figura | |
def wypisz(self): | |
kolory = ['♡', '♢', '♠', '♣'] | |
figury = {1: 'A', 11: 'W', 12: 'D', 13: 'K'} | |
return "{figura}{kolor}".format( | |
figura=figury.get(self.figura, self.figura), | |
kolor=kolory[self.kolor]) | |
def __str__(self): | |
return self.wypisz() | |
class Talia(object): | |
def __init__(self): | |
self.karty = [] | |
def wypisz(self): | |
return ", ".join(karta.wypisz() for karta in self.karty) | |
#### podziękowania dla p. Jakuba Bronowskiego za przypomnienie o sklejaniu :) | |
# result = "" | |
# for i, karta in enumerate(self.karty): | |
# result += karta.wypisz() | |
# if i < len(self.karty)-1: | |
# result += ", " | |
# return result | |
def __str__(self): | |
return self.wypisz() | |
def pop(self, i=-1): | |
return self.karty.pop(i) | |
def wydaj(self, i=-1): | |
return self.pop(i) | |
def append(self, obj): | |
self.karty.append(obj) | |
def insert(self, index, obj): | |
self.karty.insert(index, obj) | |
def dodaj(self, obj, index=None): | |
if index is None: | |
self.append(obj) | |
else: | |
self.insert(index, obj) | |
def tasuj(self): | |
random.shuffle(self.karty) | |
# for i in range(100): | |
# j = random.randrange(0, len(self.karty)) | |
# k = random.randrange(0, len(self.karty)) | |
# self.karty[j], self.karty[k] = self.karty[k], self.karty[j] | |
class Gracz(Talia): | |
def __init__(self, imie): | |
self.imie = imie | |
super().__init__() | |
def wypisz(self): | |
return "Gracz {imie}: {talia}".format(imie=self.imie, | |
talia=super().wypisz()) | |
if __name__ == '__main__': | |
talia = Talia() | |
for kolor in range(4): | |
for figura in range(1,14): | |
talia.dodaj(Karta(kolor, figura)) | |
talia.tasuj() | |
ilu_graczy = 0 | |
while ilu_graczy < 2 or ilu_graczy > 5: | |
ilu_graczy = int(input("ilu graczy? (2-5) ")) | |
imiona = ["Tomek", "Janek", "Adam", "Stefan", "Izydor"] | |
gracze = [Gracz(imiona[i]) for i in range(ilu_graczy)] | |
# gracze = [] | |
# for gracz in range(ilu_graczy): | |
# gracze.append(Gracz(imiona[gracz])) | |
for gracz in gracze: | |
for i in range(8): | |
gracz.dodaj(talia.wydaj()) | |
print(gracz) | |
print("Pozostałe karty: {talia}".format(talia=talia)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment