The application is built as a React-based web interface for visualizing and interacting with a Turing machine simulation. The project is structured around several key components:
├── TuringMachine.js # Main container component
├── TuringMachineClass.js # Core Turing machine logic
├── Components
│ ├── Tape.js # Visual tape representation
│ ├── StateViewer.js # Current state display
│ ├── PlaybackControls.js # Execution controls
│ └── TuringMachineConfig # Machine configuration editor
The backbone of the simulation is implemented in TuringMachineClass.js. This class handles the core Turing machine operations:
- State Management: Tracks current state, tape contents, and head position
- Transition Logic: Implements the state transition function (δ)
- Execution Control: Handles step-by-step execution and termination conditions
Key methods include:
stepTM(machine, config) {
// Finds and executes valid transitions based on current state and tape symbol
// Returns array of possible next configurations
}
isAcceptTM(machine, config) {
// Checks if current state is an accepting state
}
isDoneTM(machine, config) {
// Checks if machine has reached a terminal state (accept/reject)
}The tape visualization (Tape.js) provides a scrolling view of the Turing machine tape:
- Uses CSS transforms for smooth animation of tape movement
- Implements infinite tape simulation with dynamic cell generation
- Highlights the current head position with visual feedback
Manages execution flow with features like:
- Step forward/backward
- Play/pause continuous execution
- Speed control
- Input loading
- Machine reset
Provides real-time feedback about:
- Current state
- Symbol under head
- Current transition
- Next state
- Final state visualization (accept/reject)
Handles machine configuration with:
- JSON-based configuration editor
- Validation of machine definitions
- Configuration reset capability
- Error reporting
The application uses React's useState and useRef hooks for state management:
// Core state elements in TuringMachine.js
const [tape, setTape] = useState([]);
const [headPosition, setHeadPosition] = useState(0);
const [history, setHistory] = useState([]);
const [machineState, setMachineState] = useState("initial");The UI implements a neumorphic design system with:
- Soft shadows and highlights
- Consistent color palette defined in CSS variables
- Smooth transitions and animations
- Responsive layout using CSS Grid
Key visual features:
:root {
--primary: #0A2463;
--secondary: #3E92CC;
--background: #ECF0F3;
--accent: #D8315B;
--shadow-dark: rgba(10, 36, 99, 0.15);
--shadow-light: rgba(255, 250, 255, 0.7);
}-
Step-by-Step Execution
- Forward and backward stepping through computation
- Visual feedback for each transition
-
Configuration Management
- JSON-based machine definition
- Real-time validation
- Error reporting
- Default configuration for quick start
-
Visual Feedback
- Animated tape movement
- State transitions
- Accept/reject animations
- Current state highlighting
-
Playback Control
- Variable speed execution
- Play/pause functionality
- Reset capability
- Input modification
const machine = {
states: [1, 2, 3, 4, 777, 666],
alphabet: ["a", "b"],
tape_alphabet: ["a", "b", "X", "_"],
start: 1,
accept: 777,
reject: 666,
delta: [
[1, "a", 2, "X", 1], // State 1, read 'a': go to state 2, write 'X', move right
// ... additional transitions
]
};- User inputs string or loads configuration
- Machine initializes with input on tape
- User can:
- Step through computation
- Run continuous execution
- Modify speed
- Reset machine
- Load new input
- Visual feedback shows computation progress
- Machine terminates with accept/reject animation