|
| 1 | +import * as worldContent from "@/server/demo/wordle/index.md"; |
| 2 | +import { Head } from "@/ui/head"; |
| 3 | +import { clsx } from "clsx"; |
| 4 | +import { Context, Get, Post } from "ovr"; |
| 5 | + |
| 6 | +const solution = "CRANE"; |
| 7 | +const length = 5; |
| 8 | +const tries = 6; |
| 9 | + |
| 10 | +export const wordle = new Get("/demo/wordle", (c) => { |
| 11 | + c.head(<Head {...worldContent.frontmatter} />); |
| 12 | + |
| 13 | + const guesses = getGuesses(); |
| 14 | + const solved = guesses.includes(solution); |
| 15 | + const ended = solved || guesses.length === tries; |
| 16 | + |
| 17 | + return ( |
| 18 | + <div class="flex flex-col items-center justify-center gap-6"> |
| 19 | + <Board guesses={guesses} /> |
| 20 | + |
| 21 | + {ended && ( |
| 22 | + <div> |
| 23 | + {solved ? ( |
| 24 | + <span class="text-green-600">You got it in {guesses.length}!</span> |
| 25 | + ) : ( |
| 26 | + <span class="text-red-600"> |
| 27 | + Out of tries! The word was <b>{solution}</b>. |
| 28 | + </span> |
| 29 | + )} |
| 30 | + </div> |
| 31 | + )} |
| 32 | + |
| 33 | + {!ended && ( |
| 34 | + <guess.Form search class="flex gap-2" enctype="multipart/form-data"> |
| 35 | + <input type="hidden" name="id" value="id1" /> |
| 36 | + <input type="hidden" name="id" value="id2" /> |
| 37 | + <input type="file" name="file" /> |
| 38 | + <input type="text" name="text=" /> |
| 39 | + <input |
| 40 | + type="text" |
| 41 | + maxlength={length} |
| 42 | + name="guess" |
| 43 | + pattern={`[A-Za-z]{${length}}`} |
| 44 | + class={clsx("w-24 text-center font-mono tracking-widest uppercase")} |
| 45 | + placeholder="GUESS" |
| 46 | + aria-label="Enter guess" |
| 47 | + /> |
| 48 | + <button>Guess</button> |
| 49 | + </guess.Form> |
| 50 | + )} |
| 51 | + </div> |
| 52 | + ); |
| 53 | +}); |
| 54 | + |
| 55 | +export const guess = new Post("/demo/guess", async (c) => { |
| 56 | + for await (const part of c.data()) { |
| 57 | + console.log(part); |
| 58 | + } |
| 59 | + |
| 60 | + return <p>Test</p>; |
| 61 | + |
| 62 | + // const guesses = getGuesses(); |
| 63 | + // const data = await c.req.formData(); |
| 64 | + // const guess = z.string().toUpperCase().length(5).parse(data.get("guess")); |
| 65 | + |
| 66 | + // if (guesses.length < tries) guesses.push(guess); |
| 67 | + |
| 68 | + // c.redirect(wordle.url({ search: [].map((guess) => ["guess", guess]) })); |
| 69 | +}); |
| 70 | + |
| 71 | +const getGuesses = () => Context.get().url.searchParams.getAll("guess"); |
| 72 | + |
| 73 | +const Board = ({ guesses }: { guesses: string[] }) => { |
| 74 | + return ( |
| 75 | + <div class="grid gap-1"> |
| 76 | + {Array.from({ length: tries }).map((_, i) => ( |
| 77 | + <Row |
| 78 | + word={guesses[i] ?? ""} |
| 79 | + solution={solution} |
| 80 | + isCurrent={!guesses[i] && i === guesses.length} |
| 81 | + /> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +const feedback = (guess: string, solution: string) => { |
| 88 | + const res: ("g" | "y" | "b")[] = Array(length).fill("b"); |
| 89 | + const sol = solution.split(""); |
| 90 | + const flag = Array<boolean>(length).fill(false); |
| 91 | + |
| 92 | + for (let i = 0; i < length; i++) { |
| 93 | + if (guess[i] === sol[i]) { |
| 94 | + res[i] = "g"; |
| 95 | + flag[i] = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for (let i = 0; i < length; i++) { |
| 100 | + if (res[i] === "g") continue; |
| 101 | + |
| 102 | + const foundLetter = sol.findIndex((ch, j) => !flag[j] && ch === guess[i]); |
| 103 | + if (foundLetter !== -1) { |
| 104 | + res[i] = "y"; |
| 105 | + flag[foundLetter] = true; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return res; |
| 110 | +}; |
| 111 | + |
| 112 | +const Row = ({ |
| 113 | + word, |
| 114 | + solution, |
| 115 | + isCurrent, |
| 116 | +}: { |
| 117 | + word: string; |
| 118 | + solution: string; |
| 119 | + isCurrent?: boolean; |
| 120 | +}) => { |
| 121 | + const cells = Array.from({ length }).map((_, i) => word[i] ?? ""); |
| 122 | + const colors = |
| 123 | + word.length === length && !isCurrent ? feedback(word, solution) : []; |
| 124 | + return ( |
| 125 | + <div class="flex gap-1"> |
| 126 | + {cells.map((ch, i) => ( |
| 127 | + <Cell ch={ch} color={colors[i]} isCurrent={isCurrent} /> |
| 128 | + ))} |
| 129 | + </div> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +const Cell = ({ |
| 134 | + ch, |
| 135 | + color, |
| 136 | + isCurrent, |
| 137 | +}: { |
| 138 | + ch: string; |
| 139 | + color?: "g" | "y" | "b"; |
| 140 | + isCurrent?: boolean; |
| 141 | +}) => { |
| 142 | + return ( |
| 143 | + <div |
| 144 | + class={clsx( |
| 145 | + "flex h-12 w-12 items-center justify-center rounded border font-mono text-2xl font-bold shadow-sm select-none", |
| 146 | + color === "g" && "text-base-50 border-green-600 bg-green-500", |
| 147 | + color === "y" && "text-base-950 border-yellow-500 bg-yellow-400", |
| 148 | + color === "b" && "border-base-400 bg-base-300 text-base-950", |
| 149 | + !color && !isCurrent && "border-base-200 bg-background", |
| 150 | + )} |
| 151 | + > |
| 152 | + {ch} |
| 153 | + </div> |
| 154 | + ); |
| 155 | +}; |
0 commit comments