Thinking ball – random walk, biased

Here is a code which employs biased random walk to help a ball get out of a box with one exit. It is coded in python using tkinter module. Does the ball have an intelligence?

"""
Thinking ball
Ball 'tries' to find way out of a box with exit on one side.
Strategy: random walk, biased
Author: Jithesh Kuyyalil
web: https://jitheshkuyyalil.com
"""

from tkinter import *
import time

width  = 600
height = 600

gui = Tk()
dimension = str(width)+'x'+str(height)                              
gui.geometry(dimension)
gui.title("Thinking ball")
canvas = Canvas(gui, width=width, height=height, bg='black')
canvas.pack()
ov1 = canvas.create_oval(225, 225, 275, 275, fill='pink')  # ball width 50
#print(help(ov1))
canvas.create_line(50, 50, 50, 550, fill='blue')    # left
canvas.create_line(50, 550, 550, 550, fill='blue')  # bottom
canvas.create_line(550, 550, 550, 150, fill='blue') # right
canvas.create_line(550, 50, 50, 50, fill='blue')    # top
def moveBall():
    gui.update()
    time.sleep(3)
    from random import random
    while True:
        x_oval = canvas.coords(ov1)[0]  # top left
        y_oval = canvas.coords(ov1)[1]  # top left
        X_oval = canvas.coords(ov1)[2]  # bottom right
        Y_oval = canvas.coords(ov1)[3]  # bottom right
        rnd = random()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        # Is intelligence?
        # No memory?
        # boundary conditions + moving logic
        if rnd  50 and Y_oval < 150:
                canvas.move(ov1, 5, 0)
            else:
                if X_oval + 5 = 0.5 and rnd < 0.6 and Y_oval + 5 =0.6 and rnd  50: # left
            canvas.move(ov1, -5, 0)
        else:
            if y_oval -5 > 50: # top 
                canvas.move(ov1, 0, -5)
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        gui.update()
        print(canvas.winfo_x(), canvas.winfo_y())
        time.sleep(0.09)

moveBall()
mainloop()

Ball: Initial Configuration
Ball: Final Configuration

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s