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

举报
泽宇-Li 发表于 2021/04/12 23:49:41 2021/04/12
【摘要】 客户端:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001...

客户端:

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):
    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("<Button-1>", callback)
    cv.pack()
    label1= Label(root, text="客户端...")
    label1.pack()
    button1= Button(root, text="退出游戏")
    button1.bind("<Button-1>", callExit)
    button1.pack()
    # 创建UDP
    s= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    port= 8000
    host= 'localhost'
    pos= 'join|'
    sendMessage(pos)
    startNewThread()
    root.mainloop()
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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