File tree Expand file tree Collapse file tree 1 file changed +25
-20
lines changed Expand file tree Collapse file tree 1 file changed +25
-20
lines changed Original file line number Diff line number Diff line change @@ -7,10 +7,27 @@ class Track {
7
7
this . setLayout ( track )
8
8
}
9
9
10
- setLayout ( track ) {
11
- this . layout = track . split ( '\n' )
12
- this . layout = this . layout . map ( ( e ) => { return e . split ( '' ) } )
13
- this . extractCarts ( )
10
+ /**
11
+ * Displays the current state of the track with the carts placed
12
+ */
13
+ display ( ) {
14
+ let output = ''
15
+ const layout = JSON . parse ( JSON . stringify ( this . layout ) ) // Deep copy
16
+ // Include the carts
17
+ this . carts . forEach ( ( cart ) => {
18
+ // If another cart is at the spot, draw a collision instead
19
+ if ( this . cartDirections . indexOf ( layout [ cart . y ] [ cart . x ] ) >= 0 ) {
20
+ layout [ cart . y ] [ cart . x ] = 'X'
21
+ } else {
22
+ layout [ cart . y ] [ cart . x ] = cart . direction
23
+ }
24
+ } )
25
+ layout . forEach ( ( y ) => {
26
+ output += y . join ( '' )
27
+ output += '\n'
28
+ } )
29
+
30
+ return output . trim ( )
14
31
}
15
32
16
33
/**
@@ -44,22 +61,10 @@ class Track {
44
61
return this . layout [ y ] [ x ]
45
62
}
46
63
47
- /**
48
- * Displays the current state of the track
49
- */
50
- display ( ) {
51
- let output = ''
52
- const layout = JSON . parse ( JSON . stringify ( this . layout ) ) // Deep copy
53
- // Include the carts
54
- this . carts . forEach ( ( cart ) => {
55
- layout [ cart . y ] [ cart . x ] = cart . direction
56
- } )
57
- layout . forEach ( ( y ) => {
58
- output += y . join ( '' )
59
- output += '\n'
60
- } )
61
-
62
- return output . trim ( )
64
+ setLayout ( track ) {
65
+ this . layout = track . split ( '\n' )
66
+ this . layout = this . layout . map ( ( e ) => { return e . split ( '' ) } )
67
+ this . extractCarts ( )
63
68
}
64
69
}
65
70
You can’t perform that action at this time.
0 commit comments