The playersHand function in the TwentyOne contract does not handle Ace cards (cardValue = 1) correctly. It fails to dynamically adjust the value of the Ace between 1 and 11 based on the current total score. This leads to inaccurate hand totals, which can affect game outcomes.
Location: The issue occurs in the playersHand function when iterating through the player's cards and calculating the total hand value.
Cause: Ace cards are always treated as having a value of 1. There is no logic to assign the value 11 to an Ace when it benefits the player (i.e., when the total score does not exceed 21).
Code Snippet:
The code lacks a mechanism to dynamically adjust Ace values (1 or 11).
No consideration for reducing Ace values from 11 to 1 if the total score exceeds 21.
Gameplay Disruption: Players may lose unfairly because their hand totals are miscalculated.
Deviation from Rules: The contract does not align with the standard rules of Blackjack, where Aces are dynamic in value.
Player Confidence: Misleading results can erode trust in the game's fairness and the contract's reliability.
Manual Code Review: Identified the lack of Ace handling logic.
Testing Scenarios: Simulated hand calculations with combinations including Aces to observe incorrect behavior.
1 and 11 based on the total score:Initially assign 11 to an Ace.
Reduce the Ace value to 1 only if the total score exceeds 21.
Refactored Code:
Testing Scenarios:
Input: Player's hand = [1, 10, 9].
Expected Output: Total = 1 (Ace) + 10 + 9 = 20 (Ace is treated as 1).
Input: Player's hand = [1, 8].
Expected Output: Total = 11 (Ace) + 8 = 19 (Ace is treated as 11).
Automated Testing: Add unit tests to cover all scenarios involving Aces to ensure correctness.
By addressing this issue, the playersHand function will accurately calculate scores, aligning the contract's behavior with the expected rules of Blackjack.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.