واخيرا بعد 823 سنه من العمل و59240 سطر من الكود (ههه) عملت لعبه x , o و طبعا بتشتغل على التليفونات (لاني شغال على تليفون ) وإليكم الكود:
الملف الرئيسي وكود اللعبه وملف اللعب مع الروبوت وكود اللعب مع الصديق و كود كل حاجه :
from turtle import Turtle ,Screen
from random import choice
from time import sleep
window = Screen ()
window.title ('tic tac toc game')
window.tracer (0)
window.bgcolor ('silver')
write = Turtle ()
write.pensize (20)
write.hideturtle ()
write.penup ()
write.pendown ()
def create_screen (turtle):
write.penup ()
write.goto (-300,-300)
write.pendown ()
for _ in range (4):
turtle.forward (600)
turtle.left (90)
turtle.penup ()
turtle.goto (-100,300)
turtle.setheading (270)
turtle.pendown ()
turtle.forward (600)
turtle.penup ()
turtle.goto (100,300)
turtle.setheading (270)
turtle.pendown ()
turtle.forward (600)
turtle.penup ()
turtle.goto (-300,100)
turtle.setheading (0)
turtle.pendown ()
turtle.forward (600)
turtle.penup ()
turtle.goto (-300,-100)
turtle.setheading (0)
turtle.pendown ()
turtle.forward (600)
x = Turtle ()
o = Turtle ()
x.hideturtle ()
o.hideturtle ()
x.color ('blue')
o.color ('red')
def x_write (location):
x.penup ()
x.goto (location)
locations [locations.index (location)] = 'x'
x.pendown ()
x.write ('x',align='center',font=('Aerial' , 70 , 'normal'))
def o_write (location):
o.penup ()
o.goto (location)
locations [locations.index (location)] = 'o'
o.pendown ()
o.write ('o',align='center',font=('Aerial' , 70 , 'normal'))
locations = [[-200,60],[0,60],[200,60],[-200,-140],[0,-140],[200,-140],[-200,-340],[0,-340],[200,-340]]
def if_win ():
if locations [0] == locations [1] == locations [2]:
write.penup ()
write.color ('white')
write.goto (-250,200)
write.pendown ()
write.setheading (0)
write.forward (500)
return locations [0]
elif locations [3] == locations [4] == locations [5]:
write.penup ()
write.color ('white')
write.goto (-250,0)
write.pendown ()
write.setheading (0)
write.forward (500)
return locations [3]
elif locations [6] == locations [7] == locations [8]:
write.penup ()
write.color ('white')
write.goto (-250,-200)
write.pendown ()
write.setheading (0)
write.forward (500)
return locations [6]
elif locations [0] == locations [3] == locations [6]:
write.penup ()
write.color ('white')
write.goto (-200,250)
write.pendown ()
write.setheading (270)
write.forward (500)
return locations [0]
elif locations [1] == locations [4] == locations [7]:
write.penup ()
write.color ('white')
write.goto (0,250)
write.pendown ()
write.setheading (270)
write.forward (500)
return locations [1]
elif locations [2] == locations [5] == locations [8]:
write.penup ()
write.color ('white')
write.goto (200,250)
write.pendown ()
write.setheading (270)
write.forward (500)
return locations [2]
elif locations [0] == locations [4] == locations [8]:
write.penup ()
write.color ('white')
write.goto (-250,250)
write.pendown ()
write.setheading (315)
write.forward (720)
return locations [0]
elif locations [2] == locations [4] == locations [6]:
write.penup ()
write.color ('white')
write.goto (250,250)
write.pendown ()
write.setheading (225)
write.forward (720)
return locations [2]
else :
pos = []
for l in locations :
if l == 'x' or l == 'o':
pos.append (x)
if len (pos) == 9:
return 0
def playing (self,x,y):
if -300 < x < -100 and 300 > y > 100:
if not locations[0] == 'o' and not locations[0] == 'x':
self (locations[0])
else :
return 1
elif -100 < x < 100 and 300 > y > 100:
if not locations[1] == 'o' and not locations[1] == 'x':
self (locations[1])
else :
return 1
elif 100 < x < 300 and 300 > y > 100:
if not locations[2] == 'o' and not locations[2] == 'x':
self (locations[2])
else :
return 1
elif -300 < x < -100 and 100 > y > -100:
if not locations[3] == 'o' and not locations[3] == 'x':
self (locations[3])
else :
return 1
elif -100 < x < 100 and 100 > y > -100:
if not locations[4] == 'o' and not locations[4] == 'x':
self (locations[4])
else :
return 1
elif 100 < x < 300 and 100 > y > -100:
if not locations[5] == 'o' and not locations[5] == 'x':
self (locations[5])
else :
return 1
elif -300 < x < -100 and -100 > y > -300:
if not locations[6] == 'o' and not locations[6] == 'x':
self (locations[6])
else :
return 1
elif -100 < x < 100 and -100 > y > -300:
if not locations[7] == 'o' and not locations[7] == 'x':
self (locations[7])
else :
return 1
elif 100 < x < 300 and -100 > y > -300:
if not locations[8] == 'o' and not locations[8] == 'x':
self (locations[8])
else :
return 1
else :
return 1
def play_with_friend (x,y):
if window.bgcolor () == 'indian red':
returns = playing (o_write ,x,y)
window.update ()
sleep (.1)
if returns == 1:
pass
else :
window.bgcolor ('light blue')
elif window.bgcolor () == 'light blue':
returns = playing (x_write ,x,y)
window.update ()
sleep (0.1)
if returns == 1:
pass
else :
window.bgcolor ('indian red')
returns = if_win ()
if returns == 'o':
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('red')
write.write ('red is win !!',align='center',font=('Aerial', 30 ,'normal'))
elif returns == 'x':
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('blue')
write.write ('blue is win !!',align='center',font=('Aerial', 30 ,'normal'))
elif returns == 0:
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('green')
write.write (" it's a tie !! ",align='center',font=('Aerial', 30 ,'normal'))
def robot_choice ():
if locations [0] == 'x' and locations [1] == 'x' and locations [2] != 'o':
return 2
elif locations [0] == 'x' and locations [2] == 'x' and locations [1] != 'o':
return 1
elif locations [1] == 'x' and locations [2] == 'x' and locations [0] != 'o':
return 0
elif locations [3] == 'x' and locations [4] == 'x' and locations [5] != 'o':
return 5
elif locations [3] == 'x' and locations [5] == 'x' and locations [4] != 'o':
return 4
elif locations [4] == 'x' and locations [5] == 'x' and locations [3] != 'o':
return 3
elif locations [6] == 'x' and locations [7] == 'x' and locations [8] != 'o':
return 8
elif locations [6] == 'x' and locations [8] == 'x' and locations [7] != 'o':
return 7
elif locations [7] == 'x' and locations [8] == 'x' and locations [6] != 'o':
return 6
elif locations [0] == 'x' and locations [3] == 'x' and locations [6] != 'o':
return 6
elif locations [0] == 'x' and locations [6] == 'x' and locations [3] != 'o':
return 3
elif locations [3] == 'x' and locations [6] == 'x' and locations [0] != 'o':
return 0
elif locations [1] == 'x' and locations [4] == 'x' and locations [7] != 'o':
return 7
elif locations [1] == 'x' and locations [7] == 'x' and locations [4] != 'o':
return 4
elif locations [4] == 'x' and locations [7] == 'x' and locations [1] != 'o':
return 1
elif locations [2] == 'x' and locations [5] == 'x' and locations [8] != 'o':
return 8
elif locations [2] == 'x' and locations [8] == 'x' and locations [5] != 'o':
return 5
elif locations [5] == 'x' and locations [8] == 'x' and locations [2] != 'o':
return 2
elif locations [0] == 'x' and locations [4] == 'x' and locations [8] != 'o':
return 8
elif locations [0] == 'x' and locations [8] == 'x' and locations [4] != 'o':
return 4
elif locations [4] == 'x' and locations [8] == 'x' and locations [0] != 'o':
return 0
elif locations [2] == 'x' and locations [4] == 'x' and locations [6] != 'o':
return 6
elif locations [2] == 'x' and locations [6] == 'x' and locations [4] != 'o':
return 4
elif locations [4] == 'x' and locations [6] == 'x' and locations [2] != 'o':
return 2
elif locations [0] == 'o' and locations [1] == 'o' and locations [2] != 'x':
return 2
elif locations [0] == 'o' and locations [2] == 'o' and locations [1] != 'x':
return 1
elif locations [1] == 'o' and locations [2] == 'o' and locations [0] != 'x':
return 0
elif locations [3] == 'o' and locations [4] == 'o' and locations [5] != 'x':
return 5
elif locations [3] == 'o' and locations [5] == 'o' and locations [4] != 'x':
return 4
elif locations [4] == 'o' and locations [5] == 'o' and locations [3] != 'x':
return 3
elif locations [6] == 'o' and locations [7] == 'o' and locations [8] != 'x':
return 8
elif locations [6] == 'o' and locations [8] == 'o' and locations [7] != 'x':
return 7
elif locations [7] == 'o' and locations [8] == 'o' and locations [6] != 'x':
return 6
elif locations [0] == 'o' and locations [3] == 'o' and locations [6] != 'x':
return 6
elif locations [0] == 'o' and locations [6] == 'o' and locations [3] != 'x':
return 3
elif locations [3] == 'o' and locations [6] == 'o' and locations [0] != 'x':
return 0
elif locations [1] == 'o' and locations [4] == 'o' and locations [7] != 'x':
return 7
elif locations [1] == 'o' and locations [7] == 'o' and locations [4] != 'x':
return 4
elif locations [4] == 'o' and locations [7] == 'o' and locations [1] != 'x':
return 1
elif locations [2] == 'o' and locations [5] == 'o' and locations [8] != 'x':
return 8
elif locations [2] == 'o' and locations [8] == 'o' and locations [5] != 'x':
return 5
elif locations [5] == 'o' and locations [8] == 'o' and locations [2] != 'x':
return 2
elif locations [0] == 'o' and locations [4] == 'o' and locations [8] != 'x':
return 8
elif locations [0] == 'o' and locations [8] == 'o' and locations [4] != 'x':
return 4
elif locations [4] == 'o' and locations [8] == 'o' and locations [0] != 'x':
return 0
elif locations [2] == 'o' and locations [4] == 'o' and locations [6] != 'x':
return 6
elif locations [2] == 'o' and locations [6] == 'o' and locations [4] != 'x':
return 4
elif locations [4] == 'o' and locations [6] == 'o' and locations [2] != 'x':
return 2
else :
open_locations = []
for x in range (9):
if locations [x-1] != 'o' and locations [x-1] != 'x':
open_locations.append (x-1)
if len (open_locations) == 8 and locations [4] == 'o':
angles = (0,2,6,8)
return choice (angles)
if open_locations :
return choice (open_locations)
else :
return 100
def play_with_robot (x,y):
if window.bgcolor () == 'indian red':
returns = playing (o_write, x, y)
window.update ()
sleep (0.1)
if returns == 1:
pass
else :
window.bgcolor ('light blue')
win = if_win ()
if win == 'o':
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('red')
write.write ('you win !!',align='center',font=('Aerial', 30 ,'normal'))
elif win == 0:
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('green')
write.write (" it's a tie !! ",align='center',font=('Aerial', 30 ,'normal'))
if window.bgcolor () == 'light blue':
sleep (.5)
number = (0,1,2,3,4,5,6,7,8)
re = robot_choice ()
if re != 100:
returns = playing (x_write,locations [re][0] ,locations [re][1] + 140)
window.update ()
if returns == 1:
pass
else :
window.bgcolor ('indian red')
win = if_win ()
if win == 'x':
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('blue')
write.write ('robot win !!',align='center',font=('Aerial', 30 ,'normal'))
elif win == 0:
window.bgcolor ('yellow')
write.penup ()
write.goto (0,450)
write.pendown ()
write.color ('green')
write.write (" it's a tie !! ",align='center',font=('Aerial', 30 ,'normal'))
def choi (x,y):
if -250 < y < -150:
write.clear ()
window.bgcolor ('indian red')
create_screen (write)
window.onscreenclick (play_with_robot)
elif -400 < y < -300 :
write.clear ()
window.bgcolor ('indian red')
create_screen (write)
window.onscreenclick (play_with_friend)
write.penup ()
write.goto (0,400)
write.write ('do you want to play with:',align='center',font=('Aerial',15,'normal'))
write.goto (0,-50)
write.write (' x o \ngame',align='center',font=('Aerial',45,'normal'))
write.goto (-150,-200)
write.write ('1-Robot',align='center',font=('Aerial',25,'normal'))
write.goto (-150,-350)
write.write ('2-Friend',align='center',font=('Aerial',25,'normal'))
window.onscreenclick (choi)
while True :
window.update ()
window.exitonclick ()
احتمال لو انت علي الكوبيوتر او اللابتوب تحتاج تغير بعض الاشياء مثل حجم الاكس والاوه وبعض الاحداثيات (افضل تطبيق تشغل عليه الكود هو pydroid3)