Advertisement
Guest User

xo

a guest
Apr 6th, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. import random
  4.  
  5.  
  6. root = Tk()
  7. root.title("tic-tac-toe")
  8. root.geometry('340x400')
  9. field = []
  10. gamerun = True
  11. numturn = 0
  12. level = 1
  13.  
  14. def level_1():
  15.     global level
  16.     level = 1
  17.     newgame()
  18.  
  19. def level_2():
  20.     global level
  21.     level = 2
  22.     newgame()
  23.    
  24. def newgame():
  25.     for row in range(3):
  26.         for col in range(3):
  27.             field[row][col]['text'] = ''
  28.             field[row][col]['background'] = 'lightblue'
  29.     global gamerun
  30.     gamerun = True
  31.     global numturn
  32.     numturn = 0
  33.  
  34.  
  35. def click(row, col):
  36.     if gamerun and field[row][col]['text'] == '':
  37.         field[row][col]['text'] = 'X'
  38.         global numturn
  39.         numturn += 1
  40.         checkwin('X')
  41.     if numturn != 9 and gamerun:
  42.         if level == 1:
  43.             computer_move_1()
  44.         if level == 2:
  45.             computer_move_2()
  46.         checkwin('0')
  47.     if numturn == 9 and gamerun:
  48.         messagebox.showinfo('Game over', 'Draw')
  49.  
  50.  
  51. def checkwin(smb):
  52.     checkline(field[0][0], field[0][1], field[0][2], smb)
  53.     checkline(field[1][0], field[1][1], field[1][2], smb)
  54.     checkline(field[2][0], field[2][1], field[2][2], smb)
  55.     checkline(field[0][0], field[1][0], field[2][0], smb)
  56.     checkline(field[0][1], field[1][1], field[2][1], smb)
  57.     checkline(field[0][2], field[1][2], field[2][2], smb)
  58.     checkline(field[0][0], field[1][1], field[2][2], smb)
  59.     checkline(field[0][2], field[1][1], field[2][0], smb)
  60.  
  61.  
  62.  
  63. def checkline(b1, b2, b3, smb):
  64.     if b1['text'] == smb and b2['text'] == smb and b3['text'] == smb:
  65.         b1['background'] = b2['background'] = b3['background'] = 'pink'
  66.         global gamerun
  67.         gamerun = False
  68.         if smb == 'X':
  69.             messagebox.showinfo('Game over', 'X wins!')
  70.         else:
  71.             messagebox.showinfo('Game over', '0 wins!')
  72.  
  73.  
  74. def computer_move_1():
  75.     global numturn
  76.     turn = random.randint(1, 9 - numturn)
  77.     numempty = 0
  78.     for row in range(3):
  79.         for col in range(3):
  80.             if field[row][col]['text'] == '':
  81.                 numempty += 1
  82.                 if numempty == turn:
  83.                     field[row][col]['text'] = '0'
  84.                     numturn += 1
  85.                     checkwin('0')
  86.                     break
  87.         if numempty == turn:
  88.             break
  89.  
  90. def computer_move_2():
  91.     checktwozeros('0')
  92.     if gamerun:
  93.         computer_move_1()
  94.    
  95. def checktwozeros(smb):
  96.     checkline2(field[0][0], field[0][1], field[0][2], smb)
  97.     checkline2(field[1][0], field[1][1], field[1][2], smb)
  98.     checkline2(field[2][0], field[2][1], field[2][2], smb)
  99.     checkline2(field[0][0], field[1][0], field[2][0], smb)
  100.     checkline2(field[0][1], field[1][1], field[2][1], smb)
  101.     checkline2(field[0][2], field[1][2], field[2][2], smb)
  102.     checkline2(field[0][0], field[1][1], field[2][2], smb)
  103.     checkline2(field[0][2], field[1][1], field[2][0], smb)
  104.    
  105. def checkline2(b1, b2, b3, smb):
  106.     if (b1['text'] == smb and b2['text'] == smb and b3['text'] == '') or (b1['text'] == smb and b2['text'] == '' and b3['text'] == smb) or (b1['text'] == '' and b2['text'] == smb and b3['text'] == smb):
  107.         b1['text'] = b2['text'] = b3['text'] = smb
  108.         b1['background'] = b2['background'] = b3['background'] = 'pink'
  109.         global gamerun
  110.         gamerun = False
  111.         if smb == 'X':
  112.             messagebox.showinfo('Game over', 'X wins!')
  113.         else:
  114.             messagebox.showinfo('Game over', '0 wins!')
  115.        
  116. for row in range(3):
  117.     line = []
  118.     for col in range(3):
  119.         button = Button(root, text = '', width = 15, heigh = 7, background = 'lightblue', command = lambda row = row, col = col: click(row, col))
  120.         button.grid(row = row, column = col, sticky = 'nsew')
  121.         line.append(button)
  122.     field.append(line)
  123.  
  124.  
  125. level_easy_button = Button(root, text = 'Level Easy', command = level_1)
  126. level_easy_button.grid(row = 4, column = 0, columnspan = 3, sticky = 'nsew')
  127. level_med_button = Button(root, text = 'Level Medium', command = level_2)
  128. level_med_button.grid(row = 5, column = 0, columnspan = 3, sticky = 'nsew')
  129. #level_easy_button = Button(root, text = 'Level Easy', command = level_3)
  130. #level_easy_button.grid(row = 4, column = 0, columnspan = 3, sticky = 'nsew')
  131. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement