参考代码

Reference_branch
Zhao 3 years ago
parent 8aa631d969
commit 683b6e247c

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

@ -0,0 +1,5 @@
1.使用方法
在cmd进入该文件夹下![image-20220825080709679](E:\程序设计课程\国际数棋-双界面\国际数棋-双界面\image-20220825080709679.png)
键入命令: `python .\chess_main.py` 开始游戏

File diff suppressed because it is too large Load Diff

@ -0,0 +1,309 @@
import uuid
import sys
import json
import queue
import threading
import time
from socket import socket, AF_INET, SOCK_STREAM
from pip._vendor.distlib.compat import raw_input
class Client_thread(threading.Thread):
def __init__(self):
super().__init__()
#定义ip地址、端口号
self.server_name = "127.0.0.1"
self.server_port = 50005
#socket连接
self.clientSocket = socket(AF_INET, SOCK_STREAM)
self.clientSocket.setblocking(False)#设置为非阻塞
self.clientSocket.settimeout(0.5)
#用户名等定义
self.name = "xqs&zzh"
self.counterpart_name = ""
self.inQueue = queue.Queue()#加入队列
self.outQueue = queue.Queue()#离开队列
self.game_id = ""#游戏ID
self.side = -1 #红蓝方 or 先后顺序
self.quit_me = False #认输
self.stop_me = False #叫停
self.status = 0 #状态接收sever中的status
self.over = False #判定游戏是否结束
self.winner = -1 #判定谁赢
self.request ='' #接收sever中的request
self.report_me = False
self.being_report = False
def report(self):
print('report!')
self.report_me = True
def b_report(self):
print('being reported!')
being_report = True
'''def send_msg_to(self,msg):
"""
发送信息
"""
try:
#打包信息
package = json.dumps(msg)
package = package.encode('utf-8')
self.clientSocket.send(package)
return True
except Exception as e:
return False
pass
def recieve_msg_from(self):
"""
接收信息
"""
try:
data = self.clientSocket.recv(1024)
time.sleep(0.00001)
data = json.loads(data)
return data
except Exception as e:
pass
def send_blocked(self,data):
"""
传输阻塞
"""
while True:
if self.send_msg_to(data) == True:
break
if self.quit_me == True:
self.clientSocket.close()
time.sleep(0.5)
def receive_blocked(self):
"""
接收阻塞
"""
while True:
rec = self.recieve_msg_from()
if rec:
return rec
if self.quit_me == True:
self.clientSocket.close()
return
time.sleep(0.5)'''
#用于exit判断退出
def quit(self):
# 退出
print("quit!")
self.quit_me = True
#用于stop判断输赢
def stop(self):
# 退出
print("stop!")
self.stop_me = True
def run(self):
#运行线程
global rec_b
self.clientSocket.connect((self.server_name,self.server_port))
#self.send_blocked({"type":0,"msg":{"name":self.name}})
#print('ww',self.winner)
while True:
try:
#打包信息
package = json.dumps({"type":0,"msg":{"name":self.name}})
package = package.encode('utf-8')
self.clientSocket.send(package)
fg = True
except Exception as e:
fg = False
pass
if fg == True:
break
if self.quit_me == True:
self.clientSocket.close()
time.sleep(0.5)
#rec = self.receive_blocked()
while True:
#rec = self.recieve_msg_from()
try:
rec_b = self.clientSocket.recv(1024)
time.sleep(0.00001)
rec_b = json.loads(rec_b)
print(rec_b)
if rec_b:
break
except Exception as e:
#break
pass
'''if rec:
break'''
if self.quit_me == True:
self.clientSocket.close()
break
time.sleep(0.5)
#print(self.side)
if self.quit_me == False and self.stop_me ==False:
self.side = rec_b["side"]
#print(self.side)
self.status = rec_b["status"]
self.counterpart_name = rec_b["counterpart_name"]
if rec_b["status"] == 1:
self.inQueue.put(rec_b)
time.sleep(0.5)
self.game_id = rec_b["game_id"]
while (True):
global rec_a
rec_a ={}
if self.outQueue.empty() == False:
send_msg = self.outQueue.get()
#self.send_msg_to(send_msg)
print('o n e')
try:
#打包信息
package = json.dumps(send_msg)
package = package.encode('utf-8')
self.clientSocket.send(package)
#return True
except Exception as e:
#return False
pass
#rec = self.recieve_msg_from()
#n0 = True
#while True:
#print('zz')
try:
rec_a = self.clientSocket.recv(1024)
time.sleep(0.00001)
rec_a = json.loads(rec_a)
#break
except Exception as e:
#break
pass
if self.quit_me == True:#认输
'''self.send_msg_to({
"type":2,
"msg":{
"request":"quit",
"game_id":self.game_id,
"side":self.side
}
})'''
msg = {
"type":2,
"msg":{
"request":"quit",
"game_id":self.game_id,
"side":self.side
}
}
try:
#打包信息
package = json.dumps(msg)
package = package.encode('utf-8')
self.clientSocket.send(package)
#return True
except Exception as e:
#return False
pass
break
if self.stop_me == True:#叫停
'''self.send_msg_to({
"type":2,
"msg":{
"request":"stop",
"game_id":self.game_id,
"side":self.side
}
})'''
msg = {
"type":2,
"msg":{
"request":"stop",
"game_id":self.game_id,
"side":self.side
}
}
try:
#打包信息
package = json.dumps(msg)
package = package.encode('utf-8')
self.clientSocket.send(package)
#return True
except Exception as e:
#return False
pass
break
if self.report_me == True:
'''self.send_msg_to({
"type":2,
"msg":{
"request":"report",
"game_id":self.game_id,
"side":self.side
}
})'''
msg = {
"type":2,
"msg":{
"request":"report",
"game_id":self.game_id,
"side":self.side
}
}
try:
#打包信息
package = json.dumps(msg)
package = package.encode('utf-8')
self.clientSocket.send(package)
#return True
except Exception as e:
#return False
pass
break
if not rec_a:
time.sleep(0.5)
continue
if "status" in rec_a:
#回复sever发过来的消息
print(rec_a["status"])
if rec_a["status"] == 2 or rec_a["status"] == 3:
print(rec_a["status"])
self.over = True
self.winner = rec_a["side"]
print('change w')
if self.side == abs(1-rec_a["side"]):
msg = ({
"type":3,
"side":self.side
})
try:
#打包信息
package = json.dumps(msg)
package = package.encode('utf-8')
self.clientSocket.send(package)
#return True
except Exception as e:
#return False
pass
if "request" in rec_a:
self.request = rec_a["request"]
print(rec_a["request"])
print("********")
break
else:
print("received!*****************")
#print(222)
print(rec_a)
self.inQueue.put(rec_a)
else:
print("no other player, time out!")
self.clientSocket.close()

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

@ -0,0 +1,491 @@
import collections
import pygame
import pygame.font
import sys
import copy
from math import sqrt
from pygame.locals import *
import main
from client import *
import traceback
import json
import wx
import re
import random
import itertools
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
red_is_move = False
def transfer(x, y):
"""
将像素坐标转换为棋盘坐标
"""
chess_x = (x - 25) / 100
chess_y = (y - 25) / 50
return [chess_x, chess_y]
def retransfer(chess_x, chess_y):
"""
将棋盘坐标转换为像素坐标
"""
x = (int)(100 * (chess_x) + 25)
y = (int)(50 * (chess_y) + 25)
return [x, y]
def getxy(client, s):
"""
接收对方消息
"""
print("--------------")
if client.inQueue.empty() == False: # to process the rival's message
rival_move = client.inQueue.get()
if "src" in rival_move:
s.srcx = rival_move["src"]["x"]
s.srcy = rival_move["src"]["y"]
s.dstx = rival_move["dst"]["x"]
s.dsty = rival_move["dst"]["y"]
s.num = rival_move["num"]
[s.srcx, s.srcy] = retransfer(s.srcx, s.srcy)
[s.dstx, s.dsty] = retransfer(s.dstx, s.dsty)
# 置收到消息信号为1
s.get = 1
print("收集到的坐标是", s.srcx, s.srcy, s.dstx, s.dsty, s.num)
def sendxy(client, s):
"""
发送己方消息
"""
[s.ssrcx, s.ssrcy] = transfer(s.ssrcx, s.ssrcy)
[s.sdstx, s.sdsty] = transfer(s.sdstx, s.sdsty)
if (s.side == 1 and s.order == False):
print("发送到的坐标是", s.ssrcx, s.ssrcy, s.sdstx, s.sdsty)
client.outQueue.put({
"type": 1,
"msg": {
"game_id": client.game_id,
"side": client.side,
"num": s.num,
"exp": s.exp,
"src": {
"x": int(s.ssrcx),
"y": int(s.ssrcy)
},
"dst": {
"x": int(s.sdstx),
"y": int(s.sdsty)
}
}
})
def Max_solve(s):
all_core = []
core = 0
p = 0
x = -1
for key in s.lanqi.keys():
print("---------111------------", key)
core, ai = line_search(s, key)
if ai != False:
add = [ai[1], ai[2]]
if add != s.lanqi[key]["now_weizhi"]:
print("---------222----------", core)
all_core.insert(0, [key, core, ai])
for i in range(len(all_core)):
if all_core[i][1] > p:
p = all_core[i][1]
x = i
if x != -1:
print(333)
return all_core[x]
else:
print(444)
return False
def only_one_chess(s):
a = 0
num = 0
for key in s.lanqi.keys():
# print(s.lanqi[key]["now_weizhi"])
if tuple(s.lanqi[key]["now_weizhi"]) not in HongPoint:
num = num + 1
a = key
if num == 1:
print(a)
return a
else:
return False
def check_events(s, screen, client):
global red_is_move
for event in pygame.event.get():
if event.type == pygame.QUIT:
client.quit()
exit()
pygame.quit()
sys.exit()
# elif event.type == MOUSEBUTTONDOWN:
# x = event.pos[0]
# y = event.pos[1]
if s.over == False:
rival_chessman_move(client, s)
if red_is_move == True:
print("ffffffffffffff")
if whowin(s, "black"):
if fenshu(s)[0] > fenshu(s)[1]:
tishitext(s, screen)
else:
tishitext(s, screen)
ai = Max_solve(s)
if ai != False:
print("FFFFFFFFFFFFFF")
s.num = ai[0]
s.sdstx = ai[2][1]
s.sdsty = ai[2][2]
# print(s.sdstx,s.sdsty)
s.ssrcx = s.lanqi[s.num]["now_weizhi"][0]
s.ssrcy = s.lanqi[s.num]["now_weizhi"][1]
sendxy(client, s)
s.order = True
s.lanqi[s.num]["now_weizhi"] = [ai[2][1], ai[2][2]]
red_is_move = False
def victory(s, screen, text):
"""
红方胜利
"""
s.over = True
victory = pygame.image.load('images/result.png')
pic_rect = victory.get_rect()
pic_rect.centerx = 730
pic_rect.centery = 350
screen.blit(victory, pic_rect)
font = pygame.font.Font('font.ttf', 50)
rv = font.render(text, True, [255, 0, 0])
screen.blit(rv, (550, 310))
HongPoint = [(1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375),
(1225, 475), (1325, 325), (1325, 425), (1425, 375)]
HeiPoint = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475),
(325, 225), (325, 325), (325, 425), (325, 525)]
# 判断是否可以结束
def whowin(s, color):
if color == "black":
for key in s.lanqi.keys():
if tuple(s.lanqi[key]['now_weizhi']) not in HongPoint:
return False
else:
for key in s.hongqi.keys():
if tuple(s.hongqi[key]['now_weizhi']) not in HeiPoint:
return False
return True
# 判断是否可以结
def find_fenshu(x, y):
if (x == 325 and y == 525) or (x == 1125 and y == 225):
return 1
elif (x == 325 and y == 325) or (x == 1125 and y == 425):
return 2
elif (x == 325 and y == 425) or (x == 1125 and y == 325):
return 3
elif (x == 325 and y == 225) or (x == 1125 and y == 525):
return 4
elif (x == 225 and y == 275) or (x == 1225 and y == 475):
return 5
elif (x == 225 and y == 375) or (x == 1225 and y == 375):
return 6
elif (x == 225 and y == 475) or (x == 1225 and y == 275):
return 7
elif (x == 125 and y == 425) or (x == 1325 and y == 325):
return 8
elif (x == 125 and y == 325) or (x == 1325 and y == 425):
return 9
else:
return 0
# 计分板
def fenshu(s):
red_fen = 0
hei_fen = 0
for key in s.hongqi.keys():
if tuple(s.hongqi[key]['now_weizhi']) in s.HeiPoint:
red_fen += (key) * find_fenshu(s.hongqi[key]['now_weizhi'][0], s.hongqi[key]['now_weizhi'][1])
for key in s.lanqi.keys():
if tuple(s.lanqi[key]['now_weizhi']) in s.HongPoint:
hei_fen += (key) * find_fenshu(s.lanqi[key]['now_weizhi'][0], s.lanqi[key]['now_weizhi'][1])
return red_fen, hei_fen
# 绘制红蓝提示器
def tishitext(s, screen):
s_font = pygame.font.Font('pz.ttf', 50)
s_text = s_font.render('红方得分: %s' % str(fenshu(s)[0]), True, (255, 0, 0))
screen.blit(s_text, (1100, 650))
s_text = s_font.render('蓝方得分: %s' % str(fenshu(s)[1]), True, (0, 0, 0))
screen.blit(s_text, (1100, 700))
def red_order(s, screen):
"""
红棋落子
"""
font = pygame.font.Font('pz.ttf', 60)
ro = font.render("红棋落子", True, [255, 0, 0])
screen.blit(ro, (1100, 580))
def black_order(s, screen):
"""
黑棋落子
"""
font = pygame.font.Font('pz.ttf', 60)
bo = font.render("黑棋落子", True, [0, 0, 0])
screen.blit(bo, (1100, 580))
# 判断该位置有无棋子
def weizhi_panduan(s, x, y):
for key in s.hongqi.keys():
if [x, y] == s.hongqi[key]['now_weizhi']:
return True
for key in s.lanqi.keys():
if [x, y] == s.lanqi[key]['now_weizhi']:
return True
return False
# 移 规则
def yi_rule(chess_x, chess_y, x, y):
if abs(chess_x - x) <= 100 and abs(chess_y - y) <= 100:
if not (chess_x == x and chess_y == y):
return True
return False
# 邻 规则
def lin_rule(s, chess_x, chess_y, x, y):
if weizhi_panduan(s, (chess_x + x) / 2, (chess_y + y) / 2) and abs(x - chess_x) <= 200:
if not (chess_x == x and chess_y == y):
return True
return False
# 弹框规则
def tankuang(content, wx_type):
app = wx.App()
dlg = wx.MessageDialog(None, content, "提示", wx_type)
if dlg.ShowModal() == wx.ID_NO:
return False
else:
return True
def find(s, x, y):
for key in s.hongqi.keys():
if s.hongqi[key]['now_weizhi'][0] - x == 0 and s.hongqi[key]['now_weizhi'][1] - y == 0:
return (key)
for key in s.lanqi.keys():
if s.lanqi[key]['now_weizhi'][0] - x == 0 and s.lanqi[key]['now_weizhi'][1] - y == 0:
return (key)
return -1
POINTS = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475),
(325, 225), (325, 325), (325, 425), (325, 525), (425, 175), (425, 275),
(425, 375), (425, 475), (425, 575), (525, 125), (525, 225), (525, 325),
(525, 425), (525, 525), (525, 625), (625, 75), (625, 175), (625, 275),
(625, 375), (625, 475), (625, 575), (625, 675), (725, 25), (725, 125),
(725, 225), (725, 325), (725, 425), (725, 525), (725, 625), (725, 725),
(825, 75), (825, 175), (825, 275), (825, 375), (825, 475), (825, 575),
(825, 675), (925, 125), (925, 225), (925, 325), (925, 425), (925, 525),
(925, 625), (1025, 175), (1025, 275), (1025, 375), (1025, 475), (1025, 575),
(1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375),
(1225, 475), (1325, 325), (1325, 425), (1425, 375)]
def rival_chessman_move(client, s):
"""
对方棋子移动
"""
global red_is_move
getxy(client, s)
# 暂存对方选中的红棋
# print(s.order)
if s.order == True and s.get == 1:
# for key in s.hongqi.keys():
if s.srcx == s.hongqi[s.num]['now_weizhi'][0] and s.srcy == s.hongqi[s.num]['now_weizhi'][1]:
s.hongqi[s.num]['now_weizhi'] = [s.dstx, s.dsty]
# print("gggggggggggg")
# 改为蓝棋落子
s.order = False
# s.red_backups = []
# 改为选子状态
red_is_move = True
s.running = True
# AI计算-----------------------------------------------------------------------------------------------------------
AI_v = () # 用于存储AI单跨计算后的值
# 运算单跨表达式
def find_a(a, b, l):
if l == "+":
return a + b
elif l == '-':
return a - b
elif l == '*':
return a * b
elif l == "/":
if b != 0:
return a / b
else:
return 10000000 # 当被除数为0运算错误
list2 = ['+', '-', '*', '/']
def js(l, n):
global AI_v
if n == 1:
AI_v = AI_v + (l[0],)
else:
for i in range(4):
b = copy.deepcopy(l)
x = find_a(b[0], b[1], list2[i])
b.remove(b[0])
b.remove(b[0])
b.insert(0, x)
js(b, n - 1)
# 搜索单个棋子左上左下的两条移动直线上的所有情况
def line_search(s, key):
# print(1)
'''''
if color == "red":
mm = -1
cur_x = s.hongqi[key]["now_weizhi"][0]
cur_y = s.hongqi[key]["now_weizhi"][1]
else:
'''''
cur_x = s.lanqi[key]["now_weizhi"][0]
cur_y = s.lanqi[key]["now_weizhi"][1]
mm = 1
# print(cur_x, cur_y)
nochess_addup_record = [] # 往shang运动
nochess_adddown_record = [] # 往xia运动
v1 = v2 = False
for i in range(1, 8):
if (cur_x + mm * 100 * i, cur_y + 50 * i) in POINTS and not weizhi_panduan(s, cur_x + mm * 100 * i,
cur_y + 50 * i):
nochess_addup_record.insert(0, [cur_x + mm * 100 * i, cur_y + 50 * i])
if (cur_x + mm * 100 * i, cur_y - 50 * i) in POINTS and not weizhi_panduan(s, cur_x + mm * 100 * i,
cur_y - 50 * i):
nochess_adddown_record.insert(0, [cur_x + mm * 100 * i, cur_y - 50 * i])
# print(2)
if (cur_x, cur_y + 100) in POINTS and not weizhi_panduan(s, cur_x, cur_y + 100):
nochess_addup_record.append([cur_x, cur_y + 100])
if (cur_x, cur_y - 100) in POINTS and not weizhi_panduan(s, cur_x, cur_y - 100):
nochess_adddown_record.append([cur_x, cur_y - 100])
if (cur_x, cur_y + 50) in POINTS and not weizhi_panduan(s, cur_x, cur_y + 50):
nochess_addup_record.append([cur_x, cur_y + 50])
if (cur_x, cur_y - 50) in POINTS and not weizhi_panduan(s, cur_x, cur_y - 50):
nochess_adddown_record.append([cur_x, cur_y - 50])
# print(nochess_addup_record)
# print(nochess_adddown_record)
if len(nochess_addup_record):
v1 = AI_move_rule(s, key, cur_x, cur_y, nochess_addup_record)
if len(nochess_adddown_record):
v2 = AI_move_rule(s, key, cur_x, cur_y, nochess_adddown_record)
# print(7)
print(key, cur_x, cur_y, v1)
print(key, cur_x, cur_y, v2)
# ??????????????棋子为8的时候v1=[],v2!=[],那么如果当v1=v2=[],情况未讨论!!!!!!!!!
if v1 == False and v2 != False:
bb = ((v2[1] - cur_x) ** 2 + (v2[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01
return bb, v2
elif v1 != False and v2 == False:
aa = ((v1[1] - cur_x) ** 2 + (v1[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01
return aa, v1
elif v1 == False and v2 == False:
return False, False
else:
aa = ((v1[1] - cur_x) ** 2 + (v1[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01
bb = ((v2[1] - cur_x) ** 2 + (v2[2] - cur_y) ** 2) ** 0.5 + (1125 - cur_x) * 0.01
if v1[0] >= v2[0]:
return aa, v1
else:
return bb, v2
# AI基本移动规则
def AI_move_rule(s, key, cur_x, cur_y, nochess_add_record):
# print(3)
for i in range(len(nochess_add_record)):
if dankua_rule_AI(s, key, cur_x, cur_y, nochess_add_record[i][0], nochess_add_record[i][1]):
cores = 300
# print(4)
return [cores, nochess_add_record[i][0], nochess_add_record[i][1]]
elif lin_rule(s, cur_x, cur_y, nochess_add_record[i][0], nochess_add_record[i][1]):
cores = 200
# print(5)
return [cores, nochess_add_record[i][0], nochess_add_record[i][1]]
elif yi_rule(cur_x, cur_y, nochess_add_record[i][0], nochess_add_record[i][1]):
cores = 100
# print(6)
return [cores, nochess_add_record[i][0], nochess_add_record[i][1]]
return False
# AI版AI单跨规则
def dankua_rule_AI(s, value, chess_x, chess_y, x, y):
# print(8)
global AI_v
if chess_x != x:
y1 = (y - chess_y) / abs(y - chess_y)
x1 = (x - chess_x) / abs(x - chess_x)
if weizhi_panduan(s, x - x1 * 100, y - y1 * 50) and abs(y - chess_y) / 50 == abs(x - chess_x) / 100:
# print(9)
v = []
for i in range(abs(int((x - chess_x) / 100)) - 1):
v.append(find(s, chess_x + (i + 1) * 100 * x1, chess_y + (i + 1) * 50 * y1))
while -1 in v:
v.remove(-1)
# print("++++++++++++++++", v, "++++++++++++")
lis0 = list(itertools.permutations(v, len(v))) # 全排列
if v:
for j in range(len(lis0)):
js(list(lis0[j]), len(lis0[j]))
if value in AI_v:
# print(AI_v)
AI_v = ()
return True
else:
AI_v = ()
return False
# --------------------------------------------------------------------------------------------------------------------

@ -0,0 +1,66 @@
import copy
class BlackSettings():
def __init__(self):
"""
初始化游戏的设置
"""
# 设置屏幕参数
self.screen_width = 1450
self.screen_height = 800
self.back_color = (152, 245, 245) # 背景颜色
# 设置棋子参数
self.r = 25 # 棋子半径
# 用于记录棋子当前状态
self.lanqi = {0: {'color': 'blue', 'now_weizhi': [25, 375]}, 1: {'color': 'blue', 'now_weizhi': [325, 525]},
2: {'color': 'blue', 'now_weizhi': [325, 325]}, 3: {'color': 'blue', 'now_weizhi': [325, 425]},
4: {'color': 'blue', 'now_weizhi': [325, 225]}, 5: {'color': 'blue', 'now_weizhi': [225, 275]},
6: {'color': 'blue', 'now_weizhi': [225, 375]}, 7: {'color': 'blue', 'now_weizhi': [225, 475]},
8: {'color': 'blue', 'now_weizhi': [125, 425]}, 9: {'color': 'blue', 'now_weizhi': [125, 325]}}
self.hongqi = {0: {'color': 'red', 'now_weizhi': [1425, 375]}, 1: {'color': 'red', 'now_weizhi': [1125, 225]},
2: {'color': 'red', 'now_weizhi': [1125, 425]}, 3: {'color': 'red', 'now_weizhi': [1125, 325]},
4: {'color': 'red', 'now_weizhi': [1125, 525]}, 5: {'color': 'red', 'now_weizhi': [1225, 475]},
6: {'color': 'red', 'now_weizhi': [1225, 375]}, 7: {'color': 'red', 'now_weizhi': [1225, 275]},
8: {'color': 'red', 'now_weizhi': [1325, 325]}, 9: {'color': 'red', 'now_weizhi': [1325, 425]}}
self.HongPoint = [(1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375),
(1225, 475), (1325, 325), (1325, 425), (1425, 375)]
self.HeiPoint = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475),
(325, 225), (325, 325), (325, 425), (325, 525)]
# 用于暂时存储上一步棋的位置*****
self.red_backups = [] # red
self.black_backups = [] # blue
self.exp = ''
# 记录执子落子状态*********
self.running = True
# 记录传送的棋号
self.num = -1
# 记录游戏结束与否******
self.over = False
# 记录当前落子方,红棋先手 ***
self.order = 1
# 记录是stop还是quit***
self.request = ''
# 记录游戏昵称
self.name = 'jzl'
# 记录分配方0为红方
self.side = 0
self.game_id = ""
self.get = 0
self.srcx = 0
self.srcy = 0
self.dstx = 0
self.dsty = 0
self.ssrcx = 0
self.ssrcy = 0
self.sdstx = 0
self.sdsty = 0

@ -0,0 +1,440 @@
import collections
import pygame
import pygame.font
import sys
import copy
from math import sqrt
from pygame.locals import *
import main
from client import *
import traceback
import json
import wx
import re
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
def transfer(x, y):
"""
将像素坐标转换为棋盘坐标
"""
chess_x = (x-25) / 100
chess_y = (y-25) / 50
return [chess_x, chess_y]
def retransfer(chess_x, chess_y):
"""
将棋盘坐标转换为像素坐标
"""
x = (int)(100*(chess_x )+25)
y = (int)(50*(chess_y )+25)
return [x, y]
def getxy(client, s):
print("lan_rec")
"""
接收对方消息
"""
print("--------------")
if client.inQueue.empty() == False: # to process the rival's message
rival_move = client.inQueue.get()
if "src" in rival_move:
s.srcx = rival_move["src"]["x"]
s.srcy = rival_move["src"]["y"]
s.dstx = rival_move["dst"]["x"]
s.dsty = rival_move["dst"]["y"]
s.sum = rival_move["num"]
[s.srcx, s.srcy] = retransfer(s.srcx, s.srcy)
[s.dstx, s.dsty] = retransfer(s.dstx, s.dsty)
#置收到消息信号为1
s.get = 1
print("收集到的坐标是", s.srcx, s.srcy, s.dstx, s.dsty)
def sendxy(client, s):
"""
发送己方消息
"""
print("lan_rec")
[s.ssrcx, s.ssrcy] = transfer(s.ssrcx, s.ssrcy)
[s.sdstx, s.sdsty] = transfer(s.sdstx, s.sdsty)
if (s.side == 0 and s.order == True ):
client.outQueue.put({
"type": 1,
"msg": {
"game_id": client.game_id,
"side": client.side,
"num":s.num,
"src": {
"x": s.ssrcx,
"y": s.ssrcy
},
"dst": {
"x": s.sdstx,
"y": s.sdsty
}
}
})
if (s.side == 1 and s.order == False):
print("发送到的坐标是", s.ssrcx, s.ssrcy, s.sdstx, s.sdsty)
client.outQueue.put({
"type": 1,
"msg": {
"game_id": client.game_id,
"side": client.side,
"num": s.num,
"src": {
"x": s.ssrcx,
"y": s.ssrcy
},
"dst": {
"x": s.sdstx,
"y": s.sdsty
}
}
})
def check_events(s, screen, client):
for event in pygame.event.get():
if event.type == pygame.QUIT:
client.quit()
exit()
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
x=event.pos[0]
y=event.pos[1]
#getstop(client,s)
if s.over == False:
#print(s.order)
rival_chessman_move(client, s)
chessman_move(event, s, screen, client)
#认输游戏
if 25< x <225 and 600 <y<700:
s.request = 'quit'
#叫停
elif 25< x <225 and 100<y<200:
s.request = 'stop'
def victory(s, screen,text):
"""
胜利图片
"""
s.over = True
victory = pygame.image.load('images/result.png')
pic_rect = victory.get_rect()
pic_rect.centerx = 730
pic_rect.centery = 350
screen.blit(victory, pic_rect)
font = pygame.font.Font('font.ttf', 50)
rv = font.render(text, True, [255, 0, 0])
screen.blit(rv, (550, 310))
def whowin(s):
for key in s.hongqi.keys():
if tuple(s.hongqi[key]['now_weizhi']) not in HeiPoint:
return False
for key in s.lanqi.keys():
if tuple(s.lanqi[key]['now_weizhi']) not in HongPoint:
return False
return True
def find_fenshu(x, y):
if (x == 325 and y == 525) or (x == 1125 and y == 225):
return 1
elif (x == 325 and y == 325) or (x == 1125 and y == 425):
return 2
elif (x == 325 and y == 425) or (x == 1125 and y == 325):
return 3
elif (x == 325 and y == 225) or (x == 1125 and y == 525):
return 4
elif (x == 225 and y == 275) or (x == 1225 and y == 475):
return 5
elif (x == 225 and y == 375) or (x == 1225 and y == 375):
return 6
elif (x == 225 and y == 475) or (x == 1225 and y == 275):
return 7
elif (x == 125 and y == 425) or (x == 1325 and y == 325):
return 8
elif (x == 125 and y == 325) or (x == 1325 and y == 425):
return 9
else:
return 0
# 计分板
def fenshu(s):
red_fen = 0
hei_fen = 0
for key in s.hongqi.keys():
if tuple(s.hongqi[key]['now_weizhi']) in s.HeiPoint:
red_fen += (key) * find_fenshu(s.hongqi[key]['now_weizhi'][0], s.hongqi[key]['now_weizhi'][1])
for key in s.lanqi.keys():
if tuple(s.lanqi[key]['now_weizhi']) in s.HongPoint:
hei_fen += (key) * find_fenshu(s.lanqi[key]['now_weizhi'][0], s.lanqi[key]['now_weizhi'][1])
return red_fen, hei_fen
# 绘制红蓝提示器
def tishitext(s, screen):
s_font = pygame.font.Font('pz.ttf', 50)
s_text = s_font.render('红方得分: %s' % str(fenshu(s)[0]), True, (255, 0, 0))
screen.blit(s_text, (1100, 650))
s_text = s_font.render('蓝方得分: %s' % str(fenshu(s)[1]), True, (0, 0, 0))
screen.blit(s_text, (1100, 700))
def red_order(s, screen):
"""
红棋落子
"""
font = pygame.font.Font('pz.ttf', 60)
ro = font.render("红棋落子", True, [255, 0, 0])
screen.blit(ro, (1100, 580))
def black_order(s, screen):
"""
黑棋落子
"""
font = pygame.font.Font('pz.ttf', 60)
bo = font.render("黑棋落子", True, [0, 0, 0])
screen.blit(bo, (1100, 580))
#判断该位置有无棋子
def weizhi_panduan(s,x,y):
for key in s.hongqi.keys():
if [x,y] == s.hongqi[key]['now_weizhi']:
return True
for key in s.lanqi.keys():
if [x,y]==s.lanqi[key]['now_weizhi']:
return True
return False
# 移 规则
def yi_rule(chess_x,chess_y,x,y):
print("hong_yi")
if abs(chess_x-x) <= 100 and abs(chess_y-y) <= 100 :
if not(chess_x == x and chess_y == y):
return True
return False
#邻 规则
def lin_rule(s,chess_x,chess_y,x,y):
print("hong_lin")
if weizhi_panduan(s,(chess_x+x)/2 ,(chess_y+y)/2) and abs(x-chess_x)<=200:
if not(chess_x == x and chess_y == y):
return True
return False
#弹框规则
def tankuang(content, wx_type):
app=wx.App()
dlg = wx.MessageDialog(None,content,"提示",wx_type)
if dlg.ShowModal() == wx.ID_NO:
return False
else:
return True
#输入框获取值
def getInput():
try:
tmp1=['1' , '2' ,'3','4','5', '6' , '7' ,'8' , '9','0']
tmp2=[')','(']
tmp3=['+','-','/','*']
app = wx.App()
box = wx.TextEntryDialog(None,'请输入表达式:/n1.数字','运算方法','error')
if box.ShowModal() == wx.ID_OK:
answer = box.GetValue()
lena=len(answer)-1
a=0
for i in range(lena):
if answer[i] == '(':
a=a+1
if answer[i] ==')':
a=a-1
if i== lena:
if answer[i] not in tmp1 or answer[i] != ')':
print(1)
return 'error'
else:
if answer[i] not in (tmp1 + tmp2 + tmp3):
print(2)
return 'error'
else:
if answer[i] in tmp3 and answer[i+1] in tmp3:
print(3)
return 'error'
if a!=0:
return 'error'
return answer
except:
return 'error'
def find (s,x,y):
for key in s.hongqi.keys():
if s.hongqi[key]['now_weizhi'][0] - x == 0 and s.hongqi[key]['now_weizhi'][1]-y ==0:
return (key)
for key in s.lanqi.keys():
if s.lanqi[key]['now_weizhi'][0] - x == 0 and s.lanqi[key]['now_weizhi'][1]-y ==0:
return (key)
return -1
#单跨 规则
def dankua_rule(s,value,chess_x, chess_y, x, y):
print("hong_dankua")
if y == chess_y:
return False
elif x==chess_x:
y1 = (y - chess_y)/abs(y-chess_y)
if weizhi_panduan(s,x , y-y1*100):
a = (getInput())
if a!='error'and a!=None:
VALUE=eval(a)
v = []
r =re.split(r"[+-/*()]",a)
while '' in r:
r.remove('')
r = list(map(int,r))
for i in range(abs(int((y-chess_y)/100))-1):
v.append(find(s,x,chess_y+(i+1)*100*y1))
while -1 in v:
v.remove(-1)
r.sort()
v.sort()
return r==v and value == VALUE
else:
y1 = (y - chess_y)/abs(y-chess_y)
x1 = (x - chess_x)/abs(x-chess_x)
if weizhi_panduan(s,x-x1*100,y-y1*50) and abs(y-chess_y)/50 == abs(x-chess_x)/100:
a = (getInput())
if a != 'error'and a!=None :
print(1)
VALUE = eval(a)
v = []
r =re.split(r"[+-/*()]",a)
while '' in r:
r.remove('')
r = list(map(int,r))
print(r)
for i in range(abs(int((x-chess_x)/100))-1):
v.append(find(s,chess_x+(i+1)*100*x1,chess_y+(i+1)*50*y1))
while -1 in v:
v.remove(-1)
r.sort()
v.sort()
return r==v and VALUE == value
return False
HeiPoint = [(1125, 225), (1125, 325), (1125, 425), (1125, 525),(1225, 275), (1225, 375),
(1225, 475), (1325, 325), (1325, 425), (1425, 375)]
HongPoint = [(25 , 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475),
(325, 225), (325, 325), (325, 425), (325, 525)]
#判断是否可以结束
def whowin(s):
for key in s.hongqi.keys():
if tuple(s.hongqi[key]['now_weizhi']) not in s.HeiPoint:
return False
for key in s.lanqi.keys():
if tuple(s.lanqi[key]['now_weizhi']) not in s.HongPoint:
return False
return True
POINTS = [(25 , 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475),
(325, 225), (325, 325), (325, 425), (325, 525), (425, 175), (425, 275),
(425, 375), (425, 475), (425, 575), (525, 125), (525, 225), (525, 325),
(525, 425), (525, 525), (525, 625), (625, 75), (625, 175), (625, 275),
(625, 375), (625, 475), (625, 575), (625, 675), (725, 25), (725, 125),
(725, 225), (725, 325), (725, 425), (725, 525), (725, 625), (725, 725),
(825, 75), (825, 175), (825, 275), (825, 375), (825, 475), (825, 575),
(825, 675), (925, 125), (925, 225), (925, 325), (925, 425), (925, 525),
(925, 625), (1025, 175), (1025, 275), (1025, 375), (1025, 475), (1025, 575),
(1125, 225), (1125, 325), (1125, 425), (1125, 525),(1225, 275), (1225, 375),
(1225, 475), (1325, 325), (1325, 425), (1425, 375)]
#己方棋子移动
def chessman_move(event, s, screen, client):
if event.button == 1:
x,y = event.pos[0],event.pos[1]
if s.running:#鼠标第一次按下选择棋子
if s.order == True:
for key in s.hongqi.keys():
if sqrt((s.hongqi[key]['now_weizhi'][0] - x)**2+(s.hongqi[key]['now_weizhi'][1]-y)**2) < s.r:
s.red_backups = [key,s.hongqi[key]]
s.num = int(key)
#backups3 = copy.deepcopy(s.red_backups)
#s.hongqi.pop(s.red_backups[0])
s.ssrcx = (event.pos[0])//50*50+25
s.ssrcy = (event.pos[1])//50*50+25
if s.red_backups:
s.running = False
#鼠标再次按下,落下棋子
else:
x = (event.pos[0])//50*50+25
y = (event.pos[1])//50*50+25
#if (x,y) in POINTS:
if s.red_backups :#红棋
#判断所走位置是否有棋子
if not weizhi_panduan(s,x,y) and (x,y)in POINTS:
s.sdstx = x
s.sdsty = y
#移 和 临近 规则
if (yi_rule(s.red_backups[1]['now_weizhi'][0],s.red_backups[1]['now_weizhi'][1],x,y) \
or lin_rule(s,s.red_backups[1]['now_weizhi'][0],s.red_backups[1]['now_weizhi'][1],x,y))\
and (s.order == True):
s.hongqi[s.red_backups[0]] = s.red_backups[1]
s.hongqi[s.red_backups[0]]['now_weizhi'] = [x,y]
s.restract_num= 1
sendxy(client, s)
s.order = False
# huiqinumber = 1
#单跨 规则
elif (s.order == True)and(dankua_rule(s,(s.red_backups[0]),s.red_backups[1]['now_weizhi'][0],s.red_backups[1]['now_weizhi'][1], x, y)):
s.hongqi[s.red_backups[0]] = s.red_backups[1]
s.hongqi[s.red_backups[0]]['now_weizhi'] = [x,y]
s.restract_num = 1
sendxy(client, s)
s.order = False
s.red_backups = []
s.running = True
def rival_chessman_move(client, s):
"""
对方棋子移动
"""
getxy(client, s)
#暂存对方选中的黑棋
if s.order == False and s.get == 1:
for key in s.lanqi.keys():
if s.srcx == s.lanqi[key]['now_weizhi'][0] and s.srcy == s.lanqi[key]['now_weizhi'][1]:
s.black_backups = [key, s.lanqi[key]]
print("get here")
if s.black_backups:
s.running = False
#更改黑棋位置
if s.black_backups and s.get == 1:
s.lanqi[s.black_backups[0]] = s.black_backups[1]
s.lanqi[s.black_backups[0]]['now_weizhi'] = [s.dstx, s.dsty]
#改为红棋落子
s.order = True
s.black_backups = []
#改为选子状态
s.running = True

@ -0,0 +1,67 @@
import copy
class RedSettings():
def __init__(self):
"""
初始化游戏的设置
"""
# 设置屏幕参数
self.screen_width = 1450
self.screen_height = 800
self.back_color = (152, 245, 245) # 背景颜色
# 设置棋子参数
self.r = 25 # 棋子半径
# 用于记录棋子当前状态
self.lanqi = {0: {'color': 'blue', 'now_weizhi': [25, 375]}, 1: {'color': 'blue', 'now_weizhi': [325, 525]},
2: {'color': 'blue', 'now_weizhi': [325, 325]}, 3: {'color': 'blue', 'now_weizhi': [325, 425]},
4: {'color': 'blue', 'now_weizhi': [325, 225]}, 5: {'color': 'blue', 'now_weizhi': [225, 275]},
6: {'color': 'blue', 'now_weizhi': [225, 375]}, 7: {'color': 'blue', 'now_weizhi': [225, 475]},
8: {'color': 'blue', 'now_weizhi': [125, 425]}, 9: {'color': 'blue', 'now_weizhi': [125, 325]}}
self.hongqi = {0: {'color': 'red', 'now_weizhi': [1425, 375]}, 1: {'color': 'red', 'now_weizhi': [1125, 225]},
2: {'color': 'red', 'now_weizhi': [1125, 425]}, 3: {'color': 'red', 'now_weizhi': [1125, 325]},
4: {'color': 'red', 'now_weizhi': [1125, 525]}, 5: {'color': 'red', 'now_weizhi': [1225, 475]},
6: {'color': 'red', 'now_weizhi': [1225, 375]}, 7: {'color': 'red', 'now_weizhi': [1225, 275]},
8: {'color': 'red', 'now_weizhi': [1325, 325]}, 9: {'color': 'red', 'now_weizhi': [1325, 425]}}
self.HongPoint = [(1125, 225), (1125, 325), (1125, 425), (1125, 525), (1225, 275), (1225, 375),
(1225, 475), (1325, 325), (1325, 425), (1425, 375)]
self.HeiPoint = [(25, 375), (125, 325), (125, 425), (225, 275), (225, 375), (225, 475),
(325, 225), (325, 325), (325, 425), (325, 525)]
# 用于暂时存储上一步棋的位置*****
self.red_backups = [] # red
self.black_backups = [] # blue
self.exp = ''
# 红方第一步
self.first = True
# 记录执子落子状态*********
self.running = True
# 记录传送的棋号
self.num = -1
# 记录游戏结束与否******
self.over = False
# 记录当前落子方,红棋先手 ***
self.order = 1
# 记录是stop还是quit***
self.request = ''
# 记录游戏昵称
self.name = 'lyf'
# 记录分配方0为红方
self.side = 0
self.game_id = ""
self.get = 0
self.srcx = 0
self.srcy = 0
self.dstx = 0
self.dsty = 0
self.ssrcx = 0 #传输起始
self.ssrcy = 0
self.sdstx = 0 #传输终止
self.sdsty = 0

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 KiB

@ -0,0 +1,5 @@
SOCKET_HOST=127.0.0.1
SOCKET_PORT=50005
MAX_WAITING_TIME=450
MAX_THNIKING_TIME=30
MAX_TOTAL_TIME=1000

@ -0,0 +1,247 @@
# coding:utf-8
import socket
import sys
import json
import uuid
import time
from threading import Thread, Lock
if(sys.version[:1] == "3"):
import queue as Queue
from _thread import *
else:
import Queue
from thread import *
class Config:
SOCKET_HOST = '127.0.0.1' # Symbolic name meaning all available interfaces
SOCKET_PORT = 50005 # Arbitrary non-privileged port
MAX_WAITING_TIME = 180
MAX_THNIKING_TIME = 2
MAX_TOTAL_TIME = 600
class Client:
def __init__(self, conn, addr, name, side, time, total):
self.conn = conn
self.addr = addr
self.name = name
self.side = side
self.time = time
self.total = total
mutex = Lock()
mutex_playing = Lock()
playing_ones = {}
waiting_players = Queue.Queue()
#init queues
#for games in range(1, 10):
# waiting_players[games] = Queue.Queue()
def load_config():
with open('config.txt', 'r') as f:
for line in f.readlines():
line = line.strip()
if line.find('SOCKET_HOST=') >= 0:
Config.SOCKET_HOST = line[line.index('=') + 1 : ]
elif line.find('SOCKET_PORT=') >= 0:
Config.SOCKET_PORT = int(line[line.index('=') + 1 : ])
elif line.find('MAX_WAITING_TIME=') >= 0:
Config.MAX_WAITING_TIME = int(line[line.index('=') + 1 : ])
elif line.find('MAX_THNIKING_TIME=') >= 0:
Config.MAX_THNIKING_TIME = int(line[line.index('=') + 1 : ])
elif line.find('MAX_TOTAL_TIME=') >= 0:
Config.MAX_TOTAL_TIME = int(line[line.index('=') + 1 : ])
else:
pass
def get_counterpart_from(waiting_ones):
counterpart = None
mutex.acquire()
if not waiting_ones.empty():
counterpart = waiting_ones.get()
mutex.release()
return counterpart
def to_wait_in_queue(client, the_queue):
mutex.acquire()
the_queue.put(client)
mutex.release()
def remove_one_from_queue(the_queue):
mutex.acquire()
if (the_queue.qsize() == 1):
the_queue.get()
mutex.release()
def send_msg_to(client, msg):
packet = json.dumps(msg)
if (sys.version[:1] == "3"):
packet = packet.encode('utf-8')
#print(client.addr[0] + ":" + str(client.addr[1]) + "\t" + str(msg))
client.conn.send(packet)
def __start_match_between(client0, client1):
match_uuid = str(uuid.uuid4())
mutex_playing.acquire()
playing_ones[match_uuid] = (client0, client1)
mutex_playing.release()
client0.side = 0
client0.time = Config.MAX_THNIKING_TIME
client0.total = Config.MAX_TOTAL_TIME
client1.side = 1
client1.time = -1
client1.total = Config.MAX_TOTAL_TIME
msg0 = {
"status": 1,
"counterpart_name": client1.name,
"game_id": match_uuid,
"side": client0.side,
"think_time": Config.MAX_THNIKING_TIME,
"total_time": Config.MAX_TOTAL_TIME,
}
send_msg_to(client0, msg0)
msg1 = {
"status": 1,
"counterpart_name": client0.name,
"game_id": match_uuid,
"side": client1.side,
"think_time": Config.MAX_THNIKING_TIME,
"total_time": Config.MAX_TOTAL_TIME,
}
send_msg_to(client1, msg1)
def join_game_handler(msg, addr, conn):
new_client = Client(conn, addr, msg['name'], -1, -1, -1)
#game_type = msg["id"]
#the_queue = waiting_players[game_type]
counterpart = get_counterpart_from(waiting_players)
if not counterpart: #wait
to_wait_in_queue(new_client, waiting_players)
return
else:
#counterpart=get_counterpart_from(waiting_players[game_type])
__start_match_between(new_client, counterpart)
def quit_game_handler(msg):
match_uuid = msg['game_id']
if match_uuid is None:
remove_one_from_queue(waiting_players)
return
pairs = None
mutex_playing.acquire()
if (match_uuid in playing_ones):
pairs = playing_ones[match_uuid]
del playing_ones[match_uuid]
mutex_playing.release()
if pairs is not None:
if(pairs[0].side == msg['side']):
to_notify = pairs[1]
else:
to_notify = pairs[0]
msg = {
"status": 2, #exit
"game_id": match_uuid,
"side": msg['side'],
"request": msg['request'],
}
send_msg_to(to_notify, msg)
def timer_thread():
while True:
time.sleep(1)
mutex_playing.acquire()
for game_id in list(playing_ones.keys()):
(client0, client1) = playing_ones[game_id]
#print("%d - %d" % (client0.time, client1.time))
if (client0.time < 0):
client = client1
else:
client = client0
if (client.time == 0 or client.total == 0):
msg = {
"status": 3, #timeout exit
"game_id": game_id,
"side": client.side,
}
send_msg_to(client0, msg)
send_msg_to(client1, msg)
del playing_ones[game_id]
else:
client.time -= 1
client.total -= 1
mutex_playing.release()
def transfer_message(msg):
match_uuid = msg['game_id']
pairs = playing_ones[match_uuid]
if(pairs[0].side == msg['side']):
to_notify = pairs[1]
pairs[0].time = -1
else:
to_notify = pairs[0]
pairs[1].time = -1
to_notify.time = Config.MAX_THNIKING_TIME
send_msg_to(to_notify, msg)
def client_thread(conn, addr):
while True:
data = conn.recv(1024)
if not data:
break
print(data)
data = json.loads(data)
if not 'type' in data:
transfer_message(data['msg'])
continue
if data['type'] == 0:
join_game_handler(data['msg'], addr, conn)
continue
elif data['type'] == 1:
transfer_message(data['msg'])
continue
elif data['type'] == 2:
quit_game_handler(data['msg'])
break
elif data['type'] == 3:
break
else:
#delivering message between the two clients
transfer_message(data['msg'])
continue
#came out of loop
conn.close()
def main():
load_config()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket server created')
#try:
server.bind(('', Config.SOCKET_PORT))
#except (socket.error, msg):
# print ('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
# sys.exit()
print('Socket bind complete')
server.listen(10)
print('Socket now listening')
#now keep talking with the client
start_new_thread(timer_thread, ())
while True:
#wait to accept a connection - blocking call
conn, addr = server.accept()
print ('Connected with ' + addr[0] + ':' + str(addr[1]))
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(client_thread, (conn, addr))
server.close()
if __name__ == '__main__':
main()
Loading…
Cancel
Save