Workshop › Blackjack › Step 10 of 22

Hit: one more card

What we are doing

On your turn you may ask for another card: that is a "hit". phase remembers whose turn it is. Every key goes through a small table to a button name, and press does what the button says. H is Hit; the other buttons come in the next steps.

```js
let phase // 'player' (your turn), 'dealer' (the dealer draws) or 'done' (the round is over)

  phase = 'player'
}

function hit() {
  if (phase !== 'player') return
  player.push(nextCard())
}

function press(button) {
  if (button === 'Hit') hit()
}

document.addEventListener('keydown', (event) => {
  const keys = { h: 'Hit', s: 'Stand', d: 'Double', n: 'Deal', ' ': 'Deal' }
  const button = keys[event.key.toLowerCase()]
  if (!button) return
  event.preventDefault()
  press(button)
})
```

What it means

Your turn

  1. Under dealer, write phase with its comment.
  2. At the end of deal, start your turn; under deal, write hit.
  3. Above drawCard, write press and the key listener.

Skills: Keyboard and mouse input, Game states

All steps

  1. Cards
  2. A deck
  3. Shuffle
  4. Deal
  5. Cards on the table
  6. Card faces
  7. Keep drawing
  8. What is a hand worth?
  9. Aces: 11 or 1
  10. Hit: one more card
  11. Stand
  12. The hidden card
  13. Chips
  14. Who wins?
  15. The dealer's turn
  16. The next round
  17. Blackjack!
  18. Your record
  19. Double down
  20. Long hands fit
  21. Buttons
  22. Build it yourself: six decks