Python Turtle Play Tic Tac Toe

In this tutorial, we will learn about how to make Python Turtle Play Tic Tac Toe in Python Turtle and we will cover different examples related to Python Turtle Tic Tac Toe. And, we will cover these topics.

Python Turtle Tic Tac Toe Board

In this part of the Python tutorial, we will learn about how to make Play Tic Tac Toe and also learn to make a Tic Tac Toe Board in Python Turtle.

  • The Tic Tac Toe board or we can say that grid which contain nine squares and the player fill the 0’s and x’s in this grid and play.
  • The Tic Tac Toe is a game in which two players play with their turns to complete the board which has row and column and the player complete a row and a column or diagonal with three 0’s or x’s.
  • It can also play with the help of pen and pencile on the paper.

Code:

In the following code, we will import the turtle module from which we can draw the Tic Tac Toe board in Python turtle.

  • wd=tur.Screen() is used to get the screen on which we can work.
  • turt=tur.Turtle() is used to make the objects.
  • tur.title(“Pythontpoint”) is used to give the title to the window.
  • turt.color(“blue”) is used to give the blue color to the pen.
  • turt.width(“4”) is used to setting the width to 4.
  • turt.speed(2) is used to give the speed to the turtle.
  • turt.forward(300) is used to move the turtle in the forward direction.
  • turt.left(90) is used to move the turtle in the left direction.
  • turt.penup() is used to stop the drawing.
  • turt.goto(0,100) is used to move the turtle from its actual position.
  • turt.pendown() is used to start drawing.
from turtle import *
import turtle as tur

wd=tur.Screen()
 
turt=tur.Turtle()
tur.title("Pythontpoint") 
turt.color("blue")
turt.width("4")
 
turt.speed(2)
 
for i in range(4):
    turt.forward(300)
    turt.left(90)

turt.penup()
turt.goto(0,100)
turt.pendown()
 
turt.forward(300)
 
turt.penup()
turt.goto(0,200)
turt.pendown()
 
turt.forward(300)
 
turt.penup()
turt.goto(100,0)
turt.pendown()
 
turt.left(90)
turt.forward(300)
 
turt.penup()
turt.goto(200,0)
turt.pendown()
 
 
turt.forward(300)

tur.done()

Output:

After running the above code we get the following output in which we can see that the tic tac toe board is drawn on the screen.

Python turtle tic tac toe board
Python turtle tic tac toe board

Read:

Python Turtle Speed

Python Turtle Spiraling Shape

Python Turtle Color

Python Turtle Play Tic Tac Toe

In this part of the Python tutorial, we will learn about how the Python turtle Play Tic Tac Toe Board works in Python turtle.

The Python turtle Play Tic Tac Toe is a game in which two players play with their turns to complete the grid which has row and column and the player completes a row and a column or diagonal with three 0’s or x’s.

Code:

In the following code, we will import the turtle library from which we can draw and Play the Tic Tac Toe game and see how its works in python turtle.

  • sc = tur.Screen() is used to get the screen on which we can work.
  • sc.setup(800,800) is used to set the width and height of the screen.
  • sc.title(“Pythontpoint”) is used to give the title to the screen.
  • sc.bgcolor(‘black’) is used to give the black background color.
  • tur.pencolor(‘blue’) is used to give the blue color to the pen.
  • tur.pensize(10) is used to change the size of the pen.
  • tur.fd(6) is used to move the turtle in the forward direction.
  • tur.up() is used to stop the drawing.
  • tur.down() is used to start the drawing.
  • tur.circle(0.75, steps=100) is used to draw the circle which works as a zero.
  • sc.onclick(play) the game is start and player start playing on clicking on the screen.
from turtle import *
import turtle as tur

sc = tur.Screen()
sc.setup(800,800)
sc.title("Pythontpoint")
sc.setworldcoordinates(-5,-5,5,5)
sc.bgcolor('black')
sc.tracer(0,0)
tur.hideturtle()

def draw_board():
    tur.pencolor('blue')
    tur.pensize(10)
    tur.up()
    tur.goto(-3,-1)
    tur.seth(0)
    tur.down()
    tur.fd(6)
    tur.up()
    tur.goto(-3,1)
    tur.seth(0)
    tur.down()
    tur.fd(6)
    tur.up()
    tur.goto(-1,-3)
    tur.seth(90)
    tur.down()
    tur.fd(6)
    tur.up()
    tur.goto(1,-3)
    tur.seth(90)
    tur.down()
    tur.fd(6)

def draw_circle(x,y):
    tur.up()
    tur.goto(x,y-0.75)
    tur.seth(0)
    tur.color('red')
    tur.down()
    tur.circle(0.75, steps=100)

def draw_x(x,y):
    tur.color('blue')
    tur.up()
    tur.goto(x-0.75,y-0.75)
    tur.down()
    tur.goto(x+0.75,y+0.75)
    tur.up()
    tur.goto(x-0.75,y+0.75)
    tur.down()
    tur.goto(x+0.75,y-0.75)
    
def draw_piece(i,j,p):
    if p==0: return
    x,y = 2*(j-1), -2*(i-1)
    if p==1:
        draw_x(x,y)
    else:
        draw_circle(x,y)
    
def draw(b):
    draw_board()
    for i in range(3):
        for j in range(3):
            draw_piece(i,j,b[i][j])
    sc.update()

# return 1 if player 1 wins, 2 if player 2 wins, 3 if tie, 0 if game is not over
def gameover(b):
    if b[0][0]>0 and b[0][0] == b[0][1] and b[0][1] == b[0][2]: return b[0][0]
    if b[1][0]>0 and b[1][0] == b[1][1] and b[1][1] == b[1][2]: return b[1][0]
    if b[2][0]>0 and b[2][0] == b[2][1] and b[2][1] == b[2][2]: return b[2][0]
    if b[0][0]>0 and b[0][0] == b[1][0] and b[1][0] == b[2][0]: return b[0][0]
    if b[0][1]>0 and b[0][1] == b[1][1] and b[1][1] == b[2][1]: return b[0][1]
    if b[0][2]>0 and b[0][2] == b[1][2] and b[1][2] == b[2][2]: return b[0][2]
    if b[0][0]>0 and b[0][0] == b[1][1] and b[1][1] == b[2][2]: return b[0][0]
    if b[2][0]>0 and b[2][0] == b[1][1] and b[1][1] == b[0][2]: return b[2][0]
    p = 0
    for i in range(3):
        for j in range(3):
            p += (1 if b[i][j] > 0 else 0)
    if p==9: return 3
    else: return 0
    
def play(x,y):
    global turn
    i = 3-int(y+5)//2
    j = int(x+5)//2 - 1
    if i>2 or j>2 or i<0 or j<0 or b[i][j]!=0: return
    if turn == 'x': b[i][j], turn = 1, 'o'
    else: b[i][j], turn = 2, 'x'
    draw(b)
    r = gameover(b)
    if r==1:
        sc.textinput("Game over!","X's won!")
    elif r==2:
        sc.textinput("Game over!","O's won!")
    elif r==3:
        sc.textinput("Game over!", "Tie!")
    
b = [ [ 0,0,0 ], [ 0,0,0 ], [ 0,0,0 ] ]    
draw(b)
turn = 'x'
sc.onclick(play)
tur.mainloop()

Output:

After running the above code we get the following output in which we can see that the player plays the Tic Tac Toe game just by clicking on the board.

python turtle tic tac toe work
python turtle tic tac toe work

So, in this tutorial, we discussed Python Turtle Play Tic Tac Toe and also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python Turtle Tic Tac Toe Board
  • Python Turtle Tic Tac Toe Board work

We also wish you and your family a very Happy Lohri and Makar Sakranti

19 thoughts on “Python Turtle Play Tic Tac Toe”

  1. limo service offered by airporttransferdfw.com for Dallas/Fort Worth International Airport. This service provides travelers with luxurious and stylish transportation options using limousines. Passengers can book this service for a premium and extravagant travel experience, adding an element of elegance to their airport transfers.

    Reply
  2. As a preeminent DFW luxury car service, we prioritize quality and safety for our clients by performing regular maintenance on our fleet of opulent vehicles. We recognize that our clientele expects nothing less than exceptional luxury and style, which is precisely what our services offer.

    Reply
  3. Discover wheelchair-accessible car services and group transportation solutions in Washington DC. With a range of options including shuttles and general transportation, the capital caters to diverse travel needs. For luxurious and reliable transportation, consider KVLIMO’s premium offerings.

    Reply
  4. Blue Mountain Airport Taxi provides seamless travel options, including Pearson Airport taxi to Blue Mountain, convenient Innisfil to Blue Mountain service, and Toronto to Blue Mountain bus. Explore stress-free transportation with Blue Mountain cabs and choose us for reliable airport transfers.

    Reply
  5. Explore Woodstock, Ontario, with Woodstock Taxi services. Our fleet of Woodstock Taxis provides dependable transportation for residents and visitors. Your trusted choice for local commuting and travel needs.

    Reply
  6. Efficient transportation linking LAX to Long Beach. Dedicated SNA airport transportation service ensures a seamless journey. Trust our reliable airport transfer for a smooth experience between Los Angeles and Long Beach.

    Reply
  7. When you’re arranging a journey from Dubai to Abu Dhabi, it’s critical to factor in transportation. Although there are many possibilities, selecting Limo UAE is an excellent decision for multiple reasons. It provides not only a lavish and pleasant environment but also a plethora of advantages that simply can’t be matched by other travel options.

    Reply

Leave a Comment