richie3366

Reines, version primitive 1

Oct 10th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.01 KB | None | 0 0
  1. with entrees_sorties; use entrees_sorties;
  2.  
  3. procedure reines is
  4.   SIZE : constant Integer := 8;
  5.   NBTENTMAX : constant Integer := 1;
  6.   type Plateau is array (1..SIZE,1..SIZE) of Character;
  7.   grille : Plateau;
  8.  
  9.   nbReines : Integer := 0;
  10.   nbTent : Integer := 0;
  11.  
  12.   procedure dessineGrille(grille : in Plateau) is
  13.   begin
  14.     new_line;
  15.     for i in 1..SIZE loop
  16.       for j in 1..SIZE loop
  17.         put(' '&grille(i,j)&' ');
  18.       end loop;
  19.       new_line;
  20.     end loop;
  21.     new_line;
  22.   end dessineGrille;
  23.  
  24.   function valAbs(n : Integer) return Integer is
  25.   begin
  26.     if(n < 0) then
  27.       return (-1)*n;
  28.     else
  29.       return n;
  30.     end if;
  31.   end valAbs;
  32.  
  33.   function reinePlacable(grille : Plateau ; i,j : Integer) return Boolean is
  34.       estPlacable : Boolean := TRUE;
  35.     begin
  36.    
  37.     for x in 1..SIZE loop
  38.       for y in 1..SIZE loop
  39.    
  40.     if(i=x OR j=y OR (valAbs(i-x)=valAbs(j-y))) then
  41.       if(grille(x, y)='Q') then
  42.         estPlacable := FALSE;
  43.       end if;
  44.     end if;
  45.    
  46.       end loop;
  47.     end loop;
  48.    
  49.     return estPlacable;
  50.    
  51.   end reinePlacable;
  52.  
  53.   function finissable(grilleBase : Plateau; a,b, nbInit : Integer) return Boolean is
  54.     grilleLoc : Plateau;
  55.     nbReinesLoc : Integer;
  56.     nbTentLoc : Integer := 0;
  57.   begin
  58.     grilleLoc := grilleBase;
  59.     grilleLoc(a,b) := 'Q';
  60.     --dessineGrille(grilleLoc);
  61.     nbReinesLoc := nbInit+1;
  62.     --put(";{"&Integer'Image(nbReinesLoc)&"}-");
  63.     nbTentLoc := 0;
  64.  
  65.   while(nbReinesLoc < SIZE AND nbTentLoc < NBTENTMAX) loop
  66.    
  67.     for i in 1..SIZE loop
  68.       for j in 1..SIZE loop
  69.          if(reinePlacable(grilleLoc, i, j)) then
  70.            if(finissable(grilleLoc, i, j, nbReinesLoc)) then
  71.              grilleLoc(i, j) := 'Q';
  72.              nbReinesLoc := nbReinesLoc + 1;
  73.                --new_line;
  74.              --put("_nbReinesLoc :="&Integer'Image(nbReinesLoc)); --'
  75.              --new_line;
  76.              nbTentLoc := 0;
  77.            end if;
  78.          end if;
  79.       end loop;
  80.     end loop;
  81.     nbTentLoc := nbTentLoc + 1;
  82.   end loop;
  83.   --put("-{"&Integer'Image(nbReinesLoc)&"};");
  84.   if(nbReinesLoc = SIZE) then
  85.     --put("YESSSSSS");
  86.     return TRUE;
  87.   else
  88.     --put("[tentF/"&Integer'Image(nbReinesLoc)&"]");
  89.     return FALSE;
  90.   end if;
  91.  
  92.   end finissable;
  93.  
  94.  
  95.  
  96. begin
  97.  
  98.   for i in 1..SIZE loop
  99.     for j in 1..SIZE loop
  100.       grille(i,j) := '.';
  101.     end loop;
  102.     --new_line;
  103.   end loop;
  104.  
  105.   nbTent := 0;
  106.  
  107.   while(nbReines < SIZE AND nbTent < NBTENTMAX) loop
  108.    
  109.     for i in 1..SIZE loop
  110.       for j in 1..SIZE loop
  111.     if(reinePlacable(grille, i, j)) then
  112.       if(finissable(grille, i, j, nbReines)) then
  113.         grille(i, j) := 'Q';
  114.         nbReines := nbReines + 1;
  115.         --new_line;
  116.         --put("nbReines :="&Integer'Image(nbReines)); --'
  117.         --new_line;
  118.      
  119.         nbTent := 0;
  120.       end if;
  121.     end if;
  122.       end loop;
  123.     end loop;
  124.     nbTent := nbTent + 1;
  125.   end loop;
  126.  
  127.     dessineGrille(grille);
  128.  
  129.     new_line;
  130.     put("nbReines ="&Integer'Image(nbReines)&" / nbTent ="&Integer'Image(nbTent));
  131.     new_line;  
  132.  
  133. end reines;
Advertisement
Add Comment
Please, Sign In to add comment