Крестики-нолики - Зацикливается программа - Visual Basic .NET
Формулировка задачи:
Листинг программы
- Public Class Form3
- Private intzet As Integer = 0
- Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
- Button1.Text = ""
- Button1.Enabled = True
- Button2.Text = ""
- Button2.Enabled = True
- Button3.Text = ""
- Button3.Enabled = True
- Button4.Text = ""
- Button4.Enabled = True
- Button5.Text = ""
- Button5.Enabled = True
- Button6.Text = ""
- Button6.Enabled = True
- Button7.Text = ""
- Button7.Enabled = True
- Button8.Text = ""
- Button8.Enabled = True
- Button9.Text = ""
- Button9.Enabled = True
- intzet = 0
- End Sub
- Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Button1.Text = ""
- Button1.Enabled = True
- Button2.Text = ""
- Button2.Enabled = True
- Button3.Text = ""
- Button3.Enabled = True
- Button4.Text = ""
- Button4.Enabled = True
- Button5.Text = ""
- Button5.Enabled = True
- Button6.Text = ""
- Button6.Enabled = True
- Button7.Text = ""
- Button7.Enabled = True
- Button8.Text = ""
- Button8.Enabled = True
- Button9.Text = ""
- Button9.Enabled = True
- intzet = 0
- End Sub
- Sub computerplays()
- Dim objRandom As New Random
- Dim intRandom As Integer
- intRandom = Integer.Parse(objRandom.Next(1, 9))
- If intRandom = 1 And Button1.Enabled = True Then
- Button1.Text = "0"
- Button1.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(2, 9))
- End If
- If intRandom = 2 And Button2.Enabled = True Then
- Button2.Text = "0"
- Button2.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(3, 9))
- End If
- If intRandom = 3 And Button3.Enabled = True Then
- Button3.Text = "0"
- Button3.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(4, 9))
- End If
- If intRandom = 4 And Button4.Enabled = True Then
- Button4.Text = "0"
- Button4.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(5, 9))
- End If
- If intRandom = 5 And Button5.Enabled = True Then
- Button5.Text = "0"
- Button5.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(6, 9))
- End If
- If intRandom = 6 And Button6.Enabled = True Then
- Button6.Text = "0"
- Button6.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(7, 9))
- End If
- If intRandom = 7 And Button7.Enabled = True Then
- Button7.Text = "0"
- Button7.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(8, 9))
- End If
- If intRandom = 8 And Button8.Enabled = True Then
- Button8.Text = "0"
- Button8.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(1, 7))
- End If
- If intRandom = 9 And Button9.Enabled = True Then
- Button9.Text = "0"
- Button9.Enabled = False
- Exit Sub
- Else
- intRandom = Integer.Parse(objRandom.Next(1, 8))
- End If
- computerplays()
- End Sub
- Private Sub Youplays(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click
- sender.text = "X"
- sender.enabled = False
- intzet = 1
- CheckWinner()
- computerplays()
- intzet = 1
- End Sub
- Sub CheckWinner()
- If intzet >= 5 Then
- If MessageBox.Show("Ничья!") = vbOK Then
- Me.Close()
- Form1.Show()
- End If
- ElseIf Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" _
- Or Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" _
- Or Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" _
- Or Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" _
- Or Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" _
- Or Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" _
- Or Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" _
- Or Button7.Text = "O" And Button5.Text = "O" And Button3.Text = "O" Then
- If MessageBox.Show("Компьютер победил") = vbOK Then
- Me.Close()
- Form1.Show()
- End If
- ElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" _
- Or Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" _
- Or Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" _
- Or Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" _
- Or Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" _
- Or Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" _
- Or Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" _
- Or Button7.Text = "X" And Button5.Text = "X" And Button3.Text = "X" Then
- If MessageBox.Show("игрок победил") = vbOK Then
- Me.Close()
- Form1.Show()
- End If
- End If
- End Sub
- End Class
Я могу несколько раз запустить эти крестики нолики и с ней будет всё нормально, но на данной строчке intRandom = Integer.Parse(objRandom.Next(1, 9)) он зацикливается когда остается 1-2 кнопки и он не выдает окно победы компьютера, хотя "0" совпадают по данным координатам которые я ему задал.
Решение задачи: «Крестики-нолики - Зацикливается программа»
textual
Листинг программы
- Sub computerplays()
- Dim allowedMoves As New List(Of Integer) = {1,2,3,4,5,6,7,8,9}
- Dim buttons() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
- For I = 0 To 8
- If Not buttons(I).Enabled Then allowedMoves.Remove(I+1)
- Next
- Dim move As Integer
- If allowedMoves.Count = 0 Then
- ' Не осталось ни одного хода
- Return
- ElseIf allowedMoves.Count = 1 Then
- move = allowedMoves(0)
- Else
- Dim rnd As New Random
- move = allowedMoves(rnd.Next(0, allowedMoves.Count))
- End If
- buttons(move-1).Text = "0"
- buttons(move-1).Enabled = False
- End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д