Skip to content

Fix stale closure bug causing incorrect blackjack win/loss determination#29

Merged
mixtapejaxson merged 2 commits intomainfrom
copilot/fix-blackjack-win-condition
Dec 8, 2025
Merged

Fix stale closure bug causing incorrect blackjack win/loss determination#29
mixtapejaxson merged 2 commits intomainfrom
copilot/fix-blackjack-win-condition

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 7, 2025

Player with 21 loses to dealer with 17 due to React state closure capturing outdated hand value.

Bug: Player 21 vs Dealer 17 shows "Dealer wins"

Root Cause

When hit() adds a card and reaches 21, it calls setPlayerHand() then immediately stand(). The 500ms setTimeout in stand() captures the pre-update playerHand closure value. When dealerPlay() executes, it passes this stale hand to endGame(), causing incorrect win/loss evaluation.

Changes

  • Thread finalPlayerHand?: Card[] parameter through stand()dealerPlay()endGame()
  • Pass newPlayerHand from hit() when player reaches 21
  • Fallback to state variable when parameter not provided (button clicks, empty deck scenarios)
// Before: stale closure
const hit = () => {
  const newPlayerHand = [...playerHand, newCard];
  setPlayerHand(newPlayerHand);
  if (handValue === 21) stand(); // captures old playerHand
};

// After: thread fresh value
const hit = () => {
  const newPlayerHand = [...playerHand, newCard];
  setPlayerHand(newPlayerHand);
  if (handValue === 21) stand(newPlayerHand); // pass current value
};

const dealerPlay = (finalPlayerHand?: Card[]) => {
  endGame(finalPlayerHand || playerHand, newDealerHand, newDeck);
};

6 lines changed in BlackjackModal.tsx.

Original prompt

This section details on the original issue you should resolve

<issue_title>Blackjack says I lost when I clearly won</issue_title>
<issue_description>Image
See attatched image, i got 21, the dealer got 17, and somehow i lost... </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel
Copy link
Copy Markdown

vercel Bot commented Dec 7, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
mix-click Ready Ready Preview Comment Dec 7, 2025 8:02am

…chain

Co-authored-by: mixtapejaxson <79427951+mixtapejaxson@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix blackjack game to correctly identify wins Fix stale closure bug causing incorrect blackjack win/loss determination Dec 7, 2025
Copilot AI requested a review from mixtapejaxson December 7, 2025 08:11
@mixtapejaxson mixtapejaxson marked this pull request as ready for review December 8, 2025 17:13
@mixtapejaxson
Copy link
Copy Markdown
Owner

LGTM.

@mixtapejaxson mixtapejaxson merged commit c0f91d3 into main Dec 8, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blackjack says I lost when I clearly won

2 participants