@@ -479,13 +479,18 @@ function loadChallenge(challengeId) {
479479 // Clear any existing error markers
480480 Editor . clearAllMarkers ( ) ;
481481
482- // Try to load saved code, otherwise load starter code
483- const savedCode = Editor . loadSavedCode ( challengeId ) ;
484- if ( savedCode ) {
485- Editor . setCode ( savedCode ) ;
486- logDebug ( `Loaded saved code for Challenge ${ challengeId } ` ) ;
487- } else {
482+ // Debug challenge always loads fresh from main.py (no saved code)
483+ // Other challenges try saved code first, then starter code
484+ if ( challengeId === "debug" ) {
488485 loadStarterCode ( challengeId ) ;
486+ } else {
487+ const savedCode = Editor . loadSavedCode ( challengeId ) ;
488+ if ( savedCode ) {
489+ Editor . setCode ( savedCode ) ;
490+ logDebug ( `Loaded saved code for Challenge ${ challengeId } ` ) ;
491+ } else {
492+ loadStarterCode ( challengeId ) ;
493+ }
489494 }
490495
491496 // Store current challenge config
@@ -560,8 +565,31 @@ function loadChallenge(challengeId) {
560565
561566/**
562567 * Load starter code for a challenge
568+ * For debug challenge, fetches from project/main.py
563569 */
564- function loadStarterCode ( challengeId ) {
570+ async function loadStarterCode ( challengeId ) {
571+ // Debug challenge loads from project/main.py via GitHub raw
572+ if ( challengeId === "debug" ) {
573+ try {
574+ const response = await fetch (
575+ "https://raw.githubusercontent.com/TempeHS/AIDriver_MicroPython_Challanges/refs/heads/main/project/main.py"
576+ ) ;
577+ if ( response . ok ) {
578+ const code = await response . text ( ) ;
579+ Editor . setCode ( code ) ;
580+ logDebug ( "[App] Loaded starter code from project/main.py" ) ;
581+ return ;
582+ }
583+ } catch ( err ) {
584+ console . warn ( "[App] Could not fetch project/main.py:" , err ) ;
585+ }
586+ // Fallback if fetch fails
587+ Editor . setCode (
588+ "# Could not load project/main.py\n# Check your internet connection\n"
589+ ) ;
590+ return ;
591+ }
592+
565593 const starterCodes = getStarterCodes ( ) ;
566594 const code = starterCodes [ challengeId ] || "# No starter code available\n" ;
567595 Editor . setCode ( code ) ;
@@ -571,9 +599,9 @@ function loadStarterCode(challengeId) {
571599/**
572600 * Reset to starter code (discard changes)
573601 */
574- function resetToStarterCode ( ) {
602+ async function resetToStarterCode ( ) {
575603 Editor . clearSavedCode ( App . currentChallenge ) ;
576- loadStarterCode ( App . currentChallenge ) ;
604+ await loadStarterCode ( App . currentChallenge ) ;
577605 logDebug ( "[App] Code reset to starter code" ) ;
578606}
579607
@@ -1853,60 +1881,11 @@ function hideLoading() {
18531881
18541882/**
18551883 * Get starter codes for all challenges
1884+ * Note: Debug challenge code is fetched from project/main.py in loadStarterCode()
18561885 */
18571886function getStarterCodes ( ) {
18581887 return {
1859- debug : `# Debug Script: Hardware Sanity Test
1860- # Runs a sequence of movements and distance readings
1861- # This mirrors project/main.py for testing on real hardware
1862-
1863- from aidriver import AIDriver, hold_state
1864- import aidriver
1865-
1866- aidriver.DEBUG_AIDRIVER = True
1867-
1868- print("Initialising AIDriver hardware test...")
1869- my_robot = AIDriver()
1870-
1871- print("Starting tests in 3 seconds. Ensure clear space around the robot.")
1872- hold_state(3)
1873-
1874- # Test 1: Drive Forward
1875- print("Test 1: drive_forward")
1876- my_robot.drive_forward(200, 200)
1877- hold_state(2)
1878- my_robot.brake()
1879- hold_state(1)
1880-
1881- # Test 2: Drive Backward
1882- print("Test 2: drive_backward")
1883- my_robot.drive_backward(200, 200)
1884- hold_state(2)
1885- my_robot.brake()
1886- hold_state(1)
1887-
1888- # Test 3: Rotate Right
1889- print("Test 3: rotate_right")
1890- my_robot.rotate_right(200)
1891- hold_state(2)
1892- my_robot.brake()
1893- hold_state(1)
1894-
1895- # Test 4: Rotate Left
1896- print("Test 4: rotate_left")
1897- my_robot.rotate_left(200)
1898- hold_state(2)
1899- my_robot.brake()
1900- hold_state(1)
1901-
1902- # Test 5: Ultrasonic Sensor
1903- print("Test 5: ultrasonic distance readings")
1904- for i in range(5):
1905- distance = my_robot.read_distance()
1906- hold_state(0.5)
1907-
1908- print("All hardware tests completed.")
1909- ` ,
1888+ // debug: loaded dynamically from project/main.py
19101889 0 : `# Challenge 0: Fix the Code
19111890# This code has errors - can you find and fix them?
19121891
0 commit comments