-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
134 lines (134 loc) · 3.9 KB
/
bundle.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use strict";
let GAME_WIDTH;
let GAME_SIZE;
let gameState = [];
function generate() {
let beadSequence = [];
for (let i = 0; i < GAME_SIZE; i++) {
beadSequence.push(i);
}
return shuffle(beadSequence);
}
// Creates spans and sets div#playGround
function render(grid = lastState()) {
let rendered = document.createDocumentFragment();
for (let i = 0; i < grid.length; i++) {
let span = document.createElement("span");
let h2 = document.createElement("h2");
//grid[i] == 0 is always empty space
if (grid[i]) {
h2.innerText = `${grid[i]}`;
span.classList.add("bead");
}
span.appendChild(h2);
span.classList.add("position");
span.id = `${grid[i]}`;
span.onclick = handleClick;
rendered.appendChild(span);
}
let gameSpace = document.getElementById("playGround");
gameSpace.replaceChildren(rendered);
}
// Checks click location and moves if movable
function handleClick(e, grid = lastState()) {
let selId = parseInt(e.currentTarget.id);
let indexOfSel = grid.indexOf(selId);
let movability = isMovable(indexOfSel);
//Confirm move: create new history that
//will be changed and rendered
if (movability) {
setState([...lastState()]);
finishMove(indexOfSel, movability);
render();
}
}
// Will recursively move pieces to the empty space.
function finishMove(selPos, direction, grid = lastState()) {
let destPos = selPos + moveBy(direction);
// If it has not found the current empty position,
// Swap the next movable in line with the next one
// Until the empty space is actually swapped
if (destPos != grid.indexOf(0)) {
finishMove(destPos, direction);
}
[grid[selPos], grid[destPos]] = [grid[destPos], grid[selPos]];
}
function undo() {
if (gameState.length > 1) {
gameState.pop();
render();
}
}
function start() {
setGameLevel(getGameLevel());
let undoBtn = document.getElementById("undo");
undoBtn.onclick = undo;
let newGame = generate();
setState(newGame);
render();
}
start();
/*HELPERS *****************************************/
//Returns in [min,max)
function randomInRange(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
function shuffle(array) {
let shuffledGrid = [];
for (let i = 0; i < GAME_SIZE; i++) {
let randomIndex = randomInRange(0, array.length);
shuffledGrid.push(array.splice(randomIndex, 1)[0]);
}
return shuffledGrid;
}
function setState(grid) {
gameState.push(grid);
}
function isMovable(selPos, grid = gameState[gameState.length - 1]) {
let pivotPos = grid.indexOf(0);
let sameCol = Math.floor(pivotPos % GAME_WIDTH) == Math.floor(selPos % GAME_WIDTH);
let sameRow = Math.floor(pivotPos / GAME_WIDTH) == Math.floor(selPos / GAME_WIDTH);
if (sameCol) {
return selPos > pivotPos ? "up" : "down";
}
else if (sameRow) {
return selPos > pivotPos ? "left" : "right";
}
else
return null;
}
function moveBy(direction) {
switch (direction) {
case "up":
return -GAME_WIDTH;
case "down":
return GAME_WIDTH;
case "left":
return -1;
case "right":
return 1;
default:
return 0;
}
}
function lastState() {
return gameState[gameState.length - 1];
}
function setGameLevel(nr) {
GAME_WIDTH = nr;
GAME_SIZE = GAME_WIDTH * GAME_WIDTH;
let playGround = document.getElementById("playGround");
playGround.style.gridTemplateColumns = `repeat(${GAME_WIDTH},1fr)`;
}
function getGameLevel() {
let level = 0;
while (true) {
level = parseInt(prompt("Choose game level 3-5", "4"));
if (typeof level == "number" && (level >= 3 || level <= 5)) {
break;
}
}
return level;
}