-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeTileDraftFunction.js
85 lines (76 loc) · 2.74 KB
/
mergeTileDraftFunction.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
function Tile(isActive = false, currentValue = 0, alreadyMerged = false) {
this.isActive = isActive;
this.currentValue = currentValue;
this.alreadyMerged = alreadyMerged;
}
var tile1 = new Tile(true, 2);
var tile2 = new Tile();
var tile3 = new Tile();
var tile4 = new Tile();
var column1 = [tile1, tile2, tile3, tile4];
function newBoardMove(board, direction) {
var newBoardState = [];
for (let i = 0; i < this.boardSize; i++) {
var stackToBeMerged = [];
if (direction === "down") {
stackToBeMerged = getTileStack(board, "VERTICAL", i);
newBoardState.push(mergeTileStack(stackToBeMerged));
} else if (direction === "up") {
stackToBeMerged = getTileStack(board, "VERTICAL", i).reverse();
newBoardState.push(mergeTileStack(stackToBeMerged).reverse());
} else if (direction === "right") {
stackToBeMerged = getTileStack(board, "HORIZONTAL", i);
newBoardState.push(mergeTileStack(stackToBeMerged));
} else if (direction === "left") {
stackToBeMerged = getTileStack(board, "HORIZONTAL", i).reverse();
newBoardState.push(mergeTileStack(stackToBeMerged).reverse());
}
}
this.updateBoard(newBoardState);
}
function mergeTileStack(column) {
var newColumn = column;
// Loop through tiles starting at the end tile
for (var i = newColumn.length - 1; i > -1; i--) {
var currentTile = newColumn[i];
var nextTile = newColumn[i - 1];
if (!nextTile) {
newColumn.forEach(tile => {
tile.alreadyMerged = false;
});
break;
}
// If the current tile is empty, and the next tile is active,
// move next tile to current space
if (!currentTile.isActive) {
if (nextTile.isActive) {
Object.assign(newColumn[i], newColumn[i - 1]);
newColumn[i - 1] = new Tile();
newColumn = this.mergeTileStack(newColumn);
}
// Else if current tile is active and hasn't already been merged,
// and next tile is active, hasn't already been merged and equal in value,
// merge next tile with current tile and remove next tile from board
} else {
if (
currentTile.alreadyMerged === false &&
(nextTile.isActive === true &&
nextTile.alreadyMerged === false &&
nextTile.currentValue === currentTile.currentValue)
) {
newColumn[i].currentValue = newColumn[i].currentValue * 2;
newColumn[i].alreadyMerged = true;
newColumn[i - 1] = new Tile();
newColumn = this.mergeTileStack(newColumn);
}
}
}
return newColumn;
}
function getTileStack(board, direction, index) {
if (direction === "VERTICAL") {
return this.$store.getters.getBoardColumn(index);
} else if (direction === "HORIZONTAL") {
return this.$store.getters.getBoardRow(index);
}
}