Automatic Wordle Solver with Python and Turtle (Source Code Included)

Wordle is a very popular puzzle game. Here is the source code that automatically solves the wordle problems. There isn’t too much trick. This code just randomly picks a valid word. Click on the letters to match the colors on wordle and then hit enter. This program will automatically guess new words until the word has been guessed correctly.

import random
import turtle
from tkinter import * 
from tkinter import messagebox

screen = turtle.Screen()
screen.setup(1000,1000)
screen.title("Wordle Solver - PythonTurtle.Academy")
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)
screen.bgcolor('black')
turtle.color('white')

gs = 0
state = []
for i in range(6):
  state.append([-1]*5)

def getwords(words, cs, count=False):
  res = []
  cnt = 0
  for w in words:
    t = list(w)
    flag = True
    cnt = dict()
    # first loop checks only positions are set
    for l,p in cs:
      if p<0: continue
      if cs[(l,p)] > 0:
        if t[p] == l:
          t[p] = '*'
          if l in cnt: cnt[l] += 1
          else: cnt[l] = 1
        else:
          flag = False
          break
      else:
      	if t[p] == l:
      		flag = False
      		break
    if (not flag): continue
    # second loop checks only positions are not set
    for l,p in cs:
      if p!=-1: continue
      v = 0 if l not in cnt else cnt[l]
      for _ in range(cs[(l,p)]-v):
      	try:
      		p = t.index(l)
      		t[p] = '*'
      	except ValueError:
      		flag = False
      		break
      if (not flag): break
    if (not flag): continue
    # third loops checks non-existent letter
    for l,p in cs:
      if p!=-2: continue
      if l in t:
        flag = False
        break
    
    if flag: 
    	if count: cnt += 1
    	else: res.append(w)
  if count: return cnt
  else: return res

def guess_random(words):
  return random.choice(words)
		
def draw_square(coord,s,fc='black'):
  turtle.up()
  x = coord[0]
  y = coord[1]
  turtle.goto(x-s/2,y-s/2)
  turtle.seth(0)
  turtle.down()
  turtle.fillcolor(fc)
  turtle.begin_fill()
  for _ in range(4):
    turtle.fd(s)
    turtle.left(90)
  turtle.end_fill()

def get_coord(i,j):
  return -200+100*j, 300-100*i

def draw_board():
  turtle.pencolor('dark gray')
  for i in range(6):
    for j in range(5):
      draw_square(get_coord(i,j),80)

def display_word(w):
  turtle.up()
  turtle.color('white')
  for i in range(5):
    x,y = get_coord(gs,i)
    turtle.goto(x,y-23)
    turtle.write(w[i].upper(),align='center',font=('Arial',40,'bold'))

def update_cell(i,j):
  global w,state
  x, y = get_coord(i,j)
  turtle.pencolor('dark gray')
  if state[i][j] == 0:
    fc = 'dark gray'
  elif state[i][j] == 1:
    fc = 'goldenrod'
  else: fc = 'green'
  draw_square(get_coord(i,j),80,fc)
  turtle.up()
  turtle.color('white')
  turtle.goto(x,y-23)
  turtle.write(w[j].upper(),align='center',font=('Arial',40,'bold'))
  screen.update()
  
def play(x,y):
  flag = False
  for i in range(6):
    if flag: break
    for j in range(5):
      cx, cy = get_coord(i,j)
      if (cx-x)**2 + (cy-y)**2 < 1600:
        flag = True
        ci = i
        cj = j
        break
  if not flag: return
  if ci != gs: return
  state[ci][cj] = (state[ci][cj] + 1) % 3
  update_cell(ci,cj)

def submit():
  global state
  global gs
  global w,words

  for i in range(5):
    if state[gs][i] == -1: return

  cs = dict()
  for i in range(5):
    if state[gs][i] == 0: # letter doesn't exist
      cs[(w[i],-2)] = 1
    else:
      if (w[i],-1) not in cs:
        cs[(w[i],-1)] = 1
      else:
        cs[(w[i],-1)] += 1
      if state[gs][i] == 1: cs[(w[i],i)] = 0
      else: cs[(w[i],i)] = 1
  words = getwords(words,cs)
  print(len(words))
  w = guess_random(words)
  gs += 1
  display_word(w)
  if len(words) == 1:
    messagebox.showinfo("Done", "Congratulations!")
    turtle.bye()

  screen.update()

orig_words = []
f = open('wordle_words.txt','r')
for w in f:
  orig_words.append(w.strip())

cs = dict()
words = getwords(orig_words,cs)
w = guess_random(words)
w = 'tesla'
draw_board()
display_word(w)
screen.update()
screen.onclick(play)
screen.onkey(submit, 'Return')
screen.listen()
screen.mainloop()

You will also need to download a list of words as ‘wordle_words.txt’.

Mandelbrot Set with Different Paramaters (Source Code)

Using the center coordinate and zoom factor to draw the following Mandelbrot Set visualizations.

(-0.75, 0), Zoom=1
(-0.103865, -0.9584393), Zoom=167466.7.
(-0.761574, -0.0847596) Zoom=3125
(-0.761574, -0.0847596) Zoom=78125
(-0.59990625, -0.4290703125) Zoom=1024
(-1.62917, -0.0203968) Zoom=3125
(0.2613577, -0.002018128) Zoom=3354.786

Source Code:

from PIL import Image
import colorsys
import math

px, py = -0.7746806106269039, -0.1374168856037867 #Tante Renate
px, py, zoom = -0.74384935657398, -0.13170134084746293, 5788441.443619884
px, py, zoom = 2.613577e-1, -2.018128e-3, 3.354786e+3
px, py, zoom = -0.59990625, -0.4290703125, 1024
px, py, zoom = -1.038650e-1, -9.584393e-1, 1.674667e+5
px, py, zoom = -0.761574, -0.0847596, 78125
px, py, zoom = -1.62917,-0.0203968, 3125
px, py, zoom = -0.75,0,1
R = 3 
max_iteration = 512
w, h = 1250,1250
mfactor = 1

def Mandelbrot(x,y,max_iteration,minx,maxx,miny,maxy):
    zx = 0
    zy = 0
    RX1, RX2, RY1, RY2 = px-R/2, px+R/2,py-R/2,py+R/2
    cx = (x-minx)/(maxx-minx)*(RX2-RX1)+RX1
    cy = (y-miny)/(maxy-miny)*(RY2-RY1)+RY1
    i=0
    while zx**2 + zy**2 <= 4 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_Mandelbrot_image():
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
      c=Mandelbrot(x,y,max_iteration,0,w-1,0,h-1)
      v = c**mfactor/max_iteration**mfactor
      hv = 0.67-v*2
      #if hv<0: hv+=1
      r,g,b = colorsys.hsv_to_rgb(hv,1,1-(v-0.1)**2/0.9**2)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
  bitmap.save("Mandelbrot_"+str(px)+"_"+str(py)+"_"+str(zoom)+".jpg")
  bitmap.show()
  
R=3/zoom
gen_Mandelbrot_image()

Zooming 10^13 into Mandelbrot Set Animation with Python (Source Code)

Write a Python program to generate 1000+ images of Mandelbrot Set with ever zooming ranges. Please note that Turtle is not used in this project for speed consideration. Then use an external software to combine the images into a video:

Zooming 10^13 into Mandelbrot Set

Source Code:

from PIL import Image
import colorsys
import math

px, py = -0.7746806106269039, -0.1374168856037867 #Tante Renate
R = 3 
max_iteration = 2500
w, h = 1024,1024
mfactor = 0.5

def Mandelbrot(x,y,max_iteration,minx,maxx,miny,maxy):
    zx = 0
    zy = 0
    RX1, RX2, RY1, RY2 = px-R/2, px+R/2,py-R/2,py+R/2
    cx = (x-minx)/(maxx-minx)*(RX2-RX1)+RX1
    cy = (y-miny)/(maxy-miny)*(RY2-RY1)+RY1
    i=0
    while zx**2 + zy**2 <= 4 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_Mandelbrot_image(sequence):
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
      c=Mandelbrot(x,y,max_iteration,0,w-1,0,h-1)
      v = c**mfactor/max_iteration**mfactor
      hv = 0.67-v
      if hv<0: hv+=1
      r,g,b = colorsys.hsv_to_rgb(hv,1,1-(v-0.1)**2/0.9**2)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
  bitmap.save("Mandelbrot_"+str(sequence)+".jpg")

R=3
f = 0.975
RZF = 1/1000000000000
k=1
while R>RZF:
  if k>100: break
  mfactor = 0.5 + (1/1000000000000)**0.1/R**0.1
  print(k,mfactor)
  gen_Mandelbrot_image(k)
  R *= f
  k+=1

This program generates over 1000 images and may be quite slow. You can make it faster by dividing the work to multiple programs each doing a portion of those images.

Zooming 10^13 Times into Julia Set Animation

Write a Python program to generate hundreds of images of Julia Set with ever zooming ranges. Please note that Turtle is not used in this project for speed consideration. Then use an external software to combine the images into a video:

Zooming 10^13 Times into Julia Set

The video above zooms into the center at 0.0958598997051 + 1.1501990019993i with c = 0.355 + 0.355i. In each iteration the range shrinks by 5% until the range becomes 1/10^13 the size of the original range. You can modify the code to try different centers and c. The following is the source code:

from PIL import Image
import colorsys

cx = -0.7269
cy = 0.1889
R = (1+(1+4*(cx**2+cy**2)**0.5)**0.5)/2+0.001
max_iteration = 512
print(R)

w, h, zoom = 1500,1500,1

def julia(cx,cy,RZ,px,py,R,max_iteration,x,y,minx,maxx,miny,maxy):
    zx = (x-minx)/(maxx-minx)*2*RZ-RZ+px
    zy = (y-miny)/(maxy-miny)*2*RZ-RZ+py
    i=0
    while zx**2 + zy**2 < R**2 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_julia_image(sequence,cx,cy,RZ):
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
        # smllaer: right,down
      c=julia(cx,cy,RZ,0.0958598997051,1.1501990019993,R,max_iteration,x,y,0,w-1,0,h-1)
      v = c/max_iteration
      hv = 0.67-v*0.67
      r,g,b = colorsys.hsv_to_rgb(hv,1,v/2+0.45)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(r) + (int(g) << 8) + (int(b) << 16)
  bitmap.save("julia_"+str(sequence)+".jpg")
#  bitmap.show()

f = 0.95
RZF = 1/1000000000000
RZ = 1
k=1
while RZ>RZF:
  print(k,RZ)
  gen_julia_image(k,0.355,0.355,RZ)
  RZ *= f
  k+=1

Related Projects:

Julia Set Fractal Animation

Julia Set with Different Parameters

Julia Set with Different Parameters

In this project, we are generate several Julia Sets by varying the constant c in the normal Julia Set function. Turtle will be too slow to do this work. Instead we are going to use the PIL library. Please check out Julia Set Animation project for source code.

c = -0.618 (Max Iteration=38)
c = 0.355534-0.337292i (Max Iteration=4064)
c = 0.355+0.355i (Max Iteration=512)
c = 0.285+0.01i (Max Iteration=204)
c = -0.8+0.156i (Max Iteration=924)
c = -0.835+0.2321i (Max Iteration=104)
c = 0.7269+0.1889i (Max Iteration=1024)

Related Projects:

Zooming 10^13 Times into Julia Set Animation

Julia Set Fractal Animation

Julia Set Fractal Animation

Julia Set Animation

In this project, we are going to animate the Julia Set by varying the constant c in the normal Julia Set function. Turtle will be too slow to do this work. Instead we are going to use the PIL library to generate 360 images. Then, a video editing software can be used to combine 360 images into a video file. The following is the source code:

from PIL import Image
import colorsys
import math

cx = 0.7885
cy = 0
R = (1+(1+4*(cx**2+cy**2)**0.5)**0.5)/2+0.001
max_iteration = 200

w, h, zoom = 1000,1000,1

def julia(cx,cy,R,max_iteration,x,y,minx,maxx,miny,maxy):
    zx = (x-minx)/(maxx-minx)*2*R-R
    zy = (y-miny)/(maxy-miny)*2*R-R
    i=0
    while zx**2 + zy**2 < R**2 and i < max_iteration:
        temp = zx**2 - zy**2
        zy = 2*zx*zy + cy
        zx = temp + cx
        i += 1
    return i

def gen_julia_image(sequence,cx,cy):
  bitmap = Image.new("RGB", (w, h), "white")
  pix = bitmap.load()
  for x in range(w):
    for y in range(h):
      c=julia(cx,cy,R,max_iteration,x,y,0,w-1,0,h-1)
      r,g,b = colorsys.hsv_to_rgb(c/max_iteration,1,0.9)
      r = min(255,round(r*255))
      g = min(255,round(g*255))
      b = min(255,round(b*255))
      pix[x,y] = int(b) + (int(g) << 8) + (int(r) << 16)
  bitmap.save("julia_"+str(sequence)+".jpg")


for i in range(360):
  cx = 0.7885*math.cos(i*math.pi/180)
  cy = 0.7885*math.sin(i*math.pi/180)
  gen_julia_image(i,cx,cy)

Related Projects:

Julia Set with Different Parameters

Zooming 10^13 Times into Julia Set Animation

Typing Game with Python and Turtle (Source Code Included)

Develop a typing game to improve keyboard skill as demonstrated in the following YouTube video.

Typing Game with Python and Turtle

At any moment ten random letters fall from the top of screen. When you hit a correct letter from keyboard, that letter disappears and is replaced by a new random letter dropping from the top. Also, your score will increase by 1. When you hit an incorrect letter, your score will decrease by 1.

Make these letters fall at random speeds and also let the speed increase gradually as time progresses. The game will end when a letter falls to the bottom of the screen.

You will need to use Turtle’s ontimer() and onkey() events to implement this project.

Source Code:

import turtle
import random

screen = turtle.Screen()
screen.setup(1000,1000)
screen.title('Typing Game - PythonTurtle.Academy')
screen.bgcolor('blue')
screen.tracer(0,0)
turtle.hideturtle()
turtle.up()
turtle.color('red')
score_turtle = turtle.Turtle()
score_turtle.color('red')
score_turtle.up()
score_turtle.hideturtle()
turtle.goto(350,400)
turtle.write('Score: ', align='center', font=('Courier',25,'normal'))

min_speed = 5
max_speed = 30
letters = []
speeds = []
pos = []
lts = []
n = 10
game_over = False
score = 0

def increase_difficulty():
    global min_speed, max_speed
    min_speed += 1
    max_speed += 1
    screen.ontimer(increase_difficulty,5000)

def draw_game_over():
    turtle.goto(0,0)
    turtle.color('red')
    turtle.write('GAME OVER', align='center', font=('Courier',50,'normal'))
    turtle.goto(0,-150)
    turtle.color('orange')
    turtle.write('Your Score is {}'.format(score), align='center', font=('Courier',40,'normal'))
    screen.update()

def draw_score():
    score_turtle.clear()
    score_turtle.goto(420,400)
    score_turtle.write('{}'.format(score),align='center',font=('Courier',25,'normal'))
    screen.update()
    
def draw_letters():
    global game_over
    for i in range(len(letters)):
        lts[i].clear()
        lts[i].goto(pos[i])
        lts[i].write(letters[i],align='center',font=('courier',20,'normal'))
        pos[i][1] -= speeds[i]
        if pos[i][1]<-500:
            game_over = True
            draw_game_over()
            return
    screen.update()
    screen.ontimer(draw_letters,50)

def f(c): # handle keyboard press
    global score
    if c in letters:
        score += 1
        k = letters.index(c)
        while True:
            l = chr(ord('a')+random.randrange(26))
            if l not in letters:
                letters[k] = l
                break            
        pos[k] = [random.randint(-450,450),500]        
        speeds[k] = random.randint(min_speed,max_speed)
    else: score -= 1
    draw_score()
        
for _ in range(n):
    lts.append(turtle.Turtle())
    while True:
        l = chr(ord('a')+random.randrange(26))
        if l not in letters:
            letters.append(l)
            break
    speeds.append(random.randint(min_speed,max_speed))
    pos.append([random.randint(-450,450),500])
    
for i in range(n):
    lts[i].speed(0)
    lts[i].hideturtle()
    lts[i].up()
    lts[i].color('yellow')
    
draw_letters()
increase_difficulty()

screen.onkey(lambda: f('a'), 'a')
screen.onkey(lambda: f('b'), 'b')
screen.onkey(lambda: f('c'), 'c')
screen.onkey(lambda: f('d'), 'd')
screen.onkey(lambda: f('e'), 'e')
screen.onkey(lambda: f('f'), 'f')
screen.onkey(lambda: f('g'), 'g')
screen.onkey(lambda: f('h'), 'h')
screen.onkey(lambda: f('i'), 'i')
screen.onkey(lambda: f('j'), 'j')
screen.onkey(lambda: f('k'), 'k')
screen.onkey(lambda: f('l'), 'l')
screen.onkey(lambda: f('m'), 'm')
screen.onkey(lambda: f('n'), 'n')
screen.onkey(lambda: f('o'), 'o')
screen.onkey(lambda: f('p'), 'p')
screen.onkey(lambda: f('q'), 'q')
screen.onkey(lambda: f('r'), 'r')
screen.onkey(lambda: f('s'), 's')
screen.onkey(lambda: f('t'), 't')
screen.onkey(lambda: f('u'), 'u')
screen.onkey(lambda: f('v'), 'v')
screen.onkey(lambda: f('w'), 'w')
screen.onkey(lambda: f('x'), 'x')
screen.onkey(lambda: f('y'), 'y')
screen.onkey(lambda: f('z'), 'z')

screen.listen()
screen.mainloop()

Hexagon Spiral of Spirals Colored (Source Code)

After drawing pentagon spiral of pentagon spirals, draw colored version of hexagon spiral of spirals with recursion and Turtle library.

Hexagon Spiral of Spirals Colored

Source Code:

import turtle
import math

screen = turtle.Screen()
screen.title('Hexagon Spiral of Hexagon Spirals - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

colors = [ 'red', 'orange', 'green', 'teal', 'blue', 'magenta' ]

def draw_spiral(x,y,r,direction,first=False):
    if r < 1: return
    d = direction
    r_ratio = math.cos(math.radians(30))/math.cos(math.radians(30-alpha))
    d_ratio = math.sin(math.radians(30))-r_ratio*math.sin(math.radians(30-alpha))
    for i in range(6):
        if first: turtle.color(colors[i]) 
        px = x + r*math.cos(math.radians(direction))
        py = y + r*math.sin(math.radians(direction))
        r2 = r
        d = direction
        c = 0
        flag = False
        while True:
            dist = r2*d_ratio
            if c > 10 and dist < 1: break
            if dist > 4:
                draw_spiral(px,py,dist*0.4,d)
                turtle.up()
                turtle.goto(px,py)
                turtle.seth(d+180-60)
                turtle.fd(dist)
                px,py = turtle.xcor(), turtle.ycor()
            elif not flag:
                turtle.up()
                turtle.goto(px,py)
                turtle.down()
                flag = True
                turtle.seth(d+180-60)
                turtle.fd(dist)
            else:   
                turtle.seth(d+180-60)
                turtle.fd(dist)
    
            r2 = r2*r_ratio
            d += alpha
            c += 1
        direction += 60
    
    
alpha = 20
draw_spiral(0,0,800,90,True)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")

Pentagon Spiral of Pentagon Spirals Fractal (Source Code)

After finishing 5 spirals and spiral of spirals, draw the following pentagon spiral of pentagon spirals using recursion.

Pentagon Spiral of Pentagon Spirals

Source Code: (This code may run for several minutes)

import turtle
import math

screen = turtle.Screen()
screen.title('Pentagon Spiral of Pentagon Spirals - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

def draw_spiral(x,y,r,direction):
    if r < 1: return
    d = direction
    r_ratio = math.cos(math.radians(36))/math.cos(math.radians(36-alpha))
    d_ratio = math.sin(math.radians(36))-r_ratio*math.sin(math.radians(36-alpha))
    for _ in range(5):
        px = x + r*math.cos(math.radians(direction))
        py = y + r*math.sin(math.radians(direction))
        r2 = r
        d = direction
        c = 0
        flag = False
        while True:
            dist = r2*d_ratio
            if c > 10 and dist < 1: break
            if dist > 3:
                draw_spiral(px,py,dist*0.5,d)
                turtle.up()
                turtle.goto(px,py)
                turtle.seth(d+180-54)
                turtle.fd(dist)
                px,py = turtle.xcor(), turtle.ycor()
            elif not flag:
                turtle.up()
                turtle.goto(px,py)
                turtle.down()
                flag = True
                turtle.seth(d+180-54)
                turtle.fd(dist)
            else:   
                turtle.seth(d+180-54)
                turtle.fd(dist)
    
            r2 = r2*r_ratio
            d += alpha
            c += 1
        direction += 360/5
    
    
alpha = 20
draw_spiral(0,0,800,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")

Colored Spiral of Spirals with Python Turtle (Source Code)

Just for fun, draw a colored version of spiral of spirals using the colorsys library.

Colored Spiral of Spirals
Colored Spiral of Spirals

Source Code: (This may a few minutes)

import turtle
import colorsys

screen = turtle.Screen()
screen.title('Colored Spiral of Spirals Fractal - PythonTurtle.Academy')
screen.setup(1000,1000)
screen.setworldcoordinates(-1000,-1000,1000,1000)
turtle.speed(0)
turtle.hideturtle()
screen.tracer(0,0)

def draw_spiral(x,y,length,direction):
    L = length
    c = 0
    color=colorsys.hsv_to_rgb((y+900)/1800,1,0.8)
    turtle.color(color)
    while length>1 or c<20:
        if length>2:
            draw_spiral(x,y,length*0.255,160+direction)
        turtle.up()
        turtle.seth(direction)
        turtle.goto(x,y)
        if length <= 2:
            turtle.down()
        turtle.fd(length)
        x,y = turtle.xcor(), turtle.ycor()
        length *= 0.93
        direction += 20
        c += 1
LL=300
draw_spiral(500,-500,LL,90)
screen.update()
ts=turtle.getscreen()
ts.getcanvas().postscript(file = "spiral.eps")