Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- from tkinter import *
- from random import *
- root = Tk()
- count = 0
- gameOver = False
- A = [
- [0, 0, 0],
- [0, 0, 0],
- [0, 0, 0]
- ]
- def check(A, x):
- return sum(A[0]) == x or sum(A[1]) == x or sum(A[2]) == x or (A[0][0] + A[1][0] + A[2][0] == x) or (A[0][1] + A[1][1] + A[2][1] == x) or (A[0][2] + A[1][2] + A[2][2] == x) or (A[0][0] + A[1][1] + A[2][2] == x) or (A[2][0] + A[1][1] + A[0][2] == x)
- def on_click(event):
- global tmp
- global count
- global gameOver
- t = event.widget
- i = int(t.grid_info()["row"])
- j = int(t.grid_info()["column"])
- if gameOver == False and count < 9:
- if t.cget('text') == '':
- if count % 2 == 0:
- t.config(text = 'X')
- A[i][j] = 1
- else:
- t.config(text = 'O')
- A[i][j] = 10
- count += 1
- if count % 2 == 1: # был ход крестика
- #проверка!
- if check(A, 3):
- tmp.config(text = 'Победа крестика!')
- gameOver = True
- else:# был ход нолика
- #проверка!
- if check(A, 30):
- tmp.config(text = 'Победа нолика!')
- gameOver = True
- if count == 9 and gameOver == False:
- tmp.config(text = 'Ничья')
- gameOver = True
- def create_field():
- global tmp
- for i in range(3):
- for j in range(3):
- if (i + j) % 2 == 0:
- square = Label(text = '', bg = 'skyblue', width = 2, font = 'Verdana 24')
- else:
- square = Label(text = '', bg = 'blue', width = 2, font = 'Verdana 24')
- square.bind('<Button-1>', on_click)
- square.grid(row = i, column = j)
- tmp = Label(text = 'Начнем игру!', bg = 'yellow', fg = 'green')
- tmp.grid(row = 3, columnspan = 3, sticky = W + E)
- create_field()
- root.mainloop()
Add Comment
Please, Sign In to add comment