-
Notifications
You must be signed in to change notification settings - Fork 0
/
井字棋.py
218 lines (208 loc) · 5.97 KB
/
井字棋.py
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
import sys
import time as t
import turtle as tur
place = [1,2,3,4,5,6,7,8,9]
placeX = [-125 , -25 , 75 , -125 , -25 , 75 , -125 , -25 , 75]
placeY = [50 , 50 , 50 , -50 , -50 , -50 , -150 , -150 , -150]
#绘制棋盘
def Drawbroad():
#print(str(place[0]) + ' | ' + str(place[1]) + ' | ' + str(place[2]))
#print('- + - + -')
#print(str(place[3]) + ' | ' + str(place[4]) + ' | ' + str(place[5]))
#print('- + - + -')
#print(str(place[6]) + ' | ' + str(place[7]) + ' | ' + str(place[8]))
# 初始化和清屏
tur.speed(1000)
tur.hideturtle()
tur.clear()
#绘制棋盘
tur.pencolor('Black')
tur.penup()
tur.goto(-150,150)
tur.pendown()
for num in range(4):
tur.forward(300)
tur.right(90)
tur.forward(100)
tur.right(90)
tur.forward(300)
tur.left(90)
tur.forward(100)
tur.left(90)
tur.forward(300)
tur.right(90)
tur.forward(100)
tur.right(90)
tur.forward(100)
tur.right(90)
tur.forward(300)
tur.left(90)
tur.forward(100)
tur.left(90)
tur.forward(300)
#绘制棋子
num1 = 1
for num2 in place:
if num1 == num2:
tur.penup()
tur.goto(placeX[num1 - 1],placeY[num1 - 1] + 10)
tur.pendown()
tur.write(place[num1-1],'center',font = ('宋体',65))
num1 += 1
else:
tur.penup()
tur.goto(placeX[num1 - 1],placeY[num1 - 1])
tur.pendown()
tur.write(place[num1-1],'center',font = ('consolas',65))
num1 += 1
#退出游戏
def Exit():
t.sleep(1)
print('')
print('感谢游玩!')
tur.done()
t.sleep(10)
sys.exit(0)
#横排检查
def heng():
if place[0] == 'X' and place[1] == 'X' and place[2] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[0] == 'Y' and place[1] == 'Y' and place[2] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
elif place[3] == 'X' and place[4] == 'X' and place[5] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[3] == 'Y' and place[4] == 'Y' and place[5] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
elif place[6] == 'X' and place[7] == 'X' and place[8] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[6] == 'Y' and place[7] == 'Y' and place[8] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
#竖排检查
def shu():
if place[0] == 'X' and place[3] == 'X' and place[6] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[0] == 'Y' and place[3] == 'Y' and place[6] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
if place[1] == 'X' and place[4] == 'X' and place[7] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[1] == 'Y' and place[4] == 'Y' and place[7] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
if place[2] == 'X' and place[5] == 'X' and place[8] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[2] == 'Y' and place[5] == 'Y' and place[8] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
#斜排检查
def xie():
if place[0] == 'X' and place[4] == 'X' and place[8] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[0] == 'Y' and place[4] == 'Y' and place[8] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
elif place[2] == 'X' and place[4] == 'X' and place[6] == 'X':
print('')
Drawbroad()
print('')
print('玩家一获胜')
Exit()
elif place[2] == 'Y' and place[4] == 'Y' and place[6] == 'Y':
print('')
Drawbroad()
print('')
print('玩家二获胜')
Exit()
#检查棋盘,判断胜负
def CheckWinOrLose():
heng()
shu()
xie()
if __name__ == '__main__':
print('欢迎来到Pyhon井字棋(双人对战,玩家一先,玩家一的棋子是X,玩家二的棋子是Y)')
a = 0
Player = '玩家一'
while 1:
Drawbroad()
if a == 9:
print('')
print('平局')
Exit()
else:
CheckWinOrLose()
print('')
m_input = int(input('到' + Player + '下了,请输入下子的位置'))
if Player == '玩家一':
if m_input > 0 and m_input < 10:
if place[m_input - 1] == m_input:
print(Player + '在位置' + str(m_input) + '下了棋子')
place[m_input - 1] = 'X'
Player = '玩家二'
a += 1
else:
print('您输入的位置无法下棋子')
else:
print('您输入的位置无法下棋子')
elif Player == '玩家二':
if m_input > 0 and m_input < 10:
if place[m_input - 1] == m_input:
print(Player + '在位置' + str(m_input) + '下了棋子')
place[m_input - 1] = 'Y'
Player = '玩家一'
a += 1
else:
print('您输入的位置无法下棋子')
else:
print('您输入的位置无法下棋子')