扫雷游戏的实现

解决方案 »

  1.   

    一个N x N的数组,数组元素为一个结构,结构里保存着是否有雷,当前状态等信息.
    然后程序初始时随机设置数组元素的有雷属性.鼠标点击后设置数组元素当前状态属性,再判断周遭8个区域内的数组元素属性,然后计算出应该显示的雷数......
      

  2.   

    羊圈里有100只小羊羔依照编号排成一排。小仙妹想把它们一只只抓出来。但被抓的小羊羔不听话向后躲,因此小仙妹抓出的不一定是哪只小羊羔。被捉出一只小羊羔,羊圈里就少了一只。下面就是模拟这个过程的“小仙妹张牙舞爪捉羊羔算法”,简称“捉羊羔算法”。Private priFold() As Long '羊圈Private Sub Command1_Click()
      If CBool(SheepCount(priFold())) Then '如果羊圈里还有羊的话
          Text2.Text = SheepNabByFold(priFold()) '羊圈存栏数量
          Text1.Text = SheepCount(priFold()) '被抓住的小羊羔编号
          For tIndex = 0 To SheepCount(priFold()) - 1 '显示羊圈里的小羊。
            Text1.Text = Text1.Text & " " & priFold(tIndex)
          Next
        Else
          Text1.Text = "没有羊了!你还乱抓什么?"
      End If
    End SubPrivate Sub Form_Load()
      Randomize Timer
      ReDim priFold(100)
      For tIndex = 0 To 100
        priFold(tIndex) = tIndex
      Next
    End SubFunction SheepCount(ByRef pFold() As Long) As Long
      Dim tOutCount As Long
      
      Dim tSheepIndex_Start As Long
      Dim tSheepIndex_End As Long
      Dim tSheepIndex_Rnd As Long
      
      Err.Clear
      On Error Resume Next
      
      tSheepIndex_End = UBound(pFold): tSheepIndex_Start = LBound(pFold)
      If Not CBool(Err.Number) Then
        tOutCount = Abs(tSheepIndex_End - tSheepIndex_Start) + 1
      End If
      
      SheepCount = tOutCount
    End FunctionFunction SheepNabByFold(ByRef pFold() As Long) As Long
      Dim tOutSheep As Long
      Dim tSheepIndex As Long
      Dim tSheepCount As Long
      Dim tSheepIndex_Start As Long
      Dim tSheepIndex_End As Long
      Dim tSheepIndex_Rnd As Long
      Dim tFoldSpace() As Long
      
      tSheepIndex_End = UBound(pFold()): tSheepIndex_Start = LBound(pFold())
      tSheepCount = SheepCount(pFold()) '计算当前数组编号。
      tSheepIndex_Rnd = Int(Rnd * tSheepCount) + tSheepIndex_Start
      '取合法随机编号。  SheepSwap pFold(tSheepIndex_End), pFold(tSheepIndex_Rnd)
      '将末尾的元素与第随机个元素交换(注意,随机元素必须在目前存在的编号范围内)。
      
      tOutSheep = pFold(tSheepIndex_End)
      
      If tSheepCount < 2 Then
          pFold() = tFoldSpace()
        Else
          ReDim Preserve pFold(tSheepIndex_End - 1) '缩小数组,保留数值。
      End If
      
      SheepNabByFold = tOutSheep
    End FunctionSub SheepSwap(ByRef pSheepA As Long, ByRef pSheepB As Long)
      Dim tSheep As Long
      tSheep = pSheepA: pSheepA = pSheepB: pSheepB = tSheep
    End Sub
      

  3.   

    不好意思!我回复错了!非常抱歉!计算X,Y点雷数的算法:<如果当前点是雷,则爆炸……>For XIndex=X-1 To X+1
      For YIndex=Y-1 To Y+1
        MCount=MCount+(有雷判断(X,Y) And 1)
      Next
    Nexy