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.  
  9. win = 0
  10. lose = 0
  11.  
  12. def new_game():
  13.     for row in range(3):
  14.         for col in range(3):
  15.             field[row][col]['text'] = ' '
  16.             field[row][col]['background'] = 'lavender'
  17.     global game_run
  18.     game_run = True
  19.     global cross_count
  20.     cross_count = 0
  21.  
  22. def click(row, col):
  23.     if game_run and field[row][col]['text'] == ' ':
  24.         field[row][col]['text'] = '0'
  25.         global cross_count
  26.         cross_count += 1
  27.         check_win('0')
  28.         if game_run and cross_count < 5:
  29.             computer_move()
  30.             check_win('X')
  31.  
  32. def check_win(smb):
  33.     for n in range(3):
  34.         check_line(field[n][0], field[n][1], field[n][2], smb)
  35.         check_line(field[0][n], field[1][n], field[2][n], smb)
  36.     check_line(field[0][0], field[1][1], field[2][2], smb)
  37.     check_line(field[2][0], field[1][1], field[0][2], smb)
  38.  
  39.  
  40. def check_line(a1,a2,a3,smb):
  41.     if a1['text'] == smb and a2['text'] == smb and a3['text'] == smb:
  42.         a1['background'] = a2['background'] = a3['background'] = 'pink'
  43.         global game_run
  44.         if smb == '0':
  45.             global win
  46.             win+=1
  47.         else:
  48.             global lose
  49.             lose += 1
  50.         game_run = False
  51.         global label
  52.         label.configure(text=f'win {win} lose {lose}')
  53.  
  54.  
  55. def can_win(a1, a2, a3, smb):
  56.     res = False
  57.     if a1['text'] == smb and a2['text'] == smb and a3['text'] == ' ':
  58.         a3['text'] = 'X'
  59.         res = True
  60.     if a1['text'] == smb and a2['text'] == ' ' and a3['text'] == smb:
  61.         a2['text'] = 'X'
  62.         res = True
  63.     if a1['text'] == ' ' and a2['text'] == smb and a3['text'] == smb:
  64.         a1['text'] = 'X'
  65.         res = True
  66.     return res
  67.  
  68.  
  69. def computer_move():
  70.     for n in range(3):
  71.         if can_win(field[n][0], field[n][1], field[n][2], 'X'):
  72.             return
  73.         if can_win(field[0][n], field[1][n], field[2][n], 'X'):
  74.             return
  75.     if can_win(field[0][0], field[1][1], field[2][2], 'X'):
  76.         return
  77.     if can_win(field[2][0], field[1][1], field[0][2], 'X'):
  78.         return
  79.     for n in range(3):
  80.         if can_win(field[n][0], field[n][1], field[n][2], '0'):
  81.             return
  82.         if can_win(field[0][n], field[1][n], field[2][n], '0'):
  83.             return
  84.     if can_win(field[0][0], field[1][1], field[2][2], '0'):
  85.         return
  86.     if can_win(field[2][0], field[1][1], field[0][2], '0'):
  87.         return
  88.     while True:
  89.         row = random.randint(0, 2)
  90.         col = random.randint(0, 2)
  91.         if field[row][col]['text'] == ' ':
  92.             field[row][col]['text'] = 'X'
  93.             break
  94. for row in range(3):
  95.     line = []
  96.     for col in range(3):
  97.         button = Button(root, text=' ', width=4, height=2,
  98.                         font=('Verdana', 20, 'bold'),
  99.                         background='lavender',
  100.                         command=lambda row=row, col=col: click(row,col))
  101.         button.grid(row=row, column=col, sticky='nsew')
  102.         line.append(button)
  103.     field.append(line)
  104. new_button = Button(root, text='new game', command=new_game)
  105. new_button.grid(row=3, column=0, columnspan=3, sticky='nsew')
  106. label = Label(text=f'win {win} lose {lose}')
  107. label.grid(row=4, column=0, columnspan=3)
  108. root.mainloop()
  109.  
  110.  

Share with your friends:

Print