forked from Marfung37/GluingFumens
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunglueFumen.js
127 lines (112 loc) · 3.64 KB
/
unglueFumen.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
const { decoder, encoder} = require('tetris-fumen');
pieces = {
T: [
[[0, 0], [0, -1], [0, 1], [1, 0]],
[[0, 0], [0, 1], [1, 0], [-1, 0]],
[[0, 0], [0, -1], [0, 1], [-1, 0]],
[[0, 0], [0, -1], [1, 0], [-1, 0]]
],
I: [
[[0, 0], [0, -1], [0, 1], [0, 2]],
[[0, 0], [1, 0], [-1, 0], [-2, 0]],
[[0, 0], [0, -1], [0, -2], [0, 1]],
[[0, 0], [1, 0], [2, 0], [-1, 0]]
],
L: [
[[0, 0], [0, -1], [0, 1], [1, 1]],
[[0, 0], [1, 0], [-1, 0], [-1, 1]],
[[0, 0], [0, -1], [0, 1], [-1, -1]],
[[0, 0], [1, -1], [1, 0], [-1, 0]]
],
J: [
[[0, 0], [0, -1], [0, 1], [1, -1]],
[[0, 0], [1, 0], [-1, 0], [1, 1]],
[[0, 0], [0, -1], [0, 1], [-1, 1]],
[[0, 0], [-1, -1], [1, 0], [-1, 0]]
],
S: [
[[0, 0], [0, -1], [1, 0], [1, 1]],
[[0, 0], [1, 0], [0, 1], [-1, 1]],
[[0, 0], [0, 1], [-1, 0], [-1, -1]],
[[0, 0], [-1, 0], [0, -1], [1, -1]]
],
Z: [
[[0, 0], [0, 1], [1, 0], [1, -1]],
[[0, 0], [-1, 0], [0, 1], [1, 1]],
[[0, 0], [0, -1], [-1, 0], [-1, 1]],
[[0, 0], [1, 0], [0, -1], [-1, -1]]
],
O: [
[[0, 0], [1, 0], [0, 1], [1, 1]],
[[0, 0], [0, 1], [-1, 0], [-1, 1]],
[[0, 0], [-1, 0], [0, -1], [-1, -1]],
[[0, 0], [0, -1], [1, -1], [1, 0]]
]
}
rotationMapping = {
"spawn": 0,
"right": 1,
"reverse": 2,
"left": 3
}
colorMapping = {
"S": 7,
"J": 6,
"T": 5,
"Z": 4,
"O": 3,
"L": 2,
"I": 1
}
function clearedOffset(rowsCleared, yIndex) {
for (let row of rowsCleared) {
if (yIndex >= row) yIndex++;
}
return yIndex;
}
var fumenCodes = []
for(let rawInput of process.argv.slice(2)){
fumenCodes.push(...rawInput.split(" "));
}
for (let code of fumenCodes) {
let inputPages = decoder.decode(code);
board = inputPages[0]["_field"]["field"]["pieces"];
rowsCleared = [];
for (let pageNum = 0; pageNum < inputPages.length; pageNum++) {
op = inputPages[pageNum]["operation"];
piece = pieces[op["type"]][rotationMapping[op["rotation"]]];
for (let mino of piece) {
yIndex = op.y + mino[0];
yIndex = clearedOffset(rowsCleared, yIndex);
xIndex = op.x + mino[1];
index = yIndex * 10 + xIndex;
if (board[index] != 0) { console.log("error"); } // some intersect with the board
board[index] = colorMapping[op["type"]];
}
temp = [];
for (let y = -2; y < 3; y++) { // rows in which the piece might have been
yIndex = op.y + y;
yIndex = clearedOffset(rowsCleared, yIndex);
if (yIndex >= 0) { // sanity check
rowCleared = true;
for (let x = 0; x < 10; x++) {
index = yIndex * 10 + x;
rowCleared = rowCleared && (board[index] != 0);
}
if (rowCleared) {
temp.push(yIndex);
}
}
}
for (let row of temp) {
if (!rowsCleared.includes(row)) {
rowsCleared.push(row);
rowsCleared.sort(function(a,b){return a-b});
}
}
}
let outputPages = [inputPages[0]]; // lazily generating output fumen by destructively modifying the input
outputPages[0]["operation"] = undefined;
outputPages[0]["_field"]["field"]["pieces"] = board;
console.log(encoder.encode(outputPages));
}