1
+ import random
2
+ from tkinter import *
3
+ from tkinter .font import BOLD
4
+
5
+ def boardLayout ():
6
+ global w
7
+ label .config (text = "Game begins with " + w )
8
+ for row in range (3 ):
9
+ for column in range (3 ):
10
+ a [row ][column ].config (text = "." ,bg = "white" , state = NORMAL ,font = BOLD )
11
+
12
+ def chance (i ,j ):
13
+ global w
14
+ a [i ][j ].config (text = w )
15
+ a [i ][j ].config (state = DISABLED )
16
+ if w == "X" :
17
+ w = "O"
18
+ else :
19
+ w = "X"
20
+ label .config (text = "Chance of " + w )
21
+ c = checkWinCondition ()
22
+ if (c == 1 and checkFull ()== 1 ):
23
+ label .config (text = "Match Draw!" )
24
+ def checkFull ():
25
+ for i in range (3 ):
26
+ for j in range (3 ):
27
+ if (a [i ][j ]["text" ]== "." ):
28
+ return 0
29
+ return 1
30
+ def checkWinCondition ():
31
+ if (a [0 ][0 ]["text" ]== a [0 ][1 ]["text" ]== a [0 ][2 ]["text" ]!= "." ):
32
+ a [0 ][0 ].config (bg = "green" )
33
+ a [0 ][1 ].config (bg = "green" )
34
+ a [0 ][2 ].config (bg = "green" )
35
+ disableButtons ()
36
+ label .config (text = "Game over." + a [0 ][0 ]["text" ]+ " wins!" )
37
+ return 0
38
+ elif (a [0 ][0 ]["text" ]== a [1 ][0 ]["text" ]== a [2 ][0 ]["text" ]!= "." ):
39
+ a [0 ][0 ].config (bg = "green" )
40
+ a [1 ][0 ].config (bg = "green" )
41
+ a [2 ][0 ].config (bg = "green" )
42
+ disableButtons ()
43
+ label .config (text = "Game over." + a [0 ][0 ]["text" ]+ " wins!" )
44
+ return 0
45
+ elif (a [0 ][0 ]["text" ]== a [1 ][1 ]["text" ]== a [2 ][2 ]["text" ]!= "." ):
46
+ a [0 ][0 ].config (bg = "green" )
47
+ a [1 ][1 ].config (bg = "green" )
48
+ a [2 ][2 ].config (bg = "green" )
49
+ disableButtons ()
50
+ label .config (text = "Game over." + a [0 ][0 ]["text" ]+ " wins!" )
51
+ return 0
52
+ elif (a [1 ][0 ]["text" ]== a [1 ][1 ]["text" ]== a [1 ][2 ]["text" ]!= "." ):
53
+ a [1 ][0 ].config (bg = "green" )
54
+ a [1 ][1 ].config (bg = "green" )
55
+ a [1 ][2 ].config (bg = "green" )
56
+ disableButtons ()
57
+ label .config (text = "Game over." + a [1 ][0 ]["text" ]+ " wins!" )
58
+ return 0
59
+ elif (a [2 ][0 ]["text" ]== a [2 ][1 ]["text" ]== a [2 ][2 ]["text" ]!= "." ):
60
+ a [2 ][0 ].config (bg = "green" )
61
+ a [2 ][1 ].config (bg = "green" )
62
+ a [2 ][2 ].config (bg = "green" )
63
+ disableButtons ()
64
+ label .config (text = "Game over." + a [2 ][0 ]["text" ]+ " wins!" )
65
+ return 0
66
+ elif (a [0 ][2 ]["text" ]== a [1 ][1 ]["text" ]== a [2 ][0 ]["text" ]!= "." ):
67
+ a [0 ][2 ].config (bg = "green" )
68
+ a [1 ][1 ].config (bg = "green" )
69
+ a [2 ][0 ].config (bg = "green" )
70
+ disableButtons ()
71
+ label .config (text = "Game over." + a [0 ][2 ]["text" ]+ " wins!" )
72
+ return 0
73
+ elif (a [0 ][1 ]["text" ]== a [1 ][1 ]["text" ]== a [2 ][1 ]["text" ]!= "." ):
74
+ a [0 ][1 ].config (bg = "green" )
75
+ a [1 ][1 ].config (bg = "green" )
76
+ a [2 ][1 ].config (bg = "green" )
77
+ disableButtons ()
78
+ label .config (text = "Game over." + a [0 ][1 ]["text" ]+ " wins!" )
79
+ return 0
80
+ elif (a [0 ][2 ]["text" ]== a [1 ][2 ]["text" ]== a [2 ][2 ]["text" ]!= "." ):
81
+ a [2 ][2 ].config (bg = "green" )
82
+ a [0 ][2 ].config (bg = "green" )
83
+ a [1 ][2 ].config (bg = "green" )
84
+ disableButtons ()
85
+ label .config (text = "Game over." + a [0 ][2 ]["text" ]+ " wins!" )
86
+ return 0
87
+ return 1
88
+ def toss ():
89
+ d = random .randint (0 ,1 )
90
+ if (d == 0 ):
91
+ return "O"
92
+ else :
93
+ return "X"
94
+
95
+ def play ():
96
+ global w
97
+ w = toss ()
98
+ boardLayout ()
99
+
100
+ def disableButtons ():
101
+ for i in range (3 ):
102
+ for j in range (3 ):
103
+ a [i ][j ].config (state = DISABLED )
104
+
105
+ a = [[0 ,0 ,0 ],
106
+ [0 ,0 ,0 ],
107
+ [0 ,0 ,0 ]]
108
+
109
+ window = Tk ()
110
+ window .title ("Multiplayer" )
111
+ window .geometry ("250x230" )
112
+
113
+
114
+ label = Label (text = " " , font = ('Snap ITC' ,15 ))
115
+ label .pack (side = "top" )
116
+
117
+ frame = Frame (window )
118
+ frame .pack ()
119
+
120
+ for row in range (3 ):
121
+ for column in range (3 ):
122
+ a [row ][column ] = Button (frame , text = "." ,font = ('Nordic Light' ,40 ), width = 5 , height = 2 ,command = lambda row = row ,column = column : chance (row ,column ))
123
+ a [row ][column ].grid (row = row ,column = column )
124
+
125
+ reset_button = Button (text = "restart" , font = ('Baskerville Old Face' ,15 ), command = play )
126
+ reset_button .pack (side = "top" )
127
+ window .resizable (False ,False )
128
+
129
+ play ()
130
+ window .mainloop ()
0 commit comments