Mundarija:

Python Tic Tac Toe o'yini: 4 qadam
Python Tic Tac Toe o'yini: 4 qadam

Video: Python Tic Tac Toe o'yini: 4 qadam

Video: Python Tic Tac Toe o'yini: 4 qadam
Video: 0 x 0 o'yini Python dasturlash tili orqali yaratilishi Sogirtlarimizdan Masharipov Asadbekdan. 2024, Dekabr
Anonim
Python Tic Tac Toe o'yini
Python Tic Tac Toe o'yini

python tic tac toe o'yini

Bu o'yin kompyuter tili bo'lgan pythonda qilingan

Men pytharm deb nomlangan python muharriridan foydalandim, siz oddiy python kod muharriridan ham foydalanishingiz mumkin

1 -qadam: Import

Import
Import

tkinter importidan *

2 -qadam: Ildiz

Ildiz
Ildiz

ildiz = Tk ()

root.title ("3T TIC TAC TOE") # Sarlavha

text = Kirish (ildiz, shrift = ("ds-raqamli", 15)) text.pack (to'ldirish = X, padx = 5, pady = 5, ipadx = 5, ipady = 5) # Matn

taxta = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] # taxta

3 -qadam: Tahrirlovchiga qo'ygandan so'ng vazifasi ogohlantirishlarni ko'rsatadi Warrnigs -ni bosing va Assambel -ga hamma narsani bering

# Funktsiyalar

player = "X" tugmasi = stop_game = Noto'g'ri stop_game_tie = Yolg'on

def Turn (): # Chop etish Text.delete (0, END) text.insert (0, "{} 'S TURN". format (pleer))

def Exit (): root.destroy () exit ()

def Game_Over (): # Chex, agar o'yin tugasa Check_Winner ()

if stop_game == True: Hplayer () text.delete (0, END) text.insert (0, "'{}' WINNER". format (o'yinchi))

boshqa: Check_Tie ()

if stop_game_tie == To'g'ri: text.delete (0, END) text.insert (0, "TIE MATCH")

def Check_Winner (): # Global stop_game g'olibi borligini tekshiring

qator_1 = taxta [0] == taxta [1] == taxta [2]! = "-" qator_2 = taxta [3] == taxta [4] == taxta [5]! = »-" qator_3 = taxta [6] == taxta [7] == taxta [8]! = "-" agar qator_1 yoki qator_2 yoki qator_3 bo'lsa: stop_ o'yin = To'g'ri

ustun_1 = taxta [0] == taxta [3] == taxta [6]! = "-" ustun_2 = taxta [1] == taxta [4] == taxta [7]! = »-" ustun_3 = taxta [2] == taxta [5] == taxta [8]! = "-" agar ustun_1 yoki ustun_2 yoki ustun_3: stop_ o'yini = To'g'ri

diagonal_1 = taxta [0] == taxta [4] == taxta [8]! = "-" diagonal_2 = taxta [2] == taxta [4] == taxta [6]! = "-" bo'lsa diagonal_1 yoki diagonal_2: stop_game = To'g'ri

def Check_Tie (): # Uning global stop_game_tie tengligini tekshiring

agar bortda "-" bo'lmasa: stop_game_tie = To'g'ri

def Hplayer (): # Boshqarish Turn global o'yinchi

agar o'yinchi == "X": o'yinchi = "O"

boshqa: o'yinchi = "X"

def Add_Text (pos, play): # Matn qo'shish global tugmasi

Agar pos tugmachasida bo'lmasa va stop_game == False va stop_game_tie == False va pos! = 9: Turn ()

def_buttons [pos].configure (text = play) doska [pos] = pleer

button.append (pos) Hplayer () Turn () Game_Over ()

4 -qadam: Yakuniy kod oxirgi ko'rinishga ega bo'ladimi?

Yakuniy kod oxirgi ko'rinishga ega bo'ladimi?
Yakuniy kod oxirgi ko'rinishga ega bo'ladimi?
Yakuniy kod oxirgi ko'rinishga ega bo'ladimi?
Yakuniy kod oxirgi ko'rinishga ega bo'ladimi?

# GRID

def New_Match (): # Yangi Match tugmachasi global boshqaruv paneli global o'yinchi global tugmasi global stop_ o'yin global stop_ o'yin_tie

button_1.configure (text = "") button_2.configure (text = "") button_3.configure (text = "") button_4.configure (text = "") button_5.configure (text = "") button_6.configure (matn = "") button_7.configure (text = "") button_8.configure (text = "") button_9.configure (text = "") board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] o'yinchi = "X" tugmasi = stop_game = Yolg'on stop_game_tie = Noto'g'ri burilish ()

# Tugmalar

Burilish ()

ramka = Frame (root) frame.pack (yon = TOP, langar = NW)

frame1 = Frame (ramka) frame1.pack ()

button_1 = Tugma (ramka1, matn = "", kenglik = 8, balandlik = 3, buyruq = lambda: Add_Text (0, o'yinchi), bg = 'pushti') button_1.pack (yon = LEFT)

button_2 = Tugma (ramka1, matn = "", kenglik = 8, balandlik = 3, buyruq = lambda: Add_Text (1, o'yinchi), bg = 'pushti') button_2.pack (yon = LEFT)

button_3 = Tugma (ramka1, matn = "", kenglik = 8, balandlik = 3, buyruq = lambda: Add_Text (2, o'yinchi), bg = 'pushti') button_3.pack (yon = LEFT)

frame2 = Frame (ramka) frame2.pack ()

button_4 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (3, player), bg = 'blue') button_4.pack (side = LEFT)

button_5 = Tugma (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (4, player), bg = 'blue') button_5.pack (side = LEFT)

button_6 = Button (frame2, text = "", width = 8, height = 3, command = lambda: Add_Text (5, player), bg = 'blue') button_6.pack (side = LEFT)

frame3 = Frame (ramka) frame3.pack ()

button_7 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (6, player), bg = 'orange') button_7.pack (side = LEFT)

button_8 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (7, player), bg = 'orange') button_8.pack (side = LEFT)

button_9 = Button (frame3, text = "", width = 8, height = 3, command = lambda: Add_Text (8, player), bg = 'orange') button_9.pack (side = LEFT)

frame4 = Frame (ramka) frame4.pack ()

button_clear = Tugma (frame4, text = "NEW MATCH", width = 13, height = 3, command = lambda: New_Match (), bg = 'violet', foreground = 'red') button_clear.pack (yon = SOL)

exit_button = Tugma (frame4, text = "EXIT", width = 12, height = 3, command = lambda: Exit (), bg = 'green', foreground = 'orange') exit_button.pack (side = LEFT)

def_buttons = [tugma_1, tugma_2, tugma_3, tugma_4, tugma_5, tugma_6, tugma_7, tugma_8, tugma_9]

root.mainloop ()

Tavsiya: