SaveText.Ru

Без имени
  1. from tkinter import *
  2. import random
  3. root = Tk()
  4. root.title('Criss-cross')
  5. game_run = True
  6. field = []
  7. cross_count = 0
  8. def new_game():
  9.     for row in range(3):
  10.         for col in range(3):
  11.             field[row][col]['text'] = ' '
  12.             field[row][col]['background'] = 'lavender'
  13.     global game_run
  14.     game_run = True
  15.     global cross_count
  16.     cross_count = 0
  17.  
  18. def click(row, col):
  19.     if game_run and field[row][col]['text'] == ' ':
  20.         field[row][col]['text'] = 'X'
  21.         global cross_count
  22.         cross_count += 1
  23.         check_win('X')
  24.         if game_run and cross_count < 5:
  25.             computer_move()
  26.             check_win('O')
  27. def check_win(smb):
  28.     for n in range(3):
  29.         check_line(field[n][0], field[n][1], field[n][2], smb)
  30.         check_line(field[0][n], field[1][n], field[2][n], smb)
  31.     check_line(field[0][0], field[1][1], field[2][2], smb)
  32.     check_line(field[2][0], field[1][1], field[0][2], smb)
  33.  
  34. def check_line(a1,a2,a3,smb):
  35.     if a1['text'] == smb and a2['text'] == smb and a3['text'] == smb:
  36.         a1['background'] = a2['background'] = a3['background'] = 'pink'
  37.         global game_run
  38.         game_run = False
  39.  
  40.  
  41. def can_win(a1, a2, a3, smb):
  42.     res = False
  43.     if a1['text'] == smb and a2['text'] == smb and a3['text'] == ' ':
  44.         a3['text'] = 'O'
  45.         res = True
  46.     if a1['text'] == smb and a2['text'] == ' ' and a3['text'] == smb:
  47.         a2['text'] = 'O'
  48.         res = True
  49.     if a1['text'] == ' ' and a2['text'] == smb and a3['text'] == smb:
  50.         a1['text'] = 'O'
  51.         res = True
  52.     return res
  53.  
  54.  
  55. def computer_move():
  56.     for n in range(3):
  57.         if can_win(field[n][0], field[n][1], field[n][2], 'O'):
  58.             return
  59.         if can_win(field[0][n], field[1][n], field[2][n], 'O'):
  60.             return
  61.     if can_win(field[0][0], field[1][1], field[2][2], 'O'):
  62.         return
  63.     if can_win(field[2][0], field[1][1], field[0][2], 'O'):
  64.         return
  65.     for n in range(3):
  66.         if can_win(field[n][0], field[n][1], field[n][2], 'X'):
  67.             return
  68.         if can_win(field[0][n], field[1][n], field[2][n], 'X'):
  69.             return
  70.     if can_win(field[0][0], field[1][1], field[2][2], 'X'):
  71.         return
  72.     if can_win(field[2][0], field[1][1], field[0][2], 'X'):
  73.         return
  74.     while True:
  75.         row = random.randint(0, 2)
  76.         col = random.randint(0, 2)
  77.         if field[row][col]['text'] == ' ':
  78.             field[row][col]['text'] = 'O'
  79.             break
  80. for row in range(3):
  81.     line = []
  82.     for col in range(3):
  83.         button = Button(root, text=' ', width=4, height=2,
  84.                         font=('Verdana', 20, 'bold'),
  85.                         background='lavender',
  86.                         command=lambda row=row, col=col: click(row,col))
  87.         button.grid(row=row, column=col, sticky='nsew')
  88.         line.append(button)
  89.     field.append(line)
  90. new_button = Button(root, text='new game', command=new_game)
  91. new_button.grid(row=3, column=0, columnspan=3, sticky='nsew')
  92. root.mainloop()

Share with your friends:

Print