Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- import random
- root = Tk()
- root.title("tic-tac-toe")
- root.geometry('340x400')
- field = []
- gamerun = True
- numturn = 0
- level = 1
- def level_1():
- global level
- level = 1
- newgame()
- def level_2():
- global level
- level = 2
- newgame()
- def newgame():
- for row in range(3):
- for col in range(3):
- field[row][col]['text'] = ''
- field[row][col]['background'] = 'lightblue'
- global gamerun
- gamerun = True
- global numturn
- numturn = 0
- def click(row, col):
- if gamerun and field[row][col]['text'] == '':
- field[row][col]['text'] = 'X'
- global numturn
- numturn += 1
- checkwin('X')
- if numturn != 9 and gamerun:
- if level == 1:
- computer_move_1()
- if level == 2:
- computer_move_2()
- checkwin('0')
- if numturn == 9 and gamerun:
- messagebox.showinfo('Game over', 'Draw')
- def checkwin(smb):
- checkline(field[0][0], field[0][1], field[0][2], smb)
- checkline(field[1][0], field[1][1], field[1][2], smb)
- checkline(field[2][0], field[2][1], field[2][2], smb)
- checkline(field[0][0], field[1][0], field[2][0], smb)
- checkline(field[0][1], field[1][1], field[2][1], smb)
- checkline(field[0][2], field[1][2], field[2][2], smb)
- checkline(field[0][0], field[1][1], field[2][2], smb)
- checkline(field[0][2], field[1][1], field[2][0], smb)
- def checkline(b1, b2, b3, smb):
- if b1['text'] == smb and b2['text'] == smb and b3['text'] == smb:
- b1['background'] = b2['background'] = b3['background'] = 'pink'
- global gamerun
- gamerun = False
- if smb == 'X':
- messagebox.showinfo('Game over', 'X wins!')
- else:
- messagebox.showinfo('Game over', '0 wins!')
- def computer_move_1():
- global numturn
- turn = random.randint(1, 9 - numturn)
- numempty = 0
- for row in range(3):
- for col in range(3):
- if field[row][col]['text'] == '':
- numempty += 1
- if numempty == turn:
- field[row][col]['text'] = '0'
- numturn += 1
- checkwin('0')
- break
- if numempty == turn:
- break
- def computer_move_2():
- checktwozeros('0')
- if gamerun:
- computer_move_1()
- def checktwozeros(smb):
- checkline2(field[0][0], field[0][1], field[0][2], smb)
- checkline2(field[1][0], field[1][1], field[1][2], smb)
- checkline2(field[2][0], field[2][1], field[2][2], smb)
- checkline2(field[0][0], field[1][0], field[2][0], smb)
- checkline2(field[0][1], field[1][1], field[2][1], smb)
- checkline2(field[0][2], field[1][2], field[2][2], smb)
- checkline2(field[0][0], field[1][1], field[2][2], smb)
- checkline2(field[0][2], field[1][1], field[2][0], smb)
- def checkline2(b1, b2, b3, smb):
- 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):
- b1['text'] = b2['text'] = b3['text'] = smb
- b1['background'] = b2['background'] = b3['background'] = 'pink'
- global gamerun
- gamerun = False
- if smb == 'X':
- messagebox.showinfo('Game over', 'X wins!')
- else:
- messagebox.showinfo('Game over', '0 wins!')
- for row in range(3):
- line = []
- for col in range(3):
- button = Button(root, text = '', width = 15, heigh = 7, background = 'lightblue', command = lambda row = row, col = col: click(row, col))
- button.grid(row = row, column = col, sticky = 'nsew')
- line.append(button)
- field.append(line)
- level_easy_button = Button(root, text = 'Level Easy', command = level_1)
- level_easy_button.grid(row = 4, column = 0, columnspan = 3, sticky = 'nsew')
- level_med_button = Button(root, text = 'Level Medium', command = level_2)
- level_med_button.grid(row = 5, column = 0, columnspan = 3, sticky = 'nsew')
- #level_easy_button = Button(root, text = 'Level Easy', command = level_3)
- #level_easy_button.grid(row = 4, column = 0, columnspan = 3, sticky = 'nsew')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement