Beispiel #1
0
class Blackjack:

	use_color = True
	playing = True
	continue_game = True

	def __init__(self):
		self.cards = Cards()
		self.player = Player()
		self.dealer = Dealer()

	def _continueGame(self):
		if self.player.hasBlackjack() or self.dealer.hasBlackjack():
			return False
		else:
			return True

	def isBust(self):
		if self.player.isBust() or self.dealer.isBust():
			return True
		else:
			return False
	
	def new(self):
		time.sleep(2)
		self.cards = Cards()
		self.player = Player()
		self.dealer = Dealer()
		self.dealer.shuffle(self.cards)
		card1, card2 = self.player.receiveCards(self.dealer.deal(self.cards))	
		card3, card4 = Dealer.receiveCards()

		global screen
		cards_render = pygame.sprite.RenderPlain((card1,card2,card3,card4))
		cards_render.update()
		cards_render.draw(screen)
		pygame.display.flip()
			
		return self._continueGame()

	def showCards(self):

		global screen
		card1 = Dealer.revealFirstCard()
		cards_render = pygame.sprite.RenderPlain((card1))
		cards_render.update()
		cards_render.draw(screen)
		pygame.display.flip()

		time.sleep(2)
		ds = self.dealer.showCards()
		time.sleep(2)
		ps = self.player.showCards()
		time.sleep(2)
		_print('---------------------------------------', 'magenta')
		if ps == ds or (ps > 21 and ds > 21):
			_print('Push. Neither dealer nor player won.', 'magenta')

		elif ps <= 21 and ps > ds:
			_print('Player wins with {0}.'.format(ps), 'magenta')

		elif ds <= 21 and ds > ps:
			_print('Dealer wins with {0}.'.format(ds), 'magenta')

		elif ps > 21 and ds <= 21:
			_print('Dealer wins with {0}. Player bust.'.format(ds), 'magenta')
		elif ds > 21 and ps <= 21:
			_print('Player wins with {0}. Dealer bust.'.format(ps), 'magenta')
		_print('---------------------------------------', 'magenta')

	def hit(self):
		self.player.hit(self.cards)

	def stand(self):
		self.player.stand()
		response, dealer_cards = self.dealer.respond(self.cards)

		if len(dealer_cards) > 0:
			global screen
			card1 = dealer_cards[0]
			card2 = None
			cards_render = pygame.sprite.RenderPlain((card1))
			cards_render.update()
			cards_render.draw(screen)
			pygame.display.flip()

	def cls(self):
		os.system('cls' if os.name == 'nt' else 'clear')
	def playRound(self):
		# shuffle the decks
		deck = Deck(self.numOfDecks)
		deck.shuffle()

		# deal the cards
		me = Player(deck.draw(),deck.draw())
		dealer = Dealer(deck.draw(),deck.draw())

		# check for BLACKJACK!
		if me.hasBlackjack():
			if dealer.hasBlackjack():
				return PUSH
			else:
				return BLKJK

		# play my hand
		while True:

			handvalue = me.getHand().getValue()

			if handvalue > 21:
				return LOSS

			elif me.getHand().isPair():
				bestmove = basic_strategy_pair[dealer.openCardValue()][me.getHand().getHand()[0].getRank()]

			elif me.getHand().isSoft():
				bestmove = basic_strategy_soft[dealer.openCardValue()][handvalue-11]

			else:
				bestmove = basic_strategy_hard[dealer.openCardValue()][handvalue]


			# ULTRA NINJA HACK:
			#   until logic for a split is put in, reevaluate as a hard hand
			if bestmove == SP:
				if handvalue == 4: handvalue = 5
				bestmove = basic_strategy_hard[dealer.openCardValue()][handvalue]

			if bestmove == H or bestmove == Dh or bestmove == Sh:
				me.hit(deck.draw())

			elif bestmove == S or bestmove == Ds:
				break  # stand

			else:
				print("There's no reason to be here!")
				exit()

		# play dealer's hand
		while True:
			handvalue = dealer.getHand().getValue()

			if handvalue < 17:
				dealer.hit(deck.draw())

			elif handvalue <= 21:
				break  # stand

			elif handvalue > 21:
				return WIN  # dealer

			else:
				print("There's no reason to be here!")
				exit()

		# compare hands
		myValue = me.getHand().getValue()
		dealerValue = dealer.getHand().getValue()

		if dealerValue > myValue:
			return LOSS

		elif dealerValue < myValue:
			return WIN

		elif dealerValue == myValue:
			return PUSH

		else:
			print("There's no reason to be here!")
			exit()
Beispiel #3
0
class Blackjack:

	use_ai = False
	settings = { 'use_color': True, 'code-page': '850' }
	playing = True
	continue_game = True

	@staticmethod
	def loadConfiguration(config='blackjack.xml'):
		tree = ET.parse(config)
		root = tree.getroot()
		for child in root.findall('terminal'):
			Blackjack.settings['use_color'] = str2bool(child.find('use-color').text)
			Blackjack.settings['code-page'] = child.find('code-page').get('default')

	def __init__(self):
		self.cards = Cards(Blackjack.settings['use_color'])
		self.player = Player(Blackjack.settings['use_color'])
		self.dealer = Dealer(Blackjack.settings['use_color'])

	def _continueGame(self):
		if self.player.hasBlackjack() or self.dealer.hasBlackjack():
			return False
		else:
			return True

	def isBust(self):
		if self.player.isBust() or self.dealer.isBust():
			return True
		else:
			return False
	
	def new(self):
		time.sleep(2)
		self.cards = Cards(Blackjack.settings['use_color'])

		if Blackjack.use_ai:
			self.player = AI(Blackjack.settings['use_color'])
		else:
			self.player = Player(Blackjack.settings['use_color'])

		self.dealer = Dealer(Blackjack.settings['use_color'])
		self.dealer.shuffle(self.cards)
		self.player.receiveCards(self.dealer.deal(self.cards))	
		return self._continueGame()	

	def showCards(self):
		time.sleep(2)
		ds = self.dealer.showCards()
		time.sleep(2)
		ps = self.player.showCards()
		time.sleep(2)
		_print('-------------------------------------------------------', 'magenta')
		if ps == ds or (ps > 21 and ds > 21):
			_print('Push. Neither dealer nor player won.', 'magenta')

		elif ps <= 21 and ps > ds:
			_print('Player wins with {0}.'.format(ps), 'magenta')

		elif ds <= 21 and ds > ps:
			_print('Dealer wins with {0}.'.format(ds), 'magenta')

		elif ps > 21 and ds <= 21:
			_print('Dealer wins with {0}. Player bust.'.format(ds), 'magenta')
		elif ds > 21 and ps <= 21:
			_print('Player wins with {0}. Dealer bust.'.format(ps), 'magenta')
		_print('-------------------------------------------------------', 'magenta')

	def hit(self):
		self.player.hit(self.cards)

	def stand(self):
		self.player.stand()
		self.dealer.respond(self.cards)

	def aiRespond(self):
		response = self.player.respond(self.cards)
		if response == 'stand':
			response = self.dealer.respond(self.cards)
			if response == 'stand':
				Blackjack.continue_game = False

	def cls(self):
		os.system('cls' if os.name == 'nt' else 'clear')