-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
295 lines (246 loc) · 7.04 KB
/
game.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { readFileSync } from 'fs'
import promptSync from 'prompt-sync'
import fs from "node:fs"
import { drawMan0, drawMan1, drawMan2, drawMan3, drawMan4 } from './hang-man.js'
let passedGuesses = ""
let correctGuesses = ""
let tries = 0
let pickedWord = ""
let tempword = pickedWord
let underScore = "-"
let guessedword = ""
let user = ""
let currentScore = 0
let currentUser = ''
let HSFile = 'highscore.csv'
const prompt = promptSync()
let x = console.log
class Player {
constructor(name, score) {
this.name = name;
this.score = score;
this.File = 'highscore.csv'
if (!fs.existsSync(this.File)) {
const csvHeader = 'Name,Score\n';
fs.writeFileSync(this.HSFile, csvHeader);
}
const csvContent = `\n${this.name},${this.score}`
fs.appendFileSync(this.File, csvContent, (err) => {
})
}
}
function topOneHS() {
const csvData = fs.readFileSync(HSFile, 'utf8');
const rows = csvData.split('\n').map(row => row.trim());
const columns = rows[0].split(',')
let topOneHS = []
for (let i = 1; i < rows.length; i++) {
const rowValues = rows[i].split(',')
const person = {}
for (let j = 0; j < columns.length; j++) {
person[columns[j]] = rowValues[j]
}
topOneHS.push(person)
}
const personAtIndex1 = topOneHS[0]
x('Highest Score: ' + personAtIndex1.username + ": " + personAtIndex1.score + "p")
topOneHS = []
}
function getUser() {
let user = prompt("Please enter your name");
if (!user || user.trim() === "") {
x("You have to enter a username!")
getUser()
} else {
currentUser = user
if (!fs.existsSync(HSFile)) {
const csvHeader = 'Name,Score\n';
fs.writeFileSync(this.HSFile, csvHeader);
}
const userToFind = currentUser
const csvData = fs.readFileSync(HSFile, 'utf8');
const rows = csvData.split('\n').map(row => row.trim());
const columns = rows[0].split(',')
const hsObjects = []
for (let i = 1; i < rows.length; i++) {
const rowValues = rows[i].split(',')
const obj = {}
for (let j = 0; j < columns.length; j++) {
obj[columns[j]] = rowValues[j]
}
hsObjects.push(obj)
}
if (hsObjects.some(obj => obj.username === currentUser)) {
x("Welcome back " + currentUser + "!")
} else {
currentUser = user
user = new Player(user, 0)
x("Hello " + user.name)
}
}
} getUser()
topOneHS()
getRandomWord()
function getRandomWord() {
const wordList = 'wordlist.csv'
const lines = readFileSync(wordList, 'utf-8').split('\n')
const data = lines.slice(1);
const randomIndex = Math.floor(Math.random() * data.length)
const randomRow = data[randomIndex].split(',')
pickedWord = randomRow[0]
tempword = pickedWord
genclue(pickedWord)
}
function genclue() {
for (let index = 1; index < pickedWord.length; index++) {
underScore += "-"
}
x(underScore)
newLetterPrompt()
}
function newLetterPrompt() {
let guess = prompt("Guess a letter:").toUpperCase()
if (guess == '') {
newLetterPrompt()
} else {
compareWords(guess)
}
}
function compareWords(guessedword) {
if (tries > 3) {
x("You lost :( The word was: " + pickedWord)
x(drawMan4)
playAgain()
}
else {
if (guessedword.length == 1) {
if (passedGuesses.includes(guessedword)) {
x("You need to pick a new letter")
newLetterPrompt()
}
if (pickedWord.includes(guessedword) && !correctGuesses.includes(guessedword)) {
correctGuesses = correctGuesses + guessedword
let recuringLetter = pickedWord.split(guessedword).length - 1
currentScore += 10
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
x(underScore)
}
if (underScore == pickedWord) {
x("Congrats, you guessed right: " + pickedWord)
currentScore += 50
x("Your score is:" + currentScore)
playAgain()
}
else {
newLetterPrompt()
}
}
else {
switch (tries) {
case 0:
x(drawMan0)
tries = tries + 1
showGuessedWordsFunc(guessedword)
break;
case 1:
x(drawMan1)
tries = tries + 1
showGuessedWordsFunc(guessedword)
break;
case 2:
x(drawMan2)
tries = tries + 1
showGuessedWordsFunc(guessedword)
break;
case 3:
x(drawMan3)
tries = tries + 1
showGuessedWordsFunc(guessedword)
break;
case 4:
x(drawMan4)
tries = tries + 1
showGuessedWordsFunc(guessedword)
break;
default:
break;
}
}
}
else if (guessedword == pickedWord) {
x("Congrats, you guessed right: " + pickedWord)
currentScore += 100
x("your score is:" + currentScore)
playAgain()
}
else {
x("Only one letter allowed at a time!")
newLetterPrompt()
}
}
}
function showGuessedWordsFunc(guessedword) {
passedGuesses = passedGuesses + guessedword
x("guessed letters: " + passedGuesses)
x(tries + "/5 tries")
newLetterPrompt()
}
function playAgain() {
let playagain = prompt("Play again? y/n")
if (playagain == 'y') {
reset()
getRandomWord()
}
else {
x("Ok i guess...")
reset()
AddToHighscore()
}
}
function AddToHighscore() {
const userToFind = currentUser
const filePath = 'highscore.csv';
const csvData = fs.readFileSync(filePath, 'utf8');
const rows = csvData.split('\n').map(row => row.trim());
const columns = rows[0].split(',')
let highscoreObjects = []
for (let i = 1; i < rows.length; i++) {
const rowValues = rows[i].split(',')
const obj = {}
for (let j = 0; j < columns.length; j++) {
obj[columns[j]] = rowValues[j]
}
highscoreObjects.push(obj)
}
let placement = highscoreObjects.findIndex(x => x.username === currentUser)
let obj = highscoreObjects.find(o => o.username === currentUser)
if (obj.score < currentScore) {
x("You broke your previous record!")
highscoreObjects[placement] = { username: currentUser, score: currentScore }
let sortedHighscore = highscoreObjects.slice().sort((a, b) => b.score - a.score);
const header = Object.keys(sortedHighscore[0]).join(',')
const dataRows = sortedHighscore.map(obj => Object.values(obj).join(','))
const csvString = [header, ...dataRows].join('\n')
fs.writeFileSync(filePath, csvString, 'utf8');
x('We updated the highscore.')
highscoreObjects = []
sortedHighscore = []
} else {
x("You didn't break you previous record")
}
currentScore = 0
}
function reset() {
passedGuesses = ""
correctGuesses = ""
tries = 0
pickedWord = ""
tempword = pickedWord
underScore = "-"
guessedword = ''
}