Skip to content

Refactor: Extract game types, board ops, and UI components - #17

Open
sebryu wants to merge 1 commit into
mainfrom
claude/refactor-modularity-LJlD3
Open

Refactor: Extract game types, board ops, and UI components#17
sebryu wants to merge 1 commit into
mainfrom
claude/refactor-modularity-LJlD3

Conversation

@sebryu

@sebryu sebryu commented Mar 14, 2026

Copy link
Copy Markdown
Owner

Summary

This PR refactors the codebase to improve modularity and maintainability by extracting shared types, constants, board operations, and UI components into dedicated modules. Game logic remains unchanged; this is purely a structural reorganization.

Type of Change

  • Game logic change (refactored into separate modules)
  • Board rendering change (extracted BoardCell and BoardLines)
  • Screen / UI change (extracted WinModal, MiniBoard, tutorialData)
  • Utility change (new boardLayout.ts, theme.ts)
  • Config / tooling change (new module structure)

Changes Overview

New Modules Created

  1. src/engine/types.ts — Centralized type definitions

    • Piece, Turn, Phase, Position, GameMode, Difficulty
    • GameConfig, GameMove, GameState
  2. src/engine/constants.ts — Game constants

    • BOARD_SIZE = 5, TOTAL_SHEEP = 20, SHEEP_TO_WIN = 5
  3. src/engine/boardOps.ts — Board utility functions

    • isInBounds(), hasDiagonals(), getNeighbors(), getCaptureTargets(), getValidMovesForPiece()
  4. src/theme.ts — Centralized color palette

    • Replaces hardcoded hex values throughout components
  5. src/utils/boardLayout.ts — Shared board layout calculations

    • BOARD_WIDTH, CELL_SIZE, PIECE_SIZE, DOT_SIZE, MINI_BOARD_WIDTH, MINI_CELL, etc.
    • hasDiag() helper function
  6. src/components/BoardCell.tsx — Extracted cell component

    • Piece rendering, selection pulse, capture flash, valid move indicator
    • Previously inline in Board.tsx
  7. src/components/BoardLines.tsx — Extracted board lines component

    • Horizontal, vertical, and diagonal line rendering
    • Previously inline in Board.tsx
  8. src/components/WinModal.tsx — Extracted win modal

    • Win screen UI with piece display, title, subtitle, and action buttons
    • Previously inline in GameScreen.tsx
  9. src/components/MiniBoard.tsx — Extracted mini board component

    • Tutorial board with animations, piece placement, and movement
    • Previously inline in TutorialScreen.tsx
  10. src/components/tutorialData.ts — Tutorial step definitions

    • TutorialStepData, PiecePlacement, AnimStep interfaces
    • STEPS array with all 7 tutorial steps
    • Previously inline in TutorialScreen.tsx
  11. src/hooks/useGameEvents.ts — Game event detection hook

    • Detects win, phase change, capture, selection, and move events
    • Triggers haptics, sounds, and animations
    • Previously inline in GameScreen.tsx
  12. src/hooks/useAIPlayer.ts — AI player orchestration hook

    • Manages AI turn detection, move computation, and state updates
    • Drives "thinking" pulse animation
    • Previously inline in GameScreen.tsx

Modified Files

  • src/engine/gameEngine.ts — Now re-exports types and functions from new modules for backward compatibility
  • src/components/TutorialScreen.tsx — Imports STEPS from tutorialData.ts, MiniBoard from MiniBoard.tsx
  • src/components/GameScreen.tsx — Imports WinModal, useGameEvents, useAIPlayer, colors
  • src/components/Board.tsx — Imports BoardCell, BoardLines, uses boardLayout.ts constants

https://claude.ai/code/session_01CfaghKuVR5AAttkwx96CgH

Break up large monolithic files into focused, single-responsibility modules:

Engine layer:
- Extract types.ts (game type definitions)
- Extract constants.ts (BOARD_SIZE, TOTAL_SHEEP, SHEEP_TO_WIN)
- Extract boardOps.ts (adjacency, captures, valid moves)
- Slim gameEngine.ts to state transitions + re-exports for backward compat

UI layer:
- Extract BoardCell.tsx (cell rendering + animations) from Board.tsx
- Extract BoardLines.tsx (grid + diagonal rendering) from Board.tsx
- Extract MiniBoard.tsx (animated tutorial board) from TutorialScreen.tsx
- Extract tutorialData.ts (step definitions) from TutorialScreen.tsx
- Extract WinModal.tsx (victory overlay) from GameScreen.tsx

Hooks:
- Extract useGameEvents.ts (sound/haptic/animation event detection)
- Extract useAIPlayer.ts (AI move orchestration + thinking pulse)

Shared utilities:
- Add theme.ts for centralized color palette
- Add boardLayout.ts for shared board dimension calculations

All 101 existing tests pass unchanged.

https://claude.ai/code/session_01CfaghKuVR5AAttkwx96CgH
@cursor

cursor Bot commented Mar 14, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Mostly a structural refactor, but it touches core engine exports and move-generation helpers; mistakes could subtly change rules, AI behavior, or UI interactions without obvious compile errors.

Overview
Refactors the codebase for modularity by splitting engine definitions into engine/types.ts, engine/constants.ts, and engine/boardOps.ts, updating gameEngine.ts/aiEngine.ts to consume these modules, and re-exporting from gameEngine.ts for backward compatibility.

Extracts UI and side-effect logic into dedicated modules: BoardCell/BoardLines are pulled out of Board, win overlay is moved to WinModal, and tutorial step data + animated mini-board are moved to tutorialData.ts and MiniBoard.

Introduces reusable hooks and shared styling utilities by moving game event side effects to useGameEvents, AI turn orchestration to useAIPlayer, and centralizing layout constants (utils/boardLayout.ts) and colors (theme.ts), with screens updated to use the new imports.

Written by Cursor Bugbot for commit 48736f7. This will update automatically on new commits. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

width: 20,
height: 20,
borderRadius: 10,
backgroundColor: colors.validMove,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tutorial highlighted cell opacity changed during refactoring

Low Severity

The tutorial mini board's highlighted cell backgroundColor was originally 'rgba(76, 175, 80, 0.4)' but now uses colors.validMove which is 'rgba(76, 175, 80, 0.5)'. The original tutorial intentionally used a more transparent green (0.4 alpha) than the main board's valid move indicator (0.5 alpha), but the refactoring consolidated both to the same colors.validMove value, losing the distinction.

Additional Locations (1)
Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants