Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include "hbclass.ch"
  2.  
  3. #define TRIES 50
  4.  
  5. static aStates
  6.  
  7. //----------------------------------------------------------------------------//
  8.  
  9. function Main()
  10.  
  11. local n
  12.  
  13. aStates = InitStates()
  14.  
  15. for n = 1 to TRIES
  16. ? "Number of steps to exit:", AllTrim( Str( SolveIt() ) )
  17. ? "---------------------------------------"
  18. next
  19.  
  20. ? "Final scores after " + AllTrim( Str( TRIES ) ) + " tries"
  21. for n = 1 to Len( aStates )
  22. ? " room " + AllTrim( Str( aStates[ n ]:nId ) ) + " score: " + AllTrim( Str( aStates[ n ]:nScore ) )
  23. next
  24. ? "---------------------------------------"
  25.  
  26. return nil
  27.  
  28. //----------------------------------------------------------------------------//
  29.  
  30. function SolveIt()
  31.  
  32. local oState, nSteps := 0, n
  33.  
  34. oState = aStates[ hb_RandomInt( 1, Len( aStates ) - 1 ) ]
  35.  
  36. while ! oState == ATail( aStates )
  37. ? "The robot is at room: " + AllTrim( Str( oState:nId ) )
  38. if Len( oState:aOptions ) == 1
  39. ? " He can just go to room: " + AllTrim( Str( oState:aOptions[ 1 ]:nId ) )
  40. else
  41. ? " He has these choices:"
  42. for n = 1 to Len( oState:aOptions )
  43. ? " Go to room: " + AllTrim( Str( oState:aOptions[ n ]:nId ) ) + ;
  44. " (score: " + AllTrim( Str( oState:aOptions[ n ]:nScore ) ) + " )"
  45. next
  46. endif
  47. oState = oState:NextState()
  48. nSteps++
  49. end
  50.  
  51. return nSteps
  52.  
  53. //----------------------------------------------------------------------------//
  54.  
  55. function InitStates()
  56.  
  57. local aStates := Array( 6 )
  58.  
  59. AEval( aStates, { | o, n | aStates[ n ] := TState():New( n ) } )
  60.  
  61. aStates[ 1 ]:AddOption( aStates[ 5 ] )
  62.  
  63. aStates[ 2 ]:AddOption( aStates[ 4 ] )
  64. aStates[ 2 ]:AddOption( aStates[ 6 ] )
  65.  
  66. aStates[ 3 ]:AddOption( aStates[ 4 ] )
  67.  
  68. aStates[ 4 ]:AddOption( aStates[ 2 ] )
  69. aStates[ 4 ]:AddOption( aStates[ 3 ] )
  70. aStates[ 4 ]:AddOption( aStates[ 5 ] )
  71.  
  72. aStates[ 5 ]:AddOption( aStates[ 1 ] )
  73. aStates[ 5 ]:AddOption( aStates[ 4 ] )
  74. aStates[ 5 ]:AddOption( aStates[ 6 ] )
  75.  
  76. aStates[ 6 ]:AddOption( aStates[ 2 ] )
  77. aStates[ 6 ]:AddOption( aStates[ 5 ] )
  78.  
  79. return aStates
  80.  
  81. //----------------------------------------------------------------------------//
  82.  
  83. CLASS TState
  84.  
  85. DATA nId
  86. DATA aOptions INIT {}
  87. DATA nScore INIT 0
  88.  
  89. METHOD New( nId ) INLINE ::nId := nId, Self
  90. METHOD AddOption( oState ) INLINE AAdd( ::aOptions, oState )
  91. METHOD NextState()
  92.  
  93. ENDCLASS
  94.  
  95. //----------------------------------------------------------------------------//
  96.  
  97. METHOD NextState() CLASS TState
  98.  
  99. local n, nMax := 0, oState, aOptions
  100.  
  101. if Len( ::aOptions ) == 1
  102. oState = ::aOptions[ 1 ]
  103. else
  104. aOptions = AShuffle( AClone( ::aOptions ) )
  105. for n = 1 to Len( aOptions )
  106. if aOptions[ n ]:nScore >= nMax
  107. nMax = aOptions[ n ]:nScore
  108. oState = aOptions[ n ]
  109. endif
  110. next
  111. endif
  112.  
  113. if oState:nId == 6
  114. ::nScore += .1
  115. else
  116. ::nScore -= .1
  117. endif
  118.  
  119. return oState
  120.  
  121. //----------------------------------------------------------------------------//
  122.  
  123. function AShuffle( aArray )
  124.  
  125. return ASort( aArray,,, { || HB_RandomInt( 1, 1000 ) < HB_RandomInt( 1, 1000 ) } )
  126.  
  127. //----------------------------------------------------------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement