-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
113 lines (91 loc) · 2.82 KB
/
cli.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
class QuberMatrix {
constructor(word) {
this.word = word;
this.len = word.length;
this.mtx = new Array(this.len * 2);
for (let i = 0; i < this.mtx.length; i++) {
this.mtx[i] = new Array(this.len * 2);
}
}
wordToRow(offset) {
for (let i = 0; i < this.len; i++) {
this.mtx[offset][i + offset] = this.word[i];
}
}
wordToRowBackwards(offset) {
for (let i = this.len; i > 0; i--) {
this.mtx[offset + this.len - 1][offset + i - 1] = this.word[this.len - i];
}
}
wordToColumn(offset) {
for (let i = 0; i < this.len; i++) {
this.mtx[i + offset][offset] = this.word[i];
}
}
wordToColumnBackwards(offset) {
for (let i = this.len; i > 0; i--) {
this.mtx[i + offset - 1][offset + this.len - 1] = this.word[this.len - i];
}
}
writeHalfBoard(offset = 0) {
this.wordToRow(offset);
this.wordToColumn(offset);
}
writeHalfBoardBackwards(offset = 0) {
this.wordToRowBackwards(offset);
this.wordToColumnBackwards(offset);
}
writeFullBoard(offset = 0) {
this.writeHalfBoard(offset);
this.writeHalfBoardBackwards(offset);
}
drawMultiple(letter, x, y, times) {
for (let i = 0; i < times; i++) {
this.mtx[y++][x++] = letter;
}
}
materialize() {
let xLen = this.mtx.length;
let yLen = this.mtx[0].length;
let sb = [];
for (let x = 0; x < xLen; x++) {
for (let y = 0; y < yLen; y++) {
let value = this.mtx[x][y];
sb.push(value ? value : " ");
}
sb.push('\n');
}
return sb.join('').trim();
}
}
const err_msg = 'A palavra deve ter no mínimo três caracteres';
class Quber {
static to2dSimple(word) {
if (!word || word.length < 3)
return err_msg;
let mtx = new QuberMatrix(word);
mtx.writeHalfBoard();
return mtx.materialize();
}
static to2dFull(word) {
if (!word || word.length < 3)
return err_msg;
let mtx = new QuberMatrix(word);
mtx.writeFullBoard();
return mtx.materialize();
}
static to3d(word) {
if (!word || word.length < 3)
return err_msg;
let mtx = new QuberMatrix(word);
let rawOffset = Math.floor(mtx.len/2);
let offset = mtx.len > 7 ? rawOffset - 1 : rawOffset;
mtx.writeFullBoard();
mtx.writeFullBoard(offset);
mtx.drawMultiple('\\', 1, 1, offset - 1);
mtx.drawMultiple('\\', 1, mtx.len, offset - 1);
mtx.drawMultiple('\\', mtx.len, 1, offset - 1);
mtx.drawMultiple('\\', mtx.len, mtx.len, offset - 1);
return mtx.materialize();
}
}