如何利用python实现五子棋呢?(下)

网友投稿 592 2022-05-29

客户端

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

from tkinterimport *

from tkinter.messageboximport *

import socket

import threading

import os

# 主程序

root= Tk()

root.title("网络五子棋v2.0--UDP客户端")

imgs= [PhotoImage(file='./images/BlackStone.png'), PhotoImage(file='./images/WhiteStone.png')]

turn= 0

MyTurn= -1

# 画对方棋子

def drawOtherChess(x, y):

如何利用python实现五子棋呢?(下)

global turn

img1= imgs[turn]

cv.create_image((x* 40 + 20, y* 40 + 20), image=img1)

cv.pack()

maps[x][y]= str(turn)

# 换下一方走棋

if turn== 0:

turn= 1

else:

turn= 0

# 发送消息

def sendMessage(position):

global s

s.sendto(position.encode(), (host, port))

# 退出函数

def callExit(event):

position= "exit|"

sendMessage(position)

os.exit()

# 走棋函数

def callback(event):

global turn

global MyTurn

if MyTurn== -1:

MyTurn= turn

else:

if MyTurn != turn:

showinfo(title="提示", message="还没轮到自己走棋")

return

# print("clicked at",event.x,event.y)

x= event.x// 40

y= event.y// 40

print("clicked at", x, y, turn)

if maps[x][y] != " ":

showinfo(title="提示", message="已有棋子")

else:

img1= imgs[turn]

cv.create_image((x* 40 + 20, y* 40 + 20), image=img1)

cv.pack()

maps[x][y]= str(turn)

position= str(x)+ ',' + str(y)

sendMessage("move|" + position)

print("客户端走的位置", position)

label1["text"]= "客户端走的位置" + position

# 输出输赢信息

if win_lose():

if turn== 0:

showinfo(title="提示", message="黑方你赢了")

sendMessage("over|黑方你赢了!")

else:

showinfo(title="提示", message="白方你赢了!")

sendMessage("over|白方你赢了!")

# 换下一方走棋:

if turn== 0:

turn= 1

else:

turn= 0

# 画棋盘

def drawQiPan(): # 画棋盘

for iin range(0,15):

cv.create_line(20,20 + 40 * i,580,20 + 40 * i, width=2)

for iin range(0,15):

cv.create_line(20 + 40 * i,20,20 + 40 * i,580, width=2)

cv.pack()

# 输赢判断

def win_lose():

a= str(turn)

print("a=", a)

for iin range(0,11):

for jin range(0,11):

if maps[i][j]== aand maps[i+ 1][j+ 1]== aand maps[i+ 2][j+ 2]== aand maps[i+ 3][j+ 3]== aand \

maps[i+ 4][j+ 4]== a:

print("x=y轴上形成五子连珠")

return True

for iin range(4,15):

for jin range(0,11):

if maps[i][j]== aand maps[i- 1][j+ 1]== aand maps[i- 2][j+ 2]== aand maps[i- 3][j+ 3]== aand \

maps[i- 4][j+ 4]== a:

print("x=-y轴上形成五子连珠")

return True

for iin range(0,15):

for jin range(4,15):

if maps[i][j]== aand maps[i][j- 1]== aand maps[i][j- 2]== aand maps[i][j- 2]== aand maps[i][

j- 4]== a:

print("Y轴上形成了五子连珠")

return True

for iin range(0,11):

for jin range(0,15):

if maps[i][j]== aand maps[i+ 1][j]== aand maps[i+ 2][j]== aand maps[i+ 3][j]== aand maps[i+ 4][

j]== a:

print("X轴形成五子连珠")

return True

return False

# 接受消息

def receiveMessage(): # 接受消息

global s

while True:

data= s.recv(1024).decode('utf-8')

a= data.split("|")

if not data:

print('server has exited!')

break

elif a[0]== 'exit':

print('对方退出!')

label1["text"]= '对方退出!游戏结束!'

elif a[0]== 'over':

print('对方赢信息!')

label1["text"]= data.split("|")[0]

showinfo(title="提示", message=data.split("|")[1])

elif a[0]== 'move':

print('received:', data)

p= a[1].split(",")

x= int(p[0])

y= int(p[1])

print(p[0], p[1])

label1["text"]= "服务器走的位置" + p[0]+ p[1]

drawOtherChess(x, y)

s.close()

# 启动线程接受客户端消息

def startNewThread():

thread= threading.Thread(target=receiveMessage, args=())

thread.setDaemon(True)

thread.start()

if __name__== '__main__':

# 主程序

maps= [[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]for yin range(15)]

cv= Canvas(root, bg='green', width=610, height=610)

drawQiPan()

cv.bind("", callback)

cv.pack()

label1= Label(root, text="客户端...")

label1.pack()

button1= Button(root, text="退出游戏")

button1.bind("", callExit)

button1.pack()

# 创建UDP

s= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

port= 8000

host= 'localhost'

pos= 'join|'

sendMessage(pos)

startNewThread()

root.mainloop()

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spark入门
下一篇:如何避免现场设备系统损坏或丢数据?
相关文章