Крестики-нолики - Зацикливается программа - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Листинг программы
  1. Public Class Form3
  2. Private intzet As Integer = 0
  3. Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
  4. Button1.Text = ""
  5. Button1.Enabled = True
  6. Button2.Text = ""
  7. Button2.Enabled = True
  8. Button3.Text = ""
  9. Button3.Enabled = True
  10. Button4.Text = ""
  11. Button4.Enabled = True
  12. Button5.Text = ""
  13. Button5.Enabled = True
  14. Button6.Text = ""
  15. Button6.Enabled = True
  16. Button7.Text = ""
  17. Button7.Enabled = True
  18. Button8.Text = ""
  19. Button8.Enabled = True
  20. Button9.Text = ""
  21. Button9.Enabled = True
  22. intzet = 0
  23. End Sub
  24. Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  25. Button1.Text = ""
  26. Button1.Enabled = True
  27. Button2.Text = ""
  28. Button2.Enabled = True
  29. Button3.Text = ""
  30. Button3.Enabled = True
  31. Button4.Text = ""
  32. Button4.Enabled = True
  33. Button5.Text = ""
  34. Button5.Enabled = True
  35. Button6.Text = ""
  36. Button6.Enabled = True
  37. Button7.Text = ""
  38. Button7.Enabled = True
  39. Button8.Text = ""
  40. Button8.Enabled = True
  41. Button9.Text = ""
  42. Button9.Enabled = True
  43. intzet = 0
  44. End Sub
  45. Sub computerplays()
  46. Dim objRandom As New Random
  47. Dim intRandom As Integer
  48.  
  49. intRandom = Integer.Parse(objRandom.Next(1, 9))
  50.  
  51. If intRandom = 1 And Button1.Enabled = True Then
  52. Button1.Text = "0"
  53. Button1.Enabled = False
  54. Exit Sub
  55. Else
  56. intRandom = Integer.Parse(objRandom.Next(2, 9))
  57. End If
  58. If intRandom = 2 And Button2.Enabled = True Then
  59. Button2.Text = "0"
  60. Button2.Enabled = False
  61. Exit Sub
  62. Else
  63. intRandom = Integer.Parse(objRandom.Next(3, 9))
  64. End If
  65. If intRandom = 3 And Button3.Enabled = True Then
  66. Button3.Text = "0"
  67. Button3.Enabled = False
  68. Exit Sub
  69. Else
  70. intRandom = Integer.Parse(objRandom.Next(4, 9))
  71. End If
  72. If intRandom = 4 And Button4.Enabled = True Then
  73. Button4.Text = "0"
  74. Button4.Enabled = False
  75. Exit Sub
  76. Else
  77. intRandom = Integer.Parse(objRandom.Next(5, 9))
  78. End If
  79. If intRandom = 5 And Button5.Enabled = True Then
  80. Button5.Text = "0"
  81. Button5.Enabled = False
  82. Exit Sub
  83. Else
  84. intRandom = Integer.Parse(objRandom.Next(6, 9))
  85. End If
  86. If intRandom = 6 And Button6.Enabled = True Then
  87. Button6.Text = "0"
  88. Button6.Enabled = False
  89. Exit Sub
  90. Else
  91. intRandom = Integer.Parse(objRandom.Next(7, 9))
  92. End If
  93. If intRandom = 7 And Button7.Enabled = True Then
  94. Button7.Text = "0"
  95. Button7.Enabled = False
  96. Exit Sub
  97. Else
  98. intRandom = Integer.Parse(objRandom.Next(8, 9))
  99. End If
  100. If intRandom = 8 And Button8.Enabled = True Then
  101. Button8.Text = "0"
  102. Button8.Enabled = False
  103. Exit Sub
  104. Else
  105. intRandom = Integer.Parse(objRandom.Next(1, 7))
  106. End If
  107. If intRandom = 9 And Button9.Enabled = True Then
  108. Button9.Text = "0"
  109. Button9.Enabled = False
  110. Exit Sub
  111. Else
  112. intRandom = Integer.Parse(objRandom.Next(1, 8))
  113. End If
  114.  
  115. computerplays()
  116. End Sub
  117. 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
  118. sender.text = "X"
  119. sender.enabled = False
  120. intzet = 1
  121. CheckWinner()
  122. computerplays()
  123. intzet = 1
  124. End Sub
  125. Sub CheckWinner()
  126. If intzet >= 5 Then
  127. If MessageBox.Show("Ничья!") = vbOK Then
  128. Me.Close()
  129. Form1.Show()
  130. End If
  131. ElseIf Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" _
  132. Or Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" _
  133. Or Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" _
  134. Or Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" _
  135. Or Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" _
  136. Or Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" _
  137. Or Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" _
  138. Or Button7.Text = "O" And Button5.Text = "O" And Button3.Text = "O" Then
  139. If MessageBox.Show("Компьютер победил") = vbOK Then
  140. Me.Close()
  141. Form1.Show()
  142. End If
  143. ElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" _
  144. Or Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" _
  145. Or Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" _
  146. Or Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" _
  147. Or Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" _
  148. Or Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" _
  149. Or Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" _
  150. Or Button7.Text = "X" And Button5.Text = "X" And Button3.Text = "X" Then
  151. If MessageBox.Show("игрок победил") = vbOK Then
  152. Me.Close()
  153. Form1.Show()
  154. End If
  155. End If
  156. End Sub
  157. End Class
Я могу несколько раз запустить эти крестики нолики и с ней будет всё нормально, но на данной строчке intRandom = Integer.Parse(objRandom.Next(1, 9)) он зацикливается когда остается 1-2 кнопки и он не выдает окно победы компьютера, хотя "0" совпадают по данным координатам которые я ему задал.

Решение задачи: «Крестики-нолики - Зацикливается программа»

textual
Листинг программы
  1. Sub computerplays()
  2.  
  3.     Dim allowedMoves As New List(Of Integer) = {1,2,3,4,5,6,7,8,9}
  4.     Dim buttons() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
  5.     For I = 0 To 8
  6.         If Not buttons(I).Enabled Then allowedMoves.Remove(I+1)
  7.     Next
  8.  
  9.     Dim move As Integer
  10.     If allowedMoves.Count = 0 Then
  11.         ' Не осталось ни одного хода
  12.         Return
  13.     ElseIf allowedMoves.Count = 1 Then
  14.         move = allowedMoves(0)
  15.     Else
  16.         Dim rnd As New Random
  17.         move = allowedMoves(rnd.Next(0, allowedMoves.Count))
  18.     End If
  19.  
  20.     buttons(move-1).Text = "0"
  21.     buttons(move-1).Enabled = False
  22.  
  23. End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.333 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы