Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- RANKS = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
- SUITS = ["H","D","S","C"]
- class card(object):
- def __init__(self, rank, suit):
- self.rank = rank
- self.suit = suit
- def __str__(self):
- ret = self.rank + self.suit
- return ret
- def value(self):
- v = RANKS.index(self.rank) + 2
- #return (RANKS.index(self.rank) + 1)
- if v > 10 and v != 14:
- v = 10
- elif v == 14:
- v = 11
- return v
- class deck(object):
- def __init__(self):
- self.deck = []
- def new(self):
- self.deck = []
- for suit in SUITS:
- for rank in RANKS:
- self.deck.append(card(rank,suit))
- import random
- random.shuffle(self.deck)
- def __str__(self):
- ret = ""
- for card in self.deck:
- ret += str(card) + " "
- return ret
- def remove(self,card,other_hand):
- if deck.deck == []:
- print("Koniec kart")
- else:
- self.deck.remove(card)
- other_hand.add(card)
- def deal(self,players,computer,times):
- for i in range(times):
- for player in players:
- self.remove(self.deck[0],player)
- self.remove(self.deck[0],computer)
- print("Zostalo " + str(len(self.deck)) + " kart")
- class hand(object):
- def __init__(self, name):
- self.hand = []
- self.bust = False
- self.name = name
- def add(self,card):
- self.hand.append(card)
- def clear(self):
- self.hand = []
- def __str__(self):
- if self.hand != []:
- ret = ""
- for card in self.hand:
- ret += str(card) + " "
- else:
- ret = "pusta"
- return ret
- def remove(self,card,other_hand):
- self.hand.remove(card)
- other_hand.add(card)
- def get_card(self,deck):
- deck.remove(deck.deck[0],self)
- def value(self):
- value_of_cards = 0
- for card in self.hand:
- v = card.value()
- value_of_cards += v
- return value_of_cards
- def ask_number(question):
- ret = input(question)
- while ret not in range(1,7):
- ret = input(question)
- return ret
- def ask_yorn(question):
- ret = raw_input(question + " (T/N): ")
- while ret.upper() not in ["T","N"]:
- ret = raw_input(question + " (T/N): ")
- return ret.upper()
- def is_busted(player):
- if player.value() > 21:
- return True
- return False
- def reverse(number):
- if number > 0:
- return number
- else:
- number -= number + number
- return number
- class player(hand):
- #def __init__(self, name):
- #self.bust = False
- #self.name = name
- def reset(self):
- self.bust = False
- def play(self, deck):
- aight = "T"
- while aight != "N":
- print(self.name + ", Twoje karty to: " + str(player) + "[" + str(self.value()) + "]")
- aight = ask_yorn("Czy chcesz dobrac karte?")
- if aight == "T": player.get_card(deck)
- if aight == "N": break
- sleep(0.5)
- if is_busted(player) == True:
- self.bust = True
- print(self.name + ", Twoje karty to: " + str(player) + "(" + str(self.value()) + ")")
- sleep(1)
- print("Przykro mi, masz fure")
- aight = "N"
- break
- class computer(hand):
- def reset(self):
- self.bust = False
- def play(self,deck):
- stop = False
- while stop != True:
- print("Hmm, moje karty to: " + str(computer) + "(" + str(self.value()) + ")")
- sleep(2)
- if computer.value() <= 17:
- print("Dobiore...")
- computer.get_card(deck)
- else:
- print("Zostane przy tym co mam...")
- stop = True
- if is_busted(computer) == True:self.bust = True
- def sort_by_value(not_busted):
- value_town = []
- for player in not_busted:
- value_town.append([player.value(),player.name])
- value_town.sort(reverse = True)
- return value_town
- def winner(players,computer):
- not_busted = []
- winners = []
- for player in players:
- if player.bust is False:not_busted.append(player)
- if computer.bust is False:not_busted.append(computer)
- if len(not_busted) == 0:
- print("Nikt nie wygral...")
- return
- elif len(not_busted) > 0:
- not_busted = sort_by_value(not_busted)
- winning_value = not_busted[0][0]
- for player in not_busted:
- if player[0] == winning_value:
- winners.append(player)
- if len(winners) > 1:
- w = ""
- for player in winners:
- w += player[0][1] + ", "
- print("Wygrywaja " + w + "Gratulacje!")
- else:
- if computer.name in winners:
- print("Znowu wygralem... Jak zwykle...")
- else:
- print("Wygral " + winners[0][1] + ", Gratulacje!")
- print("\t\t\tWitam w grze BlackJack")
- print("Jest to gra w ktorej kazdy gracz ma za zadanie uzbierac 21 punktow")
- print("Punktacja wyglada nastepujaco:")
- print("2-10 => tyle ile ma na karcie")
- print("J,Q,K => 10")
- print("A => 11\n")
- num_players = ask_number("Podaj cyfre graczy [1-7]: ")
- players = []
- for i in range(num_players):
- anwser = "N"
- while anwser != "T":
- name = raw_input("\nPodaj nazwe uzytkownika numer " + str(i+1) + ": ")
- anwser = ask_yorn("Czy ta nazwa Ci pasuje? (" + name + ")")
- playerino = player(name)
- players.append(playerino)
- deck = deck()
- deck.new()
- computer = computer("Dealer")
- koniec_gry = "N"
- x = 1
- while koniec_gry != "T":
- deck.deal(players,computer,2)
- print("\n")
- for player in players:
- player.play(deck)
- print("\n")
- computer.play(deck)
- print("\n")
- winner(players,computer)
- koniec_gry = ask_yorn("Czy chcesz zakonczyc gre?")
- if koniec_gry == "T": break
- for player in players:
- player.clear()
- player.reset()
- computer.clear()
- computer.reset()
- if len(deck.deck) < 20:
- refill = ask_yorn("Czy potrzeba odnowic deck? [Pozostalo " + str(len(deck.deck)) + " kart]")
- if refill == "T": deck.new()
- x+=1
- print("W takim razie rozpoczynamy " + str(x) + " gre")
- raw_input("Nacisnij Enter by zakonczyc gre")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement