这个程序是判断“象”或“相”的可走到的位置 通过(x,y)与(oldx,oldy)的相对位置来判断 1.If (x - oldx) * (y - oldy) = 0 Then IsAbleToPut = False : Exit Function 同一行或同一列,不允许 2.If Abs(x - oldx) <> 2 Or Abs(y - oldy) <> 2 Then IsAbleToPut = False : Exit Function‘横坐标相对位置差不是2,不允许 3.If y < 6 Then IsAbleToPut = False : Exit Function’不允许下到对方区域 4. If x - oldx = 2 Then i = x - 1 End If If x - oldx = -2 Then i = x + 1 End If If y - oldy = 2 Then j = y - 1 End If If y - oldy = -2 Then j = y + 1 End If ‘求得可走的新位置和旧位置的“象眼”位置坐标 5.If Map(i, j) <> 0 Then IsAbleToPut = False : Exit Function ’应该是判断“象眼”位置是否已有棋子,若有,不允许走
Dim oldx As Integer, oldy As Integer '原在棋盘坐标 oldx = GetChessX(idx) oldy = GetChessY(idx) If picChess(idx).Tag = "象" Or picChess(idx).Tag = "相" Then
'如果棋子移动的目标位置与现在的位置在一条直线上,则不移动(IsAbleToPut = False),以为象不能横竖直着走。 If (x - oldx) * (y - oldy) = 0 Then IsAbleToPut = False: Exit Function
'如果棋子移动的目标位置与现在的位置距离不是两格,则不移动(IsAbleToPut = False),因为"象"走"田"吗。 If Abs(x - oldx) <> 2 Or Abs(y - oldy) <> 2 Then IsAbleToPut = False: Exit Function '如果走到对方,则不移动(IsAbleToPut = False) If y < 6 Then IsAbleToPut = False: Exit Function '以下判断象眼上有无棋子,i 和 j 是象眼的位置坐标 If x - oldx = 2 Then I = x - 1 End If
If x - oldx = -2 Then I = x + 1 End If
If y - oldy = 2 Then j = y - 1 End If
If y - oldy = -2 Then j = y + 1 End If
'如果象眼上有棋子,则不移动。 If Map(I, j) <> 0 Then IsAbleToPut = False: Exit Function '以上情况都不是,则移动棋子(IsAbleToPut = True)。 IsAbleToPut = True Exit Function End If
厉害,请受小弟一拜,以后请多多指教啊,你也是用vb.net开发的吧?
If x - oldx = 2 Then I = x - 1 End If
If x - oldx = -2 Then I = x + 1 End If
If y - oldy = 2 Then j = y - 1 End If
If y - oldy = -2 Then j = y + 1 End If If Map(i, j) <> 0 Then IsAbleToPut = False : Exit Function 可以简化成一句话:If Map((x + oldx) / 2, (y + oldy) / 2) <> 0 Then IsAbleToPut = False : Exit Function
本帖最后由 bcrun 于 2011-05-27 09:10:13 编辑
我首先声明:这段程序不是我写的。If 语句通常的用法是带 End If 的,但有些写法可以不带 End If。具体你看看 VB 中 IF 的用法。象棋中各种棋子的走法都有自己的规则,所以在编程时要将各种不可能的方法都想到,如 象、士、马不能直着走,将 一次不能走两格等等,超出规则那就是犯规了。车 和 炮,首先要直着走,因此要求 x - oldx = 0 或 y - oldy = 0 ,其次 车 目标位置与当前位置之间不应该有棋子,目标位置有对方的棋子时,要吃掉,是己方的棋子则不移动等等。炮 也一样,都得按规矩来。程序中, 变量 C 是为了计算 炮 路上棋子的个数:If c > 1 Then IsAbleToPut = False : Exit Function 如果 C > 1 ,说明在 炮 路上有至少两个棋子,不移动。If c = 0 Then If Map(x, y) <> 0 Then IsAbleToPut = False : Exit Function 在 炮 路上没有棋子且目标位置有棋子,则不能移动。If c = 1 Then If (IsMyChess(Map(x, y)) Or Map(x, y) = 0) Then IsAbleToPut = False 炮 路上有一个棋子且目标位置是己方的棋子,也不能移动。用 For 是为了计算在 炮 路上有没有或几个棋子,就这作用。当然 炮 路分横竖两条,所以两个 For 循环。
恩,谢谢!还能再问你一个问题吗?这几天我一直都想不懂,代码是看懂了,但调试出来什么反应都没有?就是图片的点击事件,在设置路径中 Private Sub SetPath() '设置路径 Dim str As String Dim path As String path = System.Windows.Forms.Application.StartupPath Dim i As Integer For i = 1 To 36 mypic(i) = New PictureBox Me.Controls.Add(mypic(i)) mypic(i).SetBounds(0, 0, (r - 1) * 2, (r - 1) * 2) mypic(i).BackColor = System.Drawing.SystemColors.GrayText mypic(i).Name = "Mypic" & i.ToString mypic(i).Width = 60 mypic(i).Height = 60 str = path & "\image\q" & i.ToString & ".png" If i < 13 And i > 0 Then mypic(i).Image = Image.FromFile(str) If i < 33 And i > 20 Then mypic(i).Image = Image.FromFile(str) mypic(i).Visible = False mypic(i).BringToFront() ' Me.Text = str ' AddHandler CType(mypic(i), PictureBox).Click, AddressOf mypic_click AddHandler mypic(i).Click, AddressOf mypic_click Next End Sub 添加了 AddHandler mypic(i).Click, AddressOf mypic_click 然后在 Private Sub mypic_click(ByVal sender As System.Object, ByVal e As System.EventArgs) If IsMyTurn = False Then Exit Sub End If Dim x, y, index As Integer x = GetChessX(CInt(CType(sender, PictureBox).Text)) y = GetChessY(CInt(CType(sender, PictureBox).Text)) If Curselect = 0 Then If Not IsMyChess(Map(x, y)) Then Label1.Text = "cuowu" Else Curselect = CInt(CType(sender, PictureBox).Text) mypic(Curselect).BackColor = System.Drawing.Color.Blue stepflag = True End If Else If IsMyChess(Map(x, y)) Then mypic(Curselect).BackColor = System.Drawing.SystemColors.GrayText Curselect = CInt(CType(sender, PictureBox).Text) mypic(Curselect).BackColor = System.Drawing.Color.Blue stepflag = True Exit Sub End If If AbleMove(Curselect, x, y) Then ListBox1.Items.Add(RecordPath(Curselect, x, y)) Map(GetChessX(Curselect), GetChessY(Curselect)) = 0 index = CInt(CType(sender, PictureBox).Text) mypic(index).Visible = False MoveChess(Curselect, x, y) Map(x, y) = Curselect If index = 1 Then Label1.Text = "hongfangyingle" MessageBox.Show("hongfangyingle", "tishi") 'send("move" + "|" + Format(Curselect) + "|" + Format(x) + "|" + Format(11 - y)) 'send("succ" + "|" + "红方赢了") Exit Sub End If ListBox1.Items.Add("己方" + CType(sender, PictureBox).Tag.ToString + "走到x" + x.ToString + "y" + y.ToString) 'send("move" + "|" + Format(Curselect) + "|" + Format(x) + "|" + Format(11 - y)) Curselect = 0 '该对方了 'SetMyTurn(False) Label1.Text = "" Else '不能走棋 mypic(Curselect).BackColor = System.Drawing.SystemColors.GrayText Label1.Text = "不能走棋" Curselect = 0 End If stepflag = False End If End Sub 由于原来那代码是做成网络版的,我这里只是想实现点击事件。我是厚着脸皮问你的,希望你不要介意,我还是菜鸟,就最后一个问题了,问完马上给分。拜托了,朋友。
通过(x,y)与(oldx,oldy)的相对位置来判断
1.If (x - oldx) * (y - oldy) = 0 Then IsAbleToPut = False : Exit Function
同一行或同一列,不允许
2.If Abs(x - oldx) <> 2 Or Abs(y - oldy) <> 2 Then IsAbleToPut = False : Exit Function‘横坐标相对位置差不是2,不允许
3.If y < 6 Then IsAbleToPut = False : Exit Function’不允许下到对方区域
4. If x - oldx = 2 Then
i = x - 1
End If
If x - oldx = -2 Then
i = x + 1
End If
If y - oldy = 2 Then
j = y - 1
End If
If y - oldy = -2 Then
j = y + 1
End If
‘求得可走的新位置和旧位置的“象眼”位置坐标
5.If Map(i, j) <> 0 Then IsAbleToPut = False : Exit Function
’应该是判断“象眼”位置是否已有棋子,若有,不允许走
Dim oldx As Integer, oldy As Integer '原在棋盘坐标
oldx = GetChessX(idx)
oldy = GetChessY(idx) If picChess(idx).Tag = "象" Or picChess(idx).Tag = "相" Then
'如果棋子移动的目标位置与现在的位置在一条直线上,则不移动(IsAbleToPut = False),以为象不能横竖直着走。
If (x - oldx) * (y - oldy) = 0 Then IsAbleToPut = False: Exit Function
'如果棋子移动的目标位置与现在的位置距离不是两格,则不移动(IsAbleToPut = False),因为"象"走"田"吗。
If Abs(x - oldx) <> 2 Or Abs(y - oldy) <> 2 Then IsAbleToPut = False: Exit Function '如果走到对方,则不移动(IsAbleToPut = False)
If y < 6 Then IsAbleToPut = False: Exit Function '以下判断象眼上有无棋子,i 和 j 是象眼的位置坐标
If x - oldx = 2 Then
I = x - 1
End If
If x - oldx = -2 Then
I = x + 1
End If
If y - oldy = 2 Then
j = y - 1
End If
If y - oldy = -2 Then
j = y + 1
End If
'如果象眼上有棋子,则不移动。
If Map(I, j) <> 0 Then IsAbleToPut = False: Exit Function '以上情况都不是,则移动棋子(IsAbleToPut = True)。
IsAbleToPut = True
Exit Function
End If
If x - oldx = 2 Then
I = x - 1
End If
If x - oldx = -2 Then
I = x + 1
End If
If y - oldy = 2 Then
j = y - 1
End If
If y - oldy = -2 Then
j = y + 1
End If
If Map(i, j) <> 0 Then IsAbleToPut = False : Exit Function
可以简化成一句话:If Map((x + oldx) / 2, (y + oldy) / 2) <> 0 Then IsAbleToPut = False : Exit Function
如果 C > 1 ,说明在 炮 路上有至少两个棋子,不移动。If c = 0 Then If Map(x, y) <> 0 Then IsAbleToPut = False : Exit Function
在 炮 路上没有棋子且目标位置有棋子,则不能移动。If c = 1 Then If (IsMyChess(Map(x, y)) Or Map(x, y) = 0) Then IsAbleToPut = False
炮 路上有一个棋子且目标位置是己方的棋子,也不能移动。用 For 是为了计算在 炮 路上有没有或几个棋子,就这作用。当然 炮 路分横竖两条,所以两个 For 循环。
很高兴可以和你成为好友,共同学习,共同进步。不过我用的还是VB6,没有用VB.NET,想学习,方法大同小异:多看书,多实践,多看别人的程序代码。祝你进步!
Dim str As String
Dim path As String
path = System.Windows.Forms.Application.StartupPath
Dim i As Integer
For i = 1 To 36
mypic(i) = New PictureBox
Me.Controls.Add(mypic(i))
mypic(i).SetBounds(0, 0, (r - 1) * 2, (r - 1) * 2)
mypic(i).BackColor = System.Drawing.SystemColors.GrayText
mypic(i).Name = "Mypic" & i.ToString
mypic(i).Width = 60
mypic(i).Height = 60 str = path & "\image\q" & i.ToString & ".png"
If i < 13 And i > 0 Then mypic(i).Image = Image.FromFile(str)
If i < 33 And i > 20 Then mypic(i).Image = Image.FromFile(str)
mypic(i).Visible = False
mypic(i).BringToFront()
' Me.Text = str
' AddHandler CType(mypic(i), PictureBox).Click, AddressOf mypic_click
AddHandler mypic(i).Click, AddressOf mypic_click
Next
End Sub
添加了 AddHandler mypic(i).Click, AddressOf mypic_click
然后在 Private Sub mypic_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If IsMyTurn = False Then
Exit Sub
End If
Dim x, y, index As Integer
x = GetChessX(CInt(CType(sender, PictureBox).Text))
y = GetChessY(CInt(CType(sender, PictureBox).Text))
If Curselect = 0 Then
If Not IsMyChess(Map(x, y)) Then
Label1.Text = "cuowu"
Else
Curselect = CInt(CType(sender, PictureBox).Text)
mypic(Curselect).BackColor = System.Drawing.Color.Blue
stepflag = True
End If
Else
If IsMyChess(Map(x, y)) Then
mypic(Curselect).BackColor = System.Drawing.SystemColors.GrayText
Curselect = CInt(CType(sender, PictureBox).Text)
mypic(Curselect).BackColor = System.Drawing.Color.Blue
stepflag = True
Exit Sub
End If
If AbleMove(Curselect, x, y) Then
ListBox1.Items.Add(RecordPath(Curselect, x, y))
Map(GetChessX(Curselect), GetChessY(Curselect)) = 0
index = CInt(CType(sender, PictureBox).Text)
mypic(index).Visible = False
MoveChess(Curselect, x, y)
Map(x, y) = Curselect
If index = 1 Then
Label1.Text = "hongfangyingle"
MessageBox.Show("hongfangyingle", "tishi")
'send("move" + "|" + Format(Curselect) + "|" + Format(x) + "|" + Format(11 - y))
'send("succ" + "|" + "红方赢了")
Exit Sub
End If
ListBox1.Items.Add("己方" + CType(sender, PictureBox).Tag.ToString + "走到x" + x.ToString + "y" + y.ToString)
'send("move" + "|" + Format(Curselect) + "|" + Format(x) + "|" + Format(11 - y))
Curselect = 0
'该对方了
'SetMyTurn(False)
Label1.Text = ""
Else '不能走棋
mypic(Curselect).BackColor = System.Drawing.SystemColors.GrayText
Label1.Text = "不能走棋"
Curselect = 0
End If
stepflag = False
End If
End Sub
由于原来那代码是做成网络版的,我这里只是想实现点击事件。我是厚着脸皮问你的,希望你不要介意,我还是菜鸟,就最后一个问题了,问完马上给分。拜托了,朋友。