repl.it
@bdmessenger/

blackjack

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import random

players = []

class Card:
  def __init__(self,symbol,value):
    self.symbol = symbol
    self.value = value
    self.text = ""

    if value == 14:
      self.text += "A"
    elif value == 11:
      self.value = 10
      self.text += "J"
    elif value == 12:
      self.value = 10
      self.text += "Q"
    elif value == 13:
      self.value = 10
      self.text += "K"
    else:
      self.text += str(value)
    
    if symbol == 0:
      self.text += "D"
    elif symbol == 1:
      self.text += "H"
    elif symbol == 2:
      self.text += "S"
    else:
      self.text += "C"
    

class Deck:
  def __init__(self):
    self.deck = []

    for k in range(6):
      for i in range(4):
        for c in range(2,15):
          self.deck.append(Card(i,c))
  
  def shuffle(self):
    random.shuffle(self.deck)

  def draw(self,num_of_cards = 1):
    self.array = []
    for i in range(num_of_cards):
      self.array.append(self.deck.pop())
    return self.array

  def checkDeckAmount(self):
    return len(self.deck)

class Player:
  def __init__(self,name):
    self.name = name
    self.currency = 500
    self.original_bet = None
    self.hand = []
    self.idle = True
  
  def switchIdle(self):
    if self.idle == True:
      self.idle = False
    else:
      self.idle = True
    return self.idle

  def bet(self,amount = 5):
    self.original_bet = amount
    self.currency -= amount
    return ["bet",amount]

  def hit(self):
    return "hit"

  def stand(self):
    return "stand"
  
  def doubleDown(self):
    double = (self.original_bet * 2)
    self.currency -= double
    return ["double",double]

  def split(self):
    self.currency -= self.original_bet
    return ["split",self.original_bet]

  def win(self,amount):
    self.currency += amount
    return "win"
  
  def tie(self):
    self.currency += self.original_bet
    return "tie"

class BlackJack:
  def __init__(self):
    self.deck = Deck()
    self.deck.shuffle()
    self.results = []
    self.dealerJack = False
  
  def addPlayer(self,name):
    players.append(Player(name))

  def currentPlayers(self):
    self.current_players = [player for player in players if player.idle != True]
    return self.current_players


  def shuffle(self):
    self.deck.shuffle()

  def drawOne(self):
    if self.deck.checkDeckAmount() == 0:
      self.newDeck()
      self.shuffle()
    return self.deck.draw()[0]

  def newDeck(self):
    self.deck = Deck()

  def calculateHand(self,player):
    self.array = []
    self.score = 0
    for card in player.hand:
      if card.value == 14:
        self.array.append(card)
      else:
        self.array.insert(0,card)
    for card in self.array:
      if self.score < 22:
        if card.value != 14:
          self.score += card.value
        else:
          self.temp_score = self.score
          self.temp_score += 11
          if self.temp_score > 21:
            self.score += 1
          else:
            self.score += 11
    if self.score > 21:
      return "bust"
    else:
      return self.score

  def checkBlackJack(self,player):
    if player == dealer:
      if self.calculateHand(player) == 21:
        self.dealerJack = True
    else:
      if self.calculateHand(player) == 21:
        return "blackjack"
    return None

  def setupRound(self):
    for player in players:
      player.switchIdle()
    for i in range(2):
      dealer.hand.append(self.drawOne())
      for player in self.currentPlayers():
        player.hand.append(self.drawOne())
    self.checkBlackJack(dealer)
    for player in self.currentPlayers():
      if self.checkBlackJack(player) == "blackjack":
        self.results.append([player,"blackjack"])
        if self.dealerJack != True:
          self.extra_money = (player.original_bet / 2)
          player.win((player.original_bet * 2) + self.extra_money)
        else:
          player.tie()
      elif self.dealerJack == True:
        self.results.append([player,self.calculateHand(player)])
    if self.dealerJack != True:
      self.startRound()
      return
    self.results.append([dealer,21])
    self.endRound()
    return

  def startRound(self):
    for player in self.currentPlayers():
      command = input()
      if command == 0:
        pass
      elif command == 1:
        pass

  def hit(self,player):
    player.hand.append(self.drawOne())
    if self.calculateHand(player) == "bust":
      return "bust"
    return "hit"

  def endRound(self):
    self.dealer_score = None
    self.players_scores = []
    for player in self.results:
      if player[0] == dealer:
        self.dealer_score = player[1]
      else:
        self.players_scores.append(player)
    if self.dealer_score != 21:
      for score in self.players_scores:
        if score[1] > self.dealer_score:
          score[0].win((score[0].original_bet * 2))
        elif score[1] == self.dealer_score:
          score[1].tie()
        else:
          pass

  
  def clearHands(self):
    dealer.clear()
    for player in players:
      player.hand.clear()

dealer = Player("dealer")
main = BlackJack()

main.addPlayer("Brant")
players[0].bet()

main.setupRound()


# for player in array:
#   print(str(player.name) + "("+str(player.original_bet)+"):\n")
#   for card in player.hand:
#     print(card.value)