-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life.pl
97 lines (80 loc) · 2.8 KB
/
game_of_life.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%%return a state of cell in a current position
cell_current_state(World,Height,Width,State):-
between(0,9,Height),
between(0,9,Width),
nth0(Height, World, HeightCells), %%nth0(?Index, ?List, ?Elem)
nth0(Width,HeightCells,State). %%nth0(?Index, ?List, ?Elem)
%%cell that is not in a range is dead by default
cell_current_state(_,_,_,0).
%%find neighbours and count sum of their states
neighbours_counter(World,Height,Width,Amount):-
Up is Height + 1,
Down is Height - 1,
Left is Width - 1,
Right is Width + 1,
cell_current_state(World,Up,Width,State1),
cell_current_state(World,Down,Width,State2),
cell_current_state(World,Height,Left,State3),
cell_current_state(World,Height,Right,State4),
cell_current_state(World,Up,Right,State5),
cell_current_state(World,Up,Left,State6),
cell_current_state(World,Down,Left,State7),
cell_current_state(World,Down,Right,State8),
Amount is State1 + State2 + State3 + State4 + State5 + State6 + State7 + State8.
%%we need to separate cells on dead and alive
%%for alive cells
cell_new_state(World,Height,Width,1,NewState):-
neighbours_counter(World,Height,Width,Amount),
((Amount =:= 2 ; Amount =:= 3) -> NewState = 1 ; NewState = 0).
%%for dead cells
cell_new_state(World,Height,Width,0,NewState):-
neighbours_counter(World,Height,Width,Amount),
( Amount =:= 3 -> NewState = 1 ; NewState = 0).
%%calculator of states.
state_controller(World,Height,Width,NewState):-
cell_current_state(World,Height,Width,State),
cell_new_state(World,Height,Width,State,NewState).
%%----------------------------------------------
%%generate new world
evolved_world(_,10,[]).
evolved_world(World,Height,[NewHeight|NewWorld]):-
evolve_world_util(World,Height,0,NewHeight),
NHeight is Height + 1,
evolved_world(World,NHeight,NewWorld).
evolve_world_util(_,_,10,[]).
evolve_world_util(World,Height,Width,[State|NextState]):-
state_controller(World,Height,Width,State),
NewWidth is Width + 1,
evolve_world_util(World,Height,NewWidth, NextState).
/*-----------------------------------------------
print functions
-------------------------------------*/
%%when 2d array is printed we stop the recursion
world_Print([]):- nl.
%%every time we print the head of 2D array(the first array)
world_Print([H|T]):-
write(H),
nl,
world_Print(T).
%%------------------------------------------
%%main loop for our game
game_loop(World):-
world_Print(World),
evolved_world(World,0,NewWorld),
write('\33\[2J'),
sleep(1),
game_loop(NewWorld).
%% main
main:-
game_loop([
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,0,0,1,0,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,0,0,0,0],
[0,0,1,0,0,0,0,1,0,0],
[0,0,1,1,1,1,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]).