Advertisement
fevzi02

крестики-нолики Tkinter

Nov 18th, 2021
544
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. class Grafic():
  3.     def __init__(self):
  4.         self.player = "X"
  5.         self.button_number = 0
  6.         self.list_value =[ ["", "", ""], ["", "", ""], ["", "", ""] ]
  7.         self.name = []
  8.         self.root = Tk()
  9.         self.ui_frame = Frame(self.root, width=100*3,height=100*3)
  10.         self.ui_frame.grid(row=0, column=0, padx=5, pady=5)
  11.         self.root.title("Крестики нолики")
  12.         ii = 0
  13.         for i in range(3):
  14.             for j in range(3):
  15.                 b = Button(self.ui_frame, text="",font = "Arial 20",
  16.                     command=lambda ii=ii, id=i, jd=j : self.button(ii, id, jd))
  17.                 b.place(x=i*100, y=j*100, width=100, height=100)
  18.                 self.name.append(b)
  19.                 ii+=1
  20.         self.root.mainloop()
  21.  
  22.     def button(self, ii, i, j):
  23.         #if self.name[ii]["text"] != "X" and self.name[ii]["text"] != "O":
  24.         self.name[ii]["text"] = self.player
  25.         self.name[ii]["state"] = "disabled"
  26.         self.list_value[i][j] = self.player
  27.         if self.examination() == 1:
  28.             self.message(True)
  29.         elif self.examination() == 2:
  30.             self.message(False)
  31.         self.change_player()
  32.  
  33.     def change_player(self):
  34.         if self.player == 'O':
  35.             self.player = 'X'
  36.         else:
  37.             self.player = 'O'
  38.  
  39.     def examination(self):
  40.         if self.list_value[0][0] == self.list_value[1][1] and self.list_value[1][1] == self.list_value[2][2] and self.list_value[1][1] != "" :
  41.             return 1
  42.         elif self.list_value[0][2] == self.list_value[1][1] and self.list_value[1][1] == self.list_value[2][0] and self.list_value[1][1] != "":
  43.             return 1
  44.  
  45.         elif self.list_value[0][0] == self.list_value[0][1] and self.list_value[0][1] == self.list_value[0][2] and self.list_value[0][2] != "":
  46.             return 1
  47.         elif self.list_value[1][0] == self.list_value[1][1] and self.list_value[1][1] == self.list_value[1][2] and self.list_value[1][2] != "":
  48.             return 1
  49.         elif self.list_value[2][0] == self.list_value[2][1] and self.list_value[2][1] == self.list_value[2][2] and self.list_value[2][2] != "":
  50.             return 1
  51.  
  52.         elif self.list_value[0][0] == self.list_value[1][0] and self.list_value[1][0] == self.list_value[2][0] and self.list_value[2][0] != "":
  53.             return 1
  54.         elif self.list_value[0][1] == self.list_value[1][1] and self.list_value[1][1] == self.list_value[2][1] and self.list_value[2][1] != "":
  55.             return 1
  56.         elif self.list_value[0][2] == self.list_value[1][2] and self.list_value[1][2] == self.list_value[2][2] and self.list_value[2][2] != "":
  57.             return 1
  58.         else:
  59.             z = 0
  60.             for i in range(3):
  61.                 for j in range(3):
  62.                     if self.list_value[i][j] != "":
  63.                         z += 1
  64.             if z == 9 :
  65.                 return 2
  66.             else:
  67.                 return 0
  68.     def restart(self):
  69.         for i in range(9):
  70.                 self.name[i]["state"] = "active"
  71.         for i in self.name:
  72.             i["text"] = ""
  73.         self.list_value =[ ["", "", ""], ["", "", ""], ["", "", ""] ]
  74.         try:
  75.             self.root_2.destroy()
  76.         except:
  77.             pass
  78.         try:
  79.             self.but0.destroy()
  80.         except:
  81.             pass
  82.  
  83.     def message(self, bool):
  84.         for i in range(9):
  85.                 self.name[i]["state"] = "disabled"
  86.         if bool:
  87.             text_ = "Победил "+str(self.player)
  88.         else:
  89.             text_ = "Ничья"
  90.  
  91.         self.root_2 = Tk()
  92.         lab0 = Label(self.root_2, text = text_+"\n"+"_"*15, font = "Arial 20", fg="red")
  93.         lab0.grid(row=0, column=1, padx=5, pady=5, ipadx=40, ipady=20)
  94.         for i in range(3):
  95.             for j in range(3):
  96.                 lab = Label(self.root_2, text = self.list_value[i][j], font = "Arial 20")
  97.                 lab.grid(row=j+1, column=i, padx=5, pady=5, ipadx=40, ipady=20)
  98.         self.but0 = Button(self.root, text="Занаво",font = "Arial 20",bg="yellow",
  99.             command=self.restart)
  100.         self.but0.grid(row=2, column=0, padx=5, pady=5, ipadx=40, ipady=20)
  101.         but1 = Button(self.root_2, text="Занаво",font = "Arial 20",bg="yellow",
  102.             command=self.restart)
  103.         but1.grid(row=4, column=1, padx=5, pady=5, ipadx=40, ipady=20)
  104.  
  105.  
  106.     #___________________________________________________________________________
  107. g = Grafic()
  108.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement