Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finally added hangman late by Vani #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Flutter.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The evaluation would be done mainly on the following basis:
3. Completion of mentioned tasks
4. Bonus Points for implementing extra features depending on your creativity😉

Remember, the deadline for this week's task is **June 7th, 2022, 23:59**.
Remember, the deadline for this week's task is **June 10th, 2022, 23:59**.

## Submission Guidelines :

Expand Down
55 changes: 55 additions & 0 deletions Js/highscore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// return btn
const btn = document.getElementById("btn");
btn.addEventListener("click", function () {
window.location.href = '../index.html';
}
);


// convert localStorage to array and get all key names from localStorage
var high_score = Object.values(localStorage);
var keys = Object.keys(localStorage);

// find index of name key which is not to print
var index = high_score.indexOf(localStorage.name);


// sorted both keys and score
for (let j = 0; j < high_score.length; j++) {

for (let k = 0; k < high_score.length; k++) {
if (k != index && j != index) {
if (parseInt(high_score[j]) > parseInt(high_score[k])) {
var temp = high_score[j];
high_score[j] = high_score[k];
high_score[k] = temp;
var temp2 = keys[j];
keys[j] = keys[k];
keys[k] = temp2;
}
}
}
}

// display high_score in page
for (var i = 0; i < high_score.length && i < 11; i++) {

if (i != index) {
// display keys and values
const tr = document.createElement('tr');
const th = document.createElement('th');
th.scope = "row";
const td1 = document.createElement('td');
const td2 = document.createElement('td');

th.textContent = i;
td1.textContent = keys[i];
td2.textContent = high_score[i];
tr.appendChild(th);
tr.appendChild(td1);
tr.appendChild(td2);
document.getElementById('highscore').appendChild(tr);
}
}

224 changes: 224 additions & 0 deletions Js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// grabbing and declaring variable
const Head = document.querySelector('#heading');
const word = document.querySelector('#word');
const incorrect_place = document.querySelector('#incorrect');
const alphabet = document.querySelector('#alphabets-place');
const number_of_lives = document.querySelector('#lives_number');
const button_of_hint = document.querySelector('#hint-button')
const URL = 'https://random-word-api.herokuapp.com/word';
const Whole_page = document.getElementById('container')
const streak_content = document.querySelector('#streck-place')
const reset_btn = document.querySelector('#restart_button')
const timer_content = document.querySelector('#timer-place')
const head = document.querySelector('.head')
const body_arms = document.querySelectorAll('.secondthing')
const words = document.querySelector('#word-place');
const legs = document.querySelectorAll('.thirdthing')
const audio = new Audio("../sound/sound.mp3");
let counting_streak = 0;
let life;
let letter_found;
let hard_level = 1;
let seconds = 120;
let array_for_hint_button = [];
var playing_player_index;

// getting player name and its score to change
for (var i = 1; i <= localStorage.length; i++) {
var the_playing_player = localStorage.key(i);
if (the_playing_player === "name") {
playing_player_index = i;
}
}
const name_of_players = localStorage.key(playing_player_index);
const player_name = localStorage.getItem(name_of_players);
const score_of_player = localStorage.getItem(player_name);




// restart button event
reset_btn.addEventListener('click', () => {
window.location.href = '../index.html';
})



// getting data from API
fetch(URL).then(function (response) {
return response.json();
}).then(function (Data) {

// checking hard_level condition of word
if (Data[0].length > 5 && hard_level === 1) {
life = 3;
const WORD_IN_CAPITAL = Data[0].toUpperCase();
const array_of_capital_letter = WORD_IN_CAPITAL.split('');
number_of_lives.textContent = life;
const length_of_word = Data[0].length;
var char_array = [];
letter_found = length_of_word;

// timer
var countdown = setInterval(() => {
seconds -= 1;
timer_content.innerHTML = seconds + "s";
if (seconds === 0) {
clearInterval(countdown);
Whole_page.classList.add('loose_win')
words.textContent = "";
array_of_capital_letter.forEach(alpha => {
words.textContent = words.textContent + " " + alpha + " ";
});
Head.textContent = "Time is up! You Lose";
Whole_page.classList.add('loose_win')
reset_btn.classList.remove('loose_win')
reset_btn.classList.add('click_able')
}
}, 1000);



// dash for word
for (let index = 0; index < length_of_word; index++) {
char_array.push('_');
words.textContent = words.textContent + ' _ ';
}




// display all aplhabets
for (let index = 0; index < 26; index++) {
alphabet.innerHTML = alphabet.innerHTML + '<button class="btn btn-outline-primary alphabet" id="alphabet">' + String.fromCharCode(65 + index) + '</button>';
}





const all_alphabet = document.querySelectorAll('#alphabet');
// nodelist to array
all_alphabet.forEach(function (alphabet) {

alphabet.addEventListener('click', function () {
// seeing click on word
audio.play();
alphabet.classList.remove('btn-outline-primary');
alphabet.classList.add('btn-primary');
alphabet.classList.add('disabled');
// find alphabet exist in word at which place
const alphabet_exist = array_of_capital_letter.indexOf(alphabet.textContent);
var index = -1;
var index_array = [];
// if alphabet exist see in word
if (alphabet_exist > -1) {
all_alphabet.forEach(e => {
if (e.textContent == alphabet.textContent) {
e.classList.add('btn-success');
e.classList.remove('btn-outline-primary');
}
});
array_of_capital_letter.forEach(alphabets => {
index++;
if (alphabets == alphabet.textContent) {
index_array.push(index);
}
});
index = -1;
counting_streak += index_array.length;
streak_content.textContent = counting_streak;
index_array.forEach(number => {
char_array[number] = alphabet.textContent;
});


words.innerHTML = "";

char_array.forEach(char => {
words.textContent = words.textContent + " " + char + " ";
})


letter_found -= index_array.length;



if (letter_found === 0) {
clearInterval(countdown);
Whole_page.classList.add('loose_win');
Head.textContent = "You Win";
// string to int convertion
var score = parseInt(score_of_player);
score += 3;
localStorage.setItem(player_name, score);
// location reload after 5 seconds
setTimeout(() => {
window.location.reload();
}
, 3000);

}
} else {
// display in incorrect_place
counting_streak = 0;
streak_content.textContent = counting_streak;
incorrect_place.textContent = incorrect_place.textContent + " " + alphabet.textContent;
all_alphabet.forEach(e => {
if (e.textContent == alphabet.textContent) {
e.classList.add('btn-danger');
e.classList.remove('btn-outline-primary');
}
});

number_of_lives.textContent = number_of_lives.textContent - 1;
life--;
if (life === 2) {
head.classList.remove('display')
}
if (life === 1) {
body_arms.forEach(thing => {
thing.classList.remove('display')
});

}
if (life === 0) {
legs.forEach(thing => {
thing.classList.remove('display')
});
clearInterval(countdown);
words.textContent = "";
array_of_capital_letter.forEach(alpha => {
words.textContent = words.textContent + " " + alpha + " ";
});
Whole_page.classList.add('loose_win')
reset_btn.classList.remove('loose_win')
reset_btn.classList.add('click_able')
Head.textContent = "You Lose";
}
}
})
button_of_hint.addEventListener('click', () => {
button_of_hint.classList.add('disabled');
for (let k = 0; k < length_of_word; k++) {
if (char_array[k] == '_') {
array_for_hint_button.push(k);
}
}
all_alphabet.forEach(e => {
if (e.textContent == array_of_capital_letter[array_for_hint_button[0]]) {
e.classList.remove('alphabet');
e.classList.add('alphabet-hint');
e.classList.remove('btn-outline-primary');
}
});
}
)
})

}
else {
location.reload();
}

})
Loading