Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # card_game.py (in Python v3.5)
- import random
- class card():
- def __init__(self,val,sym):
- self.value=val
- self.symbol=sym
- if self.symbol in "sc": self.color='B'
- else: self.color='R'
- if self.value in "JQK": self.fig=True
- else: self.fig=False
- def __str__(self):
- return self.value+self.symbol
- def detailed_info(self):
- print('Αξία=',self.value, ' Σύμβολο=',self.symbol)
- print('Χρώμα=',self.color, ' Φιγούρα=',self.fig)
- class deck():
- values="A23456789TJQK" # Όλες οι αξίες
- symbols="shcd" # Όλα τα σύμβολα
- def __init__(self):
- self.content=[] # Χαρτιά που βρίσκονται στην τράπουλα
- self.pile=[] # Χαρτιά που βγήκαν από την τράπουλα
- for s in deck.symbols:
- for v in deck.values:
- c=card(v,s)
- self.content.append(c)
- def __str__(self):
- s=""
- cntr=0
- for i in self.content:
- s=s+str(i)+" "
- cntr=cntr+1
- if cntr%13==0: s=s+'\n'
- if s[-1]!='\n': s=s+'\n'
- s=s+str(len(self.content))+"-"+str(len(self.pile))
- return s
- def shuffle(self):
- random.shuffle(self.content)
- def draw(self):
- if len(self.content)<1: return "empty"
- c=self.content[0]
- self.content=self.content[1:]
- self.pile.append(c)
- return c
- def collect(self):
- self.content=self.content+self.pile
- self.pile=[]
- class pack(deck):
- def __init__(self,number_of_decks):
- d=deck()
- self.content=d.content*number_of_decks
- self.pile=[]
- class computer_players():
- def __init__(self):
- self.computer_hand = []
- def computer_plays(self):
- global d, table, what_happened
- target_val=table[-1].value
- target_sym=table[-1].symbol
- for i in range(0, len(self.computer_hand)):
- print(self.computer_hand[i], end=", ")
- print("")
- while True:
- for c in self.computer_hand:
- if c.value==target_val or c.symbol==target_sym:
- print("Ο Η/Υ ρίχνει",c)
- self.computer_hand.remove(c)
- table.append(c)
- what_happened="computer_played"
- return
- new_card=d.draw()
- if new_card=="empty":
- what_happened="deck_finished"
- return
- else:
- print("Ο Η/Υ τραβάει φύλλο.")
- self.computer_hand.append(new_card)
- def firstDraw(self):
- self.computer_hand.append(d.draw())
- if len(self.computer_hand) == 7:
- for i in range(0, 7):
- print(self.computer_hand[i], end=", ")
- print("")
- ####################################################
- def print_info():
- global d, table, computer_hand, human_hand
- print()
- print("Η τράπουλα έχει", len(d.content), "φύλλα")
- for i in range(0, len(player)):
- print("Ο Η/Υ + " + str(i + 1) + " έχει", len(player[i].computer_hand), "φύλλα")
- print("Στο τραπέζι έχουν πέσει", len(table), "φύλλα")
- print("Το πάνω φύλλο είναι",table[-1])
- print("Τα φύλλα σου είναι",len(human_hand))
- HHS=[str(x) for x in human_hand]
- print(HHS)
- print()
- print("Διάλεξε ποιό θα πετάξεις")
- print("ή πάτα σκέτο ENTER για να τραβήξεις")
- def human_plays():
- global d, table, human_hand, what_happened
- while True:
- print_info()
- sel=input()
- if sel=="":
- new_card=d.draw()
- if new_card=="empty":
- what_happened="deck_finished"
- return
- else:
- human_hand.append(new_card)
- else:
- HHS=[str(x) for x in human_hand]
- if not(sel in HHS):
- print(sel, "??? Δεν έχεις τέτοιο φύλλο.")
- else:
- t=table[-1]
- target_val=t.value
- target_sym=t.symbol
- if sel[0]!=target_val and sel[1]!=target_sym:
- print("Δεν επιτρέπεται να ρίξεις το φύλλο ",sel)
- else:
- print("Ρίχνεις το",sel)
- ind=HHS.index(sel)
- selc=human_hand[ind]
- human_hand.remove(selc)
- table.append(selc)
- what_happened="human_played"
- return
- def initial():
- global d, table, human_hand, what_happened
- for i in range(0, x):
- player.append(computer_players())
- print("Μαζεύω τα χαρτιά...")
- d.collect()
- table=[]
- human_hand=[]
- print("Ανακατεύω την τράπουλα...")
- d.shuffle()
- print("Μοιράζω τα φύλλα...")
- table.append(d.draw())
- print("Στο τραπέζι έπεσε",table[-1])
- for i in range(0, x):
- for j in range(7):
- player[i].firstDraw()
- for i in range(7):
- human_hand.append(d.draw())
- print("Στρίβω ένα νόμισμα...", end=" ")
- if random.random()<0.5:
- print("...Παίζεις πρώτος")
- what_happened="computer_played"
- else:
- print("...Ο Η/Υ παιζει πρώτος")
- what_happened="human_played"
- def evaluate():
- global computer_hand, human_hand, what_happened
- if what_happened=="human_wins": print("ΣΥΓΧΑΡΗΤΗΡΙΑ. ΚΕΡΔΙΣΕΣ!!!")
- if what_happened=="computer_wins": print("Ο Η/Υ ΚΕΡΔΙΣΕ.")
- if what_happened=="deck_finished":
- ch=len(computer_hand)
- hh=len(human_hand)
- print("Η τράπουλα τελείωσε, ο Η/Υ έχει",ch, "φύλλα και ο παίκτης", hh)
- if ch>hh: print("ΣΥΓΧΑΡΗΤΗΡΙΑ. ΚΕΡΔΙΣΕΣ!!!")
- if ch<hh: print("Ο Η/Υ ΚΕΡΔΙΣΕ.")
- if ch==hh: print("ΙΣΟΠΑΛΙΑ!!!")
- print()
- def next_turn():
- global what_happened
- while True:
- if what_happened=="game_start":
- initial()
- elif what_happened=="human_played":
- if len(human_hand)==0:
- what_happened="human_wins"
- evaluate()
- break
- print()
- for i in range(0, len(player)):
- print("--------------------- ΣΕΙΡΑ Η/Υ " + str(i + 1) + " ----------------------")
- print()
- player[i].computer_plays()
- elif what_happened=="computer_played":
- for i in range(0, len(player) - 1):
- if len(player[i].computer_hand)==0:
- what_happened="computer_wins"
- evaluate()
- break
- print()
- print("-------------------- ΣΕΙΡΑ ΠΑΙΚΤΗ --------------------")
- human_plays()
- elif what_happened=="deck_finished":
- evaluate()
- break
- # ΚΥΡΙΩΣ ΠΡΟΓΡΑΜΜΑ
- print("ΠΑΙΧΝΙΔΙ ΜΕ ΤΡΑΠΟΥΛΟΧΑΡΤΑ (έκδοση 1)")
- print("====================================")
- print()
- # ΚΑΘΟΛΙΚΕΣ ΜΕΤΑΒΛΗΤΕΣ
- player = []
- x = int(input('NUM OF PLAYERS??'))
- d=pack(x + 1)
- table=[]
- computer_hand=[]
- human_hand=[]
- what_happened=""
- again="y"
- while again=="y":
- what_happened="game_start"
- next_turn()
- print("Θέλεις να ξαναπαίξουμε;")
- again=input("γράψε y ή n: ")
- print("Τέλος Προγράμματος")
- # --------------------------------------------------
- # card_game.py (educational version 2.1) in Python 3.5
- # 9/6/2016 (25/8/2010) K.Sgarbas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement