2022-01-27 18:36:45 +00:00
|
|
|
// global constants
|
2022-09-12 13:00:06 +00:00
|
|
|
const gridWidth = 6
|
2022-01-27 18:36:45 +00:00
|
|
|
const gridHeight = 6
|
|
|
|
const word = wordlist[Math.floor(Math.random()*wordlist.length)];
|
|
|
|
|
|
|
|
// global state variables
|
|
|
|
var gameCompleted = null
|
|
|
|
|
|
|
|
// HTML items
|
2022-01-27 20:20:51 +00:00
|
|
|
const body = document.getElementsByTagName("body")[0]
|
|
|
|
const endScreen = document.getElementById("end_screen")
|
|
|
|
const endScreenGuesses = document.getElementById("end_screen_guesses")
|
|
|
|
const endScreenMessage = document.getElementById("end_screen_message")
|
2022-01-28 02:00:55 +00:00
|
|
|
const endScreenImage = document.getElementById("end_screen_image")
|
2022-01-27 20:20:51 +00:00
|
|
|
const endScreenTime = document.getElementById("end_screen_time")
|
2022-01-28 00:30:13 +00:00
|
|
|
const gameContainer = document.getElementById("game_container")
|
2022-01-27 18:36:45 +00:00
|
|
|
const gridItems = []
|
2022-01-28 00:30:13 +00:00
|
|
|
const kbLetters = Array.from(document.getElementsByClassName("kb_letter")).filter(kbl => kbl.id === '')
|
|
|
|
const kbEnter = document.getElementById("kb_enter")
|
|
|
|
const kbBackspace = document.getElementById("kb_backspace")
|
2022-01-27 18:36:45 +00:00
|
|
|
|
|
|
|
// helper functions
|
|
|
|
const gridItemId = (row, column) => "grid_item_" + row + "_" + column
|
|
|
|
const getGridRow = (row) => gridItems[row]
|
|
|
|
const getGridItem = (row, column) => gridItems[row].items[column]
|
2022-01-28 02:23:55 +00:00
|
|
|
const getkbLetter = letter => kbLetters.filter(kbl => kbl.innerHTML.toLowerCase() === letter.toLowerCase())[0]
|
2022-01-27 18:36:45 +00:00
|
|
|
const isLetter = string => /^[a-zA-Z]$/.test(string)
|
|
|
|
|
|
|
|
|
|
|
|
function setGridLetter(row, column, letter) {
|
|
|
|
console.log([row, column])
|
|
|
|
console.log(letter)
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
getGridItem(row, column).letter = letter
|
|
|
|
getGridItem(row, column).HTMLItem.getElementsByClassName("grid_item_p")[0].innerHTML = letter !== null ? letter : ""
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function checkGridRow() {
|
|
|
|
var [row, _ ] = findCurrentLetterPosition()
|
|
|
|
var rowWord = ''
|
|
|
|
getGridRow(row).items.forEach(item => rowWord += item.letter)
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
console.log(rowWord)
|
2022-01-27 20:18:29 +00:00
|
|
|
if (!wordlist.includes(rowWord)) {
|
|
|
|
body.classList.add("incorrect")
|
|
|
|
setTimeout( () => body.classList.remove("incorrect"), 500)
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
var correct_letters = 0
|
|
|
|
var dedupword = word
|
|
|
|
|
|
|
|
for (var column = 0; column < gridWidth; column++) {
|
|
|
|
gridItem = getGridItem(row, column)
|
|
|
|
// letters are removed from dedupword as letters are matched
|
|
|
|
if (gridItem.letter === null) return
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
if (gridItem.letter === word[column]) {
|
|
|
|
gridItem.HTMLItem.classList.add('grid_correct')
|
2022-01-28 02:23:55 +00:00
|
|
|
getkbLetter(gridItem.letter).classList.add('kb_letter_correct_discovered')
|
2022-01-27 18:36:45 +00:00
|
|
|
correct_letters += 1
|
|
|
|
} else if (dedupword.includes(gridItem.letter)) {
|
|
|
|
gridItem.HTMLItem.classList.add('grid_present')
|
2022-01-28 02:23:55 +00:00
|
|
|
getkbLetter(gridItem.letter).classList.add('kb_letter_present_discovered')
|
2022-01-27 18:36:45 +00:00
|
|
|
} else {
|
|
|
|
gridItem.HTMLItem.classList.add('grid_wrong')
|
2022-01-28 02:23:55 +00:00
|
|
|
getkbLetter(gridItem.letter).classList.add('kb_letter_incorrect_discovered')
|
2022-01-27 18:36:45 +00:00
|
|
|
}
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
dedupword = dedupword.replace(gridItem.letter, '')
|
|
|
|
console.log(gridItem.letter)
|
|
|
|
console.log(dedupword)
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-28 02:07:32 +00:00
|
|
|
if (correct_letters == gridWidth) {
|
|
|
|
endGame(true)
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 18:36:45 +00:00
|
|
|
}
|
2022-01-27 20:20:51 +00:00
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
getGridRow(row).complete = true
|
2022-01-27 20:20:51 +00:00
|
|
|
if (row === gridHeight-1) endGame(false)
|
2022-01-27 18:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function init(){
|
|
|
|
// clear game div
|
|
|
|
gameContainer.innerHTML = ""
|
|
|
|
// build letter grid and populate gridItems
|
|
|
|
const letterGrid = document.createElement("div")
|
|
|
|
letterGrid.setAttribute("id", "letter_grid")
|
|
|
|
|
|
|
|
for (var row = 0; row < gridHeight; row++) {
|
|
|
|
var gridRow = document.createElement('div')
|
|
|
|
gridRow.classList.add('grid_row')
|
|
|
|
gridItems.push({
|
|
|
|
items: [],
|
|
|
|
complete: false,
|
|
|
|
})
|
|
|
|
for (var column = 0; column < gridWidth; column++) {
|
|
|
|
var gridItem = document.createElement('div')
|
|
|
|
gridItem.classList.add('grid_item')
|
|
|
|
gridItem.setAttribute('id', gridItemId(row, column))
|
|
|
|
|
|
|
|
var gridItemP = document.createElement('p')
|
|
|
|
gridItemP.classList.add('grid_item_p')
|
|
|
|
gridItem.appendChild(gridItemP)
|
|
|
|
gridRow.appendChild(gridItem)
|
|
|
|
|
|
|
|
gridItems[row].items.push({
|
|
|
|
HTMLItem: gridItem,
|
|
|
|
letter: null,
|
|
|
|
correct: null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
letterGrid.appendChild(gridRow)
|
|
|
|
}
|
|
|
|
|
|
|
|
gameContainer.appendChild(letterGrid)
|
|
|
|
gameCompleted = false
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function findNextLetterPosition() {
|
|
|
|
for (var row = 0; row < gridHeight; row++) {
|
|
|
|
for (var column = 0; column < gridWidth; column++) {
|
|
|
|
if (getGridItem(row, column).letter === null) return [row, column]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function findCurrentLetterPosition() {
|
|
|
|
var lastRow = 0
|
|
|
|
var lastColumn = 0
|
|
|
|
|
|
|
|
for (var row = 0; row < gridHeight; row++) {
|
|
|
|
for (var column = 0; column < gridWidth; column++) {
|
|
|
|
if (getGridItem(row, column).letter === null) return [lastRow, lastColumn]
|
|
|
|
lastRow = row
|
|
|
|
lastColumn = column
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [lastRow, lastColumn]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setNextLetter(key) {
|
|
|
|
if (gameCompleted) return
|
|
|
|
|
|
|
|
var [currentRow, currentColumn] = findCurrentLetterPosition()
|
|
|
|
if (key.code === "Backspace") {
|
|
|
|
if (currentColumn === -1 || getGridRow(currentRow).complete) return
|
|
|
|
|
|
|
|
setGridLetter(currentRow, currentColumn, null)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.code === "Enter") {
|
|
|
|
checkGridRow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLetter(key.key)) {
|
|
|
|
if (key.altKey || key.ctrlKey || key.metaKey) return
|
|
|
|
var [nextRow, nextColumn] = findNextLetterPosition()
|
|
|
|
if (currentRow !== nextRow && !getGridRow(currentRow).complete) return
|
|
|
|
setGridLetter(nextRow, nextColumn, key.key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 20:56:18 +00:00
|
|
|
|
2022-01-27 20:20:51 +00:00
|
|
|
function endGame(won) {
|
|
|
|
gameCompleted = true
|
|
|
|
|
|
|
|
if (won) {
|
|
|
|
endScreenMessage.innerHTML = "niceeeeeeeee"
|
2022-01-28 02:00:55 +00:00
|
|
|
endScreenImage.src = "trophy.svg"
|
2022-01-27 20:20:51 +00:00
|
|
|
} else {
|
2022-02-04 14:51:52 +00:00
|
|
|
endScreenMessage.innerHTML = `damn. better luck nex time <br>(the word was ${word})`
|
2022-01-28 02:00:55 +00:00
|
|
|
endScreenImage.src = "rusty_medal.svg"
|
2022-01-27 20:20:51 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 00:30:13 +00:00
|
|
|
endScreenGuesses.innerHTML = `guesses used: ${findCurrentLetterPosition()[0]+1}/${gridHeight}`
|
2022-01-27 20:20:51 +00:00
|
|
|
endScreen.style.display = "";
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
|
2022-01-28 00:30:13 +00:00
|
|
|
function runClickAnimation(el) {
|
|
|
|
console.log(el)
|
|
|
|
el.classList.add("clicked")
|
|
|
|
setTimeout(() => el.classList.remove("clicked"), 500)
|
|
|
|
}
|
2022-09-12 13:00:06 +00:00
|
|
|
|
|
|
|
|
2022-01-27 18:36:45 +00:00
|
|
|
// create event listeners
|
2022-01-28 00:30:13 +00:00
|
|
|
document.addEventListener('keyup', e => setNextLetter(e))
|
2022-09-12 13:00:06 +00:00
|
|
|
|
2022-01-28 00:30:13 +00:00
|
|
|
kbBackspace.addEventListener('click', () => {
|
|
|
|
runClickAnimation(kbBackspace)
|
|
|
|
setNextLetter({code: "Backspace", key: "Backspace"})
|
|
|
|
})
|
2022-09-12 13:00:06 +00:00
|
|
|
|
2022-01-28 00:30:13 +00:00
|
|
|
kbEnter.addEventListener('click', () => {
|
|
|
|
runClickAnimation(kbEnter)
|
|
|
|
setNextLetter({code: "Enter", key: "Enter"})
|
|
|
|
})
|
2022-09-12 13:00:06 +00:00
|
|
|
|
2022-01-28 00:30:13 +00:00
|
|
|
kbLetters.forEach(kbl => {
|
|
|
|
kbl.addEventListener('click', () => {
|
|
|
|
runClickAnimation(kbl)
|
|
|
|
setNextLetter({code: kbl.innerHTML, key: kbl.innerHTML })
|
|
|
|
})
|
2022-01-27 18:36:45 +00:00
|
|
|
})
|
|
|
|
|
2022-09-12 13:00:06 +00:00
|
|
|
|
2022-01-28 00:30:13 +00:00
|
|
|
// start game
|
2022-01-27 20:20:51 +00:00
|
|
|
init()
|
2022-01-28 00:30:13 +00:00
|
|
|
console.log(word)
|