-- Assignment 1. -- Eugene Burke -- Oct 23, 1996 -- -- FILE: -- life_game.adb -- is used by life_game.ads, life1.adb -- PURPOSE: -- Implements the functions of the Life board with Ada.Text_IO; with Ada.Integer_Text_IO; package body Life_Game is function Neighbor_Count (I : ROW; J : COL; Map : GRID) return INTEGER is Count : INTEGER; Xlow : ROW; Xhigh : ROW; Ylow : COL; Yhigh : COL; begin if I = 1 then Xlow := 1; else Xlow := I-1; end if; if I = MaxRow then Xhigh := 1; else Xhigh := I+1; end if; if J = 1 then Ylow := 1; else Ylow := J-1; end if; if J = MaxCol then Yhigh := 1; else Yhigh := J+1; end if; Count := 0; for X in Xlow..Xhigh loop for Y in Ylow..Yhigh loop if Map(X,Y) = alive then Count := Count + 1; end if; end loop; end loop; if Map(I,J) = alive then Count := Count - 1; end if; return Count; end Neighbor_Count; procedure Initialize (Map : out GRID) is X : INTEGER; Y : INTEGER; begin Ada.Text_IO.Put (Item => "This program is a simulation of Game of Life"); Ada.Text_IO.New_Line; for X in 1..MaxRow loop for Y in 1..MaxCol loop Map(X,Y) := dead; end loop; end loop; Ada.Text_IO.Put(Item => "On each line give a pair of"); Ada.Text_IO.Put(Item => " coordinates for a living cell"); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "Terminate the list with the special pair 0 0"); Ada.Text_IO.New_Line; Ada.Integer_Text_IO.Get(Item => X); Ada.Integer_Text_IO.Get(Item => Y); while (X /= 0 or Y /= 0) loop if (X >= 1 and X <= MaxRow and Y >= 1 and Y <= MaxCol) then Map(X,Y) := alive; else Ada.Text_IO.Put(Item => "Values are not within range"); Ada.Text_IO.New_Line; end if; Ada.Integer_Text_IO.Get(Item => X); Ada.Integer_Text_IO.Get(Item => Y); end loop; end Initialize; procedure Write_Map (Map : in GRID) is Full : constant character := '*'; Empty : constant character := ' '; --X : ROW; --Y : COL; begin Ada.Text_IO.Put(Item => "The map is below"); Ada.Text_IO.New_Line; for X in 1..MaxRow loop for Y in 1..MaxCol loop if Map(X,Y) = alive then Ada.Text_IO.Put(Item => Full); else Ada.Text_IO.Put(Item => Empty); end if; end loop; Ada.Text_IO.New_Line; end loop; end Write_Map; function Enquire return BOOLEAN is Run_again : BOOLEAN; Response : Character; begin while Response /= 'Y' and Response /= 'y' and Response /= 'N' and Response /= 'n' loop Ada.Text_IO.Put (Item => "Continue (y / n)"); Ada.Text_IO.New_Line; Ada.Text_IO.Get (Item => Response); end loop; if Response ='Y' or Response = 'y' then Run_Again := true; else Run_Again := false; end if; return Run_Again; end Enquire; end Life_Game; -- COMMENTS -- You are very lucky. You can change the board by placing -- a border of dead cells around it, to avoid checking for boundary -- conditions, and only this implementation -- must be changed. Your other two files can remain the same. -- Isn't that a nice feature of modularity. -- The only cost of adding a border is the initialization of the -- temporary grid that is used to store the next generation, which -- can be done in the declaration: -- Map : GRID := (others => (others => FALSE)); -- Loop parameters cannot be explicitly declared. In fact, you will -- be allocated different variables X and Y than your loop parameters. -- I would also prefer the names Row and Column to X and Y.