在实现黑白棋程序过程中,我遇到了个奇怪的问题,客户端和服务器端有时候无法同步在发生翻转的时候,SERVER端下了一子,结果本地没有翻转,但是CILNET端却能正确的表示SERVER所要求翻转的结果两边不同步;CLIENT端下子翻转两端都可以翻转实现同步一切正常,我给一下简要的一些代码大家帮我看看吧
CLIENT端下子
Reverse tmpX, tmpY '翻转能够翻转的棋子
picPlayField_Paint '重画棋盘的棋子
m_bMyTurn = False
Winsock1.SendData conPos & x & ": " & y //传输给SERVER下子方位
SERVER下子后
Winsock1(1).SendData conPos & x & ": " & y
Reverse tmpX, tmpY
picPlayField_Paint
m_bMyTurn = False
两端的消息接受处理函数大致相同,去询问了一个老手,他说可能是REVERSE函数有问题,下面给一下这个函数(这个函数我是书上看来的应该没错吧!·!·)
'以刚下的 棋子为中心,查找同水平线,同垂直线和同斜线上所有能够翻转的棋子并翻转
Public Sub Reverse(x%, y%)
Dim count(1 To 8) As Integer
Dim tmpX%, tmpY%, i%, j%, bMeetAnotherColor As Boolean, bGetIt As Boolean
For i = 1 To 8
count(i) = 0
'在这个方向上找到同色的棋子后看看与刚下的棋子的间距
bMeetAnotherColor = False
bGetIt = False
tmpX = x + arrDirect(i).xInc
tmpY = y + arrDirect(i).yInc
Do
If (tmpX > 0 And tmpY > 0 And tmpX <= conWidthUnit And _
tmpY <= conHeightUnit) Then
If (m_bBlack And m_bMyTurn) Or _
(Not m_bBlack And Not m_bMyTurn) Then
'刚落下的 棋子是黑色的
If m_arrStonePos(tmpX, tmpY) = "n " Then
Exit Do
Else
If m_arrStonePos(tmpX, tmpY) = "w " Then
bMeetAnotherColor = True
count(i) = count(i) + 1
Else
If bMeetAnotherColor Then
bGetIt = True
Exit Do
End If
End If
End If
Else
'刚落下的棋子为白色的
If m_arrStonePos(tmpX, tmpY) = "n " Then
Exit Do
Else
If m_arrStonePos(tmpX, tmpY) = "b " Then
bMeetAnotherColor = True
count(i) = count(i) + 1
Else
If bMeetAnotherColor Then '再遇上白子
bGetIt = True
Exit Do
End If
End If
End If
End If
tmpX = tmpX + arrDirect(i).xInc
tmpY = tmpY + arrDirect(i).yInc
Else
Exit Do
End If
Loop
'如果不是两个同色棋子中夹着count(i)个异色棋子count(i)作废
If Not bGetIt Then count(i) = 0
Next i
'翻转
For i = 1 To 8
tmpX = x
tmpY = y
If count(i) > 0 Then
For j = 1 To count(i) '将I方向上的 count(i)个棋子翻转
tmpX = tmpX + arrDirect(i).xInc
tmpY = tmpY + arrDirect(i).yInc
If (m_bBlack And m_MyTurn) _
Or (Not m_bBlack And Not m_bMyTurn) Then
m_arrStonePos(tmpX, tmpY) = "b "
Else
m_arrStonePos(tmpX, tmpY) = "w "
End If
Next j
End If
Next i
End Sub
CLIENT端下子
Reverse tmpX, tmpY '翻转能够翻转的棋子
picPlayField_Paint '重画棋盘的棋子
m_bMyTurn = False
Winsock1.SendData conPos & x & ": " & y //传输给SERVER下子方位
SERVER下子后
Winsock1(1).SendData conPos & x & ": " & y
Reverse tmpX, tmpY
picPlayField_Paint
m_bMyTurn = False
两端的消息接受处理函数大致相同,去询问了一个老手,他说可能是REVERSE函数有问题,下面给一下这个函数(这个函数我是书上看来的应该没错吧!·!·)
'以刚下的 棋子为中心,查找同水平线,同垂直线和同斜线上所有能够翻转的棋子并翻转
Public Sub Reverse(x%, y%)
Dim count(1 To 8) As Integer
Dim tmpX%, tmpY%, i%, j%, bMeetAnotherColor As Boolean, bGetIt As Boolean
For i = 1 To 8
count(i) = 0
'在这个方向上找到同色的棋子后看看与刚下的棋子的间距
bMeetAnotherColor = False
bGetIt = False
tmpX = x + arrDirect(i).xInc
tmpY = y + arrDirect(i).yInc
Do
If (tmpX > 0 And tmpY > 0 And tmpX <= conWidthUnit And _
tmpY <= conHeightUnit) Then
If (m_bBlack And m_bMyTurn) Or _
(Not m_bBlack And Not m_bMyTurn) Then
'刚落下的 棋子是黑色的
If m_arrStonePos(tmpX, tmpY) = "n " Then
Exit Do
Else
If m_arrStonePos(tmpX, tmpY) = "w " Then
bMeetAnotherColor = True
count(i) = count(i) + 1
Else
If bMeetAnotherColor Then
bGetIt = True
Exit Do
End If
End If
End If
Else
'刚落下的棋子为白色的
If m_arrStonePos(tmpX, tmpY) = "n " Then
Exit Do
Else
If m_arrStonePos(tmpX, tmpY) = "b " Then
bMeetAnotherColor = True
count(i) = count(i) + 1
Else
If bMeetAnotherColor Then '再遇上白子
bGetIt = True
Exit Do
End If
End If
End If
End If
tmpX = tmpX + arrDirect(i).xInc
tmpY = tmpY + arrDirect(i).yInc
Else
Exit Do
End If
Loop
'如果不是两个同色棋子中夹着count(i)个异色棋子count(i)作废
If Not bGetIt Then count(i) = 0
Next i
'翻转
For i = 1 To 8
tmpX = x
tmpY = y
If count(i) > 0 Then
For j = 1 To count(i) '将I方向上的 count(i)个棋子翻转
tmpX = tmpX + arrDirect(i).xInc
tmpY = tmpY + arrDirect(i).yInc
If (m_bBlack And m_MyTurn) _
Or (Not m_bBlack And Not m_bMyTurn) Then
m_arrStonePos(tmpX, tmpY) = "b "
Else
m_arrStonePos(tmpX, tmpY) = "w "
End If
Next j
End If
Next i
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货