comments for readability
This commit is contained in:
parent
461bb2d68b
commit
ad3f831936
46
src/Game.vue
46
src/Game.vue
@ -1,40 +1,36 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onUnmounted } from 'vue'
|
import { onUnmounted } from 'vue'
|
||||||
import { answers, allWords } from './words'
|
import { getWordOfTheDay, allWords } from './words'
|
||||||
import Keyboard from './Keyboard.vue'
|
import Keyboard from './Keyboard.vue'
|
||||||
import { LetterState } from './types'
|
import { LetterState } from './types'
|
||||||
|
|
||||||
// get word of the day
|
// Get word of the day
|
||||||
const now = new Date()
|
const answer = getWordOfTheDay()
|
||||||
const start = new Date(2022, 0, 0)
|
|
||||||
const diff = Number(now) - Number(start)
|
|
||||||
let day = Math.floor(diff / (1000 * 60 * 60 * 24))
|
|
||||||
while (day > answers.length) {
|
|
||||||
day -= answers.length
|
|
||||||
}
|
|
||||||
const answer = answers[day]
|
|
||||||
|
|
||||||
// board state
|
|
||||||
class Tile {
|
|
||||||
letter = ''
|
|
||||||
state = LetterState.INITIAL
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Board state. Each tile is represented as { letter, state }
|
||||||
const board = $ref(
|
const board = $ref(
|
||||||
Array.from({ length: 6 }, () => {
|
Array.from({ length: 6 }, () =>
|
||||||
return Array.from({ length: 5 }, () => new Tile())
|
Array.from({ length: 5 }, () => ({
|
||||||
})
|
letter: '',
|
||||||
|
state: LetterState.INITIAL
|
||||||
|
}))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
let message = $ref('')
|
// Current active row.
|
||||||
let allowInput = true
|
|
||||||
let currentRowIndex = $ref(0)
|
let currentRowIndex = $ref(0)
|
||||||
let shakeRowIndex = $ref(-1)
|
|
||||||
const currentRow = $computed(() => board[currentRowIndex])
|
const currentRow = $computed(() => board[currentRowIndex])
|
||||||
|
|
||||||
// keep track of revealed letter state for the keyboard
|
// Feedback state: message and shake
|
||||||
|
let message = $ref('')
|
||||||
|
let shakeRowIndex = $ref(-1)
|
||||||
|
|
||||||
|
// Keep track of revealed letters for the virtual keyboard
|
||||||
const letterStates: Record<string, LetterState> = $ref({})
|
const letterStates: Record<string, LetterState> = $ref({})
|
||||||
|
|
||||||
|
// Handle keyboard input.
|
||||||
|
let allowInput = true
|
||||||
|
|
||||||
const onKeyup = (e: KeyboardEvent) => onKey(e.key)
|
const onKeyup = (e: KeyboardEvent) => onKey(e.key)
|
||||||
|
|
||||||
window.addEventListener('keyup', onKeyup)
|
window.addEventListener('keyup', onKeyup)
|
||||||
@ -45,8 +41,8 @@ onUnmounted(() => {
|
|||||||
|
|
||||||
function onKey(key: string) {
|
function onKey(key: string) {
|
||||||
if (!allowInput) return
|
if (!allowInput) return
|
||||||
if (/^[a-z]$/.test(key)) {
|
if (/^[a-zA-Z]$/.test(key)) {
|
||||||
fillTile(key)
|
fillTile(key.toLowerCase())
|
||||||
} else if (key === 'Backspace') {
|
} else if (key === 'Backspace') {
|
||||||
clearTile()
|
clearTile()
|
||||||
} else if (key === 'Enter') {
|
} else if (key === 'Enter') {
|
||||||
|
13
src/words.ts
13
src/words.ts
@ -1,5 +1,16 @@
|
|||||||
|
export function getWordOfTheDay() {
|
||||||
|
const now = new Date()
|
||||||
|
const start = new Date(2022, 0, 0)
|
||||||
|
const diff = Number(now) - Number(start)
|
||||||
|
let day = Math.floor(diff / (1000 * 60 * 60 * 24))
|
||||||
|
while (day > answers.length) {
|
||||||
|
day -= answers.length
|
||||||
|
}
|
||||||
|
return answers[day]
|
||||||
|
}
|
||||||
|
|
||||||
// copied from Wordle source
|
// copied from Wordle source
|
||||||
export const answers = [
|
const answers = [
|
||||||
'cigar',
|
'cigar',
|
||||||
'rebut',
|
'rebut',
|
||||||
'sissy',
|
'sissy',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user