Skip to content

Fix mobile capture tap handling (Issue #52)#53

Merged
ysgrProgramming merged 1 commit into
mainfrom
feature/issue-52-fix-mobile-capture-tap-handling
Nov 27, 2025
Merged

Fix mobile capture tap handling (Issue #52)#53
ysgrProgramming merged 1 commit into
mainfrom
feature/issue-52-fix-mobile-capture-tap-handling

Conversation

@ysgrProgramming

Copy link
Copy Markdown
Owner

Summary

Fixes Issue #52: On touch devices, tapping an opponent piece to capture fails because handleTouchStart overwrites selectedSquare immediately, causing handleSquareClick in handleTouchEnd to treat the tap as selecting the destination rather than executing the move.

Changes

  • Removed selection logic from handleTouchStart to prevent overwriting selectedSquare
  • Added touchStartSquare state to track which square was touched
  • Let handleTouchEnd -> handleSquareClick handle selection and capture logic
  • Added regression tests for mobile capture tap handling

Testing

  • All existing tests pass (231 tests)
  • New tests added for mobile capture tap handling scenarios
  • Manual verification recommended on touch device or simulator

Self-Walkthrough

Requirements Mapping

  1. Requirement: Update touch handling so that tapping an opponent piece after selecting your own piece triggers handleMoveAttempt (capture) instead of re-selecting.

    • Implementation: Removed selection logic from handleTouchStart. Now handleTouchEnd calls handleSquareClick directly, which checks if selectedSquare is set and the tapped square has an opponent piece, then calls handleMoveAttempt.
  2. Requirement: Ensure taps that start on an opponent piece only select it when no source piece is currently selected.

    • Implementation: handleSquareClick logic handles this: if selectedSquare === null and the tapped square has a piece, it selects it. If selectedSquare is already set and the tapped square has an opponent piece, it attempts capture.
  3. Requirement: Add regression tests verifying mobile capture flows work for both same-color selection and opponent captures.

    • Implementation: Added 4 new tests in ChessBoard.responsive.test.tsx:
      • should capture opponent piece when tapping opponent piece after selecting own piece
      • should select opponent piece when tapping opponent piece without prior selection
      • should select own piece when tapping own piece without prior selection
      • should execute capture move when tapping opponent piece after selecting own piece
  4. Requirement: Verify desktop click/drag behavior remains unchanged.

    • Implementation: All existing tests pass, confirming desktop behavior is unchanged.

Logical Basis

The root cause was that handleTouchStart unconditionally called setSelectedSquare(square) when a piece was touched, overwriting any existing selection. This meant that when tapping an opponent piece after selecting your own piece, the selectedSquare was immediately overwritten before handleTouchEnd could process the capture.

The solution is to remove selection logic from handleTouchStart and let handleTouchEnd -> handleSquareClick handle all selection and capture logic. This ensures that the selection state is consistent when handleSquareClick is called, allowing it to correctly determine whether to select, capture, or move.

Closes #52

- Remove selection logic from handleTouchStart to prevent overwriting selectedSquare
- Let handleTouchEnd -> handleSquareClick handle selection and capture logic
- Add touchStartSquare state to track which square was touched
- Add regression tests for mobile capture tap handling

This fixes the issue where tapping an opponent piece after selecting
your own piece would re-select the opponent piece instead of executing
the capture.
@ysgrProgramming
ysgrProgramming merged commit 57fc415 into main Nov 27, 2025
2 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.

Fix mobile capture tap handling

1 participant