blackjack.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Copyright (C) 2020, 2019 Girish M, Prince Rashid
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 3 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  15. # MA 02110-1301, USA.
  16. #
  17. # authors: girish, prince
  18. import random
  19. import unicodedata
  20. # contains the core blackjack game logic, allows the player to play
  21. def playGame(playerName):
  22. print(playerName + ' is playing...')
  23. # all the variables used
  24. choice = 'hit'
  25. cards = {'A of Hearts': 11, '2 of Hearts': 2, '3 of Hearts': 3, '4 of Hearts': 4, '5 of Hearts': 5,
  26. '6 of Hearts': 6, '7 of Hearts': 7, '8 of Hearts': 8, '9 of Hearts': 9, '10 of Hearts': 10,
  27. 'J of Hearts': 10, 'Q of Hearts': 10, 'K of Hearts': 10,
  28. 'A of Spades': 11, '2 of Spades': 2, '3 of Spades': 3, '4 of Spades': 4, '5 of Spades': 5,
  29. '6 of Spades': 6, '7 of Spades': 7, '8 of Spades': 8, '9 of Spades': 9, '10 of Spades': 10,
  30. 'J of Spades': 10, 'Q of Spades': 10, 'K of Spades': 10,
  31. 'A of Diamonds': 11, '2 of Diamonds': 2, '3 of Diamonds': 3, '4 of Diamonds': 4, '5 of Diamonds': 5,
  32. '6 of Diamonds': 6, '7 of Diamonds': 7, '8 of Diamonds': 8, '9 of Diamonds': 9, '10 of Diamonds': 10,
  33. 'J of Diamonds': 10, 'Q of Diamonds': 10, 'K of Diamonds': 10,
  34. 'A of Clubs': 11, '2 of Clubs': 2, '3 of Clubs': 3, '4 of Clubs': 4, '5 of Clubs': 5, '6 of Clubs': 6,
  35. '7 of Clubs': 7, '8 of Clubs': 8, '9 of Clubs': 9, '10 of Clubs': 10, 'J of Clubs': 10, 'Q of Clubs': 10,
  36. 'K of Clubs': 10}
  37. playingCardUnicode = {'A of Hearts': '\U0001F0B1', '2 of Hearts': '\U0001F0B2', '3 of Hearts': '\U0001F0B3',
  38. '4 of Hearts': '\U0001F0B4', '5 of Hearts': '\U0001F0B5', '6 of Hearts': '\U0001F0B6',
  39. '7 of Hearts': '\U0001F0B7', '8 of Hearts': '\U0001F0B8', '9 of Hearts': '\U0001F0B9',
  40. '10 of Hearts': '\U0001F0BA', 'J of Hearts': '\U0001F0BB', 'Q of Hearts': '\U0001F0BD',
  41. 'K of Hearts': '\U0001F0BE',
  42. 'A of Spades': '\U0001F0A1', '2 of Spades': '\U0001F0A2', '3 of Spades': '\U0001F0A3',
  43. '4 of Spades': '\U0001F0A4', '5 of Spades': '\U0001F0A5', '6 of Spades': '\U0001F0A6',
  44. '7 of Spades': '\U0001F0A7', '8 of Spades': '\U0001F0A8', '9 of Spades': '\U0001F0A9',
  45. '10 of Spades': '\U0001F0AA', 'J of Spades': '\U0001F0AB', 'Q of Spades': '\U0001F0AD',
  46. 'K of Spades': '\U0001F0AE',
  47. 'A of Diamonds': '\U0001F0C1', '2 of Diamonds': '\U0001F0C2', '3 of Diamonds': '\U0001F0C3',
  48. '4 of Diamonds': '\U0001F0C4', '5 of Diamonds': '\U0001F0C5', '6 of Diamonds': '\U0001F0C6',
  49. '7 of Diamonds': '\U0001F0C7', '8 of Diamonds': '\U0001F0C8', '9 of Diamonds': '\U0001F0C9',
  50. '10 of Diamonds': '\U0001F0CA', 'J of Diamonds': '\U0001F0CB', 'Q of Diamonds': '\U0001F0CD',
  51. 'K of Diamonds': '\U0001F0CE',
  52. 'A of Clubs': '\U0001F0D1', '2 of Clubs': '\U0001F0D2', '3 of Clubs': '\U0001F0D3',
  53. '4 of Clubs': '\U0001F0D4', '5 of Clubs': '\U0001F0D5', '6 of Clubs': '\U0001F0D6',
  54. '7 of Clubs': '\U0001F0D7', '8 of Clubs': '\U0001F0D8', '9 of Clubs': '\U0001F0D9',
  55. '10 of Clubs': '\U0001F0DA', 'J of Clubs': '\U0001F0DB', 'Q of Clubs': '\U0001F0DD',
  56. 'K of Clubs': '\U0001F0DE'}
  57. count = 0
  58. firstHandCheck = True
  59. # it will contain the values for the dealt cards
  60. dealtCards = []
  61. firstCard = random.choice(list(cards.keys()))
  62. dealtCards.append(cards[firstCard])
  63. count += cards[firstCard]
  64. # since the card is dealt, we shall remove it from the deck
  65. cards.pop(firstCard)
  66. secondCard = random.choice(list(cards.keys()))
  67. dealtCards.append(cards[secondCard])
  68. count += cards[secondCard]
  69. # since the card is dealt, we shall remove it from the deck
  70. cards.pop(secondCard)
  71. if count == 22:
  72. # simply because two A's means 12
  73. count = 12
  74. # this is to print the unicode symbol for playing cards
  75. symbolOfFirstCard = unicodedata.name(playingCardUnicode[firstCard])
  76. symbolOfSecondCard = unicodedata.name(playingCardUnicode[secondCard])
  77. print(unicodedata.lookup(symbolOfFirstCard))
  78. print(unicodedata.lookup(symbolOfSecondCard))
  79. if count == 21:
  80. print("BlackJack! You won.")
  81. else:
  82. print("Your score is: ", count)
  83. choice = input('Hit or stay (type hit or stay): ')
  84. while choice in ['hit', 'yes', 'y', 'h']:
  85. newCard = random.choice(list(cards.keys()))
  86. dealtCards.append(cards[newCard])
  87. # this is to print the unicode symbol for playing cards
  88. symbolOfNewCard = unicodedata.name(playingCardUnicode[newCard])
  89. print("Your new card is: ", unicodedata.lookup(symbolOfNewCard))
  90. if newCard[0] == 'A':
  91. if count < 11:
  92. count += 11
  93. else:
  94. count += 1
  95. elif (firstCard[0] == 'A' or secondCard[0] == 'A') and firstHandCheck == True:
  96. if count < 11:
  97. count += cards[newCard]
  98. else:
  99. count += cards[newCard]
  100. if count > 21:
  101. count = count - 10
  102. firstHandCheck = False
  103. else:
  104. count = 0
  105. noOfAces = 0
  106. # first adding score of all non-aces
  107. for cardValue in dealtCards:
  108. if cardValue == 11:
  109. noOfAces += 1
  110. else:
  111. count += cardValue
  112. # finally, adding aces
  113. while noOfAces != 0:
  114. if count + 11 > 21:
  115. count += 1
  116. else:
  117. count += 11
  118. noOfAces -= 1
  119. # since the card is dealt, we shall remove it from the deck
  120. cards.pop(newCard)
  121. if count > 21:
  122. print("Bust!")
  123. break
  124. elif count == 21:
  125. print("BlackJack! You won.")
  126. break
  127. else:
  128. print("Your score is: ", count)
  129. choice = input('Hit or stay (type hit or stay): ')
  130. if count > 21:
  131. print("Your final score is: too much")
  132. count = 0
  133. else:
  134. print(playerName + "'s final score is: ", count)
  135. return count
  136. # entry point for the program, takes player names and starts game
  137. if __name__ == '__main__':
  138. continueGame = 'y'
  139. player1 = input("Please enter player 1 name: ")
  140. player2 = input("Please enter player 2 name: ")
  141. gamesWonPlayer1 = 0
  142. gamesWonPlayer2 = 0
  143. while continueGame in ['y', 'yes', 'ok', 'Yes']:
  144. player1Score = playGame(player1)
  145. player2Score = playGame(player2)
  146. if player1Score > player2Score:
  147. print(player1 + ' won this round.')
  148. gamesWonPlayer1 += 1
  149. elif player1Score == player2Score:
  150. print("It's a tie!")
  151. else:
  152. print(player2 + ' won this round.')
  153. gamesWonPlayer2 += 1
  154. continueGame = input("Would you like to play again (y/n): ")
  155. print(player1, ' won ', gamesWonPlayer1, ' rounds. ', player2, ' won ', gamesWonPlayer2, ' rounds. ')
  156. if gamesWonPlayer1 > gamesWonPlayer2:
  157. print(player1 + ' wins the game!')
  158. elif gamesWonPlayer2 > gamesWonPlayer1:
  159. print(player2 + ' wins the game!')
  160. else:
  161. print('No one wins the game!')