-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
153 lines (122 loc) · 4.38 KB
/
script.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
let passedGuesses = ""
let correctGuesses = ""
let tries = 0
let pickedWord = ""
let tempword = pickedWord
let underScore = "_"
let user = ''
import { hangmanWords } from "./wordlist.js"
// lägg detta i highscore.js och gör om den till en klass som sparar både namn och highscore
// Bugg: sätter rätt bokstav som felaktig
class Player {
constructor(name, score) {
this.name = name;
this.score = score;
localStorage.setItem(name, score)
}
}
function getUser() {
user = prompt("Please enter your name", "Harry Potter")
if (user == null || user == "") {
let anonNum = Math.floor(100 + Math.random() * 900)
user = "Anonymous" + anonNum
}
user = new Player(user, 0)
document.getElementById("currentUser").innerText = user.name
userScoreUpdate(user.score)
}
getUser()
//
getRandomWord()
function getRandomWord() { // RANDOMIZAR ett ord ur en lista av ord
let wordList = hangmanWords
const words = wordList.split(' ')
let NUMB = Math.floor(Math.random() * 7)
pickedWord = 'BENJAMIN'//words[NUMB] (Temporary word for debug purposes)
tempword = pickedWord
genclue(pickedWord)
}
function genclue() {
for (let index = 1; index < pickedWord.length; index++) { // genererar ledtråd
underScore += "_"
document.getElementById("word").innerText = underScore
}
}
document.getElementById("submit").onclick = function submit() {
let guess = document.getElementById("guessbox").value.toUpperCase() // tar in den gissade bokstaven
document.getElementById("guessbox").value = ''
if (guess == '') {
return
} else {
compareWords(guess)
}
}
function compareWords(guessedword) {
// if som räknar antalet försök
if (tries > 1) {
alert("You lost :( The word was: " + pickedWord)
let user = document.getElementById("currentUser").innerText
let score = document.getElementById("currentScore").innerText
document.getElementById("highscore").innerHTML = user + " : " + score
//localStorage.setItem(highscore, name + " : " + score)
}
else {
if (guessedword.length == 1) {// ser till att det bara finns en bokstav
if (pickedWord.includes(guessedword) && !correctGuesses.includes(guessedword)) { // Kollar att gissningen är rätt och inte upprepad
correctGuesses = correctGuesses + guessedword
let recuringLetter = pickedWord.split(guessedword).length - 1 // ta fram antalet ggr bokstaven förekommer
user.score += 10
userScoreUpdate(user.score)
// For loop som kollar att bokstaven ersätts på ALLA ställen
for (let index = 0; index < recuringLetter; index++) {
let placement = tempword.indexOf(guessedword) + 1
tempword = pickedWord.replace(guessedword, " ")
let before = underScore.slice(0, placement - 1)
let after = underScore.slice(placement)
underScore = before + guessedword + after
}
document.getElementById("word").innerText = underScore
if (document.getElementById("word").innerText == pickedWord) {
alert("Congrats! The correct word was " + pickedWord)
// Om du vinner
user.score += 50
userScoreUpdate(user.score)
return
}
return
}
if (passedGuesses.includes(guessedword)) { // Om du upprepar samma bokstav
alert("You need to pick a new letter")
return
}
else {
passedGuesses = passedGuesses + guessedword
document.getElementById("guesses").innerText = " " + passedGuesses
tries = tries + 1
document.getElementById("tries").innerText = tries + "/5"
document.getElementById("hangmanImage").style.content = "url('img/" + tries + ".png')"
//console.log(user.score)
user.score -= 5
userScoreUpdate(user.score)
}
}
else if (guessedword == pickedWord) {
alert("Congrats! The correct word was " + pickedWord)
document.getElementById("word").innerText = pickedWord
user.score += 100
userScoreUpdate(user.score)
// Om du vinner
winTracker()
}
else { // BER DIG ANVÄNDA ENBART ETT ORD
alert("Only one letter allowed at a time!")
}
}
}
function winTracker() {
// något som håller reda på score under spelandet och laddar om med nytt ord utan att fråga om ny användare.
getRandomWord()
}
function userScoreUpdate(score) {
document.getElementById("currentScore").innerText = score
}