两人对战网络中国象棋3.1具体实现

 

具体开发步骤:

1)新建一个Windows应用程序,项目命名为"象棋"如图5-7设计网络网络中国象棋界面。Visual Studio .Net集成开发环境中的【解决方案资源管理器】窗口中,双击Form1.vb文件,进入Form1.vb文件的编辑界面。

 

5-7 两人对战网络中国象棋界面

2)在Form1.vb文件的开头,添加命名空间:

Imports System.Net

Imports System.Threading

Imports System.Text

Imports System.Net.Sockets

Imports System.Math

  3)如下编写代码:

Public   Class Frmchess
    
Public Const REDPLAYER As Short = 20
    
Public Const BLACKPLAYER As Short = 0
    
'map是布局,r是棋子半径,CurSelect表示当前选中的棋子,LocalPlayer记录自己是红是黑
    Dim Map(910As Integer
    
Dim CurSelect, r As Integer
    
Public LocalPlayer As Integer
    
'IsMyTurn判断是否该自己走了
    Dim IsMyTurn, step_flag As Boolean
    
Public picChess(36As PictureBox   '象棋棋子图片控件
    '网络通信部分
    Private ReadFlag As Boolean = True  '设定侦听标示位,通过它来设定是否侦听端口号
    Private th As Thread    '定义一个线程,在线程接收信息
    Private remote As IPEndPoint  '定义一个远程结点,用以获取远程计算机IP地址和发送的信息
    Private udpclient As UdpClient  '创建一个UDP网络服务
    Private can_go As Boolean = False    '没有联机不能走棋
    Private th_flag As Boolean = False
    
Private Sub read()
        
'侦听本地的端口号
        udpclient = New UdpClient(Convert.ToInt32(txt_port.Text))
        
'remote = System.DBNull
        '设定编码类型
        Dim enc As Encoding
        enc 
= Encoding.Unicode
        
Dim idx As Long, x As Long, y As Integer
        
While ReadFlag = True
            
Dim data As Byte() = udpclient.Receive(remote)   '得到对方发送来的信息
            'Encoding.Unicode.GetBytes(Message)
            Dim strData As String = enc.GetString(data)
            
Dim a(5As String
            a 
= strData.Split("|")
            
Select Case a(0)
                
Case "join"
                    
'获取传送信息到本地端口号的远程计算机IP地址
                    Dim remoteIP As String = remote.Address.ToString()
                    
'显示接收信息以及传送信息的计算机IP地址
                    ToolStripStatusLabel1.Text = remoteIP + "已经有人加入,你是红方请走先棋"
                    can_go 
= True '能走棋
                    LocalPlayer = REDPLAYER
                    
Call Draw_qizi()        '显示棋子
                    SetMyTurn(True)
                    Button1.Enabled 
= False
                
Case "succ"
                    
'获取传送信息到本地端口号的远程计算机IP地址
                    If a(1= "黑方赢了" Then
                        MessageBox.Show(
"黑方赢了,你可以重新开始了!""你输了")
                    
End If
                    
If a(1= "红方赢了" Then
                        MessageBox.Show(
"红方赢了,你可以重新开始了!""你输了")
                    
End If
                    ToolStripStatusLabel1.Text 
= "你可以重新开局!"
                    Button2.Enabled 
= True
                
Case "move"   '对方的走棋信息,move|图片索引号|X|Y
                    If can_go = False Then
                        can_go 
= True           '可以开始走棋了
                        'LocalPlayer = BLACKPLAYER
                        Call Draw_qizi()        '显示棋子
                    End If
                    idx 
= Convert.ToInt16(a(1))
                    x 
= Convert.ToInt16(a(2))
                    y 
= Convert.ToInt16(a(3))
                    ToolStripStatusLabel1.Text 
= x & y
                    Map(GetChessX(idx), GetChessY(idx)) 
= 0
                    MoveChessTo(idx, x, y)
                    
If Map(x, y) <> 0 Then
                        picChess(Map(x, y)).Visible 
= False
                    
End If
                    Map(x, y) 
= idx
                    ListBox1.Items.Add(
"对方" + picChess(idx).Tag.ToString() + "走到x" + x.ToString + "y" + y.ToString)
                    MoveChess(idx, x, y)
                    
'Call DrawBoard()
                    SetMyTurn(True)
                
Case "exit"
                    MessageBox.Show(
"对方退出了,游戏结束!""提示")
                    ToolStripStatusLabel1.Text 
= "对方退出了,游戏结束!"
                    
Exit While
            
End Select
        
End While
        th.Abort()
    
End Sub

    
Private Sub send(ByVal info As String)      '创建UDP网络服务
        Dim SendUdp As UdpClient = New UdpClient
        
Dim remoteIP As IPAddress
        
'判断IP地址的正确性()
        Try
            remoteIP 
= IPAddress.Parse(txt_IP.Text)
        
Catch
            MessageBox.Show(
"请输入正确的IP地址!""错误")
            
Exit Sub
        
End Try
        
Dim remoteep As IPEndPoint = New IPEndPoint(remoteIP, Convert.ToInt32(txt_remoteport.Text))
        
Dim buffer() As Byte       
        
Dim enc As Encoding
        enc 
= Encoding.Unicode '设定编码类型
        Dim str As String = info
        buffer 
= enc.GetBytes(str.ToCharArray())
        
'传送信息到指定计算机的txt_remoteport端口号
        SendUdp.Send(buffer, buffer.Length, remoteep)
        
'关闭UDP网络服务()
        SendUdp.Close()
    
End Sub

    
'设置是否该自己走
    Private Sub SetMyTurn(ByVal bolIsMyTurn As Boolean)
        IsMyTurn 
= bolIsMyTurn
        
If bolIsMyTurn Then
            ToolStripStatusLabel1.Text 
= "请您开始走棋"
        
Else
            ToolStripStatusLabel1.Text 
= "对方正在思考..."
        
End If
    
End Sub

    
Public Function GetGraphicsObject(ByRef pic As PictureBox) As Graphics
        
Dim g As System.Drawing.Graphics
        
Dim bmp As Bitmap = New Bitmap(pic.Width, pic.Height)
        pic.Image 
= bmp
        g 
= Graphics.FromImage(bmp)
        
Return g
    
End Function

    
'绘制棋盘。如果愿意,可以用漂亮的棋盘图片代替
    Private Sub DrawBoard()
        
Dim i As Integer
        
'获取将用于绘图的图形对象的引用创建图形图像。 
        'Dim g As Graphics = picBoard.CreateGraphics
        '获取将用于绘图的位图图形对象的,使用这个GetGraphicsObject函数
        Dim g As Graphics = GetGraphicsObject(picBoard)
        
Dim myPen As New Pen(Color.Red)
        myPen.Width 
= 1
        
'r = picBoard.ClientRectangle.Width / 18
        r = 18
        picBoard.Height 
= r * 20
        
For i = 0 To 8 '竖线
            If i = 0 Or i = 8 Then
                myPen.Width 
= 2
            
Else
                myPen.Width 
= 1
            
End If
            g.DrawLine(myPen, r 
+ i * 2 * r, r, r + i * 2 * r, r * 2 * 10 - r + 1)
        
Next
        
For i = 0 To 9 '横线
            If i = 0 Or i = 9 Then
                myPen.Width 
= 2
            
Else
                myPen.Width 
= 1
            
End If
            g.DrawLine(myPen, r, r 
+ i * 2 * r, r * 2 * 9 - r, r + i * 2 * r)
        
Next
        
Dim rectangle As New System.Drawing.Rectangle(r + 1, r + r * 8 + 1, r * 9 * 2 - 2 * r - 22 * r - 2)
        
Dim brush1 As New System.Drawing.SolidBrush(Color.Brown)
        g.DrawEllipse(System.Drawing.Pens.Black, rectangle)
        g.DrawRectangle(System.Drawing.Pens.Blue, rectangle)
        g.FillRectangle(brush1, rectangle)
        
Dim font1 As Font = New System.Drawing.Font("Arial"20)
        
Dim brush2 As New System.Drawing.SolidBrush(Color.Yellow)
        g.DrawString(
"   汉界             楚河", font1, brush2, (r + 1), (r + r * 8 + 1))
        
'g.DrawLine(myPen, r + 1, r + r * 8 + 1, r * 9 * 2 - r - 1, r + r * 8 + r * 2 - 1)
        '画九宫斜线
        g.DrawLine(myPen, r + r * 6 + 1, r + 1, r + r * 6 + r * 4 - 1, r + r * 4 - 1)
        g.DrawLine(myPen, r 
+ r * 6 + 1, r + r * 4 - 1, r + r * 6 + r * 4 - 1, r + 1)
        g.DrawLine(myPen, r 
+ r * 6 + 1, r * 14 + r + 1, r + r * 6 + r * 4 - 1, r * 14 + r + r * 4 - 1)
        g.DrawLine(myPen, r 
+ r * 6 + 1, r * 14 + r + r * 4 - 1, r + r * 6 + r * 4 - 1, r * 14 + r + 1)
    
End Sub

    
Private Sub LoadChess()
        
Dim str As String
        
Dim path As String
        path 
= System.Windows.Forms.Application.StartupPath ' bin路径
        Dim i As Integer
        
For i = 1 To 36  '黑方对应的是 1至16,红方对应的是21至36
            picChess(i) = New PictureBox
            
Me.Controls.Add(picChess(i))
            picChess(i).SetBounds(
00, (r - 1* 2, (r - 1* 2)
            picChess(i).BackColor 
= System.Drawing.SystemColors.GrayText
            picChess(i).Name 
= "MyPic" & i.ToString
            
'picChess(i).SizeMode = PictureBoxSizeMode.AutoSize
            picChess(i).Width = 35
            picChess(i).Height 
= 35
            
str = path & "..imageq" & i.ToString & ".jpg"
            
If i < 13 And i > 0 Then picChess(i).Image = Image.FromFile(str)
            
If i < 33 And i > 20 Then picChess(i).Image = Image.FromFile(str)
            picChess(i).Text 
= i.ToString
            picChess(i).Visible 
= False
            picChess(i).BringToFront()
            
'AddHandler CType(picChess(i), PictureBox).Click, AddressOf pic_click
            AddHandler picChess(i).Click, AddressOf pic_click
        
Next
    
End Sub

    
Private Sub Draw_qizi() '显示棋盘上的棋子
        Dim str As String
        
Dim path As String
        path 
= System.Windows.Forms.Application.StartupPath ' bin路径
        Dim i As Integer
        
'默认红方棋子在下方,黑方棋子在上方
        picChess(1).Tag = "" : picChess(21).Tag = ""
        MoveChess(
151) : MoveChess(21510)
        picChess(
2).Tag = "" : picChess(22).Tag = ""
        MoveChess(
241) : MoveChess(22410)
        picChess(
3).Tag = "" : picChess(23).Tag = ""
        MoveChess(
361) : MoveChess(23610)
        picChess(
4).Tag = "" : picChess(24).Tag = ""
        MoveChess(
431) : MoveChess(24310)
        picChess(
5).Tag = "" : picChess(25).Tag = ""
        MoveChess(
571) : MoveChess(25710)
        picChess(
6).Tag = "" : picChess(26).Tag = ""
        MoveChess(
621) : MoveChess(26210)
        picChess(
7).Tag = "" : picChess(27).Tag = ""
        MoveChess(
781) : MoveChess(27810)
        picChess(
8).Tag = "" : picChess(28).Tag = ""
        MoveChess(
811) : MoveChess(28110)
        picChess(
9).Tag = "" : picChess(29).Tag = ""
        MoveChess(
991) : MoveChess(29910)
        picChess(
10).Tag = "" : picChess(30).Tag = ""
        MoveChess(
1023) : MoveChess(3028)
        picChess(
11).Tag = "" : picChess(31).Tag = ""
        MoveChess(
1183) : MoveChess(3188)
        
For i = 12 To 16
            picChess(i).Tag 
= ""
            MoveChess(i, (i 
- 12* 2 + 14)
            
str = path & "..imageq12.jpg"
            picChess(i).Image 
= Image.FromFile(str)
            picChess(
20 + i).Tag = ""
            MoveChess(
20 + i, (i - 12* 2 + 17)
            
str = path & "..imageq32.jpg"
            picChess(
20 + i).Image = Image.FromFile(str)
        
Next
        
For i = 1 To 16
            picChess(i).Visible 
= True
            picChess(
20 + i).Visible = True
        
Next
        
Dim j, c As Integer
        
'当游戏者是黑方BLACKPLAYER时,需要将棋子对调一下
        If LocalPlayer = BLACKPLAYER Then
            
For i = 1 To 9
                
For j = 1 To 5
                    
If Map(i, j) <> 0 Then
                        c 
= Map(i, 11 - j)
                        MoveChess(Map(i, j), i, 
11 - j)
                        MoveChess(c, i, j)
                    
End If
                
Next
            
Next
            SetMyTurn(
False)
        
End If
    
End Sub

    
Private Function GetPointX(ByVal x As IntegerAs Integer
        
'屏幕像素坐标转换成在棋盘中坐标值
        GetPointX = (x)  (2 * r) + 1
    
End Function

    
Private Function GetPointY(ByVal y As IntegerAs Integer
        GetPointY 
= (y)  (2 * r) + 1
    
End Function

    
Private Function GetChessX(ByVal idx As IntegerAs Integer
        
'棋子在棋盘中坐标值
        GetChessX = (picChess(idx).Left)  (2 * r) + 1
    
End Function

    
Private Function GetChessY(ByVal idx As IntegerAs Integer
        GetChessY 
= (picChess(idx).Top)  (2 * r) + 1
    
End Function

    
Private Function IsMyChess(ByVal idx As IntegerAs Boolean
        
If 0 < idx - LocalPlayer And idx - LocalPlayer < 20 Then
            IsMyChess 
= True
        
Else
            IsMyChess 
= False
        
End If
    
End Function

    
Private Sub pic_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
'这里处理公共事件 
        If IsMyTurn = False Then Exit Sub
        
Dim x, y, idx 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 
= "错误!,单击成对方棋子了!"
            
Else
                CurSelect 
= CInt(CType(sender, PictureBox).Text)
                picChess(CurSelect).BackColor 
= System.Drawing.Color.Blue
                step_flag 
= True
            
End If
        
Else '第2次棋子
            '如果是自己的棋子,则换上次选择的棋子
            If IsMyChess(Map(x, y)) Then
                
'取消上次选择的棋子,颜色恢复
                picChess(CurSelect).BackColor = System.Drawing.SystemColors.GrayText
                CurSelect 
= CInt(CType(sender, PictureBox).Text)
                
'设置选择的棋子颜色
                picChess(CurSelect).BackColor = System.Drawing.Color.Blue
                step_flag 
= True
                
Exit Sub
            
End If
            
If IsAbleToPut(CurSelect, x, y) Then '可以吃子
                '在map取掉原CurSelect棋子
                Map(GetChessX(CurSelect), GetChessY(CurSelect)) = 0
                idx 
= CInt(CType(sender, PictureBox).Text)
                picChess(idx).Visible 
= False
                MoveChess(CurSelect, x, y)
                Map(x, y) 
= CurSelect
                
If idx = 1 Then
                    ToolStripStatusLabel1.Text 
= "红方赢了"
                    MessageBox.Show(
"红方赢了""提示")
                    send(
"move" + "|" + Format(CurSelect) + "|" + Format(x) + "|" + Format(11 - y))
                    send(
"succ" + "|" + "红方赢了")
                    Button2.Enabled 
= True    '可以重新开始
                    Exit Sub
                
End If
                
If idx = 21 Then
                    ToolStripStatusLabel1.Text 
= "黑方赢了"
                    MessageBox.Show(
"黑方赢了""提示")
                    send(
"move" + "|" + Format(CurSelect) + "|" + Format(x) + "|" + Format(11 - y))
                    send(
"succ" + "|" + "黑方赢了")
                    Button2.Enabled 
= True   '可以重新开始
                    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 '不能走棋
                picChess(CurSelect).BackColor = System.Drawing.SystemColors.GrayText
                Label1.Text 
= "不能走棋"
                CurSelect 
= 0
            
End If
            step_flag 
= False
        
End If
    
End Sub

    
Private Sub picBoard_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles picBoard.MouseDown
        
'没有吃子的走棋
        Dim x, y As Integer
        
If IsMyTurn = False Then Exit Sub
        
If step_flag = True Then
            
'获取鼠标位置
            x = GetPointX(e.X)
            y 
= GetPointY(e.Y)
            
If IsAbleToPut(CurSelect, x, y) Then
                
'在map取掉原CurSelect棋子
                Map(GetChessX(CurSelect), GetChessY(CurSelect)) = 0
                MoveChess(CurSelect, x, y)
                Map(x, y) 
= CurSelect
                ListBox1.Items.Add(
"己方" + picChess(CurSelect).Tag.ToString + "走到x" + x.ToString + "y" + y.ToString)
                send(
"move" + "|" + Format(CurSelect) + "|" + Format(x) + "|" + Format(11 - y))
                CurSelect 
= 0
                
'该对方了
                SetMyTurn(False)
                Label1.Text 
= ""
            
Else '错误走棋
                Label1.Text = "不符合走棋规则"
                picChess(CurSelect).BackColor 
= System.Drawing.SystemColors.GrayText
                CurSelect 
= 0
            
End If
        
End If
        step_flag 
= False
    
End Sub

    
'判断是否能走棋,这代码最难写!
    Private Function IsAbleToPut(ByVal idx As IntegerByVal x As IntegerByVal y As IntegerAs Boolean
        
Dim j, i, c As Integer
        
Dim oldx As Integer, oldy As Integer '原在棋盘坐标
        oldx = GetChessX(idx)
        oldy 
= GetChessY(idx)
        
If IsMyChess(Map(x, y)) Then IsAbleToPut = False : Exit Function
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If (x - oldx) * (y - oldy) <> 0 Then IsAbleToPut = False : Exit Function
            
If Abs(x - oldx) > 1 Or Abs(y - oldy) > 1 Then IsAbleToPut = False : Exit Function
            
If x < 4 Or x > 6 Or (y > 3 And y < 8Then IsAbleToPut = False : Exit Function
            IsAbleToPut 
= True
            
Exit Function
        
End If
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If (x - oldx) * (y - oldy) = 0 Then IsAbleToPut = False : Exit Function
            
If Abs(x - oldx) > 1 Or Abs(y - oldy) > 1 Then IsAbleToPut = False : Exit Function
            
If x < 4 Or x > 6 Or (y > 3 And y < 8Then IsAbleToPut = False : Exit Function
            IsAbleToPut 
= True
            
Exit Function
        
End If
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If (x - oldx) * (y - oldy) = 0 Then IsAbleToPut = False : Exit Function
            
If Abs(x - oldx) <> 2 Or Abs(y - oldy) <> 2 Then IsAbleToPut = False : Exit Function
            
If y < 6 Then IsAbleToPut = False : Exit Function
            
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
            
Exit Function
        
End If
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If Abs(x - oldx) * Abs(y - oldy) <> 2 Then IsAbleToPut = False : Exit Function
            
If x - oldx = 2 Then
                
If Map(x - 1, oldy) <> 0 Then IsAbleToPut = False : Exit Function
            
End If
            
If x - oldx = -2 Then
                
If Map(x + 1, oldy) <> 0 Then IsAbleToPut = False : Exit Function
            
End If
            
If y - oldy = 2 Then
                
If Map(oldx, y - 1<> 0 Then IsAbleToPut = False : Exit Function
            
End If
            
If y - oldy = -2 Then
                
If Map(oldx, y + 1<> 0 Then IsAbleToPut = False : Exit Function
            
End If
            IsAbleToPut 
= True
            
Exit Function
        
End If
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If (x - oldx) * (y - oldy) <> 0 Then IsAbleToPut = False : Exit Function
            
If x <> oldx Then
                
For i = oldx To x Step Abs(x - oldx) / (x - oldx)
                    
If i <> x And i <> oldx Then
                        
If Map(i, y) <> 0 Then IsAbleToPut = False : Exit Function
                    
End If
                
Next
            
End If
            
If y <> oldy Then
                
For j = oldy To y Step Abs(y - oldy) / (y - oldy)
                    
If j <> y And j <> oldy Then
                        
If Map(x, j) <> 0 Then IsAbleToPut = False : Exit Function
                    
End If
                
Next
            
End If
            IsAbleToPut 
= True
            
Exit Function
        
End If
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If (x - oldx) * (y - oldy) <> 0 Then IsAbleToPut = False : Exit Function
            c 
= 0
            
If x <> oldx Then
                
For i = oldx To x Step Abs(x - oldx) / (x - oldx)
                    
If i <> x And i <> oldx Then
                        
If Map(i, y) <> 0 Then c = c + 1 'IsAbleToPut = False: Exit Function
                    End If
                
Next
            
End If
            
If y <> oldy Then
                
For j = oldy To y Step Abs(y - oldy) / (y - oldy)
                    
If j <> y And j <> oldy Then
                        
If Map(x, j) <> 0 Then c = c + 1 'IsAbleToPut = False: Exit Function
                    End If
                
Next
            
End If
            
If c > 1 Then IsAbleToPut = False : Exit Function
            
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) = 0Then IsAbleToPut = False : Exit Function
            IsAbleToPut 
= True
            
Exit Function
        
End If
        
If picChess(idx).Tag = "" Or picChess(idx).Tag = "" Then
            
If (x - oldx) * (y - oldy) <> 0 Then IsAbleToPut = False : Exit Function
            
If Abs(x - oldx) > 1 Or Abs(y - oldy) > 1 Then IsAbleToPut = False : Exit Function
            
If y >= 6 And (x - oldx) <> 0 Then IsAbleToPut = False : Exit Function
            
If y - oldy > 0 Then IsAbleToPut = False : Exit Function
            IsAbleToPut 
= True
            
Exit Function
        
End If
        IsAbleToPut 
= True
    
End Function

    
Private Sub MoveChess(ByVal idx As LongByVal x As LongByVal y As Long)
        picChess(idx).Parent 
= picBoard
        picChess(idx).Left 
= r + (x - 1* 2 * r - r + 1
        picChess(idx).Top 
= r + (y - 1* 2 * r - r + 1
        picChess(idx).BackColor 
= System.Drawing.SystemColors.GrayText
        Map(x, y) 
= idx
    
End Sub

    
'动画效果移动棋子
    Private Sub MoveChessTo(ByVal idx As LongByVal x As LongByVal y As Long)
        x 
= x * 2 * r - 2 * r
        y 
= y * 2 * r - 2 * r
        
Dim step1 As Long
        step1 
= Int(Sqrt((x - picChess(idx).Left) ^ 2 + (y - picChess(idx).Top) ^ 2)) / 50
        
If step1 = 0 Then step1 = 1
        
While Abs(picChess(idx).Left - x) > step1 Or Abs(picChess(idx).Top - y) > step1
            
If x <> picChess(idx).Left Then
                picChess(idx).Left 
= picChess(idx).Left + step1 * Abs(picChess(idx).Left - x) / (x - picChess(idx).Left)
            
End If
            
If y <> picChess(idx).Top Then
                picChess(idx).Top 
= picChess(idx).Top + step1 * Abs(picChess(idx).Top - y) / (y - picChess(idx).Top)
            
End If
            System.Windows.Forms.Application.DoEvents()
        
End While
    
End Sub

    
Private Sub cls_map()
        
Dim i As Integer, j As Integer
        
For i = 1 To 9
            
For j = 1 To 10
                Map(i, j) 
= 0
            
Next
        
Next
    
End Sub

    
Private Sub Frmchess_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        step_flag 
= False
        cls_map()
        LoadChess()
    
End Sub

    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        send(
"join|")
        
' 创建一个线程()
        th = New Thread(AddressOf read)
        th_flag 
= True
        
'启动线程
        th.Start()
        ToolStripStatusLabel1.Text 
= "程序处于等待联机状态!"
        Button1.Enabled 
= False
        
Call DrawBoard()
        LocalPlayer 
= BLACKPLAYER
    
End Sub

    
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
'重新开始
        cls_map()
        
If LocalPlayer = REDPLAYER Then '游戏者角色改变
            LocalPlayer = BLACKPLAYER
        
Else
            LocalPlayer 
= REDPLAYER
            SetMyTurn(
True)
        
End If
        
Call Draw_qizi()
    
End Sub

    
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        send(
"exit|")
        Application.Exit()
    
End Sub

End Class

注意:由于窗体关闭时,线程没有被结束,所以需要重写窗体Dispose事件。
    
Protected   Overrides   Sub Dispose(ByVal disposing As Boolean)
        
Try
            ReadFlag 
= False
            
If Not IsDBNull(udpclient) Then udpclient.Close()
            
If Not IsDBNull(th) Then th.Abort()
            th 
= Nothing
        
Catch
        
End Try
        
If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        
End If
        
MyBase.Dispose(disposing)
    
End Sub

如果你只有一台电脑,可以把这个项目编译成EXE文件,并且运行两个实例,可以将地址填写为:
127.0 . 0.1 ,这样一个作为红方,另一个作为黑方,便可以和自己对弈了。运行效果如图5 - 8所示。
 
图5
- 8   两人对战象棋运行界面

 

具题解释请见我即将出版的VB2005.net应用书!!

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值