wordle.py

Created by loic-azavant

Created on February 20, 2023

5.92 KB

Un wordle, c’est vous qui choisissez le mot, dans les parenthèses, a le derniere ligne du script. Necessite le script ion_plus.py


from kandinsky import *
from ion import *
from time import *
from ion_plus import *

def is_vowel(letter):
  if letter in "aeiouy":
    return True
  return False

def is_valid(word):
  nb_vowel = 0
  for i in word:
    if is_vowel(i.lower()):
      nb_vowel += 1
  if nb_vowel < 1 or nb_vowel==5:
    return False
  return True

def drw_rect(x,y,w,h,c):
  draw_line(x,y,x+w,y,c)
  draw_line(x,y,x,y+h,c)
  draw_line(x+w,y,x+w,y+h,c)
  draw_line(x,y+h,x+w+1,y+h,c)

def drw_sprite(nbr,col,x,y,size,c=(0,0,0)):
  for i in range(len(bin(nbr))-2):
    if nbr >> i & 1:
      fill_rect(x+(i%col)*size,y+(i//col)*size,size,size,c)

enter = 1133596484486336
erase = 9088428051582814712

keyboard = "AZERTYUIOPQSDFGHJKLMWXCVBN"

palette = (((160,160,160),(100,100,100)),
           ((0,200,0),(0,150,0)),
           ((200,200,0),(200,130,0)),
           ((100,100,100),(100,100,100)))

letters_state = [0 for i in range(26)]

cursor = [0,0]

def drw_cursor(c):
  if cursor[1]==2:
    if cursor[0]<2:
      drw_rect(61,187,37,31,c)
      return
    if cursor[0]>7:
      drw_rect(221,187,37,31,c)
      return
  drw_rect(61+cursor[0]*20,123+cursor[1]*32,17,31,c)

def move_cursor(x,y):
  cursor[0] = max(0,min(9,cursor[0]+x))
  cursor[1] = max(0,min(2,cursor[1]+y))

def draw_letter(letter,x,y,col):
  fill_rect(x,y+25,14,2,palette[col][1])
  fill_rect(x+1,y+26,12,2,palette[col][1])
  fill_rect(x,y+1,14,24,palette[col][0])
  fill_rect(x+1,y,12,26,palette[col][0])
  draw_string(letter,x+2,y+5,(255,)*3,(palette[col][0]))

def drw_keyboard():
  for i in range(26):
    draw_letter(keyboard[i],63+(i%10)*20+40*(i>19),125+32*(i>9)+32*(i>19),letters_state[i])
  
  fill_rect(63,189+25,34,2,palette[0][1])
  fill_rect(64,189+26,32,2,palette[0][1])
  fill_rect(63,190,34,24,palette[0][0])
  fill_rect(64,189,32,26,palette[0][0])
  drw_sprite(enter,8,72,196,2,(255,)*3)

  fill_rect(223,189+25,34,2,palette[0][1])
  fill_rect(224,189+26,32,2,palette[0][1])
  fill_rect(223,190,34,24,palette[0][0])
  fill_rect(224,189,32,26,palette[0][0])
  drw_sprite(erase,9,231,196,2,(255,)*3)

def drw_panel(word):
  a = 160-(len(word)*21)//2
  for i in range(5):
    for j in range(len(word)):
      fill_rect(a+j*21,5+i*23,20,18,(0,)*3)
      fill_rect(a+1+j*21,4+i*23,18,20,(0,)*3)
      fill_rect(a+1+j*21,5+i*23,18,18,(255,)*3)

def drw_current_word(word,word_to_find,y):
  a = 160-(len(word_to_find)*21)//2
  for i in range(len(word_to_find)):
    fill_rect(a+1+i*21,5+y*23,18,18,(255,)*3)
  for i in range(len(word)):
    draw_string(word[i],a+5+i*21,5+y*23)

def current_word_is_wrong(word,word_to_find,y):
  a = 160-(len(word_to_find)*21)//2
  for i in range(len(word_to_find)):
    fill_rect(a+1+i*21,5+y*23,18,18,(255,)*3)
  for j in range(5):
    for i in range(len(word)):
      fill_rect(a+1+i*21,5+y*23,18,18,(255,)*3)
      draw_string(word[i],a+5+i*21-3*(j%2),5+y*23)
    sleep(0.05)

def check_words(word1,word2,y):
  new_states = []
  for i in range(len(word2)):
    if word2[i] in word1:
      if word2[i] == word1[i]:
        new_states.append([keyboard.index(word2[i]),1])
      else:
        new_states.append([keyboard.index(word2[i]),2])
    else:
      new_states.append([keyboard.index(word2[i]),3])
  for i in range(len(new_states)):
    for j in range(i+1,len(new_states)):
      if new_states[i][0] == new_states[j][0]:
        if word1.count(keyboard[new_states[i][0]]) < new_states.count([new_states[i][0],1])+new_states.count([new_states[i][0],2]):
          if new_states[j][1] == 1:
            new_states[i][1] = 3
          else:
            new_states[j][1] = 3
  a = 160-(len(word1)*21)//2
  for i in range(len(word1)):
    fill_rect(a+1+i*21,5+y*23,18,18,palette[new_states[i][1]][0])
    draw_string(keyboard[new_states[i][0]],a+5+i*21,5+y*23,(255,)*3,palette[new_states[i][1]][0])
  for i in new_states:
    if letters_state[i[0]] == 0 or letters_state[i[0]] == 3 or (i[1] == 1 and letters_state[i[0]] == 2):
      letters_state[i[0]] = i[1]
  drw_keyboard()


def remove_last_letter(word):
  if len(word) > 0:
    del word[-1]

def enter_word(current_word,word_to_find,word_index,limit):
  if len(current_word) != len(word_to_find):
    current_word_is_wrong(current_word,word_to_find,word_index)
    return False
  if not is_valid(current_word) and limit:
    current_word_is_wrong(current_word,word_to_find,word_index)
    return False
  check_words(word_to_find,current_word,word_index)
  if "".join(current_word) == word_to_find:
    while 1: pass
  if word_index==4:
    draw_string("PERDU",135,60)
    draw_string("LE MOT ETAIT",100,85)
    draw_string(str(word_to_find),160-(len(word_to_find)//2)*10,110)
    while 1: pass
  current_word.clear()
  return True

def play_wordle_with_the_word(word,limit=True):
  if len(word)>5 and limit:
    print("Le mot est trop long")
    return
  if len(word)<5 and limit:
    print("Le mot est trop court")
    return
  if not is_valid(word) and limit:
    print("Le mot n'est pas valide")
    return
  word_to_find = word.upper()
  word_index = 0
  current_word = []
  
  drw_keyboard()
  drw_cursor((0,0,0))
  drw_panel(word_to_find)
  while 1:
    moveH = keydowntimed(3)-keydowntimed(0)
    moveV = keydowntimed(2)-keydowntimed(1)
    if moveH != 0 or moveV != 0:
      drw_cursor((255,)*3)
      move_cursor(moveH,moveV)
      drw_cursor((0,)*3)
    
    if keyinput(KEY_BACKSPACE):
      remove_last_letter(current_word)
      drw_current_word(current_word,word_to_find,word_index)
    
    if keyinput(KEY_EXE):
      word_index += enter_word(current_word,word_to_find,word_index,limit)
      
    if keyinput(4):
      if not (cursor[1]==2 and (cursor[0]<2 or cursor[0]>7)):
        if len(word_to_find)>len(current_word):
          current_word.append(keyboard[cursor[1]*10+cursor[0]-2*(cursor[1]==2)])
      elif cursor[0] > 7:
        remove_last_letter(current_word)
      else:
        word_index += enter_word(current_word,word_to_find,word_index,limit)
      drw_current_word(current_word,word_to_find,word_index)

play_wordle_with_the_word("unmottreslong",limit=False)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.