# ---- DEFINE IMPORTS ---- #
from js import console, XMLHttpRequest, Blob
import random
import json
# ---- DEFINE GLOBALS ---- #
WORD_TO_GUESS = ""
GUESS_COUNT = 0
WORD_GUESSED = False
TAG_COLOR_CLASSES = {
"unknown": "is-light",
"match": "is-success",
"wrong": "is-warning"
}
# ---- DEFINE ELEMENTS ---- #
el_task_template = Element("guess-template").select('div', from_content=True)
el_span_template = Element("guess-span-template").select('span', from_content=True)
el_new_guess = Element("guess-input")
el_input_msg = Element("input-message")
el_guess_list = Element("guess-list")
def get_new_word(*args, **kwargs):
"""
get a new word from an api and set the default tileset
"""
global WORD_TO_GUESS
global WORD_GUESSED
# --
word_len = 5
# --
WORD_GUESSED = False
# --
req = XMLHttpRequest.new()
req.open("GET", f"https://random-word-api.herokuapp.com/word?length={word_len}", False)
blob = Blob.new(["{}"], {type : 'application/json'})
req.send(blob)
word = json.loads(req.response)[0]
WORD_TO_GUESS = word
console.log(f"WORD: {WORD_TO_GUESS}")
# --
el_guess_list.element.innerText = ''
el_new_guess.element.innerHTML = ''
el_input_msg.element.style.display = "none";
# --
set_default()
def give_up(*args, **kwargs):
"""
reveal all characters in the word to guess
"""
global WORD_TO_GUESS
global WORD_GUESSED
guessHtml = el_task_template.clone("word-reveal", to=el_guess_list)
# -- color chars
for i,char in enumerate(WORD_TO_GUESS):
new_char_html = el_span_template.clone(f"guess-{i}", to=guessHtml)
new_char_html.element.innerText = char
new_char_html.element.classList.toggle(TAG_COLOR_CLASSES["match"])
guessHtml.element.appendChild(new_char_html.element)
el_guess_list.element.insertBefore(guessHtml.element, el_guess_list.element.firstChild)
WORD_GUESSED = True
def get_score(word, word_to_guess):
"""
utility function for getting a list of states for each character
in a word compared against a word to be guessed. this is used for
knowing what color each tile should be.
"""
result = []
for i, char in enumerate(word):
if char == word_to_guess[i]:
result.append("match")
elif char in word_to_guess:
result.append("wrong")
else:
result.append("unknown")
return result
def guess(*args, **kwargs):
"""
make a new guess and set the tiles with colors for the guess
"""
# --
global GUESS_COUNT
global WORD_GUESSED
global WORD_TO_GUESS
# --
next_guess_id = GUESS_COUNT+1
guess = str(el_new_guess.element.value).lower()
if WORD_GUESSED == True:
return
if guess == "":
el_input_msg.element.innerHTML = f"Get a new word to guess first!"
el_input_msg.element.style.display = "block";
if len(guess) > len(WORD_TO_GUESS):
el_input_msg.element.innerHTML = f"Guess is too long! Word must be {len(WORD_TO_GUESS)} characters"
el_input_msg.element.style.display = "block";
else:
# -- get guess score
results = get_score(guess, WORD_TO_GUESS)
guessHtml = el_task_template.clone(next_guess_id, to=el_guess_list)
# -- color chars
for i,char in enumerate(results):
new_char_html = el_span_template.clone(f"guess-{i}", to=guessHtml)
new_char_html.element.innerText = guess[i]
new_char_html.element.classList.toggle(TAG_COLOR_CLASSES[results[i]])
guessHtml.element.appendChild(new_char_html.element)
el_guess_list.element.insertBefore(guessHtml.element, el_guess_list.element.firstChild)
GUESS_COUNT += 1
# --
if guess == WORD_TO_GUESS:
WORD_GUESSED = True
def set_default(*args, **kwargs):
"""
set default blank tiles for a new word
"""
# -- get word
word = WORD_TO_GUESS
# --
guessHtml = el_task_template.clone("0", to=el_guess_list)
for i, char in enumerate(word):
new_char_html = el_span_template.clone(f"default-{i}", to=guessHtml)
new_char_html.element.innerText = "_"
guessHtml.element.appendChild(new_char_html.element)
el_guess_list.element.appendChild(guessHtml.element)
Pydle
Pydle is a simple Wordle implementation with the newly released Pyscript framework from Anaconda Inc. First generate a new word, then fill out a guess and submit with the green button.