1
1
let resolution = 10 // px per case of the field
2
2
3
- let default_cell_state = DOWN ;
3
+ let interval = undefined ;
4
4
5
5
let size2cell = ( size , resolution ) => Math . floor ( size / resolution ) ;
6
6
7
- const table_size = {
8
- width : size2cell ( window_size . innerWidth , resolution ) ,
9
- height : size2cell ( window_size . innerHeight , resolution )
10
- }
11
-
12
- let field_table = createFieldTable ( resolution , { width : table_size . width , height : table_size . height } ) ;
13
-
14
- const initial_pos = {
15
- x : Math . floor ( field_table [ 0 ] . length / 2 ) ,
16
- y : Math . floor ( field_table . length / 2 )
7
+ let table_size = {
8
+ width : size2cell ( window . innerWidth , resolution ) ,
9
+ height : size2cell ( window . innerHeight , resolution )
17
10
}
18
11
19
- let field_element = document . getElementById ( 'field' ) ;
20
-
21
- let interval = undefined ;
12
+ let initial_pos = {
13
+ x : Math . floor ( table_size . width / 2 ) ,
14
+ y : Math . floor ( table_size . height / 2 )
15
+ } ;
22
16
23
- field_element = createHtmlTable ( field_element , field_table ) ;
17
+ let field = document . getElementById ( 'field' ) ;
24
18
25
- let myAnt = new Ant ( { x : initial_pos . x , y : initial_pos . y } , field_element ) ;
19
+ createTable ( field , {
20
+ n_col : table_size . width ,
21
+ n_row : table_size . height
22
+ } ) ;
26
23
27
- drawAnt ( { x : myAnt . pos . x , y : myAnt . pos . y } , field_element ) ;
28
-
29
- function createFieldTable ( resolution , { n_col : width , n_row : height } ) {
30
- let table = [ ]
24
+ function createTable ( parent , { n_col, n_row } ) {
25
+ let table = document . createElement ( 'table' ) ;
31
26
for ( let j = 0 ; j < n_row ; j ++ ) {
32
- let row = [ ] ;
27
+ let row = document . createElement ( 'tr' ) ;
33
28
for ( let i = 0 ; i < n_col ; i ++ ) {
34
- let cell = default_cell_state ;
35
- row . push ( cell ) ;
29
+ let cell = document . createElement ( 'td' ) ;
30
+ row . appendChild ( cell ) ;
36
31
}
37
- table . push ( row ) ;
32
+ table . appendChild ( row ) ;
38
33
}
39
- return table ;
34
+ parent . appendChild ( table ) ;
40
35
}
41
36
42
- function createHtmlTable ( field_element , field_table ) {
43
- for ( let i = 0 ; i < field_table . length ; i ++ ) {
44
- let row_element = document . createElement ( 'tr' ) ;
45
- for ( let j = 0 ; j < field_table [ i ] . length ; j ++ ) {
46
- let cell_element = document . createElement ( 'td' ) ;
47
- cell_element . classList . add ( 'cell' ) ;
48
- row_element . appendChild ( cell_element ) ;
37
+ let table = field . firstChild ;
38
+
39
+ function forEachCell ( table , action ) {
40
+ let rows = table . childNodes ;
41
+ for ( row of rows ) {
42
+ let cells = row . childNodes ;
43
+ for ( cell of cells ) {
44
+ action ( cell ) ;
49
45
}
50
- field_element . appendChild ( row_element ) ;
51
46
}
52
47
}
53
48
54
- function UpdateHtmlTable ( field_element , table_field ) {
55
- let j = 0 ;
56
- const max_j = field_table . length ;
57
- const max_i = field_table [ 0 ] . length ;
58
- field_element . childNodes . forEach ( tr => {
59
- let i = 0 ;
60
- tr . childNodes . forEach ( td => {
61
- if ( i < max_i && j < max_j ) {
62
- // console.log(`cell at (${i}, ${j})`);
63
- let cell = table_field [ j ] [ i ] ;
64
- if ( isUp ( cell ) )
65
- {
66
- if ( ! td . classList . contains ( 'up' ) ) {
67
- td . classList . add ( 'up' ) ;
68
- }
69
- } else {
70
- td . classList . remove ( 'up' ) ;
71
- }
72
- i ++ ;
73
- } else {
74
- return ;
75
- }
76
- } ) ;
77
- j ++ ;
78
- } ) ;
79
- }
49
+ forEachCell ( table , ( cell ) => cell . classList . add ( 'cell' ) ) ;
80
50
81
- function isUp ( cell ) {
82
- return cell == UP ;
83
- }
51
+ let myAnt = new Ant ( { x : initial_pos . x , y : initial_pos . y } , table ) ;
84
52
85
- function drawAnt ( { x, y} , field_element ) {
86
-
87
- let previous_ant = document . getElementById ( 'ant' ) ;
88
- if ( previous_ant != null ) {
89
- // clear previous ant
90
- previous_ant . removeAttribute ( 'id' ) ;
91
- }
92
- let next_ant_cell = field_element . childNodes [ y ] . childNodes [ x ] ;
93
- // draw new ant
94
- next_ant_cell . id = 'ant' ;
95
- }
53
+ myAnt . draw ( ) ;
96
54
97
- function nextState ( ) {
55
+ function next ( ) {
98
56
myAnt . update ( ) ;
99
- drawAnt ( myAnt . pos , field_element ) ;
100
- UpdateHtmlTable ( field_element , field_table ) ;
101
- if ( myAnt . isOut ( field_table [ 0 ] . length , field_table . length ) ) {
102
- window . clearInterval ( loop ) ;
103
- }
57
+ myAnt . draw ( ) ;
104
58
}
105
59
106
60
function ant_start ( ) {
107
- interval = setInterval ( nextState , 10 ) ;
61
+ let dt = 1 / document . getElementById ( 'speed' ) . value ;
62
+ interval = setInterval ( next , 100 ) ;
108
63
}
109
64
110
-
111
65
function ant_stop ( ) {
112
66
clearInterval ( interval ) ;
113
67
}
114
68
115
-
116
69
function ant_next ( ) {
117
- nextState ( ) ;
70
+ next ( ) ;
71
+ if ( myAnt . isOut ( table_size . width , table_size . height ) ) {
72
+ ant_stop ( ) ;
73
+ }
118
74
}
119
75
120
76
function ant_reset ( ) {
121
- field_table = createFieldTable ( resolution , size . x , size . y ) ;
122
- myAnt = new Ant ( initial_pos . x , initial_pos . y , field_table ) ;
123
- UpdateHtmlTable ( field_element , field_table ) ;
124
- drawAnt ( myAnt , field_element ) ;
125
- }
77
+ forEachCell ( table , ( cell ) => cell . classList . remove ( 'up' ) ) ;
78
+ myAnt . resetPos ( { x : initial_pos . x , y : initial_pos . y } ) ;
79
+ myAnt . draw ( ) ;
80
+ }
81
+
82
+
83
+ function setUpSpeed ( speed_value ) {
84
+ let dt = 1 / speed_value * 1000 ;
85
+ clearInterval ( interval ) ;
86
+ setInterval ( ant_next , dt ) ;
87
+ }
0 commit comments