-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·173 lines (162 loc) · 4.66 KB
/
index.js
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
"use strict";
import { program } from "commander";
import readline from "readline-sync";
import jf from "jsonfile";
import LetterTree from "./letter-tree.js";
import TileTextRenderer from "./tile-text-renderer.js";
import TreeBuilder from "./tree-builder.js";
let board;
let moveRecord;
let treeBuilder;
program
.arguments("<file>")
.option("-c, --create", "Create tree")
.option("-w, --write", "Write a message")
.action(function (file) {
let json;
json = jf.readFileSync(file);
board = new LetterTree(json);
treeBuilder = new TreeBuilder(board, json.treeBuilder);
moveRecord = json.moveRecord;
TileTextRenderer.print(board);
})
.parse(process.argv);
const nav = {
L: { row: 0, col: -1 },
R: { row: 0, col: 1 },
U: { row: -1, col: 0 },
D: { row: 1, col: 0 },
};
function navigationCommand(board, command) {
const response = command;
let currTile = board.tiles.tiles[board.currTile.row][board.currTile.col];
if (["L", "R", "D", "U"].includes(response)) {
const move = nav[response];
const R = board.currTile.row + move.row;
const C = board.currTile.col + move.col;
//
// only move on tree
if (
R < board.tiles.dim.rows &&
C < board.tiles.dim.cols &&
board.tiles.tiles[R][C].n
) {
board.currTile.row = R;
board.currTile.col = C;
} else if (
board.tiles.tiles[board.currTile.row][board.currTile.col].p === "A" &&
response === "D"
) {
// create a new branch here.
board.branchOut(
board.tiles.tiles[board.currTile.row][board.currTile.col]
);
} else {
console.log("out of bounds " + board.tiles.tiles[R][C].p);
}
} else if (response === ">") {
board.tiles.expandGrid("R");
} else if (response === "<") {
board.tiles.expandGrid("L");
} else if (response === ".") {
board.tiles.expandGrid("D");
} else if (response === "}") {
board.tiles.moveAllTiles("R");
} else if (response === "{") {
board.tiles.moveAllTiles("L");
} else if (response === "[") {
board.go("L");
} else if (response === "]") {
board.go("R");
} else if (response === "=") {
board.go("U");
} else if (response === response.toLowerCase() && currTile.p === "A") {
// add a character to the tile
currTile.l = response;
}
}
// if the node has a child that is a leaf, return it
function getChildLeafNodesOf(board, node) {
const children = board.tree.graph[node];
let childLeaves = [];
const leftNodesChildren = board.tree.graph[children[0]];
const rightNodesChildren = board.tree.graph[children[1]];
if (leftNodesChildren.length === 0) {
childLeaves.push(children[0]);
}
if (rightNodesChildren.length === 0) {
childLeaves.push(children[1]);
}
return childLeaves;
}
function encodeMessage(board, message) {
const letterTiles = board.tiles.asFlatArray((t) => t.p === "A" && t.l);
const letterMapArr = letterTiles.map((t) => [
t.l,
board.tree.encodingOf(t.n),
]);
const letterMap = new Map(letterMapArr);
return message.map((letter) => letterMap.get(letter).join("")).join(" ");
}
if (program.opts().write) {
let running = true;
let count = 1;
let message = [];
while (running) {
const response = readline.keyIn(
"MESSAGE: " +
message.join("") +
"\n#" +
count +
" currently at: row: " +
board.currTile.row +
", col: " +
board.currTile.col +
`(${board.getCurrTile().p})` +
" >>>>>>>>>>>> Type message ('Q' to exit)>>>>>>>>>>>>>>"
);
message.push(response);
let currTile;
// const command = moveRecord.shift();
if (response === "Q") {
running = false;
}
currTile = board.getCurrTile();
// now insert letter
// treeBuilder.nextBuildOut();
//board.setLetterInFirstEmptyLeaf(response);
treeBuilder.insertLetter(response);
console.log(encodeMessage(board, message));
TileTextRenderer.print(board);
}
}
if (program.opts().create) {
let running = true;
let count = 1;
let moveRecord = [];
while (running) {
// const response = readline.question('>>');
console.log(" ");
const response = readline.keyIn(
"#" +
count +
" currently at: row: " +
board.getCurrTile.row +
", col: " +
board.getCurrTile.col +
`(${board.getCurrTile().p})` +
" >>>>>>>>>>>> Type L R U D or Q >>>>>>>>>>>>>>>>>>"
);
count++;
if (response === "Q") {
let output = board.toJSON();
output.moveRecord = moveRecord;
console.log(JSON.stringify(output));
running = false;
} else {
navigationCommand(board, response);
moveRecord.push(response);
TileTextRenderer.print(board);
}
}
}