-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
121 lines (114 loc) · 3.61 KB
/
utils.ts
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
import { CellType, State as CellState, PathEnd } from "./types";
export const generateStateWithPath = (
gridState: CellType[][],
pathEnds: PathEnd[],
dir: CellState
): CellType[][] => {
const newState = gridState.map((col, i) => {
let newCol: CellType[];
const pathedCell = {
state: dir,
pathed: true,
direction: dir
} as CellType;
/* If both are on the same column just replace that column */
//check of they lie on same column
if (pathEnds[0].x === pathEnds[1].x && i === pathEnds[1].x) {
//which one S or E is Above the other
const singleDir =
pathEnds[0].y < pathEnds[1].y ? CellState.B : CellState.T;
if (pathEnds[0].y < pathEnds[1].y) {
//S is above E
newCol = col.map((cell, j) => {
if (j < pathEnds[0].y || j > pathEnds[1].y) return cell;
let singleState;
singleState = j === pathEnds[0].y ? CellState.S : singleDir;
singleState = j === pathEnds[1].y ? CellState.E : singleState;
return {
state: singleState,
pathed: true,
dir: singleDir
};
});
} else {
//E is above S
newCol = col.map((cell, j) => {
if (j > pathEnds[0].y || j < pathEnds[1].y) return cell;
let singleState;
singleState = j === pathEnds[0].y ? CellState.S : singleDir;
singleState = j === pathEnds[1].y ? CellState.E : singleState;
return {
state: singleState,
pathed: true,
dir: singleDir
};
});
}
return newCol;
}
/**If both are on diff column */
const currReverseDir = dir === CellState.T ? CellState.B : CellState.T;
const endDirection =
(pathEnds[0].x - pathEnds[1].x) % 2 === 0 ? dir : currReverseDir;
if (i === pathEnds[0].x) {
newCol = col.map((cell, j) => {
if (i == pathEnds[0].x && j === pathEnds[0].y)
return {
state: CellState.S,
pathed: true,
direction: dir
};
if (dir === CellState.T && j < pathEnds[0].y) return pathedCell;
if (dir === CellState.B && j > pathEnds[0].y) return pathedCell;
else return cell;
});
return newCol;
} else if (pathEnds[1].x === i) {
newCol = col.map((cell, j) => {
if (i == pathEnds[1].x && j === pathEnds[1].y)
return {
state: CellState.E,
pathed: true,
direction: dir
};
if (endDirection === CellState.T && j > pathEnds[1].y)
return {
state: endDirection,
pathed: true,
direction: endDirection
} as CellType;
if (endDirection === CellState.B && j < pathEnds[1].y)
return {
state: endDirection,
pathed: true,
direction: endDirection
} as CellType;
else return cell;
});
return newCol;
}
if (i > pathEnds[0].x && i < pathEnds[1].x) {
newCol = col.map((cell, j) => {
const tempDir = (i - pathEnds[0].x) % 2 === 0 ? dir : currReverseDir;
return {
state: tempDir,
pathed: true,
direction: tempDir
} as CellType;
});
return newCol;
}
if (i < pathEnds[0].x && i > pathEnds[1].x) {
newCol = col.map((cell, j) => {
const tempDir = (i - pathEnds[1].x) % 2 === 0 ? dir : currReverseDir;
return {
state: tempDir,
pathed: true,
direction: tempDir
} as CellType;
});
return newCol;
} else return col;
});
return newState;
};