1- import { directions } from '../Directions' ;
2- import { _FindCellVisitState , _FindCellTypeState , _FindCell , _BoardReset , _GetNeighbors } from '../Tools' ;
3- import exit from 'exit' ;
1+ import { directions } from '../Directions' ;
2+ import { _FindCellVisitState , _FindCellTypeState , _FindCell , _BoardReset , _GetNeighbors , _SetWalls } from '../Tools' ;
3+ import exit from 'exit' ;
44import { PATH_TYPE , DEFAULT_TYPE , FLOOR_TYPE , WALL_TYPE , START_TYPE , GOAL_TYPE } from '../../../../Utility/Colors' ;
55
66
77
88
9- export function BinaryTreeCreation ( board ) {
9+ export function BinaryTreeCreation ( board , start , end ) {
1010 _BoardReset ( board ) ;
11+ let startCell = null ;
12+ let endCell = null ;
13+ if ( start === undefined || start === null ) {
14+ startCell = board [ Math . floor ( Math . random ( ) * board . length ) ] ;
15+ }
16+ if ( end === undefined || end === null ) {
17+ endCell = board [ Math . floor ( Math . random ( ) * board . length ) ] ;
18+ }
19+ startCell . type = START_TYPE ;
20+ endCell . type = GOAL_TYPE ;
21+ console . log ( 'Start: ' + startCell . id + " (" + startCell . x + ' | ' + startCell . y + ')' ) ;
22+ console . log ( 'End: ' + endCell . id + " (" + endCell . x + ' | ' + endCell . y + ')' ) ;
23+
1124 const numPoints = board . length ;
12- const numCols = Math . ceil ( Math . sqrt ( numPoints ) ) ;
13- const prev = null ;
1425 for ( var i = 0 ; i < numPoints ; i ++ ) {
26+ if ( board [ i ] === startCell || board [ i ] === endCell ) {
27+ continue ;
28+ }
1529 let node = board [ i ] ;
1630 // Needed to be percise to avoid floating point errors....
1731 let direction = Math . floor ( Math . random ( ) * 4 ) ;
18- // console.log(node);
19- // console.log(direction);
20- // console.log(directions[direction].x);
21- // console.log(board[i].x + directions[direction].x);
2232 let xcord = node . x + directions [ direction ] . x ;
2333 let ycord = node . y + directions [ direction ] . y ;
24- // console.log(xcord, ycord);
25- // console.log(node.x + directions[direction].x, node.y + directions[direction].y);
2634 const cell = _FindCell ( board , xcord , ycord ) ;
2735 // console.log(cell);
2836 // exit();
@@ -39,21 +47,16 @@ export function BinaryTreeCreation(board) {
3947 // return;
4048
4149
42- if ( ! cell . visited ) {
50+ if ( ! cell . visited && cell . type !== START_TYPE && cell . type !== GOAL_TYPE ) {
4351 // console.log(cell);
4452 cell . visited = true ;
45- cell . type = '_floor_' ;
53+ cell . type = FLOOR_TYPE ;
4654 }
4755 }
4856 }
4957
50- for ( var x = 0 ; x < numPoints ; x ++ ) {
51- let node = board [ x ] ;
58+ _SetWalls ( board ) ;
5259
53- if ( ! node . visited ) {
54- node . type = '_wall_' ;
55- }
56- }
5760
5861}
5962
0 commit comments