richie3366

morpion ada

Nov 13th, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 5.69 KB | None | 0 0
  1. with entrees_sorties; use entrees_sorties;
  2.  
  3.  
  4. procedure morpion is
  5.  
  6.   function getColByNum(num : Integer) return Integer is
  7.   begin
  8.    
  9.     return ((num-1) mod 3) + 1;
  10.    
  11.   end getColByNum;
  12.  
  13.   function getLineByNum(num : Integer) return Integer is
  14.   begin
  15.    
  16.     return ((num-1) / 3) + 1;
  17.    
  18.   end getLineByNum;
  19.  
  20.   function getOccupant(grille, lig, col : Integer) return Integer is
  21.   begin
  22.    
  23.     return ((grille/(1000**(lig-1)))/(10**(col-1)) mod 10);
  24.    
  25.   end getOccupant;
  26.  
  27.  
  28.   procedure ajouterMarque(grille : in out Integer; marque, lig, col : Integer) is
  29.   begin
  30.    
  31.     grille := grille + (marque*((1000**(lig-1))*((10**(col-1)))));
  32.    
  33.   end ajouterMarque;
  34.  
  35.   function caseDejaOccupee(grille, lig, col : Integer) return Boolean is
  36.   begin
  37.    
  38.     return (getOccupant(grille, lig, col) /= 0);
  39.    
  40.   end caseDejaOccupee;
  41.  
  42.   function estPleine(grille : Integer) return Boolean is
  43.     pleine : Boolean;
  44.   begin
  45.     pleine := TRUE;
  46.    
  47.     for a in 1..9 loop
  48.       if(((grille / (10**(a-1))) mod 10) = 0) then
  49.     pleine := FALSE;
  50.       end if;
  51.     end loop;
  52.    
  53.     return pleine;
  54.    
  55.   end estPleine;
  56.  
  57.   procedure afficherBordSuperieur is
  58.   begin
  59.     put("^^^^^");
  60.     new_line;
  61.   end afficherBordSuperieur;
  62.  
  63.   procedure afficherBordInferieur is
  64.   begin
  65.     put("vvvvv");
  66.     new_line;
  67.   end afficherBordInferieur;
  68.  
  69.   function getCharFromInteger(int1 : Integer) return Character is
  70.   begin
  71.    
  72.     if(int1 = 1) then
  73.       return 'X';
  74.     elsif(int1 = 2) then
  75.       return 'O';
  76.     else
  77.       return ' ';
  78.     end if;
  79.    
  80.   end getCharFromInteger;
  81.  
  82.  
  83.   procedure extraireLigne(grille, numLigne : in Integer; case1, case2, case3 : out Integer) is
  84.   begin
  85.    
  86.     case1 := ((grille/(1000**(numLigne-1)))     mod 10);
  87.     case2 := ((grille/(1000**(numLigne-1)))/10  mod 10);
  88.     case3 := ((grille/(1000**(numLigne-1)))/100 mod 10);
  89.    
  90.   end extraireLigne;
  91.  
  92.   procedure extraireColonne(grille, numColonne : in Integer; case1, case2, case3 : out Integer) is
  93.   begin
  94.    
  95.     case1 := ((grille/(1000**(0)))/(10**(numColonne-1)) mod 10);
  96.     case2 := ((grille/(1000**(1)))/(10**(numColonne-1)) mod 10);
  97.     case3 := ((grille/(1000**(2)))/(10**(numColonne-1)) mod 10);
  98.    
  99.   end extraireColonne;
  100.  
  101.   procedure extraireDiagonale(grille, numDiagonale : in Integer; case1, case2, case3 : out Integer) is
  102.   begin
  103.    
  104.     case1 := ((grille/(1000**(getLineByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*0)) - 1)))/(10**(getColByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*0))-1)) mod 10);
  105.     case2 := ((grille/(1000**(getLineByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*1)) - 1)))/(10**(getColByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*1))-1)) mod 10);
  106.     case3 := ((grille/(1000**(getLineByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*2)) - 1)))/(10**(getColByNum((9-(numDiagonale mod 2)*2)-(2*numDiagonale*2))-1)) mod 10);
  107.    
  108.   end extraireDiagonale;
  109.  
  110.  
  111.   function ilYAUnGagnant(grille : Integer) return Integer is
  112.     case1, case2, case3 : Integer;
  113.     gagnant : Integer;
  114.   begin
  115.    
  116.     gagnant := 0;
  117.    
  118.     for lig in 1..3 loop
  119.       extraireLigne(grille, lig, case1, case2, case3);
  120.       if(case1 = case2 AND case2 = case3 AND case1 /= 0) then
  121.     gagnant := case1;
  122.       end if;
  123.     end loop;
  124.    
  125.     for col in 1..3 loop
  126.       extraireColonne(grille, col, case1, case2, case3);
  127.       if(case1 = case2 AND case2 = case3 AND case1 /= 0) then
  128.     gagnant := case1;
  129.       end if;
  130.     end loop;
  131.    
  132.     for num in 1..2 loop
  133.       extraireDiagonale(grille, num, case1, case2, case3);
  134.       if(case1 = case2 AND case2 = case3 AND case1 /= 0) then
  135.     gagnant := case1;
  136.       end if;
  137.     end loop;
  138.    
  139.     return gagnant;
  140.    
  141.   end ilYAUnGagnant;
  142.  
  143.   procedure afficherLigne(case1, case2, case3 : Integer) is
  144.    
  145.   begin
  146.    
  147.     put('<'&getCharFromInteger(case1)&'|'
  148.        &getCharFromInteger(case2)&'|'
  149.        &getCharFromInteger(case3)&'>');
  150.     new_line;
  151.    
  152.   end afficherLigne;
  153.  
  154.   procedure afficherSeparationLigne is
  155.   begin
  156.     put("<-+-+->");
  157.     new_line;
  158.   end afficherSeparationLigne;
  159.  
  160.  
  161.   procedure afficherGrille(grille : Integer) is
  162.     case1, case2, case3 : Integer;
  163.     numLigne : Integer;
  164.   begin
  165.    
  166.     afficherBordSuperieur;
  167.     extraireLigne(grille, 3, case1, case2, case3);
  168.     afficherLigne(case1, case2, case3);
  169.     afficherSeparationLigne;
  170.     extraireLigne(grille, 2, case1, case2, case3);
  171.     afficherLigne(case1, case2, case3);
  172.     afficherSeparationLigne;
  173.     extraireLigne(grille, 1, case1, case2, case3);
  174.     afficherLigne(case1, case2, case3);
  175.     afficherBordInferieur;
  176.    
  177.   end afficherGrille;
  178.  
  179.   lig, col : Integer;
  180.   ch : Integer;
  181.   grille : Integer;
  182.   joueur, gagnant : Integer;
  183. begin
  184.   grille := 0;
  185.   afficherGrille(grille);
  186.   joueur := 1;
  187.  
  188.   while(ilYAUnGagnant(grille) = 0 AND not(estPleine(grille))) loop
  189.     get(ch);
  190.    
  191.     lig := getLineByNum(ch);
  192.     col := getColByNum(ch);
  193.    
  194.     put("Ligne ="&Integer'Image(lig));
  195.     new_line;
  196.    
  197.     put("Col ="&Integer'Image(lig));
  198.     new_line;
  199.    
  200.     put("occupant ="&Integer'Image(getOccupant(grille, lig, col)));
  201.     new_line;
  202.    
  203.     if(caseDejaOccupee(grille, lig, col)) then
  204.       put("La case est deja occupee !");
  205.       new_line;
  206.     else
  207.       ajouterMarque(grille, joueur, lig, col);
  208.       joueur := ((joueur) mod 2) + 1;
  209.     end if;
  210.    
  211.     new_line;
  212.    
  213.     afficherGrille(grille);
  214.     new_line;
  215.     new_line;
  216.   end loop;
  217.    
  218.  
  219.   gagnant := ilYAUnGagnant(grille);
  220.   if(gagnant /= 0) then
  221.     put("Le gagnant est"&Integer'Image(gagnant));
  222.   else
  223.     put("Il n'y a pas de gagnant...");
  224.   end if;
  225.   new_line;
  226.  
  227.  
  228.  
  229. end morpion;
Advertisement
Add Comment
Please, Sign In to add comment