Skip to content

Instantly share code, notes, and snippets.

@Tymski
Created April 26, 2018 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tymski/a71a12a9d27f610dd8955acbceb32cc4 to your computer and use it in GitHub Desktop.
Save Tymski/a71a12a9d27f610dd8955acbceb32cc4 to your computer and use it in GitHub Desktop.
gra w kropki
createArray = (x, y, fill = 0) => {
for (var ary = []; ary.push(Array(x).fill(fill)) < y;);
return ary;
}
board = createArray(10, 10, 3);
display = (ary = board) => console.table(ary) // function that draws the board in the console
has4 = (ary) => ary.filter(x => x >= 4).length > 0 // Is there a 4 or more in 1 dimensional array?
Has4 = (ary = board) => ary.filter((x) => has4(x)).length > 0 // Is there a 4 or more in 2 dimensional array?
split = () => {
let board2 = createArray(10, 10);
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
if (board[i][j] >= 4) {
board2[i][j] += board[i][j] - 4;
if (typeof board2[i + 1] != 'undefined') board2[i + 1][j] += 1;
if (typeof board2[i - 1] != 'undefined') board2[i - 1][j] += 1;
if (typeof board2[i][j + 1] != 'undefined') board2[i][j + 1] += 1;
if (typeof board2[i][j - 1] != 'undefined') board2[i][j - 1] += 1;
} else {
board2[i][j] += board[i][j];
}
}
}
board = JSON.parse(JSON.stringify(board2));
display(board);
if (Has4()) split();
}
move = (x, y, a = 1) => {board[x][y] += a; split()}
move(2,3,1)
display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment