shahharis Ответов: 1

Хотите оптимизировать следующий код и хотите отображать имена игроков из одной формы в другую.


Ниже приводится основная форма:
И если это возможно, мне нужна небольшая помощь в том, как я могу держать компьютер в качестве своего противника!
Public Class tictactoe
 Dim tt As Boolean = True
 Public p1 As String
 Public p2 As String
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
 Button1.Click
 If tt = True Then
     Button1.Text = "X"
     Text = "Player" + p2 + "'s turn"
     tt = False
     Button1.Enabled = False
 Else
     Button1.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button1.Enabled = False
 End If
 checkWinner()
  End Sub

  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
  Button2.Click
  If tt = True Then
     Button2.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button2.Enabled = False
 Else
     Button2.Text = "0"
     Text = "Player 1's turn"
     Button2.Enabled = False
     tt = True
 End If
 checkWinner()
 End Sub

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles
 Button3.Click
 If tt = True Then
     Button3.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button3.Enabled = False
 Else
     Button3.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button3.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub Button6_Click(sender As Object, e As EventArgs) Handles
 Button6.Click
 If tt = True Then
     Button6.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button6.Enabled = False
 Else
     Button6.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button6.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub Button5_Click(sender As Object, e As EventArgs) Handles
 Button5.Click
 If tt = True Then
     Button5.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button5.Enabled = False
 Else
     Button5.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button5.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles
 Button4.Click
 If tt = True Then
     Button4.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button4.Enabled = False
 Else
     Button4.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button4.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub Button9_Click(sender As Object, e As EventArgs) Handles
 Button9.Click
 If tt = True Then
     Button9.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button9.Enabled = False
 Else
     Button9.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button9.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub Button8_Click(sender As Object, e As EventArgs) Handles
 Button8.Click
 If tt = True Then
     Button8.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button8.Enabled = False
 Else
     Button8.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button8.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub Button7_Click(sender As Object, e As EventArgs) Handles
 Button7.Click
 If tt = True Then
     Button7.Text = "X"
     Text = "Player 2's turn"
     tt = False
     Button7.Enabled = False
 Else
     Button7.Text = "0"
     Text = "Player 1's turn"
     tt = True
     Button7.Enabled = False
 End If
 checkWinner()
 End Sub

 Private Sub ResetToolStripMenuItem_Click(sender As Object, e As EventArgs)
 Handles ResetToolStripMenuItem.Click
 RR()

 End Sub

 Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs)
 Handles ExitToolStripMenuItem.Click
 Close()
 End Sub
 Public Sub RR()
 Text = "New Game"
 Button1.Enabled = True
 Button2.Enabled = True
 Button3.Enabled = True
 Button4.Enabled = True
 Button5.Enabled = True
 Button6.Enabled = True
 Button7.Enabled = True
 Button8.Enabled = True
 Button9.Enabled = True
 tt = True
 Button1.ResetText()
 Button2.ResetText()
 Button3.ResetText()
 Button4.ResetText()
 Button5.ResetText()
 Button6.ResetText()
 Button7.ResetText()
 Button8.ResetText()
 Button9.ResetText()
 End Sub
 Private Sub checkWinner()
 If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button1.Text = "0" And Button2.Text = "0" And Button3.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button1.Text = "X" And Button5.Text = "X" And Button7.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button1.Text = "0" And Button5.Text = "0" And Button7.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button1.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button1.Text = "0" And Button6.Text = "0" And Button9.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button2.Text = "0" And Button5.Text = "0" And Button8.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button3.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button3.Text = "0" And Button4.Text = "0" And Button7.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button3.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button3.Text = "0" And Button5.Text = "0" And Button9.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button6.Text = "X" And Button5.Text = "X" And Button4.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button6.Text = "0" And Button5.Text = "0" And Button4.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 If Button9.Text = "X" And Button8.Text = "X" And Button7.Text = "X" Then
     MessageBox.Show("Player 1 wins", "Winner!")
     RR()
 ElseIf Button9.Text = "0" And Button8.Text = "0" And Button7.Text = "0" Then
     MessageBox.Show("Player 2 Wins!", "Winner!")
     RR()
 End If
 End Sub

 Private Sub StartToolStripMenuItem_Click(sender As Object, e As EventArgs)
 Handles StartToolStripMenuItem.Click
 New_Player.Show()
 End Sub

 Private Sub tictactoe_Load(sender As Object, e As EventArgs) Handles
 MyBase.Load
 Button1.Enabled = False
 Button2.Enabled = False
 Button3.Enabled = False
 Button4.Enabled = False
 Button5.Enabled = False
 Button6.Enabled = False
 Button7.Enabled = False
 Button8.Enabled = False
 Button9.Enabled = False
 End Sub
 End Class

Это другая форма, где я ввожу имена игроков,здесь я хочу Скопировать текст текстовых полей в переменную p1 в основной форме и отобразить его на форме:
Public Class New_Player
   Dim sq As New tictactoe
   Private Sub New_Player_Load(sender As Object, e As EventArgs) Handles
   MyBase.Load

   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
   Button1.Click
   sq.Label1.Text = TextBox1.Text
   End Sub

   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
   Button2.Click
   TextBox1.Clear()
   TextBox2.Clear()
   End Sub
   End Class


Что я уже пробовал:

попытался передать текст в качестве параметров функции..

1 Ответов

Рейтинг:
12

RickZeeland

Вы можете заменить все эти обработчики кнопок одним методом:

Public Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
   Debug.Print("Button " & CType(sender, Button).Text)
End Sub
Вам нужно изменить события щелчка для кнопок в Редакторе форм на "ButtonClick".