Skip to content

Instantly share code, notes, and snippets.

View renatocassino's full-sized avatar

Renato Cassino renatocassino

View GitHub Profile
@renatocassino
renatocassino / 2048-addNumber-01.js
Created August 8, 2018 01:52
2048-addNumber-01.js
const addNumber = (board, point, number=2) => {
return board.map( // Iterate over columns
(line, columnIdx) => line.map((block, lineIdx) => // Iterate over line
// If column and line index is equals, return 2, else return the number
columnIdx === point[0] && lineIdx === point[1] ? number : block
));
};
@renatocassino
renatocassino / 2048-addNumber.js
Last active August 7, 2018 16:49
2048-addNumber.js
// @param 2 in format [column:int, line:int]
// @param number with default value 2
const addNumber = (board, [columnIndex, lineIndex], number=2) => {
const line = board[columnIndex]; // Get line to change
const newLine = [
...line.slice(0, lineIndex), // Getting all values before index to change
number, // Add number to array
...line.slice(lineIndex + 1, line.length), // Getting all values after index to change
];
@renatocassino
renatocassino / 2048-getRandomIndex.js
Created August 7, 2018 16:33
2048-getRandomIndex.js
const getRandomIndex = (zeros) => {
return zeros[parseInt(Math.random() * zeros.length)];
};
@renatocassino
renatocassino / game.rs
Last active August 19, 2018 20:10
2048-rust.rs
fn add_number_bad_way(board: Vec<Vec<i32>>, point: Vec<i32>, number: i32) -> Vec<Vec<i32>> {
return board.iter().enumerate().map(|(column_index, line)| {
return line.iter().enumerate().map(|(line_index, value)| {
if column_index as i32 == point[0] && line_index as i32 == point[1] {
return number;
}
return *value;
}).collect();
}).collect();
}
@renatocassino
renatocassino / getEmptyBlocks.js
Last active August 7, 2018 14:50
getEmptyBlocks.js
const getEmptyBlocks = (board) => (
board.reduce((lineData, line, idxColumn) => { // Iterate over columns
const result = line.reduce((data, row, idx) => { // Iterate over lines
if (row !== 0) return data; // If value is different of zero, response last iterate
return [[idxColumn, idx], ...data]; // Join last iterate (or default value) with new data
}, []); // Default value to reduce in line
return [...lineData, ...result]; // Join old value with new value and return to next reduce
}, []) // Default value to reduce in column
);
@renatocassino
renatocassino / 2048-getEmptyBlocksNonFuncional.js
Last active August 6, 2018 13:11
2048-getEmptyBlocksNonFuncional.js
// Funcion non Functional
const getEmptyBlocksNonFunctional = (currentBoard) => {
const positions = []; // Variable with positions
for(let i = 0; i < currentBoard.length; i++) { // Iterate in columns
for(let j = 0; j < currentBoard[i].length; j++) { // Iterate in lines
if(currentBoard[i][j] === 0) {
positions.push([i, j]); // Append in pushes when 0
}
}
}
@renatocassino
renatocassino / 2048-creating-board.js
Created August 6, 2018 12:39
2048-creating-board.js
const board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
];
// Important, when you see this matix in code,
// you are seeing the board rotated 90 deg to left,
// because the first value is column and the second value is the line
// board[column, line]
@renatocassino
renatocassino / 2048-print-board.js
Last active August 16, 2018 02:09
Tutorial 2048 functional
const printBoard = (board) => {
const format = (n, stringSize) => {
const size = n.toString().length;
return ' '.repeat(stringSize - spaces) + n + ' ';
}
const textToPrint = board.map((column, idxLine) => {
const line = column.map((_, idxColumn) => format(board[idxColumn][idxLine]));
return line.join('|');
});
console.log('-'.repeat(4*5+5));
@renatocassino
renatocassino / 2048-game.js
Last active August 16, 2018 03:17
2048 game
const readlineSync = require('readline-sync');
const printBoard = (board) => {
const f = (n) => {
const size = n.toString().length;
const spaces = 5-size;
return ' '.repeat(spaces) + n.toString() + ' ';
};
textToPrint = [];
@renatocassino
renatocassino / init.el
Last active July 30, 2018 21:26
Emacs
(global-set-key (kbd "C-x q") 'neotree-toggle)
(setq js2-basic-offset 2)