|
|
|
@ -4,20 +4,42 @@ import requests
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
|
|
|
|
from threading import Thread
|
|
|
|
|
from queue import Queue
|
|
|
|
|
#multithreading?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Move:
|
|
|
|
|
def __init__(self,start_loc,end_loc,board):
|
|
|
|
|
self.start_row = start_loc[0]
|
|
|
|
|
self.start_col = start_loc[1]
|
|
|
|
|
self.end_row = end_loc[0]
|
|
|
|
|
self.end_col = end_loc[1]
|
|
|
|
|
self.nxt_piece = board[self.end_row][self.end_col]
|
|
|
|
|
self.cur_piece = board[self.start_row][self.start_col]
|
|
|
|
|
self.attack = self.nxt_piece != '00'
|
|
|
|
|
self.moveID = self.start_row + self.start_col*10 + self.end_row *100 +self.end_col * 1000 #Hash
|
|
|
|
|
def __eq__(self, __o: object) -> bool:
|
|
|
|
|
if isinstance(__o, Move):
|
|
|
|
|
return self.moveID == __o.moveID
|
|
|
|
|
return False
|
|
|
|
|
def startNewThread(target):
|
|
|
|
|
thread = Thread(target=target)
|
|
|
|
|
thread.daemon = True
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
class Network:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
def __init__(self,isNet=False): #注意:这里是否在传引用?
|
|
|
|
|
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
self.server = "127.0.0.1"
|
|
|
|
|
self.port = 50005
|
|
|
|
|
self.addr = (self.server, self.port)
|
|
|
|
|
|
|
|
|
|
self.msg = self.connect()
|
|
|
|
|
#self.game_state = game_state #考虑将game_state接入Network以方便修改
|
|
|
|
|
global msg_from_serv
|
|
|
|
|
thread = Thread(target = self.receive())#开线程始终监听
|
|
|
|
|
thread.setDaemon(True)
|
|
|
|
|
thread.start() #专用数据接收线程
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getPos(self):
|
|
|
|
|
return self.pos
|
|
|
|
@ -34,44 +56,82 @@ class Network:
|
|
|
|
|
packet = json.dumps(msg)
|
|
|
|
|
if (sys.version[:1] == '3'):
|
|
|
|
|
packet = packet.encode('utf-8')
|
|
|
|
|
#return self.client.recv(2048).encode()
|
|
|
|
|
self.client.send(packet)
|
|
|
|
|
print("send complete")
|
|
|
|
|
return self.client.recv(2048).encode()
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
print(e)
|
|
|
|
|
return e
|
|
|
|
|
|
|
|
|
|
def post(self, data): #using requests lib, data is json
|
|
|
|
|
headers = {'Content-Type': 'application/json'}
|
|
|
|
|
response = requests.post(url='http://localhost',data=data)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
def receive(self):
|
|
|
|
|
def receive(self): #写成持续监听模式
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
recvMsg = self.client.recv(2048).decode('utf-8')
|
|
|
|
|
msg = json.loads(recvMsg)
|
|
|
|
|
print('receiver got: ', msg)
|
|
|
|
|
return msg
|
|
|
|
|
if len(recvMsg) != 0:
|
|
|
|
|
msg_from_serv = json.loads(recvMsg)
|
|
|
|
|
self.dataDealer(msg_from_serv)
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
print(e)
|
|
|
|
|
return e
|
|
|
|
|
|
|
|
|
|
class Move:
|
|
|
|
|
def __init__(self,start_loc,end_loc,board):
|
|
|
|
|
self.start_row = start_loc[0]
|
|
|
|
|
self.start_col = start_loc[1]
|
|
|
|
|
self.end_row = end_loc[0]
|
|
|
|
|
self.end_col = end_loc[1]
|
|
|
|
|
self.nxt_piece = board[self.end_row][self.end_col]
|
|
|
|
|
self.cur_piece = board[self.start_row][self.start_col]
|
|
|
|
|
self.attack = self.nxt_piece != '00'
|
|
|
|
|
self.moveID = self.start_row + self.start_col*10 + self.end_row *100 +self.end_col * 1000 #Hash
|
|
|
|
|
def __eq__(self, __o: object) -> bool:
|
|
|
|
|
if isinstance(__o, Move):
|
|
|
|
|
return self.moveID == __o.moveID
|
|
|
|
|
return False
|
|
|
|
|
def dataDealer(self, msg_json): #根据相关数据对棋盘状态进行更新
|
|
|
|
|
if 'status' in msg_json.keys():
|
|
|
|
|
#处理相关特殊情况
|
|
|
|
|
if msg_json['status'] == '1':
|
|
|
|
|
print('Waiting 4 the other player...')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif 'src' and 'dst' in msg_json.keys(): #侦测到移动
|
|
|
|
|
theMove = Move([msg_json['src']['x'],msg_json['src']['y']],[msg_json['dst']['x'],msg_json['dst']['y']],self.game_state.board)
|
|
|
|
|
self.game_state.makeMove(theMove,toUpload = False)
|
|
|
|
|
self.game_state.exchange()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dataReceiver(self):
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
msg = self.client.recv(2048).decode('utf-8')
|
|
|
|
|
if len(msg) != 0: self.dataDealer(json.loads(msg))
|
|
|
|
|
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
print(e)
|
|
|
|
|
return e
|
|
|
|
|
|
|
|
|
|
def tell_move(self, move: Move):
|
|
|
|
|
thisMove = {
|
|
|
|
|
'type': 1,
|
|
|
|
|
'msg':{
|
|
|
|
|
"game_id": self.game_id,
|
|
|
|
|
"side": int(self.red_to_move),
|
|
|
|
|
"chessman": move.cur_piece,
|
|
|
|
|
"src": {
|
|
|
|
|
"x": move.start_row,
|
|
|
|
|
"y": move.start_col
|
|
|
|
|
},
|
|
|
|
|
"dst": {
|
|
|
|
|
"x": move.end_row,
|
|
|
|
|
"y": move.end_col
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.client.send(thisMove)
|
|
|
|
|
|
|
|
|
|
def login(self,myName):
|
|
|
|
|
myPlayerData = {
|
|
|
|
|
'type': 0, # game state type ?
|
|
|
|
|
'msg': {
|
|
|
|
|
'name': myName
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.client.send(myPlayerData)
|
|
|
|
|
class GameState:
|
|
|
|
|
def __init__(self, isNet = False, MySide = True,
|
|
|
|
|
game_id = None, client:Network = None ):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
'''
|
|
|
|
|
有关信息:
|
|
|
|
|
1. 棋盘尺寸为7*9(横向)
|
|
|
|
@ -107,51 +167,17 @@ class GameState:
|
|
|
|
|
self.blue_pieces=[7,6,5,4,3,2,1]
|
|
|
|
|
self.red_pieces=[7,6,5,4,3,2,1]
|
|
|
|
|
#红方(右)先行
|
|
|
|
|
self.isNet = isNet
|
|
|
|
|
self.game_id = game_id
|
|
|
|
|
self.red_to_move=MySide
|
|
|
|
|
self.MySide = MySide
|
|
|
|
|
self.client = client
|
|
|
|
|
self.red_to_move=True
|
|
|
|
|
self.conquered=False
|
|
|
|
|
self.win_person=''
|
|
|
|
|
self.MASSACRE=False
|
|
|
|
|
self.isStarted = False
|
|
|
|
|
|
|
|
|
|
def color(self):
|
|
|
|
|
return 'r' if self.red_to_move else 'b'
|
|
|
|
|
def exchange(self):
|
|
|
|
|
self.red_to_move = not self.red_to_move
|
|
|
|
|
#网络版相关组件
|
|
|
|
|
def upldmv_server(self,move):
|
|
|
|
|
thisMove = {
|
|
|
|
|
'type': 1,
|
|
|
|
|
'msg':{
|
|
|
|
|
"game_id": self.game_id,
|
|
|
|
|
"side": int(self.red_to_move),
|
|
|
|
|
"chessman": move.cur_piece,
|
|
|
|
|
"src": {
|
|
|
|
|
"x": move.start_row,
|
|
|
|
|
"y": move.start_col
|
|
|
|
|
},
|
|
|
|
|
"dst": {
|
|
|
|
|
"x": move.end_row,
|
|
|
|
|
"y": move.end_col
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.client.send(thisMove)
|
|
|
|
|
def __updateOther__(self):#! 注意:考虑使用多线程执行该函数。
|
|
|
|
|
#print('waiting 4 other player 2 act...')
|
|
|
|
|
thisMovejson = self.client.receive()
|
|
|
|
|
if not thisMovejson or thisMovejson['side'] == int(self.MySide):
|
|
|
|
|
return
|
|
|
|
|
print('passing checkpoint')
|
|
|
|
|
if 'chessman' in thisMovejson.keys(): #正常移动过程
|
|
|
|
|
thisMove = Move([thisMovejson['src']['x'],thisMovejson['src']['y']],[thisMovejson['dst']['x'],thisMovejson['dst']['y']],self.board)
|
|
|
|
|
self.makeMove(thisMove,toUpload=False)
|
|
|
|
|
self.exchange()
|
|
|
|
|
|
|
|
|
|
def updateEnemy(self):
|
|
|
|
|
startNewThread(self.__updateOther__)#测试多线程更新……
|
|
|
|
|
|
|
|
|
|
# 判断特殊位置
|
|
|
|
|
def inHome(self,row,col,color):
|
|
|
|
|
if color=="b":
|
|
|
|
@ -347,7 +373,7 @@ class GameState:
|
|
|
|
|
return self.conquered
|
|
|
|
|
|
|
|
|
|
#移动操作
|
|
|
|
|
def makeMove(self,move,toUpload = True):#cur是当前位置,nxt是下一个位置,参数传入为元组
|
|
|
|
|
def makeMove(self,move):#cur是当前位置,nxt是下一个位置,参数传入为元组
|
|
|
|
|
# makeMove假设这个move一定是合法的
|
|
|
|
|
# 为什么添加toUpload?防止使用makeMove更新己方棋盘时,把敌方棋盘错误上传,导致“棋子消失”。
|
|
|
|
|
if self.board[move.end_row][move.end_col] != '00':
|
|
|
|
@ -360,8 +386,5 @@ class GameState:
|
|
|
|
|
self.board[move.start_row][move.start_col] = '00'
|
|
|
|
|
self.red_to_move = not self.red_to_move
|
|
|
|
|
#由于使用多线程,考虑让另一个线程去更新red_to_move
|
|
|
|
|
if self.isNet and toUpload:
|
|
|
|
|
#因为界面更新在main中,考虑在main中执行相关指令。
|
|
|
|
|
self.upldmv_server(move)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|