Workshop › Klondike Solitaire › Step 16 of 40
To play with the mouse or a finger we must know which pile is at a point. hit(x, y) checks each pile: is x inside
its column and y inside its card? We start with the top row.
```js
// Which card, or which empty pile, is at (x, y)? The card drawn last, on top, wins.
function hit(x, y) {
for (const key of Object.keys(piles)) {
const px = pileX(key)
if (x < px || x > px + CW) continue
const pile = piles[key]
if (key[0] !== 't' && y >= TOP_Y && y <= TOP_Y + CH) return { key, index: pile.length - 1 }
}
return null
}
```Object.keys(piles) lists the pile names.continue skips to the next pile when x is left or right of this one.TOP_Y and TOP_Y + CH hits the pile; the answer is its name and the index of its top
card.return null means "nothing here".Above the keydown listener write hit with its comment, and leave an empty line between them.
Skills: Keyboard and mouse input, Collision detection
← Go through the stock again Hitting a card in a column →