ฉันเพิ่งนำมาใช้เพื่อความสนุกสนานGame of Life ของ Conwayใน Javascript (จริงๆแล้วกาแฟเป็นสิ่งเดียวกัน) เนื่องจากจาวาสคริปต์สามารถใช้เป็นภาษาที่ใช้งานได้ฉันจึงพยายามอยู่จนสุดช่วงความถี่ ฉันไม่พอใจกับผลลัพธ์ของฉัน ฉันเป็นโปรแกรมเมอร์ OO ที่ค่อนข้างดีและวิธีการแก้ปัญหาของฉันถูกตีเหมือนกันอายุเท่าเดิม คำถามยาว ๆ สั้น ๆ : รูปแบบการทำงาน (pseudocode) ของการทำคืออะไร?
นี่คือ Pseudocode สำหรับความพยายามของฉัน:
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)