-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1023 from JoelCoyos/guess-the-sequence
Guess the sequence Game added
- Loading branch information
Showing
6 changed files
with
478 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Guess the Sequence</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
<script src="script.js"></script> | ||
</head> | ||
<body> | ||
<h1>Guess the sequence</h1> | ||
<div id = "menu"> | ||
<div class="card"> | ||
<p>The goal of the game is to attempt to guess the secret code generated by the computer.</p> | ||
<p>Black pegs for correct colors in the correct positions</p> | ||
<p> White pegs for correct colors in incorrect positions</p> | ||
<p> Grey for colors that do not appear on the sequence</p> | ||
<label for="difficulty"> Select Difficulty:</label> | ||
<select id ="difficulty"> | ||
<option value="Easy">Easy</option> | ||
<option value="Medium">Medium</option> | ||
<option value ="Hard">Hard</option> | ||
</select> | ||
<button class="button" onclick="newGame()" type="button">New Game</button> | ||
</div> | ||
|
||
</div> | ||
<div id="game"> | ||
<div id="colors"> | ||
</div> | ||
<div id="guesses"> | ||
<div class = "guess"> | ||
</div> | ||
</div> | ||
|
||
<div id="results"> | ||
|
||
</div> | ||
</div> | ||
<div id="check"> | ||
<label id="attempsLeft"></label> | ||
<button class="button" onclick="checkAnswer()" type="button">Check Guess</button> | ||
</div> | ||
<div id="confetti-container"> | ||
<div class="card"> | ||
<p id="resultText">Congratulations, you won!</p> | ||
<button class="button" onclick="wonButton()" type="button">New game</button> | ||
</div> | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
const colors = ['#F44336','#4CAF50','#03A9F4','#FFEB3B',"#673AB7","#795548","#FF5722","#212121"] | ||
let sequence = [] | ||
let numberOfColors=0 | ||
let sequenceSize =0 | ||
let maxAttemps=0 | ||
let currentAttemp = 0 | ||
let currentGuess = [] | ||
let currentGuessDots=[] | ||
let currentGuessIndex=0 | ||
|
||
let confettiInterval | ||
|
||
function GenerateSequence() | ||
{ | ||
for(let i = 0;i<sequenceSize;i++) | ||
{ | ||
randomColor = Math.floor(Math.random()*numberOfColors) | ||
sequence.push(colors[randomColor]) | ||
} | ||
} | ||
|
||
function createGuessColors() | ||
{ | ||
for(let i = 0;i<numberOfColors;i++) | ||
{ | ||
var newGuessColor = document.createElement("span") | ||
newGuessColor.classList.add("dot"); | ||
newGuessColor.style.backgroundColor = colors[i] | ||
newGuessColor.setAttribute("color",colors[i]) | ||
newGuessColor.addEventListener("click",function() | ||
{ | ||
clickGuessColor(this.getAttribute("color")) | ||
}) | ||
var guessDiv = document.getElementById('colors') | ||
guessDiv.appendChild(newGuessColor) | ||
} | ||
} | ||
|
||
function createGuessSequence() | ||
{ | ||
var newGuessSequence = document.createElement("div") | ||
newGuessSequence.classList.add("guess") | ||
for(let i = 0;i<sequenceSize;i++) | ||
{ | ||
var guess = document.createElement("span") | ||
guess.classList.add("dot") | ||
guess.style.backgroundColor="#313b4b"; | ||
guess.setAttribute("index",i) | ||
currentGuessDots.push(guess) | ||
newGuessSequence.appendChild(guess) | ||
} | ||
var guessesDiv = document.getElementById("guesses") | ||
guessesDiv.appendChild(newGuessSequence) | ||
} | ||
|
||
function clickGuessColor(color) | ||
{ | ||
currentGuess.push(color) | ||
currentGuessDots[currentGuessIndex].style.backgroundColor = color | ||
currentGuessIndex++ | ||
} | ||
|
||
function checkAnswer() | ||
{ | ||
var newResultSequence = document.createElement("div") | ||
newResultSequence.classList.add("result") | ||
won = true | ||
let correctColorPosition = Array(sequenceSize) | ||
for(let i =0;i<sequenceSize;i++) | ||
{ | ||
if(currentGuess[i]==sequence[i]) | ||
{ | ||
correctColorPosition[i] = currentGuess[i] | ||
} | ||
} | ||
|
||
for(let i = 0;i<sequenceSize;i++) | ||
{ | ||
var guess = document.createElement("span") | ||
guess.classList.add("dot") | ||
let answerColor | ||
let currentColorGuess = currentGuess[i] | ||
let currentColorGuesses | ||
if(currentGuess[i]==sequence[i]) | ||
answerColor = "black" | ||
else if (sequence.includes(currentGuess[i])) | ||
{ | ||
won = false | ||
numberOfColorInSequence = sequence.filter(element => element ==currentGuess[i]).length | ||
if(correctColorPosition.filter(element => element ==currentGuess[i]).length < numberOfColorInSequence) | ||
{ | ||
let k = 0 | ||
for(let j=0;j<i;j++) | ||
{ | ||
if(currentGuess[j]==currentGuess[i]) | ||
{ | ||
k++ | ||
} | ||
} | ||
if(k < numberOfColorInSequence) | ||
answerColor = "white" | ||
else answerColor = "grey" | ||
} | ||
else | ||
{ | ||
answerColor = "grey" | ||
} | ||
} | ||
else | ||
{ | ||
answerColor = "grey" | ||
won = false | ||
} | ||
guess.style.backgroundColor=answerColor | ||
|
||
newResultSequence.appendChild(guess) | ||
} | ||
if(!won) | ||
{ | ||
var guessesDiv = document.getElementById("results") | ||
guessesDiv.appendChild(newResultSequence) | ||
currentAttemp++; | ||
document.getElementById("attempsLeft").innerText="Attempts left: " + (maxAttemps - currentAttemp).toString() + " " | ||
if(currentAttemp!=maxAttemps) | ||
{ | ||
currentGuess = [] | ||
currentGuessDots = [] | ||
currentGuessIndex = 0 | ||
createGuessSequence() | ||
} | ||
else | ||
{ | ||
document.getElementById("confetti-container").style.visibility = "visible" | ||
document.getElementById("resultText").innerHTML = "You lost" | ||
} | ||
} | ||
else | ||
{ | ||
document.getElementById("confetti-container").style.visibility = "visible" | ||
confettiInterval = setInterval(() => { | ||
showConfetti(); | ||
}, 400); | ||
} | ||
} | ||
|
||
const showConfetti = () => { | ||
|
||
const confettiContainer = document.querySelector('#confetti-container'); | ||
const confetti = document.createElement('div'); | ||
confetti.textContent = '🎉'; | ||
confetti.classList.add('confetti'); | ||
confetti.style.left = Math.random() * innerWidth + 'px'; | ||
confettiContainer.appendChild(confetti); | ||
setTimeout(() => { | ||
confetti.remove(); | ||
}, 5000); | ||
}; | ||
|
||
function newGame() | ||
{ | ||
document.getElementById("confetti-container").style.visibility = "hidden" | ||
document.getElementById("menu").style.visibility = "hidden" | ||
sequence = [] | ||
difficultySelect = document.getElementById("difficulty").options; | ||
difficultyValue = difficultySelect[difficultySelect.selectedIndex].value | ||
if(difficultyValue === "Easy") | ||
{ | ||
numberOfColors = 4 | ||
sequenceSize = 3 | ||
maxAttemps = 6 | ||
} | ||
else if(difficultyValue === "Medium") | ||
{ | ||
numberOfColors = 5 | ||
sequenceSize = 4 | ||
maxAttemps = 6 | ||
} | ||
else if(difficultyValue === "Hard") | ||
{ | ||
numberOfColors = 6 | ||
sequenceSize = 5 | ||
maxAttemps = 6 | ||
} | ||
currentAttemp = 0 | ||
currentGuess = [] | ||
currentGuessDots=[] | ||
currentGuessIndex=0 | ||
const colorsContainer = document.getElementById("colors") | ||
while (colorsContainer.firstChild) colorsContainer.removeChild(colorsContainer.firstChild); | ||
const guessesContainer = document.getElementById("guesses") | ||
while (guessesContainer.firstChild) guessesContainer.removeChild(guessesContainer.firstChild); | ||
const resultsContainer = document.getElementById("results") | ||
while (resultsContainer.firstChild) resultsContainer.removeChild(resultsContainer.firstChild); | ||
|
||
document.getElementById("attempsLeft").innerText="Attempts left: " + (maxAttemps - currentAttemp).toString() | ||
|
||
GenerateSequence() | ||
createGuessColors(); | ||
createGuessSequence() | ||
} | ||
|
||
function wonButton() | ||
{ | ||
clearInterval(confettiInterval) | ||
showMenu() | ||
} | ||
|
||
function showMenu() | ||
{ | ||
document.getElementById("confetti-container").style.visibility = "hidden" | ||
document.getElementById("menu").style.visibility = "visible" | ||
} | ||
|
||
|
||
window.onload = function(){ | ||
showMenu() | ||
|
||
} |
Oops, something went wrong.