ライフゲーム
Posted feedbacks - Prolog
SWI-Prologで.間引きは実装してません.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | in_range(X, L) :- Max is L - 1, between(0, Max, X).
surround((LX,LY), (X,Y), (X1,Y1), B) :-
member((DX, DY), [(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]),
X1 is (LX + X + DX) mod LX, Y1 is (LY + Y + DY) mod LY, member((X1,Y1), B).
live((LX,LY), (X,Y), B) :-
in_range(X, LX), in_range(Y, LY),
findall(T, surround((LX,LY), (X,Y), T, B), S),
length(S, N), (member((X,Y), B) -> (N = 2; N = 3); (N = 3)).
print_board((LX, LY), B) :-
forall(in_range(Y, LY), (forall(in_range(X, LX),
(member((X,Y), B) -> write('■'); write('□'))
), writeln(''))), writeln('').
lifegame(L, B) :-
print_board(L, B), sleep(1),
findall(X, live(L, X, B), B1), lifegame(L, B1).
init_board((LX,LY), (X,Y)) :- in_range(X, LX), in_range(Y, LY), random(10) < 3.
:- L= (10,10), findall(X, init_board(L, X), B), lifegame(L, B).
|


saws
#5330()
Rating6/12=0.50
see: Wikipedia:ライフゲーム
[ reply ]