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

507 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
  8. Celebrate in style with our Miami bachelorette party bus. Our Miami VIP bachelorette party bus offers unmatched luxury and fun. Ideal for bachelorette celebrations, this bachelorette party transportation Miami ensures a night to remember with our premium Miami bachelorette party bus rental.

    Reply
  9. Airport Limo Hamilton provides reliable and luxurious transportation services between Hamilton and various airports, including Toronto Pearson Airport (YYZ), Billy Bishop Toronto City Airport (YTZ), and Buffalo Niagara International Airport (BUF). Our fleet of modern limousines offers a comfortable and convenient experience, ensuring a stress-free journey to and from your destination.

    Reply
  10. Woodstock’s go-to transportation solution, Woodstock Taxi, and its reliable Woodstock Taxis. We ensure safe and efficient travel in Woodstock, Ontario, catering to both locals and tourists alike.

    Reply
  11. Experience personalized luxury with Private Car Service in Chicago. From airport transfers to executive rides, we offer premium transportation solutions tailored to your needs. Enjoy punctuality, comfort, and professionalism with our range of services.

    Reply
  12. Embark on a full day private Cape Peninsula tour, exploring the scenic beauty of Cape Town and its surroundings. Our Cape Peninsula private tours showcase the best of South Africa’s coastline with a personalized touch, including a visit to Cape Point.

    Reply
  13. Reliable Houston Airport Transportation with top-notch services. From Airport Transfers In Houston to transfers from Houston to Galveston. Choose our Houston Hobby Airport Shuttle or IAH Airport Shuttle for a smooth journey. Experience exceptional Airport Transfers from Houston to Galveston.

    Reply
  14. Choose our car service MSP to Rochester, MN, for a smooth journey. We provide transportation from Minneapolis Airport to Rochester, MN, and chauffeur service from Minneapolis to Rochester. Trust our reliable Minneapolis car service to Rochester, MN, for your travel needs.

    Reply
  15. Unleash the allure of Dubai in our stretch limousines. For special moments, corporate travel, or leisure, our chauffeur-driven limousines provide a lavish and memorable experience, turning heads across the city.

    Reply
  16. Experience seamless travel with our Galveston Cruise Shuttle from Houston. We provide timely transportation to the Galveston Cruise Terminal from Hobby and George Bush Airports, Downtown Houston, and surrounding areas like Sugar Land and Baytown. Reserve your spot today!

    Reply
  17. Choose the best Miami prom party bus rental for your special night. Our prom limo bus Miami provides luxury prom transportation Miami. Enjoy affordable prom party bus Miami options, including prom party bus packages Miami and exclusive Miami prom bus rental deals for Miami prom group transportation.

    Reply
  18. Experience top-notch bus service in Annapolis, Maryland, with us. We provide charter bus service Annapolis Maryland for any occasion, efficient shuttle bus services Annapolis, and reliable group transportation Annapolis, plus motor coach bus rental Annapolis, wedding transportation Annapolis, and convenient airport transfer Annapolis.

    Reply
  19. For seamless travel, opt for our Minneapolis airport car service. Our MSP airport transportation includes limo, shuttle, taxi, and sedan services. Enjoy reliable MSP car service, private cars, black cars, SUVs, and executive options for your Minneapolis airport ride.

    Reply
  20. Experience the finest VIP Limousine Winnipeg has to offer with our affordable limousine rentals. Choose from luxury stretch limousines for weddings, corporate events, or parties. Enjoy premium chauffeur service with the best Winnipeg Limo Rentals and VIP Limousine Services.

    Reply
  21. Choose our Washer Dryer Repair Chicago for prompt and affordable service. We provide same day repairs and maintenance in Schaumburg, Skokie, and Arlington Heights. Our experienced technicians ensure your appliances are running smoothly, offering the best service in the area.

    Reply
  22. Kingston Cab provides efficient taxi services, including airport transportation. Book a Kingston to Toronto Airport Shuttle for a convenient and comfortable journey. Count on reliable service for your travel needs.

    Reply

Leave a Comment