用WPF和VB编出来的,界面很简单。就是用一个uniformgrid去添加按钮,然后给按钮写后台代码,代码如下
Class MainWindow
Dim n As Integer = 10
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim btnArray As Button(,) = New Button(n - 1, n - 1) {}
Dim map As Integer(,) = Saolei.Run()
For i As Integer = 0 To n - 1
For j As Integer = 0 To n - 1
Dim btn As Button = New Button
Dim message As message = New message()
message.btnArray = btnArray
message.map = map
message.r = i
message.c = j
btnArray(i, j) = btn
btn.Foreground = Brushes.Gray
btn.Background = Brushes.Gray
btn.Tag = message
area.Children.Add(btn)
AddHandler btn.Click, New RoutedEventHandler(AddressOf Button_Click)
AddHandler btn.MouseRightButtonDown, New MouseButtonEventHandler(AddressOf Button_MouseRightButtonDown)
Next
Next
End Sub
Public Class message
Public btnArray As Button(,)
Public map As Integer(,)
Public r As Integer
Public c As Integer
Public Sub New()
End Sub
End Class
Public Class Saolei
Public Shared Function Run()
Dim map As Integer(,) = GetMap2(10, 10, 15, 3, 5)
ComputeMap(map)
Return map
End Function
''' <summary>
''' 获取扫雷游戏的布雷地图
''' </summary>
''' <param name="rowCount">行的数量</param>
''' <param name="columnCount">列的数量</param>
''' <param name="bombCount">雷的数量</param>
''' <param name="r0">初始行</param>
''' <param name="c0">初始列</param>
''' <returns>返回布雷后的地图,雷的位置的元素值为-1,其他位置的元素值为0</returns>
''' <remarks></remarks>
Private Shared Function GetMap(
ByVal rowCount As Integer, ByVal columnCount As Integer,
ByVal bombCount As Integer,
ByVal r0 As Integer, ByVal c0 As Integer) _
As Integer(,)
Dim ran As Random = New Random
Dim result As Integer(,) = New Integer(rowCount - 1, columnCount - 1) {}
For i As Integer = 1 To bombCount Step 0
Dim r As Integer = ran.Next(rowCount)
Dim c As Integer = ran.Next(columnCount)
If r = r0 AndAlso c = c0 Then
' 初始位置不布雷
Continue For
End If
If result(r, c) <> -1 Then
' 此位置没有雷,则
result(r, c) = -1
i += 1
End If
Next
Return result
End Function
Private Shared Function GetMap2(
ByVal rowCount As Integer, ByVal columnCount As Integer,
ByVal bombCount As Integer,
ByVal r0 As Integer, ByVal c0 As Integer) _
As Integer(,)
Dim ran As Random = New Random
Dim result As Integer(,) = New Integer(rowCount - 1, columnCount - 1) {}
Dim allData As List(Of Integer) = New List(Of Integer)
For i As Integer = 0 To rowCount * columnCount - 1
allData.Add(i)
Next
allData.Remove(r0 * columnCount + c0)
For i As Integer = 1 To bombCount
Dim tempIndex As Integer = ran.Next(allData.Count)
Dim tempValue As Integer = allData(tempIndex)
Dim r As Integer = tempValue \ columnCount
Dim c As Integer = tempValue Mod columnCount
result(r, c) = -1
allData.RemoveAt(tempIndex)
Next
Return result
End Function
''' <summary>
''' 计算扫雷地图
''' </summary>
''' <param name="map"></param>
''' <remarks></remarks>
Private Shared Sub ComputeMap(ByVal map As Integer(,))
For r As Integer = 0 To map.GetLength(0) - 1
For c As Integer = 0 To map.GetLength(1) - 1
If map(r, c) = -1 Then
' 雷的位置不计算
Continue For
End If
Dim beginRow As Integer = Math.Max(0, r - 1)
Dim endRow As Integer = Math.Min(map.GetLength(0) - 1, r + 1)
Dim beginColumn As Integer = Math.Max(0, c - 1)
Dim endColumn As Integer = Math.Min(map.GetLength(1) - 1, c + 1)
Dim count As Integer = 0
For i As Integer = beginRow To endRow
For j As Integer = beginColumn To endColumn
If map(i, j) = -1 Then
count += 1
End If
Next
Next
map(r, c) = count
Next
Next
End Sub
''' <summary>
''' 打印地图
''' </summary>
''' <param name="map"></param>
''' <remarks></remarks>
Private Shared Sub Print(ByVal map As Integer(,))
For r As Integer = 0 To map.GetLength(0) - 1
For c As Integer = 0 To map.GetLength(1) - 1
Dim val As Integer = map(r, c)
Select Case val
Case -1
Console.ForegroundColor = ConsoleColor.Red
Case 0
Console.ForegroundColor = ConsoleColor.Green
Case Else
Console.ForegroundColor = ConsoleColor.Yellow
End Select
If val = -1 Then
Console.Write(" *")
Else
Console.Write(" {0}", val)
End If
Console.ResetColor()
Next
Console.WriteLine()
Next
End Sub
End Class
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim btn As Button = CType(sender, Button)
Dim message As message = btn.Tag
Dim btnArray As Button(,) = message.btnArray
Dim map As Integer(,) = message.map
Dim r As Integer = message.r
Dim c As Integer = message.c
Dim val As Integer = map(r, c)
Dim currentBtn = btnArray(r, c)
Select Case val
Case -1
currentBtn.Foreground = Brushes.Red
currentBtn.Content = "*"
currentBtn.FontSize = 30
MessageBox.Show("you lose")
Environment.Exit(exitCode:=0)
Case 0
currentBtn.Foreground = Brushes.Yellow
lightup(btnArray, map, r, c)
Case Else
currentBtn.Foreground = Brushes.Blue
currentBtn.Content = val.ToString()
End Select
Dim count As Integer = 0
For i As Integer = 0 To n - 1
For j As Integer = 0 To n - 1
If btnArray(i, j).Content <> String.Empty AndAlso btnArray(i, j).Content <> "♥" Then
count += 1
End If
Next
Next
If count = n * n - 15 Then
MessageBox.Show("you are so clever")
End If
End Sub
Private Sub lightup(ByVal btnArray As Button(,), ByVal map As Integer(,), ByVal r As Integer, ByVal c As Integer)
Dim val As Integer = map(r, c)
Dim beginRow As Integer = Math.Max(0, r - 1)
Dim endRow As Integer = Math.Min(map.GetLength(0) - 1, r + 1)
Dim beginColumn As Integer = Math.Max(0, c - 1)
Dim endColumn As Integer = Math.Min(map.GetLength(1) - 1, c + 1)
Dim count As Integer = 0
For i As Integer = beginRow To endRow
For j As Integer = beginColumn To endColumn
If map(i, j) = 0 Then
If btnArray(i, j).Content = String.Empty Then
btnArray(i, j).Content = map(i, j).ToString()
btnArray(i, j).Foreground = Brushes.Yellow
lightup(btnArray, map, i, j)
End If
Else
btnArray(i, j).Content = map(i, j).ToString()
btnArray(i, j).Foreground = Brushes.Yellow
End If
Next
Next
End Sub
Private Sub Button_MouseRightButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
Dim btn As Button = CType(sender, Button)
If btn.Content = String.Empty Then
btn.Content = "♥"
btn.FontSize = 30
btn.Foreground = Brushes.Red
Else
btn.Content = ""
End If
End Sub
End Class