% From Wikipedea. % The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead (or populated and unpopulated, respectively). Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: % Any live cell with fewer than two live neighbours dies, as if by underpopulation. % Any live cell with two or three live neighbours lives on to the next generation. % Any live cell with more than three live neighbours dies, as if by overpopulation. % Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. % These rules, which compare the behavior of the automaton to real life, can be condensed into the following: % Any live cell with two or three live neighbours survives. % Any dead cell with three live neighbours becomes a live cell. % All other live cells die in the next generation. Similarly, all other dead cells stay dead. %The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed, live or dead; births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick.[nb 1] Each generation is a pure function of the preceding one. The rules continue to be applied repeatedly to create further generations. module("process")? public(process) ? persistent(problem) ? global(time <- 0)? global(time_limit <- 100) ? global(square_size <- 40) ? global(unit_length <- 10) ? global(bar_height <- 20) ? global(world_size) ? global(g_world)? global(g_prior_world)? % global(g_next_world)? import("xtools")? import("xtools_utils")? :: A:occasion(x => int, y => int, color => int). process :- def_font(title_font, new_font(cond(life_demo#using_demo_fonts, "-bitstream-bitstream charter-bold-i-normal--0-0-0-0-p-0-adobe-standard", "-bitstream-bitstream charter-bold-i-normal--0-0-0-0-p-0-adobe-standard"))), world_size <- 20, time <- 0, init_world, xvisAlloccasions. %%%% Create an X-window for the output %%%% tx_box := text_box(font_id => title_font). tx_field_button := text_field_button(font_id => title_font). ps_button := push_button(font_id => title_font). position_in_world(X:int, Y:int) -> ( X + (Y - 1) * world_size ). assign_world(X:int, Y:int, Val:int) :- cond(in_grid(X, Y), ( % write("assign X = ", X), % write("assign Y = ", Y), Pos = position_in_world(X, Y), % write("assign Pos = ", Pos), % write("assign Val = ", Val),nl, % write("assign position = ", Pos),nl, g_world.Pos <- Val ), write("assign out of bounds X = ", X, " Y = ", Y) ). assign_prior_world(X:int, Y:int, Val:int) :- cond(in_grid(X, Y), ( % write("assign X = ", X), % write("assign Y = ", Y), Pos = position_in_world(X, Y), % write("assign Pos = ", Pos), % write("assign Val = ", Val),nl, % write("assign position = ", Pos),nl, g_prior_world.Pos <- Val ), write("assign out of bounds X = ", X, " Y = ", Y) ). get_value(X:int, Y:int) -> cond(X < 1, random(2), cond(X > world_size, random(2), cond(Y < 1, random(2), cond(Y > world_size, random(2), g_world.position_in_world(X, Y) ) ) ) ). get_prior_value(X:int, Y:int) -> cond(X < 1, random(2), cond(X > world_size, random(2), cond(Y < 1, random(2), cond(Y > world_size, random(2), g_prior_world.position_in_world(X, Y) ) ) ) ). in_grid(X, Y) :- X > 0, Y > 0, X =< world_size, Y =< world_size. prehends_for_X(X, Y) :- % write("prehend_for_X X = ", X, " Y = ", Y), nl, cond(X =< world_size, ( cond(Y =< world_size, ( prehends_for_Y(X, Y + 1) % write("prehend_for_X AAA X = ", X, " Y + 1 = ", Y + 1) ) ), prehends_for_X(X + 1, 0) % write("prehend_for_X BBB X = ", X, " Y = ", Y, " Done") ), write("Done") ). prehends_for_Y(X, Y) :- % write("prehend_for_Y X = ", X, " Y = ", Y), nl, prehends_for_X_Y(X - 1, Y - 1, 0, Count_1), prehends_for_X_Y(X - 1, Y, Count_1, Count_2), prehends_for_X_Y(X - 1, Y + 1, Count_2, Count_3), prehends_for_X_Y(X , Y - 1, Count_3, Count_4), prehends_for_X_Y(X , Y + 1, Count_4, Count_5), prehends_for_X_Y(X + 1, Y - 1, Count_5, Count_6), prehends_for_X_Y(X + 1, Y, Count_6, Count_7), prehends_for_X_Y(X + 1, Y + 1, Count_7, Count_8), determine_fate(X, Y, Count_8), cond(Y < world_size, ( prehends_for_Y(X, Y + 1) ) % write("prehend_for_Y Done") ). prehends_for_X_Y(X, Y, Count_In, Count_Out) :- Count_Out = Count_In + get_prior_value(X, Y). % write("Prior = ", prior_world),nl, % write("X_Y X = ", X, " Y = ", Y, "In = ", Count_In, " Out = ", Count_Out), % nl. % Any live cell with two or three live neighbours survives. % Any dead cell with three live neighbours becomes a live cell. % All other live cells die in the next generation. Similarly, all other dead cells stay dead. determine_fate(X, Y, Count) :- % prehends_for_Y(X, Y, Val), Val = get_prior_value(X, Y), % write("Fate X = ", X, " Y = ", Y, " Count = ", Count, " Val = ",Val), nl, cond((Val =:= 1), % was alive cond((Count =:= 2) or (Count =:= 3), lives(X, Y), % survives dies(X, Y)), % dies cond(Count =:= 3, % was dead lives(X, Y), % becomes alive dies(X, Y)) % stays dead ). dies(X, Y) :- assign_world(X, Y, 0). % dies % write("dies X = ", X, " Y = ", Y). lives(X, Y) :- assign_world(X, Y, 1). % lives % write("lives X = ", X, " Y = ", Y). init_world :- write("init0001 world_size = ", world_size), nl, init_X(1), write("init0002"), nl. init_X(X) :- init_Y(X, 1), cond(X < world_size, init_X(X + 1) ). init_Y(X, Y) :- write("init Y X = ", X, " world_size = ", world_size), nl, write("init Y Y = ", Y, " world_size = ", world_size), nl, assign_prior_world(X, Y, random(2)), assign_world(X, Y, random(2)), cond(Y < world_size, init_Y(X, Y + 1), ( write("init Y done"),nl ), write("init Y done 2"),nl ). creative_advance(Grid) :- write("create 0001"),nl, time <- time + 1, write("create 0002"),nl, g_prior_world <- g_world, write("Time = ", time, " Prior World = ", g_world ),nl, prehends_for_X(1, 0), nl, write("Time = ", time, " World = ", g_world ),nl, write("create 0003"),nl, draw_X(Grid.window, 1), xRefreshWindow(Grid.window). % draw_grid(Grid) % ; % succeed. % was xvisAllTasks(L) in x_schedule xvisAlloccasions :- write("djd00001"),nl, Done = get_choice, write("djd00002"),nl, % mark(g_world), write("djd00003"),nl, % creative_advance(time, world_size ), write("djd00004"),nl, World_panel = panel(50, 50, border => 20, title => "World of Life"), write("djd00005"),nl, Grid = grid_c(width => S:(world_size * square_size), height => S, % action => show_me(Grid), border => 0, color_id => d_field), write("djd00006"),nl, % create_occasions(World, Grid.window), % create_fields(World, Grid.window), Push = ps_button(text => "Quit", action => (set_choice(Done), fail)), write("djd00007"),nl, Advance = ps_button(text => "Advance", action => creative_advance(Grid)), Block = hb_list [ %vl_list Events, vl_list [Grid]], % vl_list Durations], write("djd00008"),nl, Buttons = hc_list [Advance, h_box(50), Push], A = vc_list [% TitleBar, v_box(10), Block, v_box(30), Buttons], write("djd00009"),nl, World_panel contains A, write("djd00010"),nl, create_box(World_panel), draw_grid(Grid) ; succeed. draw_grid(Grid) :- GW = Grid.window, Cid = Grid.color_id, draw_s_lines(GW, shade_colors.Cid, 0), draw_h_lines(GW, highlight_colors.Cid, 0), draw_X(GW, 1). % cond(Val =:= 0, % fill_square(GW, X, Y, red), % fill_square(GW, X, Y, blue)), % fill_diagonal(GW, main_colors.blocked, 0). draw_X(GW, X) :- draw_Y(GW, X, 1), cond(X < world_size, draw_X(GW, X + 1) ). draw_Y(GW, X, Y) :- Val = get_value(X, Y), cond(Val =:= 0, (fill_square(GW, X, Y, red), write("draw Y X = ", X, " Y = ", Y, "red"), nl) , (fill_square(GW, X, Y, blue), write("draw Y X = ", X, " Y = ", Y, "blue"), nl) ), cond(Y < world_size, draw_Y(GW, X, Y + 1), ( write("draw Y done"),nl ), write("draw Y done 2"),nl ). draw_s_lines(3 => world_size) :- !. draw_s_lines(GW, C, N) :- xDrawLine(GW, 0, Y:(N*square_size), Max:(world_size*square_size - 1), Y, color => C, line_width => 1), xDrawLine(GW, Y, 0, Y, Max, color => C, line_width => 1), draw_s_lines(GW, C, N+1). draw_h_lines(3 => world_size) :- !. draw_h_lines(GW, C, N) :- xDrawLine(GW, 0, Y:((N+1)*square_size-1), Max:(world_size*square_size - 1), Y, color => C, line_width => 1), xDrawLine(GW, Y, 0, Y, Max, color => C, line_width => 1), draw_h_lines(GW, C, N+1). fill_square(GW, X, Y, C) :- xFillRectangle(GW, (X-1) * square_size + 1, (Y-1) * square_size + 1, square_size - 2, square_size - 2, color => C). show_me(Grid) :- xRefreshWindow(Grid.window). nth(1, [H|@]) -> H. nth(N, [@|T]) -> nth(N-1, T).