SHARE
TWEET

Untitled

a guest Jun 3rd, 2018 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fun {MakeAsked N}
  2.    Rows = {MakeTuple rows N} in
  3.    for I in 1..N do
  4.       Rows.I = {MakeTuple cols N}
  5.    end
  6.    Rows
  7. end
  8.  
  9. fun {CheckAllAsked Asked N}
  10.    NumAsked = {NewCell 0} in
  11.    for I in 1..N do
  12.       for J in 1..N do
  13.          NumAsked := NumAsked + Asked.I.J
  14.       end
  15.    end
  16.    @NumAsked == 8
  17. end
  18.  
  19. fun {AskAllNeighbors X Y Alive Neighbors}
  20.    {ForAll Neighbors proc {$ N}
  21.       {Send N getAlive(From:pos(X Y) Alive)}
  22.    end}
  23. end
  24.  
  25. fun {MakeAgent X Y Alive Neighbors Map N}
  26.    fun {Behavior Msg State}
  27.       case Msg of getAlive(From:pos(X Y) FromAlive) then
  28.          NewAskedAlive = State.askedAlive + FromAlive in
  29.          State.asked.X.Y = true
  30.          if {CheckAllAsked State.asked N} then NewAlive in
  31.             if NewAskedAlive == 2 then NewAlive = State.alive
  32.             elseif NewAskedAlive == 3 then NewAlive = 1
  33.             else NewAlive = 0 end
  34.             {AskAllNeighbors X Y NewAlive Neighbors}
  35.             {AdjoinList State [alive:NewAlive asked:{MakeAsked} askedAlive:0]}
  36.          else
  37.             {AdjoinList State [askedAlive:NewAskedAlive]}
  38.          end
  39.       end
  40.    end
  41. in
  42.    {NewPortObject Behavior state(
  43.       alive:Alive
  44.       asked:{MakeAsked}
  45.       askedAlive:0
  46.    )}
  47. end
  48.  
  49. fun {MakeMap N}
  50.    Rows = {MakeTuple row N} in
  51.    for I in 1..N do
  52.       Rows.I = {MakeTuple col N}
  53.       for J in 1..N do
  54.          Neighbors = [
  55.             % Left side
  56.             Rows.(1+((I+N-1) mod N)).(1+((J+N-1) mod N))
  57.             Rows.(1+((I+N-1) mod N)).J
  58.             Rows.(1+((I+N-1) mod N)).(1+((J+1) mod N))
  59.             % Top and bottom
  60.             Rows.I.(1+((J+N-1) mod N))
  61.             Rows.I.(1+((J+1) mod N))
  62.             % Right side
  63.             Rows.(1+((I+1) mod N)).(1+((J+N-1) mod N))
  64.             Rows.(1+((I+1) mod N)).J
  65.             Rows.(1+((I+1) mod N)).(1+((J+1) mod N))
  66.          ]
  67.          Alive = {OS.rand} mod 2
  68.       in
  69.          Rows.I.J = {MakeAgent I J Alive Neighbors Map N}
  70.          {AskAllNeighbors I J Alive Neighbors}
  71.       end
  72.    end
  73.    Rows
  74. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top