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.
Read:
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.
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
Comments are closed.