Advertisement
iStrzalka

BlackJack (PL ver)

Sep 16th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.53 KB | None | 0 0
  1. from time import sleep
  2.  
  3. RANKS = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
  4. SUITS = ["H","D","S","C"]
  5.  
  6. class card(object):
  7.     def __init__(self, rank, suit):
  8.         self.rank = rank
  9.         self.suit = suit
  10.  
  11.     def __str__(self):
  12.         ret = self.rank + self.suit
  13.         return ret
  14.  
  15.     def value(self):
  16.         v = RANKS.index(self.rank) + 2
  17.         #return (RANKS.index(self.rank) + 1)
  18.         if v > 10 and v != 14:
  19.             v = 10
  20.         elif v == 14:
  21.             v = 11
  22.         return v
  23.  
  24. class deck(object):
  25.     def __init__(self):
  26.         self.deck = []
  27.  
  28.     def new(self):
  29.         self.deck = []
  30.         for suit in SUITS:
  31.             for rank in RANKS:
  32.                 self.deck.append(card(rank,suit))
  33.         import random
  34.         random.shuffle(self.deck)
  35.  
  36.     def __str__(self):
  37.         ret = ""
  38.         for card in self.deck:
  39.             ret += str(card) + " "
  40.         return ret
  41.    
  42.     def remove(self,card,other_hand):
  43.         if deck.deck == []:
  44.             print("Koniec kart")
  45.         else:
  46.             self.deck.remove(card)
  47.             other_hand.add(card)
  48.    
  49.     def deal(self,players,computer,times):
  50.         for i in range(times):
  51.             for player in players:
  52.                 self.remove(self.deck[0],player)
  53.             self.remove(self.deck[0],computer)
  54.         print("Zostalo " + str(len(self.deck)) + " kart")
  55.                
  56. class hand(object):
  57.     def __init__(self, name):
  58.         self.hand = []
  59.         self.bust = False
  60.         self.name = name
  61.        
  62.     def add(self,card):
  63.         self.hand.append(card)
  64.  
  65.     def clear(self):
  66.         self.hand = []
  67.  
  68.     def __str__(self):
  69.         if self.hand != []:
  70.             ret = ""
  71.             for card in self.hand:
  72.                 ret += str(card) + " "
  73.         else:
  74.             ret = "pusta"
  75.         return ret
  76.  
  77.     def remove(self,card,other_hand):
  78.         self.hand.remove(card)
  79.         other_hand.add(card)
  80.  
  81.     def get_card(self,deck):
  82.         deck.remove(deck.deck[0],self)
  83.  
  84.     def value(self):
  85.         value_of_cards = 0
  86.         for card in self.hand:
  87.             v = card.value()
  88.             value_of_cards += v
  89.         return value_of_cards
  90.  
  91.  
  92. def ask_number(question):
  93.     ret = input(question)
  94.     while ret not in range(1,7):
  95.         ret = input(question)
  96.     return ret
  97.  
  98. def ask_yorn(question):
  99.     ret = raw_input(question + " (T/N): ")
  100.     while ret.upper() not in ["T","N"]:
  101.         ret = raw_input(question + " (T/N): ")
  102.     return ret.upper()
  103.  
  104. def is_busted(player):
  105.     if player.value() > 21:
  106.         return True
  107.     return False
  108.  
  109.  
  110.  
  111. def reverse(number):
  112.     if number > 0:
  113.         return number
  114.     else:
  115.         number -= number + number
  116.         return number
  117.  
  118. class player(hand):
  119.     #def __init__(self, name):
  120.         #self.bust = False
  121.         #self.name = name
  122.  
  123.     def reset(self):
  124.         self.bust = False
  125.        
  126.     def play(self, deck):
  127.         aight = "T"
  128.         while aight != "N":
  129.             print(self.name + ", Twoje karty to: " + str(player) + "[" + str(self.value()) + "]")
  130.             aight = ask_yorn("Czy chcesz dobrac karte?")
  131.             if aight == "T": player.get_card(deck)
  132.             if aight == "N": break
  133.             sleep(0.5)
  134.             if is_busted(player) == True:
  135.                 self.bust = True
  136.                 print(self.name + ", Twoje karty to: " + str(player) + "(" + str(self.value()) + ")")
  137.                 sleep(1)
  138.                 print("Przykro mi, masz fure")
  139.                 aight = "N"
  140.                 break
  141.  
  142. class computer(hand):
  143.     def reset(self):
  144.         self.bust = False
  145.  
  146.     def play(self,deck):
  147.         stop = False
  148.         while stop != True:
  149.             print("Hmm, moje karty to: " + str(computer) + "(" + str(self.value()) + ")")
  150.             sleep(2)
  151.             if computer.value() <= 17:
  152.                 print("Dobiore...")
  153.                 computer.get_card(deck)
  154.             else:
  155.                 print("Zostane przy tym co mam...")
  156.                 stop = True
  157.         if is_busted(computer) == True:self.bust = True
  158.  
  159. def sort_by_value(not_busted):
  160.     value_town = []
  161.     for player in not_busted:
  162.         value_town.append([player.value(),player.name])
  163.     value_town.sort(reverse = True)
  164.     return value_town
  165.  
  166. def winner(players,computer):
  167.     not_busted = []
  168.     winners = []
  169.     for player in players:
  170.         if player.bust is False:not_busted.append(player)
  171.     if computer.bust is False:not_busted.append(computer)
  172.  
  173.     if len(not_busted) == 0:
  174.         print("Nikt nie wygral...")
  175.         return
  176.    
  177.     elif len(not_busted) > 0:
  178.         not_busted = sort_by_value(not_busted)
  179.         winning_value = not_busted[0][0]
  180.         for player in not_busted:
  181.             if player[0] == winning_value:
  182.                 winners.append(player)
  183.  
  184.     if len(winners) > 1:
  185.         w = ""
  186.         for player in winners:
  187.             w += player[0][1] + ", "
  188.         print("Wygrywaja " + w + "Gratulacje!")
  189.     else:
  190.         if computer.name in winners:
  191.             print("Znowu wygralem... Jak zwykle...")
  192.         else:
  193.             print("Wygral " + winners[0][1] + ", Gratulacje!")
  194.  
  195.  
  196. print("\t\t\tWitam w grze BlackJack")
  197. print("Jest to gra w ktorej kazdy gracz ma za zadanie uzbierac 21 punktow")
  198. print("Punktacja wyglada nastepujaco:")
  199. print("2-10 => tyle ile ma na karcie")
  200. print("J,Q,K => 10")
  201. print("A => 11\n")
  202.  
  203. num_players = ask_number("Podaj cyfre graczy [1-7]: ")
  204. players = []
  205. for i in range(num_players):
  206.     anwser = "N"
  207.     while anwser != "T":
  208.         name = raw_input("\nPodaj nazwe uzytkownika numer " + str(i+1) + ": ")
  209.         anwser = ask_yorn("Czy ta nazwa Ci pasuje? (" + name + ")")
  210.     playerino = player(name)
  211.     players.append(playerino)
  212.  
  213. deck = deck()
  214. deck.new()
  215. computer = computer("Dealer")
  216.  
  217. koniec_gry = "N"
  218. x = 1
  219. while koniec_gry != "T":
  220.     deck.deal(players,computer,2)
  221.     print("\n")
  222.     for player in players:
  223.         player.play(deck)
  224.         print("\n")
  225.     computer.play(deck)
  226.     print("\n")
  227.     winner(players,computer)
  228.     koniec_gry = ask_yorn("Czy chcesz zakonczyc gre?")
  229.     if koniec_gry == "T": break
  230.     for player in players:
  231.         player.clear()
  232.         player.reset()
  233.     computer.clear()
  234.     computer.reset()
  235.     if len(deck.deck) < 20:
  236.         refill = ask_yorn("Czy potrzeba odnowic deck? [Pozostalo " + str(len(deck.deck)) + " kart]")
  237.         if refill == "T": deck.new()
  238.     x+=1
  239.     print("W takim razie rozpoczynamy " + str(x) + " gre")
  240.    
  241. raw_input("Nacisnij Enter by zakonczyc gre")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement