لعبه x , o (للجوالات و الكومبيوترات واللابتوبات)

واخيرا بعد 823 سنه من العمل و59240 سطر من الكود (ههه) عملت لعبه x , o و طبعا بتشتغل على التليفونات (لاني شغال على تليفون :joy:) وإليكم الكود:

الملف الرئيسي وكود اللعبه وملف اللعب مع الروبوت وكود اللعب مع الصديق و كود كل حاجه :

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)

16 إعجابًا

ملاحظه : اذا كنت تشغل الكود من الكومبيوتر او اللابتوب تحتاج الضغط على زر الماوس الأيمن وإذا كنت تشغل الكود من التليفون تحتاج الضغط باصبعك

7 إعجابات

ما شاء الله جميلة جدا :heart:
ممكن تبعتلنا الملف التشغيلي

7 إعجابات

للاسف مش عارف اعمل ملف تشغيلي من الهاتف :disappointed_relieved:

8 إعجابات

ما انتبهت أنك قلت ولا يهمك

7 إعجابات

اللهم بارك جميل جدا مبدع
واضح إنك عملت فيها مجهود كبير

7 إعجابات

اشتغلت عليها 823 سنه ههههه :joy:

7 إعجابات

وشكرا لك علي تجربه اللعبه

7 إعجابات

جميل اخي بارك الله فيك اللعبة اكثر من رائعة ومبتكرة بالفعل تجربة ممتعة
وخصوصا لوجود الخيارين اللعب مع الكمبيوتر واللعبة مع الشخص اخر احسنت العمل استمر على نفس المنوال وباذن الله تصل الى ما هو اكبر

بالفعل تجربة ممتعة ورائعة

8 إعجابات

ما أجمل ذلك الشعور…شعور الإنجاز والتغلب على التحديات…مبروك عليك هذا إنجاز عليك الإحتفال به ومكافأة النفس :star_struck:

8 إعجابات

شكرا جزيلا اخي بدر لتجربه اللعبه :rose: والحمد لله ان اللعبه عجبتك :heart: ، عملت اللعبه دي بفضل الله ثم @Mhmd لانه عرفني على داله(onscreenclick / onclick) وإلا كان زماني معتزل لحد ما اجيب كومبيوتر او لابتوب.

6 إعجابات

شكرا جزيلا اخي @Coder :rose:بالفعل انه شعور جميل ان تتذوق طعم النجاح بعد فتره من التعب وشكرا لك علي لعب اللعبه :heart:

7 إعجابات

بسم الله مشاء الله الله ينور

7 إعجابات

شكرا لكلامك الجميل :tulip:

6 إعجابات

اللهم بارك
مجهود رائع
ربنا يزيدك

من الحاجات اللى عجبتنى جدا … ان عمرك 12 سنة … وبتقضى وقتك فى شئ مفيد … وعملت شغل ممتاز

حقيقى مبسوط بيك :heart::heart::heart::heart::heart::heart:

8 إعجابات

شكرا جزيلا لك اخي (الكبير) @Haytham_Amer علي اعجابك بالعبه :heart: ، ليس العمر هو المهم بل المهم هو ما تفعله في العمر :smiley:

8 إعجابات

هل عندك صورة كيف تشتغل على الهاتف؟

7 إعجابات

اللهم بارك العمر صغير لكن الفعل كبير
بارك الله فيك

6 إعجابات

جميل جدا جدا بارك الله فيك
اجمل حركة انك مسوي يتغير اللون لما يلعب الروبوت
الخلفية ايضا رائعة

7 إعجابات

التعب يزول عند النتيجة واول راتب :joy:

6 إعجابات