JSFiddle - React, Tailwind, and code Playground
by jrab227
JavaScript
var Board = {
"1": {
"1": "G",
"2": "G",
"3": "R"
},
"2": {
"1": "B",
"2": "R",
"3": "R"
},
"3": {
"1": "B",
"2": "B",
"3": "G"
},
}
var gravity = function(board, indexes) {
for (var ind = 0 ; ind < indexes.length; ind++) {
var col = indexes[ind]
var store = []
for (var j = 3; j > 0; j--){
if (board[j][col] != undefined) {
store = [board[j][col]].concat(store)
}
}
while (store.length < 3) {
store = [undefined].concat(store)
}
for (var k = 1; k < 4; k++){
updateBoard(board, [k, col], store[k -1])
}
}
return board
}
var combinations = function(index) {
return [
[index[0], index[1] - 1],
[index[0], index[1] + 1],
[index[0] - 1, index[1]],
[index[0] + 1, index[1]],
]
}
var pop = function(index, board) {
var columns = []
var newboard = board
var neighbors = combinations(index)
//Neighbors
for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i]
if (isSameColor(board, neighbor, index)) {
columns.push(neighbor[1])
newboard = updateBoard(newboard, neighbor, undefined)
}
}
//columns
columns.push(neighbor[1])
newboard = updateBoard(newboard, index, undefined)
return [newboard, columns]
}
//Out of bounds checker
var isOutOfBounds = function(index) {
var val = false
if (index[0] < 1 || index[0] > 3) {
if (index[1] < 1 || index[1] > 3) {
val = true
}
}
return val
}
var updateBoard = function(board, index, value) {
board[index[0]][index[1]] = value
return board
}
var isSameColor = function(board, neighbor, index) {
if (isOutOfBounds(neighbor)){
return false
}
return board[index[0]][index[1]] === board[neighbor[0]][neighbor[1]]
}
var popIndex = combinations([2, 2])
console.log("same color check")
console.log(isSameColor(Board, [2, 2], [2, 3]))
console.log("\n")
console.log("Pop Index")
console.log(popIndex)
console.log("\n")
var...