From 36ed928dc5bb067d5bded300719b3aad26649164 Mon Sep 17 00:00:00 2001 From: RezHackXYZ Date: Sat, 3 May 2025 14:13:37 +0530 Subject: [PATCH] mvp MADE! --- src/wordle/display.svelte | 20 ++++---- src/wordle/game.svelte | 35 +++++++------ src/wordle/keyboard.svelte | 14 +++--- src/wordle/logic.svelte.js | 100 ++++++++++++++++++++++++++++++++++--- 4 files changed, 130 insertions(+), 39 deletions(-) diff --git a/src/wordle/display.svelte b/src/wordle/display.svelte index ea5562f..6dfa49a 100644 --- a/src/wordle/display.svelte +++ b/src/wordle/display.svelte @@ -1,8 +1,8 @@ -
+
{#each words as word}
{#each word as letter} @@ -11,16 +11,14 @@
{/each}
- {CurrentWord.v[0]} - {CurrentWord.v[1]} - {CurrentWord.v[2]} - {CurrentWord.v[3]} - {CurrentWord.v[4]} + {#each Array(WordLegnth) as _, i} + {CurrentWord.v[i] || ''} + {/each}
diff --git a/src/wordle/game.svelte b/src/wordle/game.svelte index c1e540a..756b720 100644 --- a/src/wordle/game.svelte +++ b/src/wordle/game.svelte @@ -1,22 +1,27 @@
- - + +
\ No newline at end of file + #root { + width: 885px; + height: calc(100% - 75px); + border-radius: 20px; + margin: 20px; + margin-top: 5px; + border: 2px solid #444; + font-family: "JetBrains Mono", monospace; + } + diff --git a/src/wordle/keyboard.svelte b/src/wordle/keyboard.svelte index db77449..fa8413d 100644 --- a/src/wordle/keyboard.svelte +++ b/src/wordle/keyboard.svelte @@ -13,15 +13,15 @@
{#each keys.slice(10, 19) as key} - ButtonPressed(key[0])} class={key[1]} + >{key[0]} {/each}
{#each keys.slice(19) as key} - ButtonPressed(key[0])} class={key[1]} + >{key[0]} {/each}
@@ -43,9 +43,9 @@ } button { - background-color: #202020; + background: none; font-family: "JetBrains Mono", monospace; - color: #808080; + color: #888; width: 80px; height: 70px; display: flex; @@ -66,6 +66,6 @@ background-color: #2b2b2b; } .o { - background-color: #202829; + background-color: #00202c; } diff --git a/src/wordle/logic.svelte.js b/src/wordle/logic.svelte.js index 4808761..4b1beb8 100644 --- a/src/wordle/logic.svelte.js +++ b/src/wordle/logic.svelte.js @@ -1,7 +1,11 @@ import wordExists from "word-exists"; -import {generate} from "random-words"; +import { generate } from "random-words"; +import { onMount } from "svelte"; -export let CorrectWord = generate({ minLength: 5, maxLength: 5 }); +export let WordLegnth = 5; + +let CorrectWord = generate({ minLength: WordLegnth, maxLength: WordLegnth }); +console.log("CorrectWord: ", CorrectWord); export let words = $state([]); export let CurrentWord = $state({ v: [] }); export let keys = $state([ @@ -35,12 +39,85 @@ export let keys = $state([ ["⏎", "o"], ]); -function SendWord(word) {} +function GameWin() { + alert("You win!"); + document.location.reload(); +} + +function SendWord(word) { + let result = Array(word.length).fill(null); + let used = Array(CorrectWord.length).fill(false); + + // First pass: exact matches + for (let i = 0; i < word.length; i++) { + if (word[i].toLowerCase() === CorrectWord[i].toLowerCase()) { + result[i] = [word[i].toUpperCase(), "c"]; + used[i] = true; + } + } + + // Second pass: wrong place but correct letter + for (let i = 0; i < word.length; i++) { + if (result[i]) continue; + + let found = false; + for (let j = 0; j < CorrectWord.length; j++) { + if ( + !used[j] && + word[i].toLowerCase() === CorrectWord[j].toLowerCase() + ) { + found = true; + used[j] = true; + break; + } + } + + result[i] = found + ? [word[i].toUpperCase(), "d"] + : [word[i].toUpperCase(), "w"]; + } + + words.push(result); + + setTimeout(() => { + document.getElementById("DisplayOfWords").scrollTo({ + top: document.getElementById("DisplayOfWords").scrollHeight, + behavior: "smooth", + }); + }, 100); + + // Update keyboard status + for (let [letter, status] of result) { + let keyIndex = keys.findIndex( + (k) => k[0].toLowerCase() === letter.toLowerCase() + ); + if (keyIndex !== -1) { + let current = keys[keyIndex][1]; + if (status === "c") { + keys[keyIndex][1] = "c"; + } else if (status === "d" && current === "n") { + keys[keyIndex][1] = "d"; + } else if (status === "w" && current === "n") { + keys[keyIndex][1] = "w"; + } + } + } + + // Check for win + if (result.every(([_, status]) => status === "c")) { + GameWin(); + } +} export function ButtonPressed(key) { + document.getElementById("DisplayOfWords").scrollTo({ + top: document.getElementById("DisplayOfWords").scrollHeight, + behavior: "smooth", + }); + if (key === "⏎") { - if (CurrentWord.v.length === 5) { - let word = CurrentWord.v.join(""); + if (CurrentWord.v.length === WordLegnth) { + let word = CurrentWord.v.join("").toUpperCase(); if (wordExists(word)) { SendWord(CurrentWord.v); CurrentWord.v = []; @@ -53,9 +130,20 @@ export function ButtonPressed(key) { CurrentWord.v.pop(); return; } - if (CurrentWord.v.length === 5) { + if (CurrentWord.v.length === WordLegnth) { return; } CurrentWord.v.push(key); } +export function handleKey(event) { + const key = event.key.toLowerCase(); + + if (key === "enter") { + ButtonPressed("⏎"); + } else if (key === "backspace") { + ButtonPressed("⌫"); + } else if (/^[a-z]$/.test(key)) { + ButtonPressed(key.toUpperCase()); + } +}