Advertisement
tsounakis

PAIZEI_NA_DOULEVEI

Nov 23rd, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.00 KB | None | 0 0
  1. # card_game.py (in Python v3.5)
  2. import random
  3.  
  4. class card():
  5.     def __init__(self,val,sym):
  6.         self.value=val
  7.         self.symbol=sym
  8.         if self.symbol in "sc": self.color='B'
  9.         else: self.color='R'
  10.         if self.value in "JQK": self.fig=True
  11.         else: self.fig=False
  12.     def __str__(self):
  13.         return self.value+self.symbol
  14.     def detailed_info(self):
  15.         print('Αξία=',self.value, '    Σύμβολο=',self.symbol)
  16.         print('Χρώμα=',self.color, '    Φιγούρα=',self.fig)
  17.    
  18. class deck():
  19.     values="A23456789TJQK"  # Όλες οι αξίες
  20.     symbols="shcd"          # Όλα τα σύμβολα
  21.     def __init__(self):
  22.         self.content=[]     # Χαρτιά που βρίσκονται στην τράπουλα
  23.         self.pile=[]        # Χαρτιά που βγήκαν από την τράπουλα
  24.         for s in deck.symbols:
  25.             for v in deck.values:
  26.                 c=card(v,s)
  27.                 self.content.append(c)
  28.     def __str__(self):
  29.         s=""
  30.         cntr=0
  31.         for i in self.content:
  32.             s=s+str(i)+" "
  33.             cntr=cntr+1
  34.             if cntr%13==0: s=s+'\n'
  35.         if s[-1]!='\n': s=s+'\n'
  36.         s=s+str(len(self.content))+"-"+str(len(self.pile))
  37.         return s
  38.     def shuffle(self):
  39.         random.shuffle(self.content)
  40.     def draw(self):
  41.         if len(self.content)<1: return "empty"
  42.         c=self.content[0]
  43.         self.content=self.content[1:]
  44.         self.pile.append(c)
  45.         return c
  46.     def collect(self):
  47.         self.content=self.content+self.pile
  48.         self.pile=[]
  49.  
  50. class pack(deck):
  51.     def __init__(self,number_of_decks):
  52.         d=deck()
  53.         self.content=d.content*number_of_decks
  54.         self.pile=[]
  55.  
  56. class computer_players():
  57.     def __init__(self):
  58.         self.computer_hand = []
  59.     def computer_plays(self):
  60.         global d, table, what_happened
  61.         target_val=table[-1].value
  62.         target_sym=table[-1].symbol
  63.         for i in range(0, len(self.computer_hand)):
  64.             print(self.computer_hand[i], end=", ")
  65.         print("")
  66.         while True:
  67.             for c in self.computer_hand:
  68.                 if c.value==target_val or c.symbol==target_sym:
  69.                     print("Ο Η/Υ ρίχνει",c)
  70.                     self.computer_hand.remove(c)
  71.                     table.append(c)
  72.                     what_happened="computer_played"
  73.                     return
  74.             new_card=d.draw()
  75.             if new_card=="empty":
  76.                 what_happened="deck_finished"
  77.                 return
  78.             else:
  79.                 print("Ο Η/Υ τραβάει φύλλο.")
  80.                 self.computer_hand.append(new_card)
  81.     def firstDraw(self):
  82.         self.computer_hand.append(d.draw())
  83.         if len(self.computer_hand) == 7:
  84.             for i in range(0, 7):
  85.                 print(self.computer_hand[i], end=", ")
  86.             print("")
  87.    
  88.  
  89.         ####################################################
  90.  
  91.  
  92.  
  93. def print_info():
  94.     global d, table, computer_hand, human_hand
  95.     print()
  96.     print("Η τράπουλα έχει", len(d.content), "φύλλα")
  97.     for i in range(0, len(player)):
  98.         print("Ο Η/Υ + " + str(i + 1) + " έχει", len(player[i].computer_hand), "φύλλα")
  99.     print("Στο τραπέζι έχουν πέσει", len(table), "φύλλα")
  100.     print("Το πάνω φύλλο είναι",table[-1])
  101.     print("Τα φύλλα σου είναι",len(human_hand))
  102.     HHS=[str(x) for x in human_hand]
  103.     print(HHS)
  104.     print()
  105.     print("Διάλεξε ποιό θα πετάξεις")
  106.     print("ή πάτα σκέτο ENTER για να τραβήξεις")
  107.  
  108. def human_plays():
  109.     global d, table, human_hand, what_happened
  110.     while True:
  111.         print_info()
  112.         sel=input()
  113.         if sel=="":
  114.             new_card=d.draw()
  115.             if new_card=="empty":
  116.                 what_happened="deck_finished"
  117.                 return
  118.             else:
  119.                 human_hand.append(new_card)
  120.         else:
  121.             HHS=[str(x) for x in human_hand]
  122.             if not(sel in HHS):
  123.                 print(sel, "???   Δεν έχεις τέτοιο φύλλο.")
  124.             else:
  125.                 t=table[-1]
  126.                 target_val=t.value
  127.                 target_sym=t.symbol
  128.                 if sel[0]!=target_val and sel[1]!=target_sym:
  129.                     print("Δεν επιτρέπεται να ρίξεις το φύλλο ",sel)
  130.                 else:
  131.                     print("Ρίχνεις το",sel)
  132.                     ind=HHS.index(sel)
  133.                     selc=human_hand[ind]
  134.                     human_hand.remove(selc)
  135.                     table.append(selc)
  136.                     what_happened="human_played"
  137.                     return
  138.  
  139. def initial():
  140.     global d, table, human_hand, what_happened
  141.     for i in range(0, x):
  142.         player.append(computer_players())
  143.     print("Μαζεύω τα χαρτιά...")
  144.     d.collect()
  145.     table=[]
  146.     human_hand=[]
  147.     print("Ανακατεύω την τράπουλα...")
  148.     d.shuffle()
  149.     print("Μοιράζω τα φύλλα...")
  150.     table.append(d.draw())
  151.     print("Στο τραπέζι έπεσε",table[-1])
  152.     for i in range(0, x):
  153.         for j in range(7):
  154.             player[i].firstDraw()
  155.     for i in range(7):
  156.         human_hand.append(d.draw())
  157.     print("Στρίβω ένα νόμισμα...", end=" ")
  158.     if random.random()<0.5:
  159.         print("...Παίζεις πρώτος")
  160.         what_happened="computer_played"
  161.     else:
  162.         print("...Ο Η/Υ παιζει πρώτος")
  163.         what_happened="human_played"
  164.  
  165. def evaluate():
  166.     global computer_hand, human_hand, what_happened
  167.     if what_happened=="human_wins": print("ΣΥΓΧΑΡΗΤΗΡΙΑ. ΚΕΡΔΙΣΕΣ!!!")
  168.     if what_happened=="computer_wins": print("Ο Η/Υ ΚΕΡΔΙΣΕ.")
  169.     if what_happened=="deck_finished":
  170.         ch=len(computer_hand)
  171.         hh=len(human_hand)
  172.         print("Η τράπουλα τελείωσε, ο Η/Υ έχει",ch, "φύλλα και ο παίκτης", hh)
  173.         if ch>hh: print("ΣΥΓΧΑΡΗΤΗΡΙΑ. ΚΕΡΔΙΣΕΣ!!!")
  174.         if ch<hh: print("Ο Η/Υ ΚΕΡΔΙΣΕ.")
  175.         if ch==hh: print("ΙΣΟΠΑΛΙΑ!!!")
  176.     print()
  177.  
  178. def next_turn():
  179.     global what_happened
  180.     while True:
  181.         if what_happened=="game_start":
  182.             initial()
  183.         elif what_happened=="human_played":
  184.             if len(human_hand)==0:
  185.                 what_happened="human_wins"
  186.                 evaluate()
  187.                 break
  188.             print()
  189.             for i in range(0, len(player)):
  190.                 print("--------------------- ΣΕΙΡΑ Η/Υ " + str(i + 1) + " ----------------------")
  191.                 print()
  192.                 player[i].computer_plays()
  193.         elif what_happened=="computer_played":
  194.             for i in range(0, len(player) - 1):
  195.                 if len(player[i].computer_hand)==0:
  196.                     what_happened="computer_wins"
  197.                     evaluate()
  198.                     break
  199.                 print()
  200.             print("-------------------- ΣΕΙΡΑ ΠΑΙΚΤΗ --------------------")
  201.             human_plays()
  202.         elif what_happened=="deck_finished":
  203.             evaluate()
  204.             break
  205.  
  206. # ΚΥΡΙΩΣ ΠΡΟΓΡΑΜΜΑ
  207. print("ΠΑΙΧΝΙΔΙ ΜΕ ΤΡΑΠΟΥΛΟΧΑΡΤΑ (έκδοση 1)")
  208. print("====================================")
  209. print()
  210.  
  211. # ΚΑΘΟΛΙΚΕΣ ΜΕΤΑΒΛΗΤΕΣ
  212. player = []
  213. x = int(input('NUM OF PLAYERS??'))
  214. d=pack(x + 1)
  215. table=[]
  216. computer_hand=[]
  217. human_hand=[]
  218. what_happened=""
  219.  
  220. again="y"
  221. while again=="y":
  222.     what_happened="game_start"
  223.     next_turn()
  224.     print("Θέλεις να ξαναπαίξουμε;")
  225.     again=input("γράψε y ή n: ")
  226. print("Τέλος Προγράμματος")
  227.  
  228.  
  229. # --------------------------------------------------
  230. # card_game.py (educational version 2.1) in Python 3.5
  231. # 9/6/2016 (25/8/2010) K.Sgarbas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement