# Experimental Math Rocks # Math-640 Project: Explore Solitaire games using Depth First Search (DFS) # Phil Benjamin # Control flow program and test program HelpDFS := proc() print(`DFScontrol()`); end proc: # General DFS Control # All arguments to this proc are procs applied to the problem # All procs use global variables (thus using information hiding from control) DFScontrol := proc(Init,IsTerm,NextProg,NextAlt,NextBack) local stepSuccess,backSuccess,termSuccess; Init(); if IsTerm() then return true; end if; stepSuccess := true; termSuccess := false; while stepSuccess and not termSuccess do stepSuccess := NextProg(); if not stepSuccess then stepSuccess := NextAlt(); end if; if not stepSuccess then # Need to backtrack until success backSuccess := true; while backSuccess and not stepSuccess do backSuccess := NextBack(); if backSuccess then stepSuccess := NextAlt(); end if; end do; end if; if stepSuccess then termSuccess := IsTerm(); end if; end do; return termSuccess; end proc: # Test procs with(Maplets[Examples]): TestGetInput := proc(myMsg,myTitle) return parse(GetInput(myMsg,title=myTitle)); end proc: TestMsg := proc(myMsg) return Message(myMsg,title="Note",type=information); end proc: # Test DFScontrol TestGlobalState := "": TestDFScontrol := proc() return DFScontrol(TestInit,TestIsTerm, TestNextProg,TestNextAlt,TestNextBack); end proc: TestInit := proc() global TestGlobalState; TestGlobalState := GetInput("Init:"); end proc: TestIsTerm := proc() global TestGlobalState; return parse(GetInput(cat("IsTerm: ",TestGlobalState))); end proc: TestNextProg := proc() global TestGlobalState; local NewGlobalState; NewGlobalState := GetInput("NextProg:",value=TestGlobalState); if NewGlobalState <> TestGlobalState then TestGlobalState := NewGlobalState; return true; end if; return false; end proc: TestNextAlt := proc() global TestGlobalState; local NewGlobalState; NewGlobalState := GetInput("NextAlt:",value=TestGlobalState); if NewGlobalState <> TestGlobalState then TestGlobalState := NewGlobalState; return true; end if; return false; end proc: TestNextBack := proc() global TestGlobalState; local NewGlobalState; NewGlobalState := GetInput("NextBack:",value=TestGlobalState); if NewGlobalState <> TestGlobalState then TestGlobalState := NewGlobalState; return true; end if; return false; end proc: