挑战:编写实现John H. Conway的Game of Life元胞子自动机的最短程序.[ 链接 ]
编辑:经过大约一周的比赛,我选择了一个胜利者:pdehaan,用perl 打算用一个角色击败Matlab解决方案.
对于那些没有听过生命游戏的人来说,你需要一个方格单元格(理想情况下是无限的).细胞可以存活(填充)或死亡(空).我们通过应用以下规则确定下一步中哪些细胞存活:
您的程序将读入指定为命令行参数的40x80字符ASCII文本文件,以及要执行的迭代次数(N).最后,它将在N次迭代后输出到ASCII文件out.txt系统的状态.
以下是运行相关文件的示例:
in.txt:
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.............................................
.......................................X........................................
................................XXXXXX.X........................................
................................X...............................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
Run Code Online (Sandbox Code Playgroud)
迭代100次:
Q:\>life in.txt 100
Run Code Online (Sandbox Code Playgroud)
结果输出(out.txt)
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................ …Run Code Online (Sandbox Code Playgroud) language-agnostic code-golf conways-game-of-life rosetta-stone
为了实验,我(很久以前)实施了康威的生命游戏(我知道这个相关的问题!).
我的实现通过保留2个布尔数组来表示"最后状态"和"正在更新状态"(每次迭代时交换2个数组).虽然速度相当快,但我常常想知道如何优化它.
例如,一个想法是在迭代N处预先计算可以在迭代(N + 1)处修改的区域(因此,如果一个单元不属于这样的区域,则甚至不会考虑在迭代(N + 1)).我知道这很模糊,我从来没有花时间详细介绍......
你对如何优化(速度)Game of Life迭代有任何想法(或经验!)吗?
language-agnostic algorithm performance conways-game-of-life
我一直在玩康威的生命游戏,最近发现了一些非常快速的实现,如Hashlife和Golly.(在这里下载Golly - http://golly.sourceforge.net/)
我无法理解的一件事是编码器如何实现无限网格?我们无法保持无限的任何东西,如果你跑得很好并且让几个滑翔机飞过边缘,等待几分钟然后向外缩放,你会看到滑翔机仍在那里空间逃跑,所以在神的名字中如何以编程方式处理这个无限的概念?是否存在记录良好的模式或什么?
非常感谢
algorithm cellular-automata sparse-matrix data-structures conways-game-of-life
好吧,有很多"康威的生命游戏"问题,但这个问题非常具体.我将不得不首先向您抛出一堆代码,将其分解并告诉您问题所在.
所以这是迄今为止我的Conway的生命游戏实现,现在它仅限于调试控制台(JSfiddle - http://jsfiddle.net/georeith/C9Gyr/8/ - 启动它,打开你的控制台):
var utils = {};
/*
* utils.extend()
* - Extend initial object with all properties of following objects, objects later in the argument list take precedence.
*/
utils.extend = function(obj) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = args.length; i--;) {
for (var prop in args[i]) {
obj[prop] = args[i][prop];
}
}
return obj;
}
/*
* utils.defaults()
* - Overwrite initial object with properties of following objects only if key is …Run Code Online (Sandbox Code Playgroud) 我的Conway在Python中的生活游戏实现似乎没有正确遵循规则,我无法弄清楚可能出错的地方.当我将最终配置放入Golly时,它会继续超出我的范围.
我首先通过将我的程序停止进入Golly的配置来识别程序,然后注意到它可以进一步传送.
我还把我的游戏中的整个小板放到了Golly中,它与我的配置有很大的不同.Golly是一款广泛使用的生命模拟器游戏.
我尝试了几种不同的方法来解决我的问题:
and/ orstatements.neighbors()通过将其插入到自己的程序中并设置一些网格配置来测试我的功能.neighbors()了一个位置.它工作得很好.看着我的代码,我看不出它为什么不起作用.我没有得到错误,它有效,它只是错误.模式的进展与它们的应用方式大不相同.这也是我编写的第一个> 100行程序,如果没有松散地遵循教程,请原谅我,如果答案是显而易见的.
相关代码如下:
#Function to find number of live neighbors
def neighbors(row, column):
adjacents = 0
#Horizontally adjacent
if row > 0:
if board[row-1][column]:
adjacents += 1
if column > 0:
if board[row][column-1]:
adjacents += 1
if row < thesize-1:
if board[row+1][column]:
adjacents += 1
if column < thesize-1:
if board[row][column+1]:
adjacents += 1
#Diagonally adjacent
if row > 0 and …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种快速且节省内存的方法来实现Conway的生命游戏.
限制因素:96x128板,大约2kB RAM和52MHz处理器(请参阅技术规范:http://www.getinpulse.com/features).
我目前的天真解决方案将每个单元表示为矩阵中的单个位(96*128/8 = 1,536字节),但速度太慢.可以使用哪些技巧来提高性能?
存储活细胞的坐标(例如在此实现中http://dotat.at/prog/life/life.html)会占用太多内存.
我正在努力在Haskell开发一个小小的Conway的生命游戏.我写了一个库,生命游戏,它可以管理细胞网格并计算它们的代数(参见github.com/qleguennec/lifegame.git).世代是一个无限的列表.到目前为止,图书馆工作得很好,但缺乏文档.这是一个小型图书馆,它不应该如此难以进入.
现在,我在这里的,我试图用lifegame加上掌舵实际显示世代到屏幕上.这是我写的:
import FRP.Helm
import FRP.Helm.Graphics (Element(..))
import qualified FRP.Helm.Window as Window
import FRP.Helm.Animation (Frame, AnimationStatus(..), animate, absolute)
import FRP.Helm.Time (second, running, delta)
import FRP.Elerea.Simple
import Cell.Display (allGenFrames)
import LifeGame.Data.CellGrid (CellGrid(..), randCellGrid)
render :: Form -> (Int, Int) -> Element
render form (x, y) = collage x y $ [form]
main :: IO ()
main = do
cg <- randCellGrid 50 50
anim <- return . absolute $ allGenFrames cg …Run Code Online (Sandbox Code Playgroud) 我实现了康威的生命游戏.我希望通过使用并行性来加速它.
life :: [(Int, Int)] -> [(Int, Int)]
life cells = map snd . filter rules . freq $ concatMap neighbours cells
where rules (n, c) = n == 3 || (n == 2 && c `elem` cells)
freq = map (length &&& head) . group . sort
parLife :: [(Int, Int)] -> [(Int, Int)]
parLife cells = parMap rseq snd . filter rules . freq . concat $ parMap rseq neighbours cells
where rules (n, c) = n == …Run Code Online (Sandbox Code Playgroud) 1)在下面的代码中,创建gameOfLive变量的原因是function gameOfLife()什么?
2)什么是gol?它看起来像一个数组,但我不熟悉语法或它所谓的.
我正在学习http://sixfoottallrabbit.co.uk/gameoflife/
if (!window.gameOfLife) var gameOfLife = function() {
var gol = {
body: null,
canvas: null,
context: null,
grids: [],
mouseDown: false,
interval: null,
control: null,
moving: -1,
clickToGive: -1,
table: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(''),
tableBack: null,
init: function(width, height) {
gol.body = document.getElementsByTagName('body')[0];
gol.canvas = document.createElement('canvas');
if (gol.canvas.getContext) {
gol.context = gol.canvas.getContext('2d');
document.getElementById('content').appendChild(gol.canvas);
gol.canvas.width = width;
gol.canvas.height = height;
gol.canvas.style.marginLeft = "8px";
gol.control = document.getElementById('gridcontrol');
gol.canvas.onmousedown = gol.onMouseDown;
gol.canvas.onmousemove = gol.onMouseMove;
gol.canvas.onmouseup …Run Code Online (Sandbox Code Playgroud) 我制作了一个小型的生命游戏程序,它自己迭代几代。问题是每次迭代时, putStrLn 函数都会显着减慢,我无法弄清楚原因。这是代码:
import Control.Concurrent
data CellState = Dead | Alive
data Position = Position Integer Integer
type Generation = Position -> CellState
is_alive :: CellState -> Bool
is_alive Alive = True
is_alive Dead = False
neighbors :: Position -> [Position]
neighbors (Position x y) =
[(Position (x-1) (y-1)), (Position x (y-1)), (Position (x+1) (y-1)), (Position (x+1) y),
(Position (x+1) (y+1)), (Position x (y+1)), (Position (x-1) (y+1)), (Position (x-1) y)]
alive_neighbors :: Generation -> Position -> Int
alive_neighbors generation position …Run Code Online (Sandbox Code Playgroud) haskell ×3
algorithm ×2
javascript ×2
code-golf ×1
elerea ×1
embedded ×1
frp ×1
low-memory ×1
performance ×1
python ×1
sdl ×1