Advertisement
Guest User

Galton board

a guest
Oct 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.68 KB | None | 0 0
  1. with Ada.Text_IO; use Ada.Text_IO;
  2. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  3. with Ada.Numerics.Discrete_Random;
  4.  
  5. procedure U2015_2 is
  6.    
  7.    type Nail_Line is array(1..10) of Boolean;
  8.    type Nail_Line_Mirror is array(1..9) of Boolean;
  9.    type Nail_Board is array(1..20) of Nail_Line;
  10.    type Nail_Board_Mirror is array(1..20) of Nail_Line_Mirror;
  11.    
  12.    function Randomizer(Max : in Integer) return Integer is
  13.       subtype Random_Number is
  14.     Integer range 1..Max;
  15.       package Randomized is
  16.      new Ada.Numerics.Discrete_Random(Random_Number);
  17.       use Randomized;
  18.      
  19.       G : Generator;
  20.       N : Integer;
  21.    begin
  22.       Reset(G);
  23.       N := Random(G);
  24.       return N;      
  25.    end Randomizer;
  26.    
  27.    function Check_Position(Board : in Nail_Board; Y,X : in Integer) return Boolean is
  28.    begin
  29.       if Board(Y)(X) = True then
  30.      return False;
  31.       end if;
  32.      
  33.       if X = 1 then
  34.      if Board(Y)(X+1) = False then
  35.         return True;
  36.      else return False;
  37.      end if;
  38.       elsif X = 10 then
  39.      if Board(Y)(X-1) = False then
  40.         return True;
  41.      else return False;
  42.      end if;
  43.       elsif Board(Y)(X+1) = False and Board(Y)(X-1) = False then
  44.      return True;
  45.       else
  46.      return False;
  47.       end if;
  48.    end Check_Position;
  49.    
  50.    procedure Set_Nails(Board : out Nail_Board; Mirror : out Nail_Board_Mirror) is
  51.       Nail_Counter : Integer := 0;
  52.       X, Y : Integer;
  53.    begin
  54.       while Nail_Counter < 70 loop
  55.       Y := Randomizer(20);
  56.       if Nail_Counter = 69 then
  57.      X := 10;
  58.       else
  59.      X := Randomizer(10);
  60.       end if;
  61.      if Check_Position(Board,Y,X) = True then
  62.         Board(Y)(X) := True;
  63.         if X = 10 then
  64.            Nail_Counter := Nail_Counter+1;
  65.         else
  66.            Mirror(Y)(10-X) := True;
  67.            Nail_Counter := Nail_Counter+2;
  68.         end if;
  69.      end if;
  70.       end loop;
  71.       Put("Antal spikar: ");
  72.       Put(Nail_Counter,0);
  73.    end Set_Nails;
  74.    
  75.    procedure Print_Board is
  76.       My_Nail_Board : Nail_Board := (others => (others => False));
  77.       My_Nail_Board_Mirror : Nail_Board_Mirror := (others => (others => False));
  78.    begin
  79.      
  80.       Set_Nails(My_Nail_Board, My_Nail_Board_Mirror);
  81.       New_Line;
  82.       for Y in 1..20 loop
  83.      Put("|");
  84.      for X in 1..10 loop
  85.         if My_Nail_Board(Y)(X) = True then
  86.            Put(".");
  87.         else
  88.            Put(" ");
  89.         end if;
  90.      end loop;
  91.      for X in 1..9 loop
  92.         if My_Nail_Board_Mirror(Y)(X) = True then
  93.            Put(".");
  94.         else
  95.            Put(" ");
  96.         end if;  
  97.         end loop;
  98.         Put_Line("|");
  99.       end loop;
  100.       Put_Line("|   |WIN|   |WIN|   |");
  101.       Put_Line("|___|___|___|___|___|");
  102.      
  103.    end Print_Board;
  104.    
  105. begin
  106.    
  107.    Print_Board;
  108.    
  109.    
  110. end U2015_2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement