Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import shuffle as rsh
- values = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10,'A':11}
- class Player:
- '''
- Class Player
- =======
- Input: Name, Balance
- Output(print): Player Info
- =======
- Methods:
- Win/Lose
- Input: Amount
- Output: Balance
- '''
- def __init__(self,name,money):
- self.name = name
- self.balance = money
- self.bet=0
- def __str__(self):
- return 'Player Infon---------nName: {}nBalance: DKK
- {}'.format(self.name,self.balance)
- def win(self):
- self.balance+=self.bet
- def lose(self):
- self.balance-=self.bet
- class Deck:
- '''
- Class Deck
- =======
- Input: nothing
- Output(print): deck, number of cards
- =======
- Methods:
- Create/Show/Shuffle
- Input: nothing
- Output: deck
- '''
- def __init__(self,):
- #start with an empty list
- self.deck = []
- def __str__(self):
- return ''
- def create(self):
- for suit in ['D','C','H','S']:
- for rank in [2,3,4,5,6,7,8,9,10,'J','Q','K','A']:
- deck=self.deck
- deck.append(suit+str(rank))
- def showme(self):
- return 'Cards: {} , Total: {}'.format(self.deck,len(self.deck))
- def shuffle(self):
- rsh(self.deck)
- def deal(self):
- card = self.deck.pop()
- return card
- class Hand:
- '''
- Class Hand
- '''
- def __init__(self,name):
- self.hand = []
- self.value = 0
- self.aces = 0
- self.name = name
- """ def showme(self):
- if len(self.hand) == 0:
- return '{} has the following cards: 0'.format(self.name)
- else:
- return '{} has the following cards:
- {}'.format(self.name,self.hand) """
- def add_card(self,newcard):
- self.hand.append(newcard)
- self.value+=values[newcard[1:]]
- if newcard[1:]=='A':
- self.aces+=1
- def adjust_for_ace(self):
- while self.value >21 and self.aces:
- self.value-=10
- self.aces-=1
- #function for take bet
- def take_bet(player):
- while True:
- try:
- chips=int(input('How many chips do you want to bet? :'))
- if player.balance < chips:
- print('Sorry you dont have the amount! Your balance is
- {}'.format(player.balance))
- else:
- player.bet = chips
- break
- except:
- print('Only integer allowed')
- #function hit
- def hit(deck,hand):
- hand.add_card(deck.deal())
- hand.adjust_for_ace()
- playing = True
- #function hit or stand
- def hit_or_stand(deck,hand):
- global playing
- while playing:
- x = input("Would you like to Hit or Stand? Enter 'h' or 's' : ")
- if x[0].lower() == 's':
- print('nPlayer stands. Dealer is playing')
- playing = False
- elif x[0].lower() == 'h':
- hit(deck,hand)
- else:
- print('Please, try again!')
- continue
- break
- #show functions
- def show_some(playerhand,dealerhand):
- print("Dealer's hand:")
- print('<hidden>')
- print(dealerhand.hand[1])
- print("nPlayer's hand:", *playerhand.hand, sep='n')
- def show_all(playerhand,dealerhand):
- print("nDealers's hand:", *dealerhand.hand, sep='n')
- #print("Total:" + sum())
- print("nPlayer's hand:", *playerhand.hand, sep='n')
- #scenarios
- def player_busts(player):
- print('{} busts!'.format(player.name))
- player.lose()
- def player_blackjack(player):
- print("{} got blackjack!".format(player.name))
- player.win()
- def dealer_blackjack(player,dealer):
- print("{} got blackjack!".format(dealer.name))
- player.lose()
- def player_wins(player):
- print('{} wins!'.format(player.name))
- player.win()
- def dealer_busts(player,dealer):
- print('{} busts'.format(dealer.name))
- player.win()
- def dealer_wins(player,dealer):
- print('{} wins'.format(dealer.name))
- player.lose()
- def tie_game():
- print('Dealer and Player tie!')
- def insert_money():
- while True:
- try:
- ampl = int(input('Insert the amount of DKK for converting
- chips!n1 DKK = 1 chip : '))
- if ampl > 0:
- return ampl
- else:
- print('Only positive Integer!')
- except:
- print('Please input integer number!')
- #starting with 2 card on hand
- def starting_cards(playerhand,dealerhand):
- for i in range(2):
- playerhand.add_card(deck.deal())
- dealerhand.add_card(deck.deal())
- #Game is Starting
- namepl = input("What's your name: ")
- if namepl =='' or namepl == ' ':
- namepl = 'Anonymus'
- #creating players
- player = Player(namepl,insert_money())
- dealer = Player('Dealer',1000000)
- while True:
- print('nWelcome to BlackJack! Get as close to 21 as you can without
- going over!nDealer stops when reaching 17. Aces count as 1 or 11.')
- #creating deck
- deck = Deck()
- deck.create()
- for i in range(0,3):
- deck.shuffle()
- print('nYour name is: {}'.format(namepl))
- # creating player's hand
- player_hand = Hand(namepl)
- dealer_hand = Hand('Dealer')
- starting_cards(player_hand,dealer_hand)
- take_bet(player)
- show_some(player_hand,dealer_hand)
- while playing:
- hit_or_stand(deck,player_hand)
- show_some(player_hand,dealer_hand)
- if player_hand.value > 21:
- show_all(player_hand,dealer_hand)
- player_busts(player)
- break
- if player_hand.value == 21:
- player_blackjack(player)
- if dealer_hand.value == 21:
- dealer_blackjack(player,dealer)
- if player_hand.value < 21:
- while dealer_hand.value < 17:
- hit(deck,dealer_hand)
- show_all(player_hand,dealer_hand)
- if dealer_hand.value > 21:
- dealer_busts(player,dealer)
- elif dealer_hand.value > player_hand.value:
- dealer_wins(player,dealer)
- elif dealer_hand.value < player_hand.value:
- player_wins(player)
- else:
- tie_game()
- print("nTotal money {} DKK!".format(player.balance))
- # Ask to play again
- new_game = input("Would you like to play another hand? Enter 'y' or 'n'
- ")
- if new_game[0].lower()=='y':
- playing=True
- continue
- else:
- print("Thanks for playing!")
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement