wordle.py

Created by ziii

Created on November 24, 2023

2.08 KB

A basic implementation of the wordle game. There is obviously no room for a dictionnary on a calculator so there are no checks for the spelling, and you’ll need to ask a friend to give you a word.

when executing the script enter a word in the console, you’ll then see the play screen, just press the keys with the letters you want, alpha or shift mode doesn’t matter.


from time import monotonic as now
from ion import keydown
from kandinsky import fill_rect as f,draw_string as d
def draw_char(c,n_g,i,l_a,col1,col2):
  d(c,(32-l_a)*5+10*i,n_g*20+2,col1,col2)
def under_letter(n_g,i,l_a,col,backcol=None):
  if backcol != None:
    f((32-l_a)*5+10*i,n_g*20+2,10,18,backcol)
  f((32-l_a)*5+10*i+1,n_g*20+18,8,2,col)
BACKGROUND=(31,32,33)
TEXT=(220,220,220)
RIGHT=(74,202,0)
IN_WORD=(252,252,51)
answer=input("enter a word : \n")
l_a=len(answer)
assert l_a > 0, "entered nothing"
c_count=[0 for _ in range(26)]
for i in range(l_a):  
  if 122>ord(answer[i])>96:
    answer=answer[:i]+chr(ord(answer[i])-32)+answer[i+1:]
  elif not 90>ord(answer[i])>64:
    assert False, "not a valid word"
  c_count[ord(answer[i])-65]+=1
n_guessed=0
guess=""
t=now()
dt=0.25
ended=False
f(0,0,320,222,BACKGROUND)
for i in range(l_a):
  under_letter(0,i,l_a,TEXT)
while not ended:
  if now()-t>dt:
    for i in range(18,46):
      if keydown(i):
        t=now()
        c=chr( i+47 - (i>35) - (i>40) )
        draw_char(c,n_guessed,len(guess),l_a,TEXT,BACKGROUND)
        guess+=c
        if len(guess)==l_a:
          c_count_copy=[i for i in c_count]
          for i in range(l_a):
            if answer[i]==guess[i]:
              under_letter(n_guessed,i,l_a,RIGHT)
              c_count_copy[ord(guess[i])-65]-=1
          for i in range(l_a):
            if answer[i]!=guess[i] and c_count_copy[ord(guess[i])-65]>0:
              under_letter(n_guessed,i,l_a,IN_WORD)
              c_count_copy[ord(guess[i])-65]-=1
          n_guessed+=1
          if guess==answer:
            d("CORRECT !",115,n_guessed*20,TEXT,BACKGROUND)
            ended=True
          elif n_guessed==6:
            d("YOU LOST ! ANSWER :",65,120,TEXT,BACKGROUND)
            d("{}".format(answer),(32-l_a)*5,140,TEXT,BACKGROUND)
            ended=True
          else:
            for i in range(l_a):
              under_letter(n_guessed,i,l_a,TEXT)
          guess=""
        break
    else:
      if keydown(17):
        t=now()
        guess=guess[:-1]
        under_letter(n_guessed,len(guess),l_a,TEXT,BACKGROUND)