|
|
|
@ -1,7 +1,15 @@
|
|
|
|
|
import socket
|
|
|
|
|
import socket
|
|
|
|
|
import this
|
|
|
|
|
import requests
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
|
|
|
|
from threading import Thread
|
|
|
|
|
#multithreading?
|
|
|
|
|
def startNewThread(target):
|
|
|
|
|
thread = Thread(target=target)
|
|
|
|
|
thread.daemon = True
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
class Network:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
@ -28,6 +36,7 @@ class Network:
|
|
|
|
|
packet = packet.encode('utf-8')
|
|
|
|
|
#return self.client.recv(2048).encode()
|
|
|
|
|
self.client.send(packet)
|
|
|
|
|
print("send complete")
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
@ -38,8 +47,9 @@ class Network:
|
|
|
|
|
|
|
|
|
|
def receive(self):
|
|
|
|
|
try:
|
|
|
|
|
msg = json.loads(self.client.recv(2048))
|
|
|
|
|
print(msg)
|
|
|
|
|
recvMsg = self.client.recv(2048).decode('utf-8')
|
|
|
|
|
msg = json.loads(recvMsg)
|
|
|
|
|
print('receiver got: ', msg)
|
|
|
|
|
return msg
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
print(e)
|
|
|
|
@ -100,6 +110,7 @@ class GameState:
|
|
|
|
|
self.isNet = isNet
|
|
|
|
|
self.game_id = game_id
|
|
|
|
|
self.red_to_move=MySide
|
|
|
|
|
self.MySide = MySide
|
|
|
|
|
self.client = client
|
|
|
|
|
self.conquered=False
|
|
|
|
|
self.win_person=''
|
|
|
|
@ -107,6 +118,8 @@ class GameState:
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
@ -126,11 +139,19 @@ class GameState:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.client.send(thisMove)
|
|
|
|
|
def dnldmv_server(self):
|
|
|
|
|
def __updateOther__(self):#! 注意:考虑使用多线程执行该函数。
|
|
|
|
|
#print('waiting 4 other player 2 act...')
|
|
|
|
|
thisMovejson = self.client.receive()
|
|
|
|
|
thisMove = Move([thisMovejson['src']['x'],thisMovejson['src']['y']],[thisMovejson['dst']['x'],thisMovejson['dst']['y']],self.board)
|
|
|
|
|
return thisMove
|
|
|
|
|
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":
|
|
|
|
@ -326,8 +347,9 @@ class GameState:
|
|
|
|
|
return self.conquered
|
|
|
|
|
|
|
|
|
|
#移动操作
|
|
|
|
|
def makeMove(self,move):#cur是当前位置,nxt是下一个位置,参数传入为元组
|
|
|
|
|
def makeMove(self,move,toUpload = True):#cur是当前位置,nxt是下一个位置,参数传入为元组
|
|
|
|
|
# makeMove假设这个move一定是合法的
|
|
|
|
|
# 为什么添加toUpload?防止使用makeMove更新己方棋盘时,把敌方棋盘错误上传,导致“棋子消失”。
|
|
|
|
|
if self.board[move.end_row][move.end_col] != '00':
|
|
|
|
|
nxt_piece = self.board[move.end_row][move.end_col]
|
|
|
|
|
if nxt_piece[0] == 'r':
|
|
|
|
@ -336,9 +358,10 @@ class GameState:
|
|
|
|
|
self.blue_pieces.remove(int(nxt_piece[1]))
|
|
|
|
|
self.board[move.end_row][move.end_col] = self.board[move.start_row][move.start_col]
|
|
|
|
|
self.board[move.start_row][move.start_col] = '00'
|
|
|
|
|
if not self.isNet: self.red_to_move = not self.red_to_move
|
|
|
|
|
else:
|
|
|
|
|
#直接在这里完成两步实现看起来不可行……
|
|
|
|
|
#因为界面更新在main中
|
|
|
|
|
self.red_to_move = not self.red_to_move
|
|
|
|
|
#由于使用多线程,考虑让另一个线程去更新red_to_move
|
|
|
|
|
if self.isNet and toUpload:
|
|
|
|
|
#因为界面更新在main中,考虑在main中执行相关指令。
|
|
|
|
|
self.upldmv_server(move)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|