tsoding / ada-gol Goto Github PK
View Code? Open in Web Editor NEWSimple Game of Life implementation in Ada.
License: MIT License
Simple Game of Life implementation in Ada.
License: MIT License
I don't know how open you are to pull requests, so I apologize if using an issue is an improper way to approach this. I just happened onto your video about this code and enjoyed it. I saw you were trying different techniques to handle adding the deltas to the rows and columns and I wanted to suggest an alternative for you.
You can define mathematical operator functions that take in your Rows (or Cols) type and an Integer, do the math and return a Rows (or Cols) type.:
function "+"(L: Rows; R: Integer) return Rows is (Rows'Mod(Integer(L)+R));
function "+"(L: Cols; R: Integer) return Cols is (Cols'Mod(Integer(L)+R));
These are functions using the "expression function" syntax (as opposed to the begin/end syntax you are more familiar with), but you can also use classical syntax:
function "+"(L: Rows; R: Integer) return Rows is
begin
return (Rows'Mod(Integer(L)+R));
end;
function "+"(L: Cols; R: Integer) return Cols is
begin
return (Cols'Mod(Integer(L)+R));
end;
Basically you convert the Rows (or Cols) variable into an integer, do the addition, then apply the 'Mod attribute to the result to both handle wraparound and convert it back to the appropriate return type.
This allows your Count_Neighbors procedure to use the -1 .. 1 ranges again like so:
function Count_Neighbors(B: Board; Row0: Rows; Col0: Cols) return Neighbors is
begin
return Result : Neighbors := 0 do
for Delta_Row in -1 .. 1 loop
for Delta_Col in -1 .. 1 loop
if Delta_Row /= 0 or Delta_Col /= 0 then
declare
Row : constant Rows := Row0 + Delta_Row;
Col : constant Cols := Col0 + Delta_Col;
begin
if B(Row, Col) = Alive then
Result := Result + 1;
end if;
end;
end if;
end loop;
end loop;
end return;
end;
It helps avoid the issue you had in the video with the loops not being executed due to the -1 converting to 7 because it allows the loop variables Delta_Row and Delta_Cols to remain as Integers while also allowing you to add an Integer to your user defined types Rows and Cols.
Anyways, I again apologize if I approached this improperly
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.