Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Tic_Tac_Toe.py
- from tkinter import *
- import math
- import random
- root = Tk()
- root.title("Tk_Tic_Tac_Toe")
- root.geometry("+-10+0")
- canvas_width_px = 197
- canvas_height_px = 197
- canvas = {}
- def button(i, j):
- canvas[i,j] = Canvas(root, width=canvas_width_px, height=canvas_height_px, bg="white")
- canvas[i,j].bind("<Button-1>", lambda event, row=i, col=j: click(row, col))
- canvas[i,j].grid(row=i,column=j)
- def reset(e=None):
- global sss
- sss = ''
- def click(row, col):
- global play
- if '!' not in sss:
- if p == 'X':
- canvas[row, col].create_line(10, 10, canvas_width_px - 10, canvas_height_px - 10, width=7, fill=colour[p], capstyle='round')
- canvas[row, col].create_line(canvas_width_px - 10, 10, 10, canvas_height_px - 10, width=7, fill=colour[p], capstyle='round')
- else:
- canvas[row, col].create_oval(10, 10, canvas_width_px - 10, canvas_height_px - 10, width=7, outline=colour[p])
- canvas[row, col].unbind("<Button-1>")
- play = str(row*3+col+1)
- def turns():
- lbl.config(text=p+"'s Turn - Click Here To Reset")
- colour={'O':"deep sky blue",'X':"lawn green"}
- lbl=Label(font=('arial',30))
- lbl.grid(row=3,column=0,columnspan=3,sticky='EW')
- lbl.bind("<Button-1>", reset)
- Z_WIN_CHECK='123 456 789 147 258 369 159 357' ### for win checking
- play = '?'
- sss = 'go'
- while play.lower() != 'q':
- p = 'O'
- for i in range(3):
- for j in range(3):
- button(i, j)
- turns()
- win_check = Z_WIN_CHECK
- while sss:
- if play in win_check:
- win_check=win_check.replace(play, p)
- if p*3 in win_check:
- sss = "Player '"+p+"' Won !!!"
- lbl.config(text=sss)
- else:
- p = ('X' if p == 'O' else 'O')
- turns()
- play = '?'
- root.update()
- sss = 'go'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement