项目输出
项目先决条件
要使用python构建井字游戏,我们需要tkinter模块和python的基本概念
tkinter模块是用于渲染图形的标准图形用户界面。
tkinter.messagebox用于显示消息框
要安装tkinter模块,我们在命令提示符下使用了pip install命令:
1
|
pip install tkinter |
项目文件结构
这些是使用python构建井字游戏的步骤:
- 导入模块
- 初始化窗口
- 检查结果的功能
- 检查获胜者的功能
- 定义标签和按钮
1.导入模块
1
2
|
from tkinter import * import tkinter.messagebox as msg |
在此步骤中,我们导入tkinter和messsagebox模块
2.初始化窗口
1
2
3
4
5
6
7
|
root = tk() root.title( 'tic-tac-toe---dataflair' ) digits = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] mark = '' “ count = 0 panels = [ "panel" ] * 10 |
- tk()用于初始化窗口
- title()用于设置窗口的标题
3.检查结果的功能
1
2
3
4
5
6
7
8
9
|
def win(panels,sign): return ((panels[ 1 ] = = panels[ 2 ] = = panels [ 3 ] = = sign) or (panels[ 1 ] = = panels[ 4 ] = = panels [ 7 ] = = sign) or (panels[ 1 ] = = panels[ 5 ] = = panels [ 9 ] = = sign) or (panels[ 2 ] = = panels[ 5 ] = = panels [ 8 ] = = sign) or (panels[ 3 ] = = panels[ 6 ] = = panels [ 9 ] = = sign) or (panels[ 3 ] = = panels[ 5 ] = = panels [ 7 ] = = sign) or (panels[ 4 ] = = panels[ 5 ] = = panels [ 6 ] = = sign) or (panels[ 7 ] = = panels[ 8 ] = = panels [ 9 ] = = sign)) |
在此功能中,将通过检查哪个玩家连续打出三个标记(上,下,对角或对角线)来检查结果。
4.检查获胜者的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
def checker(digit): global count, mark, digits if digit = = 1 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mar button1.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 2 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button2.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 3 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button3.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 4 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button4.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 5 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button5.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 6 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button6.config(text = mark) count = count + 1 sign if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 7 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button7.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 8 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button8.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 9 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button9.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if (count> 8 and win(panels, 'x' ) = = false and win(panels, 'o' ) = = false): msg.showinfo( "result" , "match tied" ) root.destroy() |
玩家总共有9次点击以玩游戏。玩家每次单击时,如果count的值大于8,则通过将count的值增加1来减少机会,则游戏结果为平局
- 如果count的值为偶数,则玩家1将玩,否则玩家2将玩。
- config()用于用适当的文本标记按钮
- messagebox小部件中的showinfo()方法用于显示一些相关信息
- destroy()停止mainloop退出程序
5.定义标签和按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
label(root,text = "player1 : x" ,font = "times 15" ).grid(row = 0 ,column = 1 ) label(root,text = "player2 : o" ,font = "times 15" ).grid(row = 0 ,column = 2 ) button1 = button(root,width = 15 ,font = ( 'times 16 bold' ),height = 7 ,command = lambda :checker( 1 )) button1.grid(row = 1 ,column = 1 ) button2 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda :checker( 2 )) button2.grid(row = 1 ,column = 2 ) button3 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 3 )) button3.grid(row = 1 ,column = 3 ) button4 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 4 )) button4.grid(row = 2 ,column = 1 ) button5 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 5 )) button5.grid(row = 2 ,column = 2 ) button6 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 6 )) button6.grid(row = 2 ,column = 3 ) button7 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 7 )) button7.grid(row = 3 ,column = 1 ) button8 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 8 )) button8.grid(row = 3 ,column = 2 ) button9 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 9 )) button9.grid(row = 3 ,column = 3 ) root.mainloop() |
label()小部件,用于显示用户无法修改的文本。
button()小部件显示按钮
- root是我们引用的窗口的名称
- 文本存储我们在标签上显示的值
- 文字所使用的字体
- 单击按钮时将调用命令
- lambda()函数用于将特定数据发送到回调函数。
要运行程序时,将执行mainloop()方法。
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
from tkinter import * import tkinter.messagebox as msg root = tk() root.title( 'tic-tac-toe---project gurukul' ) #labels label(root,text = "player1 : x" ,font = "times 15" ).grid(row = 0 ,column = 1 ) label(root,text = "player2 : o" ,font = "times 15" ).grid(row = 0 ,column = 2 ) digits = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] #for player1 sign = x and for player2 sign= y mark = '' #counting the no. of click count = 0 panels = [ "panel" ] * 10 def win(panels,sign): return ((panels[ 1 ] = = panels[ 2 ] = = panels [ 3 ] = = sign) or (panels[ 1 ] = = panels[ 4 ] = = panels [ 7 ] = = sign) or (panels[ 1 ] = = panels[ 5 ] = = panels [ 9 ] = = sign) or (panels[ 2 ] = = panels[ 5 ] = = panels [ 8 ] = = sign) or (panels[ 3 ] = = panels[ 6 ] = = panels [ 9 ] = = sign) or (panels[ 3 ] = = panels[ 5 ] = = panels [ 7 ] = = sign) or (panels[ 4 ] = = panels[ 5 ] = = panels [ 6 ] = = sign) or (panels[ 7 ] = = panels[ 8 ] = = panels [ 9 ] = = sign)) def checker(digit): global count, mark, digits #check which button clicked if digit = = 1 and digit in digits: digits.remove(digit) ##player1 will play if the value of count is even and for odd player2 will play if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button1.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 2 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button2.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 3 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button3.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 4 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button4.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 5 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button5.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 6 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button6.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 7 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button7.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 8 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button8.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() if digit = = 9 and digit in digits: digits.remove(digit) if count % 2 = = 0 : mark = 'x' panels[digit] = mark elif count % 2 ! = 0 : mark = 'o' panels[digit] = mark button9.config(text = mark) count = count + 1 sign = mark if (win(panels,sign) and sign = = 'x' ): msg.showinfo( "result" , "player1 wins" ) root.destroy() elif (win(panels,sign) and sign = = 'o' ): msg.showinfo( "result" , "player2 wins" ) root.destroy() ###if count is greater then 8 then the match has been tied if (count> 8 and win(panels, 'x' ) = = false and win(panels, 'o' ) = = false): msg.showinfo( "result" , "match tied" ) root.destroy() ####define buttons button1 = button(root,width = 15 ,font = ( 'times 16 bold' ),height = 7 ,command = lambda :checker( 1 )) button1.grid(row = 1 ,column = 1 ) button2 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda :checker( 2 )) button2.grid(row = 1 ,column = 2 ) button3 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 3 )) button3.grid(row = 1 ,column = 3 ) button4 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 4 )) button4.grid(row = 2 ,column = 1 ) button5 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 5 )) button5.grid(row = 2 ,column = 2 ) button6 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 6 )) button6.grid(row = 2 ,column = 3 ) button7 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 7 )) button7.grid(row = 3 ,column = 1 ) button8 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 8 )) button8.grid(row = 3 ,column = 2 ) button9 = button(root,width = 15 ,height = 7 ,font = ( 'times 16 bold' ),command = lambda : checker( 9 )) button9.grid(row = 3 ,column = 3 ) root.mainloop() |
以上就是python实现简单的井字棋游戏的详细内容,更多关于python 井字棋游戏的资料请关注服务器之家其它相关文章!
原文链接:https://data-flair.training/blogs/python-tic-tac-toe/