Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. !c29_1_5snakes_ladders.f95
  2. integer function next_state(i,trans,n)
  3. real::trans(n,n),cum_prob(n),r
  4. integer::mnloc(1)
  5. !  the random next_state after i
  6. endfunction  
  7.  
  8. program snakes_ladders_individual
  9. real::trans(9,9)=0,average=0
  10. integer,parameter::inf=10**5
  11. integer::n,next_state,nummoves,history(200)
  12. ! 1, 2, ..., 9 = board positions,  trans = transition probabilities
  13. trans(1,7)=.5; trans(1,5)=.5
  14. trans(2,5)=.5; trans(2,4)=.5
  15. trans(3,4)=.5; trans(3,5)=.5
  16. trans(4,5)=.5; trans(4,1)=.5
  17. trans(5,1)=.5; trans(5,7)=.5
  18. trans(7,4)=.5; trans(7,9)=.5
  19. trans(8,9)=.5; trans(8,9)=.5
  20. do n=1,inf
  21.   !play game
  22.   history=0;i=1;history(1)=1
  23.   do k=2,200
  24.      i=next_state(i,trans,9)
  25.      history(k)=i; if(i==9) exit
  26.   enddo
  27.   !update records and print play
  28.   nummoves=count(history/=0)-1
  29.   average=(average*(n-1)+real(nummoves))/n
  30.   if(mod(n,6000)/=1)cycle
  31.   print 11,nummoves,history(1:nummoves+1)
  32. enddo
  33. 11 format(i3," moves",200(i3))
  34. print '(a,f5.3,/)', "Average number of moves is ",average
  35. endprogram
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement