Python Forum

Full Version: Problem with my Black Jack game...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys,

I'm new here so I'm not sure, what informations you need, but here you go:

As a school project I want to program Black Jack as a game. The game works, but there's a problem with the question where it asks if you want to pull a card (It's in german, because im swiss sorry). The exact problem is, that the program should automatically stop, if the value of the player's cards or the value of the dealer's cards is more than 21 - but it doesn't.

So my questions are:
--> Why does it not work?
--> How can I fix it?

As a next step I thought of importing Pictures of Black Jack cards (in pygame) and to show the pictures, when this card is pulled during the program.

--> Have you an idea how I should do it?
--> Have you some advices?

As an attachment you find my current program.

Thanks for your help!
Your problem might be that SpielerZug() calls askIfSpielerZug() and askIfSpielerZug() calls SpielerZug(). When you have a function or functions that call each other like this it is called recursion.

Recursion can be used to solve self-referencing problems like the Fibonacci sequence:
Output:
def fib(n): if n < 2: return 1 return fib(n-2) + fib(n-1
This function will keep calling itself until the value for n is < 2. Depending on the starting value of n this might be hundreds function calls or more. Recursive solutions must be carefully planned. Once a recursive algorithm finds the solution it must gracefully unwind all these function calls to return the value to the initial caller.

Recursion is not a good fit for playing blackjack, and your program is not "unwinding the recursion". Instead of recursion, your program should use a loop. The main loop for blackjack would look something like this:
while sum(player_cards) < 21 and ask_player_draw():
    player_draw()

while sum(dealer_cards) < 17:
    dealer_draw()
The player continues to draw cards until stopping or going bust. When the player is done the dealer draws card until going bust or the value of the hand is greater than 16. The hands are evaluated to determine if the player wins, the dealer wins, or if the game was a draw.

This function is poorly designed.
def randomizer():
    global Auswahl
    Auswahl = randint(0, 311)
You should not use global variables to pass information in and out of functions. Use arguments to pass information into a function and return to pass a value back from a function.

Another problem with the function is it doesn't act like dealing cards from a deck. When you deal a card from a deck that card cannot be delt again. Your function can deal the same card multiple times.

I would use random.shuffle() to make a shuffled deck of cards.
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, J, D, K, A] * 24
random.shuffle(deck)
Now, instead of generating a random number and pulling that card from the deck, you just pop the top card. Instead of this:
def randomizer():
    global Auswahl
    Auswahl = randint(0, 311)

randomizer()
Umwandlung = AlleDecks[Auswahl]
SpielerKarten.append(Umwandlung)
All you have to do is this:
SpielerKarten.append(deck.pop())
(Sep-27-2022, 07:23 AM)deanhystad Wrote: [ -> ]Your problem might be that SpielerZug() calls askIfSpielerZug() and askIfSpielerZug() calls SpielerZug(). When you have a function or functions that call each other like this it is called recursion.

Recursion can be used to solve self-referencing problems like the Fibonacci sequence:
Output:
def fib(n): if n < 2: return 1 return fib(n-2) + fib(n-1
This function will keep calling itself until the value for n is < 2. Depending on the starting value of n this might be hundreds function calls or more. Recursive solutions must be carefully planned. Once a recursive algorithm finds the solution it must gracefully unwind all these function calls to return the value to the initial caller.

Recursion is not a good fit for playing blackjack, and your program is not "unwinding the recursion". Instead of recursion, your program should use a loop. The main loop for blackjack would look something like this:
while sum(player_cards) < 21 and ask_player_draw():
    player_draw()

while sum(dealer_cards) < 17:
    dealer_draw()
The player continues to draw cards until stopping or going bust. When the player is done the dealer draws card until going bust or the value of the hand is greater than 16. The hands are evaluated to determine if the player wins, the dealer wins, or if the game was a draw.

This function is poorly designed.
def randomizer():
    global Auswahl
    Auswahl = randint(0, 311)
You should not use global variables to pass information in and out of functions. Use arguments to pass information into a function and return to pass a value back from a function.

Another problem with the function is it doesn't act like dealing cards from a deck. When you deal a card from a deck that card cannot be delt again. Your function can deal the same card multiple times.

I would use random.shuffle() to make a shuffled deck of cards.
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, J, D, K, A] * 24
random.shuffle(deck)
Now, instead of generating a random number and pulling that card from the deck, you just pop the top card. Instead of this:
def randomizer():
    global Auswahl
    Auswahl = randint(0, 311)

randomizer()
Umwandlung = AlleDecks[Auswahl]
SpielerKarten.append(Umwandlung)
All you have to do is this:
SpielerKarten.append(deck.pop())

I think I fixed the issue with the recursion, which I used instead of a loop. I know, that the randomizer was poorly designed, but in school we had to use a simpler version of python (called TigerJython) and we I had no chance to do it otherwise, at least I theoretically get, what you wanted to say there.

My question now is: In order to pull a card from the deck, all I have to do it to replace all these phrases:

def randomizer():
global Auswahl
Auswahl = randint(0, 311)

randomizer()
Umwandlung = AlleDecks[Auswahl]
SpielerKarten.append(Umwandlung)

with just SpielerKarten.append(deck.pop()) or DealerKarten.append(deck.pop())?

Thanks for your answer