|
| 1 | +from tkinter import * |
| 2 | +from tkinter import messagebox |
| 3 | +import random as r |
| 4 | +import functools |
| 5 | + |
| 6 | + |
| 7 | +def button(f): |
| 8 | + button1 = Button(f, text=" ", fg="Black", font="Arial 20 bold", height=1, width=2, padx=25, pady=20, bg="White", borderwidth=0, |
| 9 | + activebackground="White", cursor="hand2", state=NORMAL) |
| 10 | + return button1 |
| 11 | + |
| 12 | + |
| 13 | +def full(): |
| 14 | + for row in range(3): |
| 15 | + for col in range(3): |
| 16 | + if b[row][col]['text'] == " ": |
| 17 | + return False |
| 18 | + return True |
| 19 | + |
| 20 | + |
| 21 | +def empty(): |
| 22 | + for row in range(3): |
| 23 | + for col in range(3): |
| 24 | + b[row][col].config(text=" ", state=NORMAL) |
| 25 | + |
| 26 | + |
| 27 | +def winner(s): |
| 28 | + return b[0][0]['text'] == b[0][1]['text'] == b[0][2]['text'] == s or b[1][0]['text'] == b[1][1]['text'] == b[1][2]['text'] == s\ |
| 29 | + or b[2][0]['text'] == b[2][1]['text'] == b[2][2]['text'] == s or b[0][0]['text'] == b[1][0]['text'] == b[2][0]['text'] == s\ |
| 30 | + or b[0][1]['text'] == b[1][1]['text'] == b[2][1]['text'] == s or b[0][2]['text'] == b[1][2]['text'] == b[2][2]['text'] == s\ |
| 31 | + or b[0][0]['text'] == b[1][1]['text'] == b[2][2]['text'] == s or b[0][2]['text'] == b[1][1]['text'] == b[2][0]['text'] == s |
| 32 | + |
| 33 | + |
| 34 | +user_checked = [] |
| 35 | +com_checked = [] |
| 36 | + |
| 37 | + |
| 38 | +def click(event, param1, param2): |
| 39 | + sign = 0 |
| 40 | + if (param1, param2) in user_checked or (param1, param2) in com_checked: |
| 41 | + messagebox.showwarning("Warning", "Select Another Box.") |
| 42 | + else: |
| 43 | + user_checked.append((param1, param2)) |
| 44 | + b[param1][param2]['text'] = 'O' |
| 45 | + |
| 46 | + if winner("O"): |
| 47 | + messagebox.showinfo("Winner", "You Won...") |
| 48 | + user_score.set(user_score.get() + 1) |
| 49 | + empty() |
| 50 | + user_checked.clear() |
| 51 | + com_checked.clear() |
| 52 | + sign = 1 |
| 53 | + |
| 54 | + if full(): |
| 55 | + messagebox.showinfo("Tie", "Match Tie...!") |
| 56 | + tie_score.set(tie_score.get() + 1) |
| 57 | + empty() |
| 58 | + user_checked.clear() |
| 59 | + com_checked.clear() |
| 60 | + sign = 1 |
| 61 | + |
| 62 | + if sign == 0: |
| 63 | + unchecked = [] |
| 64 | + for row in range(3): |
| 65 | + for col in range(3): |
| 66 | + if b[row][col]['text'] == " ": |
| 67 | + unchecked.append([row, col]) |
| 68 | + index = r.sample(unchecked, 1) |
| 69 | + b[index[0][0]][index[0][1]].config(text="X", state=DISABLED) |
| 70 | + com_checked.append((index[0][0], index[0][1])) |
| 71 | + if winner("X"): |
| 72 | + messagebox.showinfo("Winner", "Computer Won...") |
| 73 | + com_score.set(com_score.get() + 1) |
| 74 | + empty() |
| 75 | + user_checked.clear() |
| 76 | + com_checked.clear() |
| 77 | + else: |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +root = Tk() |
| 82 | +root.title("Tic Tac Toe") |
| 83 | +root.geometry("420x500") |
| 84 | +root.resizable(0, 0) |
| 85 | +root.configure(bg="Black") |
| 86 | +Label(root, text="TIC TAC TOE", fg="White", bg="Black", font="times 25 bold").pack() |
| 87 | + |
| 88 | +Frame(root, bg="White").pack(fill=X, pady=10) |
| 89 | +frame = Frame(root, bg="Black") |
| 90 | +Label(frame, text="YOU", fg="Yellow", bg="Black", font="System 10 bold", padx=50, pady=5).pack(side=LEFT) |
| 91 | +Label(frame, text="TIE", fg="Yellow", bg="Black", font="System 10 bold", padx=48, pady=5).pack(side=LEFT) |
| 92 | +Label(frame, text="COMPUTER", fg="Yellow", bg="Black", font="System 10 bold", padx=30, pady=5).pack(side=LEFT) |
| 93 | +frame.pack(pady=5) |
| 94 | + |
| 95 | +frame = Frame(root, bg="Black") |
| 96 | +user_score = IntVar() |
| 97 | +tie_score = IntVar() |
| 98 | +com_score = IntVar() |
| 99 | +user_score.set(0) |
| 100 | +tie_score.set(0) |
| 101 | +com_score.set(0) |
| 102 | +user = Entry(frame, textvariable=user_score, fg="white", bg="Black", font="System 10 bold", width=10, borderwidth=0, |
| 103 | + justify=CENTER) |
| 104 | +user.pack(side=LEFT, padx=35) |
| 105 | +tie = Entry(frame, textvariable=tie_score, fg="white", bg="Black", font="System 10 bold", width=10, borderwidth=0, |
| 106 | + justify=CENTER) |
| 107 | +tie.pack(side=LEFT, padx=15) |
| 108 | +com = Entry(frame, textvariable=com_score, fg="white", bg="Black", font="System 10 bold", width=10, borderwidth=0, |
| 109 | + justify=CENTER) |
| 110 | +com.pack(side=LEFT, padx=50) |
| 111 | +frame.pack(pady=10) |
| 112 | + |
| 113 | +Frame(root, bg="White").pack(fill=X, pady=15) |
| 114 | + |
| 115 | +b = [[], [], []] |
| 116 | +for i in range(3): |
| 117 | + frame = Frame(root, highlightbackground="Black", highlightthickness=3, bg="Black") |
| 118 | + for j in range(3): |
| 119 | + b[i].append(button(frame)) |
| 120 | + b[i][j].bind("<Button-1>", functools.partial(click, param1=i, param2=j)) |
| 121 | + b[i][j].pack(side=LEFT, padx=5) |
| 122 | + frame.pack() |
| 123 | + |
| 124 | +root.mainloop() |
0 commit comments