Python Forum
Problem with my Black Jack game...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with my Black Jack game...
#1
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!
Larz60+ write Sep-27-2022, 02:23 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please use rather than attachments.

Attached Files

.py   BlackJack_2.py (Size: 4.78 KB / Downloads: 125)
Reply
#2
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())
Reply
#3
(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
Larz60+ write Sep-27-2022, 06:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please read (and use): BBCode
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  quiz game - problem with loading statements from file lapiduch 2 1,028 Apr-20-2023, 06:13 PM
Last Post: deanhystad
  Quoridor game. Problem: inserting player positions and walls in a checkerboard Roidesflammes 1 3,106 Aug-03-2021, 06:52 PM
Last Post: riii
  problem on creating a small game nayo43 5 2,699 Dec-13-2020, 01:03 PM
Last Post: jefsummers
  Black holes Home alfredocaronia 2 1,968 Mar-28-2020, 05:52 PM
Last Post: alfredocaronia
  Help with a Matrix nxn with black and white input Rosko 3 1,920 Nov-24-2019, 07:39 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020