python如何实现网络中的五子棋呢?(上)

举报
泽宇-Li 发表于 2021/04/12 23:49:14 2021/04/12
【摘要】 服务器端:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100...

服务器端:

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
import os
import socket
import threading
 
from tkinterimport *
from tkinter.messageboximport *
 
 
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 callPos(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,true)
    x= event.x// 40
    y= event.y// 40
    print("clicked at", x, y, turn)
    if maps[x][y] != " ":
        showinfo(title="提示", message="已有棋子")
    else:
        img1= images[turn]
        cv.create_image((x* 40 + 20, y* 40 + 20), image=img1)
        cv.pack()
        maps[x][y]= str(turn)
        pos= str(x)+ "," + str(y)
        sendMessage("move|" + pos)
        print("服务器走的位置", pos)
        label1["text"]= "服务器走的位置" + pos
        # 输出输赢信息
        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 sendMessage(pos):
    global s
    global addr
    s.sendto(pos.encode(), addr)
 
 
# 退出函数
def callExit(event):
    pos= "exit|"
    sendMessage(pos)
    os.exit()
 
 
# 画对方棋子
def drawOtherChess(x, y):
    global turn
    img1= images[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 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
 
 
# 输出map地图
def print_map():
    for jin range(0,15):
        for iin range(0,15):
            print(maps[i][j], end=' ')
        print('w')
 
 
# 接受消息
def receiveMessage():
    global s
    while True# 接受客户端发送的消息
        global addr
        data, addr= s.recvfrom(1024)
        data= data.decode('utf-8')
        a= data.split("|")
        if not data:
            print('client has exited!')
            break
        elif a[0]== 'join'# 连接服务器的请求
            print('client 连接服务器!')
            label1["text"]= 'client连接服务器成功,请你走棋!'
        elif a[0]== 'exit':
            print('client对方退出!')
            label1["text"]= 'client对方退出,游戏结束!'
        elif a[0]== 'over':
            print('对方赢信息!')
            label1["text"]= data.split("|")[0]
            showinfo(title="提示", message=data.split("1")[1])
        elif a[0]== 'move':
            print('received:', data,'from', addr)
            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__':
    root= Tk()
    root.title("网络五子棋v2.0-服务器端")
    images= [PhotoImage(file='./images/BlackStone.png'), PhotoImage(file='./images/WhiteStone.png')]
    turn= 0
    MyTurn= -1
    maps= [[" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]for yin range(15)]
    cv= Canvas(root, bg='green', width=610, height=610)
    drawQiPan()
    cv.bind("<Button-1>", callPos)
    cv.pack()
    label1= Label(root, text="服务器端...")
    label1.pack()
    button1= Button(root, text="退出游戏")
    button1.bind("<Button-1>", callExit)
    button1.pack()
    # 创建UDP SOCKET
    s= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(('localhost',8000))
    addr= ('localhost',8000)
    startNewThread()
    root.mainloop()
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。