Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "hbclass.ch"
- #define TRIES 50
- static aStates
- //----------------------------------------------------------------------------//
- function Main()
- local n
- aStates = InitStates()
- for n = 1 to TRIES
- ? "Number of steps to exit:", AllTrim( Str( SolveIt() ) )
- ? "---------------------------------------"
- next
- ? "Final scores after " + AllTrim( Str( TRIES ) ) + " tries"
- for n = 1 to Len( aStates )
- ? " room " + AllTrim( Str( aStates[ n ]:nId ) ) + " score: " + AllTrim( Str( aStates[ n ]:nScore ) )
- next
- ? "---------------------------------------"
- return nil
- //----------------------------------------------------------------------------//
- function SolveIt()
- local oState, nSteps := 0, n
- oState = aStates[ hb_RandomInt( 1, Len( aStates ) - 1 ) ]
- while ! oState == ATail( aStates )
- ? "The robot is at room: " + AllTrim( Str( oState:nId ) )
- if Len( oState:aOptions ) == 1
- ? " He can just go to room: " + AllTrim( Str( oState:aOptions[ 1 ]:nId ) )
- else
- ? " He has these choices:"
- for n = 1 to Len( oState:aOptions )
- ? " Go to room: " + AllTrim( Str( oState:aOptions[ n ]:nId ) ) + ;
- " (score: " + AllTrim( Str( oState:aOptions[ n ]:nScore ) ) + " )"
- next
- endif
- oState = oState:NextState()
- nSteps++
- end
- return nSteps
- //----------------------------------------------------------------------------//
- function InitStates()
- local aStates := Array( 6 )
- AEval( aStates, { | o, n | aStates[ n ] := TState():New( n ) } )
- aStates[ 1 ]:AddOption( aStates[ 5 ] )
- aStates[ 2 ]:AddOption( aStates[ 4 ] )
- aStates[ 2 ]:AddOption( aStates[ 6 ] )
- aStates[ 3 ]:AddOption( aStates[ 4 ] )
- aStates[ 4 ]:AddOption( aStates[ 2 ] )
- aStates[ 4 ]:AddOption( aStates[ 3 ] )
- aStates[ 4 ]:AddOption( aStates[ 5 ] )
- aStates[ 5 ]:AddOption( aStates[ 1 ] )
- aStates[ 5 ]:AddOption( aStates[ 4 ] )
- aStates[ 5 ]:AddOption( aStates[ 6 ] )
- aStates[ 6 ]:AddOption( aStates[ 2 ] )
- aStates[ 6 ]:AddOption( aStates[ 5 ] )
- return aStates
- //----------------------------------------------------------------------------//
- CLASS TState
- DATA nId
- DATA aOptions INIT {}
- DATA nScore INIT 0
- METHOD New( nId ) INLINE ::nId := nId, Self
- METHOD AddOption( oState ) INLINE AAdd( ::aOptions, oState )
- METHOD NextState()
- ENDCLASS
- //----------------------------------------------------------------------------//
- METHOD NextState() CLASS TState
- local n, nMax := 0, oState, aOptions
- if Len( ::aOptions ) == 1
- oState = ::aOptions[ 1 ]
- else
- aOptions = AShuffle( AClone( ::aOptions ) )
- for n = 1 to Len( aOptions )
- if aOptions[ n ]:nScore >= nMax
- nMax = aOptions[ n ]:nScore
- oState = aOptions[ n ]
- endif
- next
- endif
- if oState:nId == 6
- ::nScore += .1
- else
- ::nScore -= .1
- endif
- return oState
- //----------------------------------------------------------------------------//
- function AShuffle( aArray )
- return ASort( aArray,,, { || HB_RandomInt( 1, 1000 ) < HB_RandomInt( 1, 1000 ) } )
- //----------------------------------------------------------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement