#4s: Codding in Python {TIC TAK TOE USING TURTLE}

import turtle as t
t.setup(350,410)
def grid():
        t.speed(0)
        t.fd(200)
        t.fd(-300)
        t.fd(100)
        t.right(-90)
        t.fd(100)
        t.bk(300)
        t.fd(100)
        t.right(90)
        t.fd(200)
        t.fd(-300)
        t.fd(200)
        t.right(-90)
        t.fd(200)
        t.fd(-300)
def rules():
        t.pu()
        t.goto(-155,170)
        t.pd()

        t.write('For X use left mouse button, for O use right mouse button.',font=('Arial',9))
        t.pu()
        t.goto(-155,155)
        t.pd()
        t.write('X will start first, kindly do not cheat',font=('Arial',9))
        
        
rules()

t.pu()

t.goto(-50,50)
t.pd()
grid()
p1=[]
p2=[]

        
def writo(q,w,a,f):
        print('Player 2 wins')
        t.pd()
        t.speed(0)
        t.pensize(5)
        t.color('Red')
        t.pu()
        t.goto(q,w)
        t.pd()
        t.seth(a)
        t.speed(1)
        t.fd(f)
        t.pu()
        t.clear()
        t.speed(0)
        t.goto(-50,0)
        t.seth(0)
        t.write('Player 2 wins!',font=('Arial',15))
def writx(q,w,a,f):
        print('Player 1 wins')
        t.pd()
        t.color('Red')
        t.pensize(5)
        t.pu()
        t.goto(q,w)
        t.pd()
        t.seth(a)
        t.speed(1)
        t.fd(f)
        t.pu()
        t.clear()
        t.goto(-50,0)
        t.seth(0)
        t.speed(0)
        t.write('Player 1 wins!',font=('Arial',15))
def writq():
        print('Match tied!')
                                
        t.clear()
        t.goto(0,0)
        t.seth(0)
        t.write('Match tied!')
def cross():
        t.setheading(0)
        t.pd()
        t.speed(0)
        t.right(45)
        for tt in range(4):
            t.fd(50)
            t.fd(-50)
            t.right(-90)
def circle():
        t.seth(0)
        t.pu()
        t.fd(40)
        t.right(-90)
        t.pd()
        t.circle(40)
        
def logic(x,x1,y,y1,a,b,c,d):
    if t.xcor()<x and t.xcor()>x1:
        if t.ycor()<y and t.ycor()>y1:
            t.pu()
            t.goto(a,b)
            t.pd()
            d.append(c)
            cross()
            t.pu()
def logico(x,x1,y,y1,a,b,c,d):
    if t.xcor()<x and t.xcor()>x1:
        if t.ycor()<y and t.ycor()>y1:
            t.pu()
            t.goto(a,b)
            t.pd()
            d.append(c)
            circle()
            t.pu()

def mex(x,y):
        if len(p1)==len(p2):
                for tq in range(1):
                        if tq==0:
                                t.goto(x,y)
                                
                                logic(-50,-150,150,50,-100,100,'a',p1)
                                logic(50,-50,150,50,0,100,'b',p1)
                                logic(150,50,150,50,100,100,'c',p1)
                                logic(150,50,50,-50,100,0,'f',p1)
                                logic(150,50,-50,-150,100,-100,'i',p1)
                                logic(50,-50,-50,-150,0,-100,'h',p1)
                                logic(50,-50,50,-50,0,0,'e',p1)
                                logic(-50,-150,-50,-150,-100,-100,'g',p1)
                                logic(-50,-150,50,-50,-100,0,'d',p1)
                                if a in (p1):
                                    if b in (p1):
                                            if c in (p1):
                                                    writx(-150,100,0,300)
                                                    break
                                    if e in (p1):
                                            if i in (p1):
                                                    writx(-150,150,-45,450)
                                                    break
                                    if d in (p1):
                                            if g in (p1):
                                                    writx(-100,150,-90,300)
                                                    break
                                if d in (p1):
                                    if e in (p1):
                                            if f in (p1):
                                                    writx(-150,0,0,300)
                                                    break
                                if b in (p1):
                                        if e in (p1):
                                                if h in (p1):
                                                        writx(0,150,-90,300)
                                                        break
                                if i in (p1):
                                    if f in (p1):
                                            if c in (p1):
                                                    writx(100,150,-90,300)
                                                    break
                                    
                                    if h in (p1):
                                            if g in (p1):
                                                    writx(-150,-100,0,300)
                                                    break
                                if c in (p1):
                                        if e in (p1):
                                                if g in (p1):
                                                        writx(150,150,-135,450)
                                                        break
                                if len(p1)==5 and len(p2)==4:
                                        writq()
                                        break
        
                    
def meo(x,y):
        
        if len(p1)-1==len(p2):
                for r in range(1):
                        t.goto(x,y)
                                                
                        logico(-50,-150,150,50,-100,100,'a',p2)
                        logico(50,-50,150,50,0,100,'b',p2)
                        logico(150,50,150,50,100,100,'c',p2)
                        logico(150,50,50,-50,100,0,'f',p2)
                        logico(150,50,-50,-150,100,-100,'i',p2)
                        logico(50,-50,-50,-150,0,-100,'h',p2)
                        logico(50,-50,50,-50,0,0,'e',p2)
                        logico(-50,-150,-50,-150,-100,-100,'g',p2)
                        logico(-50,-150,50,-50,-100,0,'d',p2)
                        if a in (p2):
                                if b in (p2):
                                        if c in (p2):
                                                writo(-150,100,0,300)
                                                break
                                if e in (p2):
                                        if i in (p2):
                                                writo(-150,150,-45,450)
                                                break
                                if d in (p2):
                                        if g in (p2):
                                                writo(-100,150,-90,300)
                                                break
                        if d in (p2):
                                if e in (p2):
                                        if f in (p2):
                                                writo(-150,0,0,300)
                                                break
                        if b in (p2):
                                if e in (p2):
                                        if h in (p2):
                                                writo(0,150,-90,300)
                                                break
                        if i in (p2):
                                if f in (p2):
                                        if c in (p2):
                                                writo(100,150,-90,300)
                                                break
                                
                                if h in (p2):
                                        if g in (p2):
                                                writo(-150,-100,0,300)
                                                break
                        if c in (p2):
                                if e in (p2):
                                        if g in (p2):
                                                writo(150,150,-135,450)
                                                break        
                
t.pu()
t.ht()
t.onscreenclick(mex,1)
t.onscreenclick(meo,3)
a='a'
b='b'
c='c'
d='d'
e='e'
f='f'
g='g'
h='h'
i='i'

t.mainloop()

Download CODES here.

Comments