|
| 1 | +import tkinter |
| 2 | +from tkinter import * |
| 3 | +import os |
| 4 | +import time |
| 5 | + |
| 6 | +def new_game(r): |
| 7 | + if r == 0: |
| 8 | + sys.exit() |
| 9 | + elif r == 2: |
| 10 | + if player == 'X': |
| 11 | + win_game.configure(text ='O',fg = 'red') |
| 12 | + else: |
| 13 | + win_game.configure(text='X', fg='red') |
| 14 | + else: |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +def new_game_t(): |
| 19 | + matrix = [[0, 0, 0], |
| 20 | + [0, 0, 0], |
| 21 | + [0, 0, 0]] |
| 22 | + |
| 23 | + matrix_1 = [[0, 0, 0], |
| 24 | + [0, 0, 0], |
| 25 | + [0, 0, 0]] |
| 26 | + |
| 27 | + for i in range(3): |
| 28 | + for j in range(3): |
| 29 | + matrix[i][j] = Button(font=("Times", 80, "bold", 'italic'), width=4, bg='pink', |
| 30 | + command=lambda r=i, c=j: callback(r, c)) |
| 31 | + matrix[i][j].grid(row=i, column=j) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +def callback(r,c): |
| 41 | + global player |
| 42 | + if player == 'X' and matrix_1[r][c]==0 and stop_game == False : |
| 43 | + matrix[r][c].configure(text = 'X',fg = 'blue',bg = 'white') |
| 44 | + matrix_1[r][c] = 'X' |
| 45 | + player='O' |
| 46 | + |
| 47 | + if player == 'O' and matrix_1[r][c] ==0 and stop_game ==False : |
| 48 | + matrix[r][c].configure(text='O', fg='red', bg='white') |
| 49 | + matrix_1[r][c] = 'O' |
| 50 | + player = 'X' |
| 51 | + |
| 52 | + |
| 53 | + winner() |
| 54 | + # new_game(player) |
| 55 | + |
| 56 | +def winner(): |
| 57 | + global stop_game |
| 58 | + for i in range(3): |
| 59 | + if matrix_1[i][0] == matrix_1[i][1] == matrix_1[i][2] != 0: |
| 60 | + matrix[i][0].configure(bg = 'yellow') |
| 61 | + matrix[i][1].configure(bg = 'yellow') |
| 62 | + matrix[i][2].configure(bg = 'yellow') |
| 63 | + stop_game = True |
| 64 | + |
| 65 | + for i in range(3): |
| 66 | + if matrix_1[0][i] == matrix_1[1][i] == matrix_1[2][i] != 0: |
| 67 | + matrix[0][i].configure(bg = 'yellow') |
| 68 | + matrix[1][i].configure(bg = 'yellow') |
| 69 | + matrix[2][i].configure(bg = 'yellow') |
| 70 | + stop_game = True |
| 71 | + |
| 72 | + if matrix_1[0][0]==matrix_1[1][1]==matrix_1[2][2]!=0: |
| 73 | + matrix[0][0].configure(bg='yellow') |
| 74 | + matrix[1][1].configure(bg='yellow') |
| 75 | + matrix[2][2].configure(bg='yellow') |
| 76 | + stop_game = True |
| 77 | + |
| 78 | + if matrix_1[2][0]==matrix_1[1][1]==matrix_1[0][2]!=0: |
| 79 | + matrix[2][0].configure(bg='yellow') |
| 80 | + matrix[1][1].configure(bg='yellow') |
| 81 | + matrix[0][2].configure(bg='yellow') |
| 82 | + stop_game = True |
| 83 | + |
| 84 | + |
| 85 | + return stop_game |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + |
| 89 | + |
| 90 | + canvas = Tk() |
| 91 | + canvas.title("TIC TAC TOE") |
| 92 | + |
| 93 | + matrix = [[0,0,0], |
| 94 | + [0,0,0], |
| 95 | + [0,0,0]] |
| 96 | + |
| 97 | + matrix_1 = [[0,0,0], |
| 98 | + [0,0,0], |
| 99 | + [0,0,0]] |
| 100 | + |
| 101 | + for i in range(3): |
| 102 | + for j in range(3): |
| 103 | + matrix[i][j] = Button(font=("Times",80,"bold",'italic'),width = 4,bg = 'pink',command = lambda r=i,c=j:callback(r,c)) |
| 104 | + matrix[i][j].grid(row = i,column = j) |
| 105 | + |
| 106 | + |
| 107 | + quit_game = Button(text = 'EXIT',font = ('Times',20,'bold'),width = 10,height = 6,fg = 'red',bg = 'yellow',command =lambda r =0,:new_game(r)) |
| 108 | + quit_game.grid(row = 0,column = 4) |
| 109 | + |
| 110 | + win_game = Button(text = 'Winner',font = ('Times',20,'bold'),width = 10,height = 6,fg = 'red',bg = 'yellow',command =lambda r =2:new_game(r)) |
| 111 | + win_game.grid(row = 2,column = 4) |
| 112 | + |
| 113 | + New_game = Button(text = 'NEW GAME',font = ('Times',20,'bold'),width = 10,height = 6,fg = 'red',bg = 'yellow',command = new_game_t) |
| 114 | + New_game.grid(row = 1,column = 4) |
| 115 | + |
| 116 | + |
| 117 | + player = 'O' |
| 118 | + stop_game =False |
| 119 | + |
| 120 | + mainloop() |
| 121 | + |
0 commit comments