Re: [問題] 有人寫過生命遊戲的程式嗎?已回收
※ 引述《AMooMA (阿斌)》之銘言:
: 以下是這款遊戲的規則:
: ◆ 對於存活的細胞(塗色的方格):
: 當八個鄰近細胞中,只有零個或一個是活細胞時, 則該細胞會因孤獨而死亡
: 當八個鄰近細胞中,恰有二或三個是活細胞時,則該細胞繼續存活
: 當八個鄰近細胞中,有四個或超過四個是活細胞時,則該細胞會因擁擠而死亡
: ◆ 對於死亡的細胞(未塗色的空方格):
: 當八個鄰近細胞中,恰有三個是活細胞時,則該處誕生一個活細胞
: ------------------------------------------------------------------------
: 請問有寫過這個遊戲的大大
: 或對這款遊戲有興趣的神人
: 可以告訴我會用到的相關指令或程式碼嗎?
: 感謝!
好玩的問題XD
%% 三層function 一次滿足 很黃很暴力之迴圈寫法
function map = life_game(msize,cell_level,duration)
% Usage:
% map = life_game(msize,cell_level,duration)
% Input:
% msize: matrix size, can be a scalar or a [r c]
% cell_level: determind the initial cell level, between 0 to 1
% duration: how many life-cycle want to perform
% Output:
% map: the final cell distribution
% --
% Kevin @ yymmdd: 100610
temp = zeros(msize);
temp = imnoise(temp,'salt & pepper',cell_level);
map = zeros(msize+2);
map(2:end-1,2:end-1) = temp;
for ii = 1:duration
figure,imagesc(map),colormap(gray);axis image,axis off;
new_map = zeros(size(map));
for jj = 1:size(temp,2)
for kk = 1:size(temp,1)
if temp(kk,jj)
new_map(kk+1,jj+1) = live_rule(map(kk:kk+2,jj:jj+2));
else
new_map(kk+1,jj+1) = dead_rule(map(kk:kk+2,jj:jj+2));
end
end
end
map = new_map;
end
figure,imagesc(map),colormap(gray);axis image,axis off;
map = map(2:end-1,2:end-1); return
end
function output = live_rule(pattern)
a = sum(sum(pattern))-1;
if a <= 1 || a >= 4
output = 0;
else
output = 1;
end
return
end
function output = dead_rule(pattern)
a = sum(sum(pattern));
if a == 3
output = 1;
else
output = 0;
end
return
end
% 打完收工!
應該沒錯啦 ~ 請慢用 ~
--
◥◥ ╔═╦═╗ ╔═╗ ╔═══╗
╭──╮╱◥ ║ ║╔══╗╔═╦╗╔═╗╔══╗║ ║
◎ ╰╮ ║ ╣║ ═ ║║ ║║ ║║ ║║ ║
╱ │ ║ ║║ ╣║ ║║ ║║ ║╚╗ ╔╝
▁╱ │ ╚═╩═╝╚══╝╚══╝╚═╝╚═╩╝ ╚═╝
▏ ◎╯ 順手雜記 → http://www.wretch.cc/album/kevint By luh4@PTT
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.148.130
→
06/10 06:23, , 1F
06/10 06:23, 1F
推
06/10 07:16, , 2F
06/10 07:16, 2F
→
06/10 07:18, , 3F
06/10 07:18, 3F
推
06/10 08:49, , 4F
06/10 08:49, 4F
推
06/10 08:56, , 5F
06/10 08:56, 5F
→
06/10 10:52, , 6F
06/10 10:52, 6F
→
06/10 10:54, , 7F
06/10 10:54, 7F
→
06/10 11:18, , 8F
06/10 11:18, 8F
→
06/10 11:18, , 9F
06/10 11:18, 9F
→
06/10 11:40, , 10F
06/10 11:40, 10F
→
05/03 16:53, , 11F
05/03 16:53, 11F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):