Git Product home page Git Product logo

ada-gol's People

Contributors

rexim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ada-gol's Issues

Alternative way to handle the deltas

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.