@ADoctor/

Blackjack

Python

Original name: NewEnragedRectangles

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
from queue import LifoQueue
import random
SUIT = [2,3,4,5,6,7,8,9,10,'J','Q','K','A']

def getPlayerNum(typeOfPlayer):
	while True:
		try:
			players = int(input("How many {0} players will take part? ".format(typeOfPlayer)))
			if players < 1:
				print("Sorry, that's too low. Please input a number higher than 0.")
			else:
				break
		except ValueError:
			print("Sorry, that's not a number. Please input a number.")
	return players

def getCardsValue(cardsUsed):
	AcesToIgnore = 0
	for i in cardsUsed:
		if i == 'A':
			AcesToIgnore += 1
	totalIterations = AcesToIgnore+1
	cardsValue = []
	x = 0
	AcesIgnored = 0
	for n in range(0,totalIterations):
		for i in cardsUsed:
			if i == 'J' or i == 'Q' or i == 'K':
				x += 10
			elif i == 'A':
				if AcesToIgnore == AcesIgnored:
					x += 11
				else:
					x += 1
					AcesIgnored += 1
			else:
				x += i
		cardsValue.append(x)
		AcesToIgnore -= 1
	cardsValue = max(cardsValue)
	return cardsValue

def getDeckNum():
	while True:
		try:
			decks = int(input("How many decks would you like to use? "))
			if decks < 1:
				print("Sorry, that's not a valid number - it must be at least 1.")
			else:
				return decks
		except ValueError:
			print("Sorry, that's not a number.")

def getVars():
	totalHumans = getPlayerNum("human")
	totalComputers = getPlayerNum("non-dealer computer")
	playerinfo = []
	for i in range(0,totalHumans):
		name = input("What is the name of player {0}? ".format(i))
		playerinfo.append(['H',name])
	for i in range(0,totalComputers):
		playerinfo.append(['C',i])
	playerinfo.append(['D',0])
	totalDecks = getDeckNum()
	totalRounds = 1 #getRoundNum()
	#startMoney = getStartMoney()
	return playerinfo,totalDecks,totalRounds

def shuffleCards(allDecks):
	fullDeck = LifoQueue()
	random.shuffle(allDecks)
	for i in allDecks:
		fullDeck.put(i)
	return fullDeck

def dealCards(fullDeck,amountofPlayers):
	cardsUsed = []
	for i in range(amountofPlayers):
		cardsForOne = []
		for j in range(2):
			cardsForOne.append(fullDeck.get())
		cardsUsed.append(cardsForOne)
	return cardsUsed

def getPlayerBetterIntentions(fullDeck,cardsUsed):
	print("Cards:",cardsUsed)
	while True:
		intention = input("Do you wish to hit or stand? H/S ").upper()
		if intention == 'S':
			bust = False
			break
		elif intention == 'H':
			cardsUsed.append(fullDeck.get())
			cardsValue = getCardsValue(cardsUsed)
			print("Cards:",cardsUsed)
			if cardsValue > 21:
				print("Bust!")
				bust = True
				break
		else:
			print("Sorry, that's not currently a valid option. Please input 'H' or 'S'.")
	return bust,cardsUsed,fullDeck

def getComputerBetterIntentions(fullDeck,cardsUsed):
	stopNumber = 0

def getBetterIntentions(fullDeck,playerInfo):
	amountOfPlayers = 0
	amountOfComputers = 0
	for i in playerInfo:
		if i[0] == 'C':
			amountOfComputers += 1
		else:
			amountOfPlayers += 1
	for i in range(0,amountOfPlayers-1):
		for j in range(0,50):
			print("")
		bust = False
		print("Dealer's first card:",playerInfo[len(playerInfo)-1][2][1])
		print("Player",i,"- Human")
		bust,playerInfo[i][2],fullDeck = getPlayerBetterIntentions(fullDeck,playerInfo[i][2])
		if bust == True:
			playerInfo[i].append('Bust')
		else:
			playerInfo[i].append('Not Bust')
	for i in range(amountOfPlayers-1,len(playerInfo)-1):
		bust = False
		print("Player",i,"- Computer")
		bust,playerInfo[i][2],fullDeck = getComputerBetterIntentions(fullDeck,playerInfo[i][2])
		if bust == True:
			playerInfo[i].append('Bust')
		else:
			playerInfo[i].append('Not Bust')
			
	print("Bust Players:")
	for i in playerInfo:
		if i[3] == 'Bust':
			print(i[1],"with cards",i[2])
	return playerInfo,fullDeck

def getComputerDealerIntentions(fullDeck,cardsUsed,amountofBetters,bustPlayers):
	cardsValue = getCardsValue(cardsUsed)	
	while cardsValue < 17:
		if getCardsValue(cardsUsed) >= 17:
			break
		cardsUsed.append(fullDeck.get())
		cardsValue = getCardsValue(cardsUsed)
	if cardsValue > 21:
		bustPlayers.append('D')
	return fullDeck,cardsUsed,bustPlayers

def getDealerIntentions(fullDeck,playerInfo,amountOfBetters,bustPlayers,bustCards):
	fullDeck,playerInfo[amountOfBetters-1][2],bustPlayers = getComputerDealerIntentions(fullDeck,playerInfo[amountOfBetters-1][2],amountOfBetters,bustPlayers)
	for i in range(1,100):
		print("")
	return playerInfo,fullDeck,bustPlayers

def deduceResults(playerInfo):
	print("")
	results = []
	for i in range(0,len(playerInfo)):
		if i == len(playerInfo)-1:
			print("Dealer's cards:",playerInfo[i][2])
			if 'D' in bustPlayers:
				results.append(['D',0])
			else:
				results.append(['D',getCardsValue(cardsUsed[i])])
		else:
			print("Player "+str(i)+"'s cards:",playerInfo[i][2])
			if i in bustPlayers:
				results.append([i,0])
			else:
				for i in playerInfo[i][2]:
					if getCardsValue(playerInfo[i][2]) < 21:
						results.append([i,getCardsValue(playerInfo[i][2])])
					else:
						results.append([i,getCardsValue(playerInfo[i][2])])
	if results[len(results)-1][1] != 0:
		print("The dealer had a total of",str(results[len(results)-1][1])+".")
	else:
		print("The dealer went bust.")
	for i in range(0,len(results)):
		if i != len(results)-1:
			if results[i][1] > results[len(results)-1][1]:
				print("Player",i,"wins with a score of "+str(results[i][1])+"!")
			elif results[i][1] != 0:
				print("Player",i,"loses with a score of "+str(results[i][1])+".")
			else:
				print("Player",i,"went bust and lost.")
	print("")

def playGame(playerInfo,amountOfDecks,totalRounds):
	allDecks = []
	for i in range(0,(amountOfDecks*4)):
		for j in SUIT:
			allDecks.append(j)
	currentRound = 0
	fullDeck = shuffleCards(allDecks)
	while currentRound < totalRounds:
		if fullDeck.qsize() < 13*amountOfDecks:
			fullDeck = shuffleCards(allDecks)
		#bets = getStartingBets()
		amountofBetters = len(playerInfo)
		cardsUsed = dealCards(fullDeck,amountofBetters)
		for i in range(0,len(cardsUsed)):
			playerInfo[i].append(cardsUsed[i])
		print(playerInfo)
		playerInfo,fullDeck = getBetterIntentions(fullDeck,playerInfo)
		playerInfo,fullDeck = getDealerIntentions(fullDeck,playerInfo,amountofBetters)
		deduceResults(playerInfo)
		#updateBanks(bets,money)
		currentRound += 1

def main():
	print("""=========================================================================
♠    ♥    ♦    ♣    Welcome to the Blackjack Quarter!    ♣    ♦    ♥    ♠
=========================================================================
""")
	playerInfo,decksNum,rounds = getVars()
	print("")
	while True:
		wantsToPlay = input("Do you want to play a game? Y/N ").upper()
		if wantsToPlay == 'Y':
			print("")
			playGame(playerInfo,decksNum,rounds)
		elif wantsToPlay == 'N':
			print("Thanks for coming!")
			quit()
		else:
			print("Sorry, that's not a valid option.")
			print("")

main()