ライツアウトのプログラムがうまく作動しません.
visual studio2019,windowsフォームアプリケーションにて作業しています。
ライツアウトというゲームは画像のような5x5でできている赤と青の色を一色にすればクリアというものです。今回はすべて青になった場合をクリアとします。
関数number(x)はクリックしたボタンとその四方が赤であれば青に、青であれば赤にするよう作りました。(ボタン番号は左から123…となります。)
以下プログラムを書きましたが,リセットボタン以外のボタンを押しても色がどこも変わりません。修正点ありましたらよろしくお願いします。
Private Sub Button26_Click(sender As System.Object, e As System.EventArgs) Handles Button26.Click
Dim ary As Integer() = New Integer() {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 'Fisher-Yatesアルゴリズムでシャッフルする Dim rng As New System.Random() Dim n As Integer = ary.Length While n > 1 n -= 1 Dim k As Integer = rng.Next(n + 1) Dim tmp As Integer = ary(k) ary(k) = ary(n) ary(n) = tmp End While light(Button1, ary(0)) light(Button2, ary(1)) light(Button3, ary(2)) light(Button4, ary(3)) 省略 light(Button24, ary(23)) light(Button25, ary(24)) End Sub
Public Sub light(o As Object, num As Integer) If num = 1 Then o.backcolor = Color.Red Else o.backcolor = Color.Cyan End If End Sub Dim mybutton(25) As Object Private Sub number(x As Integer) mybutton(1) = Button1.BackColor mybutton(2) = Button2.BackColor mybutton(3) = Button3.BackColor mybutton(4) = Button4.BackColor 省略 mybutton(22) = Button22.BackColor mybutton(23) = Button23.BackColor mybutton(24) = Button24.BackColor mybutton(25) = Button25.BackColor If (x - 1) > 0 And x <> (1 And 6 And 11 And 16 And 21) Then If mybutton(x - 1) = Color.Red Then mybutton(x - 1) = Color.Cyan ElseIf mybutton(x - 1) = Color.Cyan Then mybutton(x - 1) = Color.Red End If End If If (x + 1) < 26 And x <> (5 And 10 And 15 And 20 And 25) Then If mybutton(x + 1) = Color.Red Then mybutton(x + 1) = Color.Cyan ElseIf mybutton(x + 1) = Color.Cyan Then mybutton(x + 1) = Color.Red End If End If If (x - 5) < 26 And x - 5 > 0 Then If mybutton(x - 5) = Color.Red Then mybutton(x - 5) = Color.Cyan ElseIf mybutton(x - 5) = Color.Cyan Then mybutton(x - 5) = Color.Red End If End If If (x + 5) < 26 And x <> (21 And 22 And 23 And 24 And 25) Then If mybutton(x + 5) = Color.Red Then mybutton(x + 5) = Color.Cyan ElseIf mybutton(x + 5) = Color.Cyan Then mybutton(x + 5) = Color.Red End If End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim x As Integer=1 number(x) End Sub 以降button25まで End Class
あなたの回答
tips
プレビュー