DhanushNehru / Python-Scripts

A curated list of python scripts for automating your tasks
https://dhanushnehru.github.io/Python-Scripts/
MIT License
551 stars 259 forks source link

Made minor updation in BlackjackGame.py #334

Closed vignesh1507 closed 1 month ago

vignesh1507 commented 1 month ago

Minor issues in the code are as follows:

  1. Incorrect use of dealer in refreshPlayerCard:

    • The code referred to dealer instead of self.dealer when resetting the dealer's cards in the refreshPlayerCard method, leading to a potential NameError because dealer isn't defined in the method's scope.
  2. No shuffling of the card deck:

    • In the genDeck method, while generating the deck, the cards were not shuffled. Without shuffling, cards would be drawn in the same predictable order, affecting randomness.
  3. Ace value adjustment issue:

    • The value of the Ace ('A') was always considered as 11 points in the getScore method. When the total score exceeded 21, the value of Ace should be adjusted to 1. However, this was not implemented until after calculating the score. The logic was also missing in the main loop.
  4. Betting issues:

    • There was no validation for whether players had enough money to place their bet, potentially leading to a situation where players could bet more than they have.
  5. Game logic errors in playRound3:

    • The conditions for determining the winner between the dealer and players were sometimes logically incorrect, causing errors like displaying both "dealer wins" and "dealer loses" for the same player. This was due to improper use of elif blocks that didn't adequately cover all possible scenarios.
  6. Duplicate result outputs:

    • Some conditions in the playRound3 method led to duplicate results being printed, especially in cases where both the dealer and the player went bust (score > 21).
  7. Missing adjustAceValue method call:

    • The logic to adjust the value of Ace wasn't invoked automatically after drawing cards. Players with Ace cards could exceed 21 without adjusting the value of their Ace cards.

Changes made:

  1. Fixed the refreshPlayerCard method: Now using self.dealer instead of dealer to clear the dealer's cards.

  2. Modified getScore to call adjustAceValue: This ensures Ace values are adjusted properly if the score exceeds 21.

  3. Added random.shuffle in genDeck: This shuffles the deck after generation to randomize card draws.

  4. Revised removeLossers: Changed to filter the player list using a list comprehension, removing players with no money.