具体开发步骤:
1)新建一个Windows应用程序,项目命名为"象棋"。如图5-7设计网络网络中国象棋界面。在Visual Studio .Net集成开发环境中的【解决方案资源管理器】窗口中,双击Form1.vb文件,进入Form1.vb文件的编辑界面。
2)在Form1.vb文件的开头,添加命名空间:
Imports System.Net
Imports System.Threading
Imports System.Text
Imports System.Net.Sockets
Imports System.Math
3)如下编写代码:
Public Class FrmchessClass Frmchess
Public Const REDPLAYER As Short = 20
Public Const BLACKPLAYER As Short = 0
'map是布局,r是棋子半径,CurSelect表示当前选中的棋子,LocalPlayer记录自己是红是黑
Dim Map(9, 10) As Integer
Dim CurSelect, r As Integer
Public LocalPlayer As Integer
'IsMyTurn判断是否该自己走了
Dim IsMyTurn, step_flag As Boolean
Public picChess(36) As 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()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(5) As 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()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()Sub SetMyTurn(ByVal bolIsMyTurn As Boolean)
IsMyTurn = bolIsMyTurn
If bolIsMyTurn Then
ToolStripStatusLabel1.Text = "请您开始走棋"
Else
ToolStripStatusLabel1.Text = "对方正在思考…"
End If
End Sub
Public Function GetGraphicsObject()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()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 – 2, 2 * 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()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(0, 0, (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()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(1, 5, 1) : MoveChess(21, 5, 10)
picChess(2).Tag = "士" : picChess(22).Tag = "仕"
MoveChess(2, 4, 1) : MoveChess(22, 4, 10)
picChess(3).Tag = "士" : picChess(23).Tag = "仕"
MoveChess(3, 6, 1) : MoveChess(23, 6, 10)
picChess(4).Tag = "象" : picChess(24).Tag = "相"
MoveChess(4, 3, 1) : MoveChess(24, 3, 10)
picChess(5).Tag = "象" : picChess(25).Tag = "相"
MoveChess(5, 7, 1) : MoveChess(25, 7, 10)
picChess(6).Tag = "马" : picChess(26).Tag = "马"
MoveChess(6, 2, 1) : MoveChess(26, 2, 10)
picChess(7).Tag = "马" : picChess(27).Tag = "马"
MoveChess(7, 8, 1) : MoveChess(27, 8, 10)
picChess(8).Tag = "车" : picChess(28).Tag = "车"
MoveChess(8, 1, 1) : MoveChess(28, 1, 10)
picChess(9).Tag = "车" : picChess(29).Tag = "车"
MoveChess(9, 9, 1) : MoveChess(29, 9, 10)
picChess(10).Tag = "炮" : picChess(30).Tag = "炮"
MoveChess(10, 2, 3) : MoveChess(30, 2, 8)
picChess(11).Tag = "炮" : picChess(31).Tag = "炮"
MoveChess(11, 8, 3) : MoveChess(31, 8, 8)
For i = 12 To 16
picChess(i).Tag = "卒"
MoveChess(i, (i – 12) * 2 + 1, 4)
str = path & "..imageq12.jpg"
picChess(i).Image = Image.FromFile(str)
picChess(20 + i).Tag = "兵"
MoveChess(20 + i, (i – 12) * 2 + 1, 7)
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()Function GetPointX(ByVal x As Integer) As Integer
'屏幕像素坐标转换成在棋盘中坐标值
GetPointX = (x) (2 * r) + 1
End Function
Private Function GetPointY()Function GetPointY(ByVal y As Integer) As Integer
GetPointY = (y) (2 * r) + 1
End Function
Private Function GetChessX()Function GetChessX(ByVal idx As Integer) As Integer
'棋子在棋盘中坐标值
GetChessX = (picChess(idx).Left) (2 * r) + 1
End Function
Private Function GetChessY()Function GetChessY(ByVal idx As Integer) As Integer
GetChessY = (picChess(idx).Top) (2 * r) + 1
End Function
Private Function IsMyChess()Function IsMyChess(ByVal idx As Integer) As Boolean
If 0 < idx – LocalPlayer And idx – LocalPlayer < 20 Then
IsMyChess = True
Else
IsMyChess = False
End If
End Function
Private Sub pic_click()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()Sub picBoard_MouseDown(ByVal sender As Object, ByVal 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()Function IsAbleToPut(ByVal idx As Integer, ByVal x As Integer, ByVal y As Integer) As 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 < 8) Then 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 < 8) Then 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) = 0) Then 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()Sub MoveChess(ByVal idx As Long, ByVal x As Long, ByVal 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()Sub MoveChessTo(ByVal idx As Long, ByVal x As Long, ByVal 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()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()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()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()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()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()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 两人对战象棋运行界面
Public Const REDPLAYER As Short = 20
Public Const BLACKPLAYER As Short = 0
'map是布局,r是棋子半径,CurSelect表示当前选中的棋子,LocalPlayer记录自己是红是黑
Dim Map(9, 10) As Integer
Dim CurSelect, r As Integer
Public LocalPlayer As Integer
'IsMyTurn判断是否该自己走了
Dim IsMyTurn, step_flag As Boolean
Public picChess(36) As 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()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(5) As 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()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()Sub SetMyTurn(ByVal bolIsMyTurn As Boolean)
IsMyTurn = bolIsMyTurn
If bolIsMyTurn Then
ToolStripStatusLabel1.Text = "请您开始走棋"
Else
ToolStripStatusLabel1.Text = "对方正在思考…"
End If
End Sub
Public Function GetGraphicsObject()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()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 – 2, 2 * 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()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(0, 0, (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()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(1, 5, 1) : MoveChess(21, 5, 10)
picChess(2).Tag = "士" : picChess(22).Tag = "仕"
MoveChess(2, 4, 1) : MoveChess(22, 4, 10)
picChess(3).Tag = "士" : picChess(23).Tag = "仕"
MoveChess(3, 6, 1) : MoveChess(23, 6, 10)
picChess(4).Tag = "象" : picChess(24).Tag = "相"
MoveChess(4, 3, 1) : MoveChess(24, 3, 10)
picChess(5).Tag = "象" : picChess(25).Tag = "相"
MoveChess(5, 7, 1) : MoveChess(25, 7, 10)
picChess(6).Tag = "马" : picChess(26).Tag = "马"
MoveChess(6, 2, 1) : MoveChess(26, 2, 10)
picChess(7).Tag = "马" : picChess(27).Tag = "马"
MoveChess(7, 8, 1) : MoveChess(27, 8, 10)
picChess(8).Tag = "车" : picChess(28).Tag = "车"
MoveChess(8, 1, 1) : MoveChess(28, 1, 10)
picChess(9).Tag = "车" : picChess(29).Tag = "车"
MoveChess(9, 9, 1) : MoveChess(29, 9, 10)
picChess(10).Tag = "炮" : picChess(30).Tag = "炮"
MoveChess(10, 2, 3) : MoveChess(30, 2, 8)
picChess(11).Tag = "炮" : picChess(31).Tag = "炮"
MoveChess(11, 8, 3) : MoveChess(31, 8, 8)
For i = 12 To 16
picChess(i).Tag = "卒"
MoveChess(i, (i – 12) * 2 + 1, 4)
str = path & "..imageq12.jpg"
picChess(i).Image = Image.FromFile(str)
picChess(20 + i).Tag = "兵"
MoveChess(20 + i, (i – 12) * 2 + 1, 7)
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()Function GetPointX(ByVal x As Integer) As Integer
'屏幕像素坐标转换成在棋盘中坐标值
GetPointX = (x) (2 * r) + 1
End Function
Private Function GetPointY()Function GetPointY(ByVal y As Integer) As Integer
GetPointY = (y) (2 * r) + 1
End Function
Private Function GetChessX()Function GetChessX(ByVal idx As Integer) As Integer
'棋子在棋盘中坐标值
GetChessX = (picChess(idx).Left) (2 * r) + 1
End Function
Private Function GetChessY()Function GetChessY(ByVal idx As Integer) As Integer
GetChessY = (picChess(idx).Top) (2 * r) + 1
End Function
Private Function IsMyChess()Function IsMyChess(ByVal idx As Integer) As Boolean
If 0 < idx – LocalPlayer And idx – LocalPlayer < 20 Then
IsMyChess = True
Else
IsMyChess = False
End If
End Function
Private Sub pic_click()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()Sub picBoard_MouseDown(ByVal sender As Object, ByVal 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()Function IsAbleToPut(ByVal idx As Integer, ByVal x As Integer, ByVal y As Integer) As 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 < 8) Then 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 < 8) Then 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) = 0) Then 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()Sub MoveChess(ByVal idx As Long, ByVal x As Long, ByVal 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()Sub MoveChessTo(ByVal idx As Long, ByVal x As Long, ByVal 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()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()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()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()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()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()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 两人对战象棋运行界面