脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 基于python的socket实现单机五子棋到双人对战

基于python的socket实现单机五子棋到双人对战

2021-07-14 11:22rayrayrayk Python

这篇文章主要为大家详细介绍了基于python的socket实现单机五子棋到双人对战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

基于python的socket实现单机五子棋到双人对战,供大家参考,具体内容如下

本次实验使用python语言。通过socket进行不同机器见的通信,具体可以分为以下四步:1.创建serversocket和socket;2.打开链接到socket的输入/输出流;3.按照协议对socket进行读/写操作;4.关闭输入输出流、关闭socket。

由于是双人对战,服务器必须应对多人及以上的客户端的连接,因此本实验还引入了python的threading多线程模块,通过监听实时监控网络状态,同时利用socket.listen(2)引入排队等待机制。

chess类

?
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
#五子棋类
import os
class chessboard(object):
  def __init__(self):
    self.size = 16
    #初始化棋盘
    self.__board=[[' ' for n in range(self.size)] for m in range(self.size)]
    n = 0
    #添加桌面标签
    while n < self.size:
      ntr=str(n)
      self.__board[0][n] = ntr.zfill(2)
      self.__board[n][0] = ntr.zfill(2)
      n=n+1
    self.id=0
  #胜利条件
  def is_end(self):
    ch_stack=[]
    #行检查
    for i in range(self.size):
      for j in range(self.size):
        #判断是否结束
        chess=self.__board[i][j]
        if len(ch_stack)==5 and ch_stack[-1]=='* ':
          print('winner=id 1')
          return 1
        elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':
          print('winner=id 2')
          return 2
 
        if chess==' ':
          ch_stack.clear()
        else:
          if (not ch_stack) or ch_stack[-1] == chess:
            ch_stack.append(chess)
          else:
            ch_stack.clear()
            ch_stack.append(chess)
      ch_stack.clear()
    ch_stack.clear()
    #列检查
    for j in range(self.size):
      for i in range(self.size):
        #判断是否结束
        if len(ch_stack)==5 and ch_stack[-1]=='* ':
          print('winner=id 1')
          return 1
        elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':
          print('winner=id 2')
          return 2
        chess=self.__board[i][j]
        if chess==' ':
          ch_stack.clear()
        else:
          if (not ch_stack) or ch_stack[-1] == chess:
            ch_stack.append(chess)
          else:
            ch_stack.clear()
            ch_stack.append(chess)
      ch_stack.clear()
    ch_stack.clear()
    #左斜检查
    #下三角
    for i in range(self.size):
      for j in range(1,self.size):
        #判断是否结束
        if len(ch_stack)==5 and ch_stack[-1]=='* ':
          print('winner=id 1')
          return 1
        elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':
          print('winner=id 2')
          return 2
 
        if i+j<self.size:
          chess=self.__board[i+j][j]
          if chess==' ':
            ch_stack.clear()
          else:
            if (not ch_stack) or ch_stack[-1] == chess:
              ch_stack.append(chess)
            else:
              ch_stack.clear()
              ch_stack.append(chess)
        else:
          break
      ch_stack.clear()
    ch_stack.clear()
    #上三角
    for i in range(self.size):
      for j in range(1,self.size):
        #判断是否结束
        if len(ch_stack)==5 and ch_stack[-1]=='* ':
          print('winner=id 1')
          return 1
        elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':
          print('winner=id 2')
          return 2
        if i+j<self.size:
          chess=self.__board[j][j+i]
          if chess==' ':
            ch_stack.clear()
          else:
            if (not ch_stack) or ch_stack[-1] == chess:
              ch_stack.append(chess)
            else:
              ch_stack.clear()
              ch_stack.append(chess)
        else:
          break
      ch_stack.clear()
    ch_stack.clear()
    #右斜检查
    #上三角
    for i in range(self.size):
      for j in range(1,self.size):
        #判断是否结束
        if len(ch_stack)==5 and ch_stack[-1]=='* ':
          print('winner=id 1')
          return 1
        elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':
          print('winner=id 2')
          return 2
        if self.size-i-j+1>0:
          chess=self.__board[self.size-i-j][j]
          if chess==' ':
            ch_stack.clear()
          elif not chess:
            break
          else:
            if (not ch_stack) or ch_stack[-1] == chess:
              ch_stack.append(chess)
            else:
              ch_stack.clear()
              ch_stack.append(chess)
        else:
          break
      ch_stack.clear()
    ch_stack.clear()
    #下三角
    for i in range(self.size):
      for j in range(1,self.size):
        # 判断是否结束
        if len(ch_stack) == 5 and ch_stack[-1] == '* ':
          print('winner=id 1')
          return 1
        elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':
          print('winner=id 2')
          return 2
        if self.size-i-j> 0:
          chess = self.__board[j][self.size-i-j]
          if chess == ' ':
            ch_stack.clear()
          elif not chess:
            break
          else:
            if (not ch_stack) or ch_stack[-1] == chess:
              ch_stack.append(chess)
            else:
              ch_stack.clear()
              ch_stack.append(chess)
        else:
          break
      ch_stack.clear()
    ch_stack.clear()
    return 0
  def draw(self):
    #clear()
    for x in self.__board:
      print(x)
    return 0
  def drop_chess(self,x,y,id):
    if id==1 and self.__board[x][y]==' ':
      self.__board[x][y]='* '
      return 1
    elif id==2 and self.__board[x][y]==' ':
      self.__board[x][y]='@ '
      return 1
    else:
      return 0

然后是用while循环实现的单机版五子棋

?
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
# -*- coding: utf-8 -*-
#单机版五子棋
from chess import chessboard
def changeid(id):
  if id==1:
    return 2
  elif id==2:
    return 1
  else:
    return 0
t=chessboard()
id=1#初始化id
t.draw()
while (not t.is_end()):#end函数
  print('your id is %d,input your next drop(x,y)'% id)
  x=input()
  y=input()
  x=int(x)
  y=int(y)
  if t.drop_chess(x,y,id):
    t.draw()
  else:
    print('_________illegal input,please check again_________')
    continue
  id=changeid(id)

———————分割线———————

由于要实现双人对战,所以服务器端必须要用多线程使其服务多个客户端,因此使用threading

服务器端

?
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
# -*- coding: utf-8 -*-
#服务器
import os
import socket
import json
import threading
import time
import sys
from chess import chessboard
t=chessboard()
id=1#初始化id
def handle():
  while (not t.is_end()):
    for c in socks:
      global id
      json_string0 = json.dumps(t._chessboard__board)
      c.sendto(json_string0.encode('utf-8'), address)
      msg1 = 'your id is %d,input your next drop' % id + "\r\n"
      c.send(msg1.encode('utf-8'))
      msg2x = c.recv(1024)
      msg2y = c.recv(1024)
      x = int(msg2x.decode('utf-8'))
      y = int(msg2y.decode('utf-8'))
      print('processing......\n')
      if t.drop_chess(x, y, id):
        json_string = json.dumps(t._chessboard__board)
        c.sendto(json_string.encode('utf-8'), address)
      else:
        msg3 = '_________illegal input,please check again_________'
        c.send(msg3.encode('utf-8'))
        continue
      id = changeid(id)
def clear():
  os.system('cls')
def changeid(id):
  if id==1:
    return 2
  elif id==2:
    return 1
  else:
    return 0
# 创建 socket 对象
s = socket.socket(socket.af_inet, socket.sock_stream)
# 获取本地主机名
host = socket.gethostname()
port = 9999
# 绑定端口号
s.bind((host, port))
address=(host, port)
# 设置最大连接数,超过后排队
s.listen(2)
socks=[]
th = threading.thread(target=handle)
th.start()
while 1:
  c, addr = s.accept()
  print
  'connected from:', addr
  socks.append(c)
s.close()

然后是客户端

?
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
# -*- coding: utf-8 -*-
#客户端
import socket
import time
 
# 创建 socket 对象
c = socket.socket(socket.af_inet, socket.sock_stream)
# 获取本地主机名
#host = socket.gethostname()
host='10.41.114.198'
# 设置端口号
port = 9999
# 连接服务,指定主机和端口
c.connect((host, port))
address=(host, port)
while 1:
  #s=c.accept()
  print('')
  print('__________________wait__________________')
  msg0 = c.recv(2048).decode('utf-8') # 棋盘大于1024
  for x in msg0:
    if x == '[':
      print('')
    else:
      print(x, end='')
  print('')
  msg1 = c.recv(1024)#接收输入提示
  print (msg1.decode('utf-8'))
  time.sleep(1)
  x = input('x=')
  y = input('y=')
  c.send(x.encode('utf-8'))
  c.send(y.encode('utf-8'))
  msg3 = c.recv(2048).decode('utf-8')#棋盘大于1024
  if msg3=='_________illegal input,please check again_________':
    print(msg3)
    continue
  else:
    #print(msg3)
    for x in msg3:
      if x=='[':
        print('')
      else:
        print(x, end='')
    print('')
    print('__________________wait__________________')
  print('')
c.close()

注意socket传输时只能传送bytes,因此list先用json转成str,再encode编码

使用方法:先更改客户端host为自己地址,然后先打开服务端,然后打开多个客户端(大于2个开始排队),然后开始输入x,y坐标开始游戏。

基于python的socket实现单机五子棋到双人对战

由于时间紧急,暂时未处理单个客户端退出后的程序初始化问题,可能过几日会补上。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_24740969/article/details/83759171

延伸 · 阅读

精彩推荐