-
Notifications
You must be signed in to change notification settings - Fork 4
/
zcl_cg_conways_game_of_life.clas.abap
255 lines (170 loc) · 6.44 KB
/
zcl_cg_conways_game_of_life.clas.abap
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
CLASS zcl_cg_conways_game_of_life DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
TYPES:
BEGIN OF ty_coordinate,
col TYPE i,
row TYPE i,
END OF ty_coordinate.
TYPES:
BEGIN OF ty_cell.
INCLUDE TYPE ty_coordinate.
TYPES: alive TYPE abap_bool,
END OF ty_cell.
TYPES:
tty_coordinates TYPE HASHED TABLE OF ty_coordinate
WITH UNIQUE KEY col row.
TYPES:
tty_cell TYPE HASHED TABLE OF ty_cell
WITH UNIQUE KEY col row
WITH NON-UNIQUE SORTED KEY key_alive
COMPONENTS alive.
DATA: m_size TYPE i READ-ONLY.
METHODS:
constructor
IMPORTING
!size TYPE i,
fill_randomly
IMPORTING
!i_seed TYPE i OPTIONAL
RETURNING
VALUE(r_board) TYPE REF TO zcl_cg_conways_game_of_life,
turn,
get_cell
IMPORTING
!i_col TYPE i
!i_row TYPE i
RETURNING
VALUE(r_alive) TYPE abap_bool,
multiple_turns
IMPORTING
!i_count TYPE i,
get_turns
RETURNING
VALUE(r_turn_count) TYPE i,
get_alive_cells_count
RETURNING
VALUE(r_cells_alive) TYPE i .
PRIVATE SECTION.
TYPES: BEGIN OF ty_neighbour.
INCLUDE TYPE ty_coordinate.
TYPES: neighbours_coordinates TYPE tty_coordinates,
END OF ty_neighbour,
tty_neighbour TYPE HASHED TABLE OF ty_neighbour
WITH UNIQUE KEY col row.
DATA:
cells TYPE tty_cell,
neighbours_buffer TYPE tty_neighbour,
turn_count TYPE i.
METHODS:
activate_cell
IMPORTING
i_col TYPE i
i_row TYPE i,
get_alive_neighbours_of_cell
IMPORTING
i_col TYPE i
i_row TYPE i
RETURNING
VALUE(r_alive_neighbours) TYPE i,
get_neighbours_of_cell
IMPORTING
i_col TYPE i
i_row TYPE i
RETURNING
VALUE(r_neighbours) TYPE tty_cell,
_execute_rule
IMPORTING
i_cell_alive TYPE abap_bool
i_alive_neighbours TYPE i
RETURNING
VALUE(r_cell_alive) TYPE abap_bool.
ENDCLASS.
CLASS zcl_cg_conways_game_of_life IMPLEMENTATION.
METHOD activate_cell.
cells[ col = i_col
row = i_row ]-alive = abap_true.
ENDMETHOD.
METHOD constructor.
m_size = size.
cells = VALUE tty_cell( FOR col = 1 WHILE col <= m_size
FOR row = 1 WHILE row <= m_size
( col = col
row = row ) ).
ENDMETHOD.
METHOD fill_randomly.
DATA(rnd) = cl_abap_random_int=>create( seed = COND #( WHEN i_seed IS SUPPLIED THEN i_seed
ELSE CONV #( sy-uzeit ) )
min = 0
max = 1 ).
LOOP AT cells ASSIGNING FIELD-SYMBOL(<cell>).
<cell>-alive = boolc( rnd->get_next( ) = 1 ).
ENDLOOP.
r_board = me.
ENDMETHOD.
METHOD get_alive_cells_count.
r_cells_alive = lines( FILTER #( cells USING KEY key_alive
WHERE alive = abap_true ) ).
ENDMETHOD.
METHOD get_alive_neighbours_of_cell.
r_alive_neighbours = lines( FILTER #( get_neighbours_of_cell( i_col = i_col
i_row = i_row )
USING KEY key_alive
WHERE alive = abap_true ) ).
ENDMETHOD.
METHOD get_cell.
r_alive = cells[ col = i_col
row = i_row ]-alive.
ENDMETHOD.
METHOD get_neighbours_of_cell.
DATA: neighbours_coordinates TYPE zcl_cg_conways_game_of_life=>ty_neighbour-neighbours_coordinates.
ASSIGN neighbours_buffer[ col = i_col
row = i_row ] TO FIELD-SYMBOL(<neighbour>).
IF sy-subrc = 0.
neighbours_coordinates = <neighbour>-neighbours_coordinates.
ELSE.
neighbours_coordinates = VALUE #( FOR cell IN cells
WHERE ( ( row = i_row + 1 AND col = i_col + 1 )
OR ( row = i_row AND col = i_col + 1 )
OR ( row = i_row + 1 AND col = i_col )
OR ( row = i_row - 1 AND col = i_col - 1 )
OR ( row = i_row AND col = i_col - 1 )
OR ( row = i_row - 1 AND col = i_col )
OR ( row = i_row + 1 AND col = i_col - 1 )
OR ( row = i_row - 1 AND col = i_col + 1 ) )
( col = cell-col row = cell-row ) ).
INSERT VALUE #( col = i_col
row = i_row
neighbours_coordinates = neighbours_coordinates )
INTO TABLE neighbours_buffer.
ENDIF.
r_neighbours = VALUE #( FOR neighbour IN neighbours_coordinates
( cells[ row = neighbour-row
col = neighbour-col ] ) ).
ENDMETHOD.
METHOD get_turns.
r_turn_count = turn_count.
ENDMETHOD.
METHOD multiple_turns.
DO i_count TIMES.
turn( ).
ENDDO.
ENDMETHOD.
METHOD turn.
turn_count = turn_count + 1.
DATA(new_cells) = VALUE tty_cell( FOR cell IN cells
LET alive_neighbours = get_alive_neighbours_of_cell( i_col = cell-col
i_row = cell-row )
IN ( col = cell-col
row = cell-row
alive = _execute_rule( i_cell_alive = cell-alive
i_alive_neighbours = alive_neighbours ) ) ).
cells = new_cells.
ENDMETHOD.
METHOD _execute_rule.
r_cell_alive = COND #( WHEN i_alive_neighbours = 3 THEN abap_true
WHEN i_alive_neighbours = 2 AND i_cell_alive = abap_true THEN abap_true ).
ENDMETHOD.
ENDCLASS.