Skip to content

Commit

Permalink
Merge pull request #118 from theKidCelo/Decoupling-language
Browse files Browse the repository at this point in the history
create seperate file for functionality
  • Loading branch information
Aatmaj-Zephyr authored Apr 2, 2023
2 parents 9024a97 + da22dd8 commit fce7db8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 94 deletions.
95 changes: 1 addition & 94 deletions MemoryGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ document.onreadystatechange = function () {
document.querySelector(".loader").style.display = "none";
document.querySelector(".main").style.display = "block";
}

};

//PascalCase followed
Expand All @@ -31,97 +32,3 @@ for (let f = 1; f <= 16; f++) {
// document.getElementById("button" + f).hover="background-color: white; color: black; border: 2px solid #4CAF50;"
//Button formatting
}
function Buttonclicked(a) {
// eslint-disable-line
if (!isEnableClickTile) {
return;
}
//Function to be executed once button is clicked.
//a is the parameter which tells which button is clicked.
document.getElementById("button" + a).src = `./images/${Cards[a]}.png`; // Flip the Card over
document.getElementById("button" + a).disabled = "disabled"; //Disable clicking the same Card.
document.getElementById("button" + a).style =
"opacity: 0.7;cursor: not-allowed;color:Blue";
checkscore(a); //Check the Cards.
}

function checkscore(a) {
if (lastbutton == null) {
lastbutton = a;
//If it is the first Card (from two Cards) to be chosen,
//then set the value of lastbutton to the button pressed.
} else {
setClickableOnTiles(false);
if (Cards[a] != Cards[lastbutton]) {
//If they don't match.....
let temp = lastbutton;
document.getElementById("button" + a).style =
"transition-duration: 0.8s;background-color:Tomato;color:White;";
document.getElementById("button" + temp).style =
"transition-duration: 0.8s;background-color:Tomato;color:White;";
//If invalid, then set the background background-color Orange.
window.setTimeout(function () {
document.getElementById("button" + a).src = `./images/question.png`;
// document.getElementById("button" + a).style = style;
// document.getElementById("button" + temp).style = style;
document.getElementById("button" + temp).src = `./images/question.png`;
document.getElementById("button" + temp).style = "";
document.getElementById("button" + a).style = "";
setClickableOnTiles(true);
}, 1200);
//With delay, flip them back. and enable clicking.

lastbutton = null; //reset lastbutton.
} else {
//If they match....
score = score + 1; //Increase the score.
document.getElementById("button" + a).style =
"transition-duration: 0.8s;background-color:lime;color:Blue;";
document.getElementById("button" + lastbutton).style =
"transition-duration: 0.8s;background-color:lime;color:Blue;";
//Change their colours to green.
document.getElementById("button" + lastbutton).style =
"pointer-events:none;";
document.getElementById("button" + a).style = "pointer-events:none;";
//Disable them from further clicking.
document.getElementById("score").innerHTML = "Score=" + score;
//Display the score.
setClickableOnTiles(true);
lastbutton = null;
}

turns = turns + 1; //Increase number of turns.
document.getElementById("Turns").innerHTML = "Turns taken=" + turns;
//display the number of turns taken.
if (score == (Cards.length - 1) / 2) {
gameover(); //game over.
}
}
}

function gameover() {
window.setTimeout(function () {
alert("Game Over");
//Alert that the game is over after a delay.
}, 1000);
document.getElementById("score").innerHTML = "Game over!";
document.getElementById("Turns").innerHTML = "You took " + turns + " turns";

window.setTimeout(function () {
location.reload()
// Reload after game is over after a delay.
}, 1000);

}

function setClickableOnTiles(isEnable) {
const tiles = document.querySelectorAll("img.button");
for (const tile of tiles) {
if (isEnable) {
tile.classList.remove("cursor-not-allowed");
} else {
tile.classList.add("cursor-not-allowed");
}
}
isEnableClickTile = isEnable;
}
95 changes: 95 additions & 0 deletions game-functionality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function Buttonclicked(a) {
// eslint-disable-line
if (!isEnableClickTile) {
return;
}
//Function to be executed once button is clicked.
//a is the parameter which tells which button is clicked.
document.getElementById("button" + a).src = `./images/${Cards[a]}.png`; // Flip the Card over
document.getElementById("button" + a).disabled = "disabled"; //Disable clicking the same Card.
document.getElementById("button" + a).style =
"opacity: 0.7;cursor: not-allowed;color:Blue";
checkscore(a); //Check the Cards.
}

function checkscore(a) {
if (lastbutton == null) {
lastbutton = a;
//If it is the first Card (from two Cards) to be chosen,
//then set the value of lastbutton to the button pressed.
} else {
setClickableOnTiles(false);
if (Cards[a] != Cards[lastbutton]) {
//If they don't match.....
let temp = lastbutton;
document.getElementById("button" + a).style =
"transition-duration: 0.8s;background-color:Tomato;color:White;";
document.getElementById("button" + temp).style =
"transition-duration: 0.8s;background-color:Tomato;color:White;";
//If invalid, then set the background background-color Orange.
window.setTimeout(function () {
document.getElementById("button" + a).src = `./images/question.png`;
// document.getElementById("button" + a).style = style;
// document.getElementById("button" + temp).style = style;
document.getElementById("button" + temp).src = `./images/question.png`;
document.getElementById("button" + temp).style = "";
document.getElementById("button" + a).style = "";
setClickableOnTiles(true);
}, 1200);
//With delay, flip them back. and enable clicking.

lastbutton = null; //reset lastbutton.
} else {
//If they match....
score = score + 1; //Increase the score.
document.getElementById("button" + a).style =
"transition-duration: 0.8s;background-color:lime;color:Blue;";
document.getElementById("button" + lastbutton).style =
"transition-duration: 0.8s;background-color:lime;color:Blue;";
//Change their colours to green.
document.getElementById("button" + lastbutton).style =
"pointer-events:none;";
document.getElementById("button" + a).style = "pointer-events:none;";
//Disable them from further clicking.
document.getElementById("score").innerHTML = "Score=" + score;
//Display the score.
setClickableOnTiles(true);
lastbutton = null;
}

turns = turns + 1; //Increase number of turns.
document.getElementById("Turns").innerHTML = "Turns taken=" + turns;
//display the number of turns taken.
if (score == (Cards.length - 1) / 2) {
gameover(); //game over.
}
}
}

function gameover() {
window.setTimeout(function () {
alert("Game Over");
//Alert that the game is over after a delay.
}, 1000);
document.getElementById("score").innerHTML = "Game over!";
document.getElementById("Turns").innerHTML = "You took " + turns + " turns";

window.setTimeout(function () {
location.reload()
// Reload after game is over after a delay.
}, 1000);

}

function setClickableOnTiles(isEnable) {
const tiles = document.querySelectorAll("img.button");
for (const tile of tiles) {
if (isEnable) {
tile.classList.remove("cursor-not-allowed");
} else {
tile.classList.add("cursor-not-allowed");
}
}
isEnableClickTile = isEnable;
}

1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ <h1 class="h1">Memory game</h1>
<p id="Turns">Turns taken=0</p>
</div>
<span class="loader"></span>
<script src="game-functionality.js"></script>
<script src="MemoryGame.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/darkmode-js.min.js"></script>
<script>
Expand Down

0 comments on commit fce7db8

Please sign in to comment.