-
Notifications
You must be signed in to change notification settings - Fork 2
/
rearrangeLetters.js
96 lines (76 loc) · 1.94 KB
/
rearrangeLetters.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
var cloneArray = require('./cloneArray');
var ArraysDeepEqual = function(arr1, arr2)
{
return JSON.stringify(arr1) === JSON.stringify(arr2);
};
var traverseBoard = function(board, callback)
{
for (var i = board.length - 1; i >= 0; i--) {
var row =board[i];
for (var j = row.length - 1; j >= 0; j--) {
var square =row[j];
callback(j,i,square, board);
}
}
};
var deleteIfRemoved = function(board, toBeRemoved)
{
for (var i = board.length - 1; i >= 0; i--) {
var row =board[i];
for (var j = row.length - 1; j >= 0; j--) {
if(toBeRemoved[i][j])
{
board[i][j] = '*';
}
}
}
};
var moveIfSpace = function(board)
{
var oldBoard;
do{
//keep pushing elements down
oldBoard = cloneArray(board);
for (var i = 0; i < board.length - 1; i++) {
var row = board[i];
for (var j = 0; j < row.length; j++) {
var square = board[i][j];
var below = board[i+1][j];
if(square !== '*' && below === '*'){
board[i+1][j] = square;
board[i][j] = '*';
}
}
}
}while(!ArraysDeepEqual(oldBoard, board));
do{
//keep pushing elements right
oldBoard = cloneArray(board);
for (var i = 0; i < board.length; i++) {
var row = board[i];
for (var j = 0; j < row.length -1; j++) {
var square = board[i][j];
var right = board[i][j+1];
if(square !== '*' && right === '*'){
board[i][j+1] = square;
board[i][j] = '*';
}
}
}
}while(!ArraysDeepEqual(oldBoard, board));
};
var getOneBelow = function(board, coord)
{
return board;
};
module.exports = function(board, toBeRemoved)
{
// for each board square
// check if board has any to be removed on or below it
//if it does, replace the letter with the letter above it
//
deleteIfRemoved(board, toBeRemoved);
moveIfSpace(board);
console.log('rearranged');
console.log(board);
};