Skip to content

Commit

Permalink
Remove unsolvable puzzles... oops
Browse files Browse the repository at this point in the history
  • Loading branch information
cassidoo committed Dec 31, 2018
1 parent f82d599 commit 1f10ef1
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,38 @@ function randomGrid() {
for (let i = 0; i < 5; i++) {
let row = [];
for (let j = 0; j < 5; j++) {
row.push(Math.round(Math.random()));
row.push(0);
}
grid.push(row);
}

return grid;
}

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

function generateGrid(grid) {
let board = document.getElementById('board');
board.innerHTML = '';
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
let button = `<button class="fj" id="flapjack-${i}-${j}"`
+ `onClick="handleClick(${i}, ${j})">`
+ `onClick="handleClick(${i}, ${j}, ${false})">`
+ `${grid[i][j] === 0 ? '<div class="butter"></div>' : ''}`;
+ `</button>`;
button = htmlToElement(button);
board.appendChild(button);
}
}

for (let k = 0; k < 30; k++) {
handleClick(getRandomInt(4), getRandomInt(4), true);
}
}

function handleClick(x, y) {
function handleClick(x, y, init) {
let g = grid;

g[x][y] = flip(g[x][y]);
Expand Down Expand Up @@ -82,15 +91,17 @@ function handleClick(x, y) {
}
grid = g;

if (audio) {
snd.play();
snd.currentTime = 0;
}
if (!init) {
if (audio) {
snd.play();
snd.currentTime = 0;
}

if (checkWin()) {
win();
} else {
addClick();
if (checkWin()) {
win();
} else {
addClick();
}
}
}

Expand Down

0 comments on commit 1f10ef1

Please sign in to comment.