Skip to content

Commit

Permalink
RoofRunning v.1.0.1 - Big update
Browse files Browse the repository at this point in the history
Fixes:
- Finally, after some headaches, managed to create a way to check if the current cube setup is solvable and if not the game will automatically terminate.
  • Loading branch information
MaximilianAdF committed Feb 14, 2024
1 parent 7bd033d commit 0c91831
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 34 deletions.
1 change: 0 additions & 1 deletion Laundromat/Laundromat.css
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ body {
color: green;
cursor: pointer;
display: inline-block;
font-family: CerebriSans-Regular,-apple-system,system-ui,Roboto,sans-serif;
padding: 7px 20px;
text-align: center;
text-decoration: none;
Expand Down
2 changes: 1 addition & 1 deletion LockPick/LockPick.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ body {
flex-direction: column;
align-items: center;
justify-content: center;
margin: 2% auto;
margin: 0% auto;
width: 600px;
height: 800px;
}
Expand Down
6 changes: 3 additions & 3 deletions RoofRunning/RoofRunning.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ body {
flex-direction: column;
align-items: center;
justify-content: center;
margin: 2% auto;
margin: 0 auto;
width: 600px;
height: 670px;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ body {
gap: 10px;
margin-top: -50px;
margin-bottom: 15px;
margin-left: 440px;
margin-left: 475px;
display: none;
color: white;
padding: 0px 18px;
Expand All @@ -127,7 +127,7 @@ body {
gap: 10px;
margin-top: -50px;
margin-bottom: 15px;
margin-left: 440px;
margin-left: 475px;
display: none;
color: white;
padding: 0px 10px;
Expand Down
148 changes: 135 additions & 13 deletions RoofRunning/RoofRunning.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ var Cube = /** @class */ (function () {
this.cubeLeftShift();
}
};
Cube.prototype.getRandomColorClass = function () {
var colors = ["cuber", "cubeg", "cubeb"];
var randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex];
};
Cube.prototype.getConnectedCubes = function () {
var _this = this;
var connectedCubes = new Set();
Expand All @@ -94,25 +99,139 @@ var Cube = /** @class */ (function () {
}
return connectedCubes;
};
Cube.prototype.getRandomColorClass = function () {
var colors = ["cuber", "cubeg", "cubeb"];
var randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex];
};
Cube.prototype.checkwin = function () {
var container = document.getElementById("container");
var divsInsideContainer = container.querySelectorAll('.empty');
if (divsInsideContainer.length === 25) {
endGame("win");
}
if (!checkSolvable()) {
endGame("lose");
}
};
Cube.prototype.squareClick = function () {
this.removeConnectedCubes();
this.checkwin();
};
return Cube;
}());
//Make function that checks solvability of the board
//The functions below together checks solvability of the board
function helpFunct(container, queue) {
if (getColorCount().includes(1)) {
return false;
}
var c = 0;
container.forEach(function (cube) {
if (cube.classList.contains("empty")) {
c++;
}
});
if (c === 25) {
return true;
}
while (queue.length > 0) {
var connectedCubes = queue.shift();
if (helpFunct(cubesUpdate(container, connectedCubes), updateQueue(container))) {
return true;
}
}
return false;
}
function cubesUpdate(container, connectedCubes) {
connectedCubes.forEach(function (cube) {
var _a;
var idx = container.indexOf(cube);
cube.classList.remove(cube.classList.item(1));
cube.classList.add("empty");
var row = Math.floor(idx / 5);
for (var i = 0; i < row; i++) {
_a = [container[idx - 5 * (i + 1)], container[idx - 5 * i]], container[idx - 5 * i] = _a[0], container[idx - 5 * (i + 1)] = _a[1];
}
});
for (var i = 24; i >= 0; i--) {
var currCube = container[i];
var col = i % 5;
if (currCube.classList.contains("empty") && col < 4) {
for (var j = col; j < 4; j++) {
var tempCube = container[i + (j - col)];
container[i + (j - col)] = container[i + 1 + (j - col)];
container[i + 1 + (j - col)] = tempCube;
}
}
}
return container;
}
function updateQueue(container) {
var queue = [];
var visited = new Set();
container.forEach(function (cube) {
if (!cube.classList.contains("empty")) {
var connectedCubes = getConnectedCubes(container, cube);
for (var i = 0; i < connectedCubes.size; i++) {
var connectedCube = connectedCubes[i];
if (!visited.has(connectedCube)) {
visited.add(connectedCube);
}
else {
queue.push(connectedCubes);
break;
}
}
}
});
return queue;
}
function checkSolvable() {
var container = document.getElementById("container");
var containerCopy = Array.from(container.childNodes).map(function (node) { return node.cloneNode(true); }); //Deep copy of the container so modification of the DOM cubes does not affect the original container
var queue = updateQueue(containerCopy);
return helpFunct(containerCopy, queue);
}
function getConnectedCubes(container, cube) {
var connectedCubes = new Set();
var queue = [cube];
while (queue.length > 0) {
var currentCube = queue.shift();
connectedCubes.add(currentCube);
var neighbors = getAdjacentCubes(container, currentCube);
neighbors.forEach(function (neighbor) {
if (!connectedCubes.has(neighbor) && neighbor.classList.contains(cube.classList.item(1))) {
connectedCubes.add(neighbor);
queue.push(neighbor);
}
});
}
if (connectedCubes.size === 1) {
connectedCubes.clear();
}
return connectedCubes;
}
function getAdjacentCubes(container, cube) {
var idx = container.indexOf(cube);
var adjacentCubes = [];
var row = Math.floor(idx / 5);
var col = idx % 5;
if (col - 1 >= 0)
adjacentCubes.push(container[idx - 1]);
if (col + 1 < 5)
adjacentCubes.push(container[idx + 1]);
if (row - 1 >= 0)
adjacentCubes.push(container[idx - 5]);
if (row + 1 < 5)
adjacentCubes.push(container[idx + 5]);
return adjacentCubes;
}
function getColorCount() {
var container = document.getElementById("container");
var colors = ["cuber", "cubeg", "cubeb"];
var colorCount = [0, 0, 0];
colors.forEach(function (color, idx) {
var count = container.querySelectorAll(".".concat(color)).length;
colorCount[idx] = count;
});
return colorCount;
}
// ------------------------------------------------------------^
function endGame(outcome) {
var timerProgress = document.querySelector(".timer-progress-bar");
var overlay = document.querySelector(".overlay");
Expand All @@ -137,18 +256,21 @@ function endGame(outcome) {
}, 2000);
}
function resetGame() {
var container = document.getElementById("container");
container.innerHTML = "";
clearInterval(timerInterval);
secondsRemaining = 15;
generateCubes();
runTimer();
}
function generateCubes() {
var container = document.getElementById("container");
for (var i = 0; i < 25; i++) {
var cube = new Cube();
container === null || container === void 0 ? void 0 : container.appendChild(cube.element);
}
do {
console.log(1);
var container = document.getElementById("container");
container.innerHTML = "";
for (var i = 0; i < 25; i++) {
var cube = new Cube();
container === null || container === void 0 ? void 0 : container.appendChild(cube.element);
}
} while (!checkSolvable()); // Regenerate the cubes if the board is not solvable
}
function updateTimerDisplay() {
var timerProgress = document.querySelector(".timer-progress-bar");
Expand Down
Loading

0 comments on commit 0c91831

Please sign in to comment.