Skip to content

Commit 583d4a9

Browse files
committed
feat: 完成3个任务 - 变量监视面板、结果点击高亮、递归树增强
1. 任务1950: 右侧算法代码区域已实现debug效果 - 变量监视面板显示局部变量和调用栈 - 变量变化时有高亮动画 2. 任务1951: 找到的组合点击高亮递归树路径 - ResultList组件添加点击事件 - TreeVisualization组件支持路径高亮 - 金色高亮显示选中路径 3. 任务1952: 递归树增加更多示意元素 - 连接线添加箭头标记 - 深度标签和参考线 - 连接线旁显示+字母标签 - 当前节点脉冲动画 - 叶子节点显示完整路径
1 parent 7fadcd7 commit 583d4a9

File tree

8,917 files changed

+1887985
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

8,917 files changed

+1887985
-0
lines changed

.kiro/specs/phone-letter-combinations/design.md

Lines changed: 403 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Requirements Document
2+
3+
## Introduction
4+
5+
本系统是一个电话号码字母组合算法的可视化演示应用。基于 LeetCode 第17题,该应用使用 TypeScript、React 和 D3.js 技术栈,通过动画分步展示回溯算法如何生成给定数字字符串(2-9)对应的所有字母组合。用户可以输入数字字符串,观察算法的递归调用过程、当前路径构建和结果收集的完整流程。
6+
7+
## Glossary
8+
9+
- **Digit_String**: 仅包含数字 2-9 的输入字符串,长度为 1-4
10+
- **Letter_Mapping**: 数字到字母的映射关系,与电话按键相同(2→abc, 3→def, 4→ghi, 5→jkl, 6→mno, 7→pqrs, 8→tuv, 9→wxyz)
11+
- **Combination**: 由输入数字对应字母组成的一个完整字母组合结果
12+
- **Backtracking_Algorithm**: 回溯算法,通过递归探索所有可能路径并在必要时回退
13+
- **Current_Path**: 算法执行过程中正在构建的字母序列
14+
- **Recursion_Tree**: 表示算法递归调用关系的树形结构
15+
- **Animation_Step**: 算法执行过程中的一个可视化步骤
16+
- **Visualization_Canvas**: D3.js 渲染动画的画布区域
17+
18+
## Requirements
19+
20+
### Requirement 1
21+
22+
**User Story:** As a user, I want to input a digit string (2-9), so that I can see all possible letter combinations.
23+
24+
#### Acceptance Criteria
25+
26+
1. WHEN a user enters a digit string in the input field THEN the System SHALL validate that the string contains only digits 2-9
27+
2. WHEN a user enters an invalid character (0, 1, or non-digit) THEN the System SHALL display an error message and prevent algorithm execution
28+
3. WHEN a user enters a valid digit string with length between 1 and 4 THEN the System SHALL enable the start animation button
29+
4. WHEN a user enters a digit string longer than 4 characters THEN the System SHALL truncate the input to 4 characters and display a warning
30+
31+
### Requirement 2
32+
33+
**User Story:** As a user, I want to see the digit-to-letter mapping displayed, so that I can understand the relationship between digits and letters.
34+
35+
#### Acceptance Criteria
36+
37+
1. WHEN the application loads THEN the System SHALL display the complete digit-to-letter mapping (2→abc through 9→wxyz)
38+
2. WHEN a user enters a digit THEN the System SHALL highlight the corresponding letters in the mapping display
39+
3. WHEN the algorithm processes a specific digit THEN the System SHALL visually emphasize that digit's letter options in the mapping
40+
41+
### Requirement 3
42+
43+
**User Story:** As a user, I want to control the animation playback, so that I can learn at my own pace.
44+
45+
#### Acceptance Criteria
46+
47+
1. WHEN a user clicks the "Start" button THEN the System SHALL begin the step-by-step animation from the first step
48+
2. WHEN a user clicks the "Pause" button during animation THEN the System SHALL pause the animation at the current step
49+
3. WHEN a user clicks the "Next Step" button THEN the System SHALL advance the animation by exactly one step
50+
4. WHEN a user clicks the "Previous Step" button THEN the System SHALL revert the animation by exactly one step
51+
5. WHEN a user clicks the "Reset" button THEN the System SHALL return the animation to the initial state
52+
6. WHEN a user adjusts the speed slider THEN the System SHALL update the animation speed accordingly
53+
54+
### Requirement 4
55+
56+
**User Story:** As a user, I want to see the recursion tree visualization, so that I can understand how the backtracking algorithm explores different paths.
57+
58+
#### Acceptance Criteria
59+
60+
1. WHEN the algorithm starts THEN the System SHALL render an empty recursion tree root node using D3.js
61+
2. WHEN the algorithm makes a recursive call THEN the System SHALL animate a new child node being added to the tree
62+
3. WHEN the algorithm backtracks THEN the System SHALL visually indicate the backtracking by highlighting the path being abandoned
63+
4. WHEN a complete combination is found THEN the System SHALL highlight the path from root to leaf as a successful result
64+
5. WHEN the tree grows beyond the visible area THEN the System SHALL provide pan and zoom capabilities
65+
66+
### Requirement 5
67+
68+
**User Story:** As a user, I want to see the current path being built, so that I can track the algorithm's progress.
69+
70+
#### Acceptance Criteria
71+
72+
1. WHEN the algorithm adds a letter to the current path THEN the System SHALL display the updated path with the new letter highlighted
73+
2. WHEN the algorithm removes a letter during backtracking THEN the System SHALL animate the letter being removed from the path
74+
3. WHEN the current path forms a complete combination THEN the System SHALL visually indicate completion before adding to results
75+
76+
### Requirement 6
77+
78+
**User Story:** As a user, I want to see all found combinations displayed, so that I can verify the algorithm's output.
79+
80+
#### Acceptance Criteria
81+
82+
1. WHEN a complete combination is found THEN the System SHALL add the combination to the results list with an animation
83+
2. WHEN the algorithm completes THEN the System SHALL display the total count of combinations found
84+
3. WHEN displaying results THEN the System SHALL show combinations in the order they were discovered
85+
86+
### Requirement 7
87+
88+
**User Story:** As a user, I want to see the algorithm's code with current line highlighting, so that I can understand which part of the code is executing.
89+
90+
#### Acceptance Criteria
91+
92+
1. WHEN the application loads THEN the System SHALL display the backtracking algorithm pseudocode
93+
2. WHEN an animation step executes THEN the System SHALL highlight the corresponding line of code
94+
3. WHEN the algorithm enters a recursive call THEN the System SHALL indicate the recursion depth visually
95+
96+
### Requirement 8
97+
98+
**User Story:** As a user, I want the application to handle edge cases gracefully, so that I can explore different scenarios.
99+
100+
#### Acceptance Criteria
101+
102+
1. WHEN a user enters an empty string THEN the System SHALL display an empty result set without errors
103+
2. WHEN a user enters a single digit THEN the System SHALL display the corresponding 3 or 4 letters as individual combinations
104+
3. WHEN the animation is running and user changes input THEN the System SHALL reset and prepare for new input
105+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Implementation Plan
2+
3+
- [x] 1. Initialize project and setup core infrastructure
4+
- [x] 1.1 Create Vite + React + TypeScript project structure
5+
- Initialize project with `npm create vite@latest`
6+
- Configure TypeScript strict mode
7+
- Configure Vite dev server to use port 65388
8+
- Install dependencies: react, d3, fast-check, vitest
9+
- _Requirements: 1.1, 1.3_
10+
- [x] 1.2 Define core type definitions and constants
11+
- Create `types.ts` with all interfaces (AnimationStep, TreeNode, AppState, etc.)
12+
- Create `constants.ts` with DIGIT_TO_LETTERS mapping
13+
- _Requirements: 2.1_
14+
15+
- [x] 2. Implement input validation module
16+
- [x] 2.1 Create validateInput function
17+
- Implement validation logic for digits 2-9
18+
- Handle invalid characters, empty input, and length truncation
19+
- _Requirements: 1.1, 1.2, 1.4, 8.1_
20+
- [x] 2.2 Write property tests for input validation
21+
- **Property 1: Input Validation Correctness**
22+
- **Validates: Requirements 1.1, 1.2**
23+
- [x] 2.3 Write property test for input truncation
24+
- **Property 2: Input Truncation Consistency**
25+
- **Validates: Requirements 1.4**
26+
27+
- [x] 3. Implement algorithm engine and step generator
28+
- [x] 3.1 Create letterCombinations algorithm function
29+
- Implement backtracking algorithm
30+
- Return all valid combinations for given digit string
31+
- _Requirements: 6.2, 8.2_
32+
- [x] 3.2 Create step generator for animation
33+
- Generate AnimationStep array from algorithm execution
34+
- Include all step types: INIT, ENTER_RECURSION, SELECT_LETTER, ADD_TO_PATH, FOUND_COMBINATION, BACKTRACK, EXIT_RECURSION, COMPLETE
35+
- Track tree state, current path, and results at each step
36+
- _Requirements: 4.2, 5.1, 5.2, 6.1_
37+
- [x] 3.3 Write property test for algorithm output correctness
38+
- **Property 12: Algorithm Output Correctness**
39+
- **Validates: Requirements 6.2**
40+
- [x] 3.4 Write property test for single digit combinations
41+
- **Property 14: Single Digit Combination Count**
42+
- **Validates: Requirements 8.2**
43+
- [x] 3.5 Write property test for algorithm round-trip
44+
- **Property 15: Algorithm Round-Trip Correctness**
45+
- **Validates: Requirements 1.1, 6.2**
46+
47+
- [x] 4. Checkpoint - Ensure all tests pass
48+
- Ensure all tests pass, ask the user if questions arise.
49+
50+
- [x] 5. Implement animation controller
51+
- [x] 5.1 Create useAnimationController custom hook
52+
- Implement play, pause, nextStep, prevStep, reset functions
53+
- Manage currentStepIndex and isPlaying state
54+
- Handle speed control with configurable interval
55+
- _Requirements: 3.1, 3.2, 3.3, 3.4, 3.5, 3.6_
56+
- [x] 5.2 Write property tests for step navigation
57+
- **Property 3: Step Navigation Forward Invariant**
58+
- **Property 4: Step Navigation Backward Invariant**
59+
- **Validates: Requirements 3.3, 3.4**
60+
- [x] 5.3 Write property test for reset functionality
61+
- **Property 5: Reset State Consistency**
62+
- **Validates: Requirements 3.5**
63+
64+
- [x] 6. Implement tree state management
65+
- [x] 6.1 Create tree state update functions
66+
- Implement addNode, markBacktracked, markCompleted functions
67+
- Maintain tree structure consistency
68+
- _Requirements: 4.2, 4.3, 4.4_
69+
- [x] 6.2 Write property tests for tree operations
70+
- **Property 6: Tree Growth on Recursion**
71+
- **Property 7: Backtrack Path Marking**
72+
- **Property 8: Completion Path Highlighting**
73+
- **Validates: Requirements 4.2, 4.3, 4.4**
74+
75+
- [x] 7. Implement path and results management
76+
- [x] 7.1 Create path state management functions
77+
- Implement addToPath, removeFromPath functions
78+
- Track current path state
79+
- _Requirements: 5.1, 5.2, 5.3_
80+
- [x] 7.2 Write property tests for path operations
81+
- **Property 9: Path Addition Consistency**
82+
- **Property 10: Path Removal on Backtrack**
83+
- **Property 11: Results Ordering Preservation**
84+
- **Validates: Requirements 5.1, 5.2, 6.1, 6.3**
85+
86+
- [x] 8. Checkpoint - Ensure all tests pass
87+
- Ensure all tests pass, ask the user if questions arise.
88+
89+
- [x] 9. Build UI components
90+
- [x] 9.1 Create InputPanel component
91+
- Text input for digit string
92+
- Real-time validation feedback
93+
- Error message display
94+
- _Requirements: 1.1, 1.2, 1.3, 1.4_
95+
- [x] 9.2 Create ControlPanel component
96+
- Play, Pause, Next, Prev, Reset buttons
97+
- Speed slider control
98+
- Button state management (disabled states)
99+
- _Requirements: 3.1, 3.2, 3.3, 3.4, 3.5, 3.6_
100+
- [x] 9.3 Create MappingDisplay component
101+
- Display digit-to-letter mapping grid
102+
- Highlight active digit and letter
103+
- _Requirements: 2.1, 2.2, 2.3_
104+
- [x] 9.4 Create PathDisplay component
105+
- Show current path being built
106+
- Animate letter additions and removals
107+
- Indicate completion state
108+
- _Requirements: 5.1, 5.2, 5.3_
109+
- [x] 9.5 Create ResultList component
110+
- Display found combinations
111+
- Show total count
112+
- Highlight newly added results
113+
- _Requirements: 6.1, 6.2, 6.3_
114+
- [x] 9.6 Create CodeDisplay component
115+
- Display algorithm pseudocode
116+
- Highlight current executing line
117+
- Show recursion depth indicator
118+
- _Requirements: 7.1, 7.2, 7.3_
119+
120+
- [x] 10. Implement D3.js tree visualization
121+
- [x] 10.1 Create TreeVisualization component
122+
- Set up D3.js SVG canvas
123+
- Implement tree layout algorithm
124+
- Render nodes and edges
125+
- _Requirements: 4.1, 4.2_
126+
- [x] 10.2 Add tree animation and interaction
127+
- Animate node additions
128+
- Highlight active, completed, and backtracked nodes
129+
- Implement pan and zoom for large trees
130+
- _Requirements: 4.3, 4.4, 4.5_
131+
- [x] 10.3 Write property test for code line correspondence
132+
- **Property 13: Code Line Correspondence**
133+
- **Validates: Requirements 7.2**
134+
135+
- [x] 11. Integrate all components in App
136+
- [x] 11.1 Create main App component
137+
- Compose all UI components
138+
- Wire up state management
139+
- Handle input change during animation
140+
- _Requirements: 8.3_
141+
- [x] 11.2 Add styling and layout
142+
- Create responsive layout
143+
- Apply consistent styling
144+
- Ensure accessibility
145+
- _Requirements: All UI requirements_
146+
147+
- [x] 12. Final Checkpoint - Ensure all tests pass
148+
- Ensure all tests pass, ask the user if questions arise.
149+

0 commit comments

Comments
 (0)