Guest User

Untitled

a guest
Apr 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. with Ada.Text_Io, Plateau, Configs, TableHach, File,
  2. Ada.Unchecked_Deallocation ;
  3. use Ada, Ada.Text_Io, Plateau, Configs, TableHach ;
  4.  
  5. function CheminGagnant return ConfigConnue is
  6.  
  7. procedure Deallocate is new Unchecked_Deallocation(ConfigCell,
  8. ConfigConnue) ;
  9.  
  10. -- current Etat/Config
  11. InitConfig: constant Config := Exporte_Etat ;
  12. ---- an irrelevant Coup used to instantiate the initial ConfigConnue
  13. --C: Coup;
  14. -- pointing to the currently checked Config (head of queue)
  15. CurrentConf: ConfigConnue ;
  16.  
  17. -- Adds all the neigbours of the given Config into the queue.
  18. procedure AddNeighbours(CurrentConf: in ConfigConnue) is
  19. -- represent all the moves which can be played one after the other
  20. -- (and thus the reachable neighbour Configs)
  21. TempECoup: Enum_Coup ;
  22. -- the current neighbour's Config
  23. NeighbourConfig: Config ;
  24. begin
  25. -- get the first usable move
  26. Demarrer(TempECoup) ;
  27.  
  28. -- while there is a next possible move, add the related Config
  29. -- into the queue
  30. while (not Fin(TempECoup)) loop
  31. -- get the Config of the current move
  32. Effectue(Courant(TempECoup)) ;
  33. NeighbourConfig := Exporte_Etat ;
  34. -- if this Config is not already in the hash table, add it
  35. -- (this avoids adding to many elements in the File, but is not
  36. -- a sufficient condition to filter all the irrelevant ones)
  37. if (Cherche(NeighbourConfig) = null) then
  38. File.Inserer(new ConfigCell'
  39. (NeighbourConfig, CurrentConf,
  40. Courant(TempECoup), null)) ;
  41. end if ;
  42.  
  43. -- set the board to the previous position
  44. Met_Plateau(CurrentConf.all.Clef) ;
  45. -- get the next possible move
  46. Avancer(TempECoup) ;
  47. end loop ;
  48. end AddNeighbours ;
  49.  
  50. begin
  51. File.Vider ;
  52. -- insert the initial Etat into the queue as its first element
  53. CurrentConf := new ConfigCell ;
  54. CurrentConf.Clef := InitConfig ;
  55. File.Inserer(CurrentConf) ;
  56.  
  57. -- Each iteration is associated with a unique Config (Etat).
  58. -- The first element of the queue is added into the hash table, and
  59. -- each one of its neighbours is then added into the Queue unless
  60. -- they are already existing in the hash table, or unless the current
  61. -- queue head is a winning Config.
  62. while (not File.Estvide) loop
  63. -- get the head of the queue (the currently checked Config)
  64. File.Extraire(CurrentConf) ;
  65. if (TableHach.Cherche(CurrentConf.all.Clef) = null) then
  66. -- set the board to the current Config
  67. Met_Plateau(CurrentConf.all.Clef) ;
  68. -- insert the head of the queue into the hash table
  69. TableHach.Inserer(CurrentConf) ;
  70. -- check if the added Config is a winning one
  71. if (Plateau.A_Gagne) then
  72. -- if it is, set the board back to the initial Config
  73. Plateau.Met_Plateau(InitConfig) ;
  74. File.Vider ;
  75. -- complete the hash table and return the initial Config
  76. return TableHach.SetWinningConfig(CurrentConf) ;
  77. end if ;
  78. -- add the relevant neighbours of the current Config
  79. AddNeighbours(CurrentConf) ;
  80. else
  81. Deallocate(CurrentConf) ;
  82. end if;
  83. end loop ;
  84. -- reaching this code part means no winning Config has been found
  85. Met_Plateau(InitConfig) ;
  86. File.Vider ;
  87. return null ;
  88. end ;
Add Comment
Please, Sign In to add comment