ExcelVBAでマインスイーパーを作ってみた

ここにコードをコピペしてちゃんと使えるかどうかのテストです。

物自体は1年前くらいにつくったんじゃないかなー

 

説明としてはExcelマインスイーパーができる、ただそれだけのものです。

 

こんな感じで

f:id:hariusagi224:20181020234404p:plain

 

こんなもん欲しがる人はいないと思いますが、一応Excel開いて開発→VisualBasic→左の枠を右クリック→挿入→標準モジュールで、開いたところに付録のコードをぶち込み、ボタンを4個用意してそれぞれにマクロを登録し、セルを方眼紙状態にすれば遊べる…はず

 

まあ、こんな辺鄙なところに来る人の目的はどちらかというと「どうやって」作るかだと思いますが、如何せん昔に作ったものなので忘れてしまった…

解読は各自でお願いします

 

しかし、そのままコード張り付けるとインデントが無くなるのか…

ちょっと調べてみないとな…

 

以下付録

Sub せつめい()
Dim rc1 As VbMsgBoxResult
rc1 = MsgBox("・セルを選択した後「せんたく」ボタンでマスを開けます。", 1 + 32, "ゲームの説明")
If rc1 = vbOK Then
Dim rc2 As VbMsgBoxResult
rc2 = MsgBox("爆弾が埋まっていそうな場所は「ふらっぐ」で目印をつけられます。", 1 + 64, "ゲームの説明")
If rc2 = vbOK Then
Dim rc3 As VbMsgBoxResult
rc3 = MsgBox("全ての爆弾に「ふらっぐ」を立てたらゲームクリアです。", 1 + 48, "ゲームの説明")
If rc3 = vbOK Then
MsgBox "※数式バーはしまってプレイしてくださいm(__)m", 0 + 16, "ゲームの説明"
End If
End If
End If
End Sub
Sub せんたく()
Dim a As Integer
Dim b As Integer
a = Selection.Row
b = Selection.Column
If Cells(a, b).Interior.Color = RGB(0, 0, 0) Then
Cells(a, b).Interior.Color = RGB(255, 255, 255)
If Cells(a, b).Value = "*" Then
MsgBox "げーむおーばー", 0 + 16, "げーむおーばー"
End If
If Cells(a, b).Value = 0 Then
For i = a - 1 To a + 1
For j = b - 1 To b + 1
Cells(i, j).Interior.Color = RGB(255, 255, 255)
Next
Next
End If
End If
End Sub
Sub ふらっぐ()
Dim s As String
s = Selection.Address
If Range(s).Interior.Color = RGB(0, 0, 0) Then
Range(s).Interior.Color = RGB(255, 0, 0)
Range(s).Font.Color = RGB(255, 0, 0)
ElseIf Range(s).Interior.Color = RGB(255, 0, 0) Then
Range(s).Interior.Color = RGB(0, 0, 0)
Range(s).Font.Color = RGB(0, 0, 0)
End If
Dim clear As Boolean
Dim h As Integer
Dim v As Integer
h = Cells(4, 2).Value
v = Cells(5, 2).Value
clear = True
For i = 4 To v + 3
For j = 4 To h + 3
If Cells(i, j).Interior.Color = RGB(0, 0, 0) And Cells(i, j).Value = "*" Then
clear = False
ElseIf Cells(i, j).Interior.Color = RGB(255, 0, 0) And Cells(i, j).Value <> "*" Then
clear = False
End If
Next
Next
If clear = True Then
MsgBox "げーむくりあ", 0 + 64, "おめでとう"
End If
End Sub
Sub せっと()
Range(Cells(4, 4), Cells(100, 100)).Value = Null
Range(Cells(4, 4), Cells(100, 100)).Interior.Color = RGB(255, 255, 255)
Range(Cells(4, 4), Cells(100, 100)).Font.Color = RGB(255, 255, 255)
Dim bomb As Integer
Dim v As Integer
Dim h As Integer
bomb = Cells(3, 2).Value
v = Cells(4, 2).Value
h = Cells(5, 2).Value
Debug.Print bomb
Debug.Print v
Debug.Print h
For i = 0 To bomb - 1
a = Int(Rnd * v + 4)
b = Int(Rnd * h + 4)
Cells(a, b).Value = "*"
Next
For j = 4 To v + 3
For k = 4 To h + 3
If Cells(j, k) <> "*" Then
Cells(j, k) = WorksheetFunction.CountIf(Range(Cells(j - 1, k - 1), Cells(j + 1, k + 1)), "*")
End If
Next
Next
Range(Cells(4, 4), Cells(v + 3, h + 3)).Interior.Color = RGB(0, 0, 0)
Range(Cells(4, 4), Cells(v + 3, h + 3)).Font.Color = RGB(0, 0, 0)
End Sub