この記事は 『Excelでリバーシを作ろう!! マクロ、VBAを1から学ぶ』 のサンプルです。
前:【三目並べ:6. 勝敗を判定する】
https://qiita.com/sano192/items/af4de0401046f18ce50e
Excelのマクロ(VBA)で「三目並べ」「マインスイーパー」「リバーシ」を作る解説本です!
プログラミングが全くわからない人でも大丈夫! 丁寧な解説と図でしっかり理解しながら楽しくプログラミングを学ぶ事ができます!
値段:300円(Kindle Unlimited対象)
【kindle】
【booth(pdf】
これでゲームスタートから終了までの処理が書けました。
最後にプログラムが問題なく動作するか、テストを行いましょう。
開始~終了まで色々なパターンで何回か遊んでみましょう。
・黒が勝つ場合
・白が勝つ場合
・引き分けになる場合
実はこのプログラムには重大な欠陥があります。
「Start」ボタンを押した時点でセル(B2)~セル(D4)のどこかを選んでいると初手でそのマスに石を置けないのです。
原因はWorksheet_SelectionChangeの仕様にあります。
この処理は「選択されているセルが変更されたら」スタートします。最初に盤面のどこかを選んでいると、そこから選ぶセルが変更されない限り処理が始まらないため、そのマスには石が置けないのです。
解決策として、
「「Start」ボタンを押した時点で盤面外を選択状態にさせる」
という方法を取りましょう。
Sub GameStart()~End Subの中に「Cells(1, 1).Select」という一文を加えてください。
Range("B2", "D4").ClearContents
Cells(2, 6) = "黒番"
Cells(1, 1).Select
これは「1行1列のセル(=A1)を選択状態にする」という処理です。これがスタートと同時に行われることで必ずセル(A1)を選択した状態からスタートできます。
プログラムを書いたら必ずどこかしらに不具合は生じます。できた! と思っても問題がないか必ずテストを行うようにしてください。
これで「三目並べ」は完成です。
お疲れ様でした。
【コード全文】
'ゲーム開始の処理
Sub GameStart()
Range("B2", "D4").ClearContents
Cells(2, 6) = "黒番"
Cells(1, 1).Select
End Sub
'セルをクリックしたときの処理
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' 変数の宣言
Dim Gyou, Retu, Result
' クリックしたセルの行番号 , 列番号
Gyou = Target.Row
Retu = Target.Column
' クリックしたセルが盤面の範囲内なら
If 2 <= Gyou And Gyou <= 4 And 2 <= Retu And Retu <= 4 Then
' セルが空白なら
If Cells(Gyou, Retu) <> "" Then
' 処理を途中終了
Exit Sub
End If
' 黒番なら
If Cells(2, 6) = "黒番" Then
Cells(Gyou, Retu) = "●"
Cells(2, 6) = "白番"
' 白番なら
ElseIf Cells(2, 6) = "白番" Then
Cells(Gyou, Retu) = "○"
Cells(2, 6) = "黒番"
End If
End If
Result = Judge
If Result = 1 Then
Cells(2, 6) = "黒の勝ち"
ElseIf Result = 2 Then
Cells(2, 6) = "白の勝ち"
ElseIf Result = 3 Then
Cells(2, 6) = "引き分け"
End If
End Sub
'勝敗を判定する関数
Function Judge()
' 黒が3つ並んでいるか
If Cells(2, 2) = "●" And Cells(2, 3) = "●" And Cells(2, 4) = "●" Then
Judge = 1
ElseIf Cells(3, 2) = "●" And Cells(3, 3) = "●" And Cells(3, 4) = "●" Then
Judge = 1
ElseIf Cells(4, 2) = "●" And Cells(4, 3) = "●" And Cells(4, 4) = "●" Then
Judge = 1
ElseIf Cells(2, 2) = "●" And Cells(3, 2) = "●" And Cells(4, 2) = "●" Then
Judge = 1
ElseIf Cells(2, 3) = "●" And Cells(3, 3) = "●" And Cells(4, 3) = "●" Then
Judge = 1
ElseIf Cells(2, 4) = "●" And Cells(3, 4) = "●" And Cells(4, 4) = "●" Then
Judge = 1
ElseIf Cells(2, 2) = "●" And Cells(3, 3) = "●" And Cells(4, 4) = "●" Then
Judge = 1
ElseIf Cells(2, 4) = "●" And Cells(3, 3) = "●" And Cells(4, 2) = "●" Then
Judge = 1
' 白が3つ並んでいるか
ElseIf Cells(2, 2) = "○" And Cells(2, 3) = "○" And Cells(2, 4) = "○" Then
Judge = 2
ElseIf Cells(3, 2) = "○" And Cells(3, 3) = "○" And Cells(3, 4) = "○" Then
Judge = 2
ElseIf Cells(4, 2) = "○" And Cells(4, 3) = "○" And Cells(4, 4) = "○" Then
Judge = 2
ElseIf Cells(2, 2) = "○" And Cells(3, 2) = "○" And Cells(4, 2) = "○" Then
Judge = 2
ElseIf Cells(2, 3) = "○" And Cells(3, 3) = "○" And Cells(4, 3) = "○" Then
Judge = 2
ElseIf Cells(2, 4) = "○" And Cells(3, 4) = "○" And Cells(4, 4) = "○" Then
Judge = 2
ElseIf Cells(2, 2) = "○" And Cells(3, 3) = "○" And Cells(4, 4) = "○" Then
Judge = 2
ElseIf Cells(2, 4) = "○" And Cells(3, 3) = "○" And Cells(4, 2) = "○" Then
Judge = 2
End If
' 黒または白の勝ちの場合
If Judge = 1 Or Judge = 2 Then
' 途中終了
Exit Function
End If
Dim Gyou, Retu
' 空白マスがあるか確認する
For Gyou = 2 To 4
For Retu = 2 To 4
If Cells(Gyou, Retu) = "" Then
Judge = 4
End If
Next Retu
Next Gyou
' 全てのマスが埋まっている場合
If Judge <> 4 Then
Judge = 3
End If
End Function
前:【三目並べ:6. 勝敗を判定する】
https://qiita.com/sano192/items/af4de0401046f18ce50e