mission1.adb

with Mouv ;
with Gada.Text_IO ;
with Gada.Graphics ;

procedure Mission1 is

   package M   renames Mouv ;
   package Txt renames Gada.Text_IO ;
   package Gr  renames Gada.Graphics ;

   -- Affiche la position reçue en argument
   procedure Affiche_Pos (Pos : M.T_Pos) is
   begin
      Txt.Put_Line("X = " & Integer'Image(Pos.X) & ", Y = " & Integer'Image(Pos.Y)) ;
      Txt.New_Line ;
   end Affiche_Pos ;


   -- Affiche (en français) la direction reçue en argument.
   procedure Affiche_Direction (Dir : M.T_Direction) is
   begin
      case Dir is
         when M.Up    => Txt.Put("Haut ") ;
         when M.Down  => Txt.Put("Bas ") ;
         when M.Left  => Txt.Put("Gauche ") ;
         when M.Right => Txt.Put("Droite ") ;
         when M.None  => Txt.Put("Rien ") ;
      end case ;
   end Affiche_Direction ;


   -- Affiche (textuellement) la séquence reçue en argument.
   procedure Affiche_Sequence (Seq : M.T_Sequence) is
   begin
      for Index in Seq'Range loop
         Affiche_Direction(Seq(Index)) ;
      end loop ;
      Txt.New_Line ;
   end Affiche_Sequence ;


   -- Calcule la position finale obtenue à partir de l'origine indiquée
   -- et de la séquence de déplacements indiquée.
   function Calcule_Position (Origine : M.T_Pos ; Seq : M.T_Sequence) return M.T_Pos is
      Pos : M.T_Pos := Origine ;
   begin
      for Index in Seq'Range loop
         case Seq(Index) is
            when M.Up    => Pos.Y := Pos.Y + 1 ;
            when M.Down  => Pos.Y := Pos.Y - 1 ;
            when M.Left  => Pos.X := Pos.X - 1 ;
            when M.Right => Pos.X := Pos.X + 1 ;
            when M.None  => null ;
         end case ;
      end loop ;

      return Pos ;
   end Calcule_Position ;


   -- Transforme une chaîne comme "UUDLLR" en séquence de directions
   -- (ici Up Up Down Left Left Right)
   function Transforme (Chaine : String) return M.T_Sequence is
      Resultat : M.T_Sequence(Chaine'Range) ;
      Dir : M.T_Direction ;
   begin
      for Index in Chaine'Range loop
         case Chaine(Index) is
            when 'U' => Dir := M.Up ;
            when 'D' => Dir := M.Down ;
            when 'L' => Dir := M.Left ;
            when 'R' => Dir := M.Right ;
            when others => Dir := M.None ;
         end case ;

         Resultat(Index) := Dir ;
      end loop ;

      return Resultat ;
   end Transforme ;


   -- Séquence de test
   Test1 : M.T_Sequence := (M.Up, M.Up, M.Right) ;

   -- Position de l'origine
   Pos_Depart : M.T_Pos := (4, 2) ;
begin

   -- Affiche la position de l'origine et la séquence de test.
   Txt.Put_Line("Origine : ") ;
   Affiche_Pos( Pos_Depart) ;
   Affiche_Sequence( Test1 ) ;

   -- Affiche la position après la séquence de test
   Affiche_Pos( Calcule_Position( Pos_Depart, Test1) ) ;


   --
   -- Affiche la position après la séquence entrée par l'utilisateur.
   --
   Txt.Put("Entrez maintenant votre propre séquence : ") ;

   declare
      Chaine_Perso : String := Txt.FGet ;
      Sequence : M.T_Sequence := Transforme(Chaine_Perso) ;
   begin
      Affiche_Sequence(Sequence) ;
      Affiche_Pos( Calcule_Position( Pos_Depart, Sequence) ) ;
   end ;

end Mission1 ;