我最近实施的乐趣Conway的生命游戏在Javascript(实际上CoffeeScript的,但同样的事情)。由于javascript可以用作功能性语言,因此我一直试图保持这种状态。我对结果不满意。我是一个相当不错的面向对象程序员,而我的解决方案充满了同样的古老。这么长的问题很简短:这样做的(伪代码)功能样式是什么?
这是我的尝试的伪代码:
class Node
update: (board) ->
get number_of_alive_neighbors from board
get this_is_alive from board
if this_is_alive and number_of_alive_neighbors < 2 then die
if this_is_alive and number_of_alive_neighbors > 3 then die
if not this_is_alive and number_of_alive_neighbors == 3 then alive
class NodeLocations
at: (x, y) -> return node value at x,y
of: (node) -> return x,y of node
class Board
getNeighbors: (node) ->
use node_locations to check 8 neighbors
around node and return count
nodes = for 1..100 new Node
state = new NodeState(nodes)
locations = new NodeLocations(nodes)
board = new Board(locations, state)
executeRound:
state = clone state
accumulated_changes = for n in nodes n.update(board)
apply accumulated_changes to state
board = new Board(locations, state)