首先我必须说我是vb.net
的新手。我有一个想法做一个井字游戏,同时我必须练习使用OOP。首先,我创建了9个对象作为按钮。我猜我会删除代码并提出问题。
Public Class XO
Friend WithEvents BT As System.Windows.Forms.Button
Public value As Integer
Public index As Integer
Public line1 As Integer
Public line2 As Integer
Public clicks As Integer = 1
Public Shared Turn As String = "X"
Public x As String = "X"
Public Sub New(ByVal val As Integer, ByVal indx As Integer)
Me.value = val
Me.index = indx
Me.BT = New System.Windows.Forms.Button
Me.BT.Size = New System.Drawing.Size(50, 50)
If indx <= 3 Then
Me.BT.Location = New System.Drawing.Point((index - 1) * 50, 0)
ElseIf indx <= 6 Then
Me.BT.Location = New System.Drawing.Point((index - 4) * 50, 50)
ElseIf indx <= 9 Then
Me.BT.Location = New System.Drawing.Point((index - 7) * 50, 100)
End If
line1 = setline1(Me.index)
line2 = setline2(Me.index)
End Sub
Public Function setline1(ByVal index As Integer)
Select Case index
Case 1, 2, 3
Return 1
Case 4, 5, 6
Return 2
Case 7, 8, 9
Return 3
End Select
End Function
Public Function setline2(ByVal index As Integer)
Select Case index
Case 1, 4, 7
Return 1
Case 2, 5, 8
Return 2
Case 3, 6, 9
Return 3
End Select
End Function
Private Sub BTclick(sender As System.Object, e As System.EventArgs) Handles BT.Click
'If BT.Text = "" Then
' If Turn = "O" Then
' BT.Text = "O"
' Turn = "X"
' ElseIf Turn = "X" Then
' BT.Text = "X"
' Turn = "O"
' End If
'End If
BT.Text = "X"
End Sub
End Class
我的问题是:我已经创建了按钮位置作为索引,因此您在代码1,2,3中看到返回1作为第1行。我想告诉程序,如果第1行有3个按钮包含String&# 34; X&#34;然后是msgbox(&#34; X是赢家&#34;)
答案 0 :(得分:0)
您应该使用通用列表,以便更轻松地参考按钮。尝试以下实现,看看它是否有帮助。如果你想像你想要的那样拥有自己的对象,那么你只需要创建这些对象的列表。无论哪种方式,您都需要跟踪对象(作为集合)。
Public Class Form1
Dim buttons As List(Of Button) = New List(Of Button)()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Create the 9 buttons
For i = 0 To 8 Step 1
Dim btn As Button = New Button
btn.Name = "Button" & i.ToString()
btn.Text = ""
'Do what you need to do with you button like placement and such.
'Try like this.
'0|1|2
'3|4|5
'6|7|8
Select Case i
Case 0
btn.Location = New Point(1, 1)
Case 1
btn.Location = New Point(1, 1)
Case 2
btn.Location = New Point(1, 1)
Case 3
btn.Location = New Point(1, 1)
Case 4
btn.Location = New Point(1, 1)
Case 5
btn.Location = New Point(1, 1)
Case 6
btn.Location = New Point(1, 1)
Case 7
btn.Location = New Point(1, 1)
Case 8
btn.Location = New Point(1, 1)
End Select
'Do what you need to do with you button like placement and such.
'All buttons should be handled by the same event handler for simplicity.
AddHandler btn.Click, AddressOf ButtonHandler
buttons.Add(btn)
Next
End Sub
Public Sub CheckFirstLine()
'Check for a winner
'If you placed your buttons like this.
'0|1|2
'3|4|5
'6|7|8
'then
'You could handle the check of the first line like this
If buttons.Item(0).Text = "X" And buttons.Item(1).Text = "X" And buttons.Item(2).Text = "X" Then
MessageBox.Show("X Wins")
End If
End Sub
Private Sub ButtonHandler(sender As Object, e As EventArgs)
Dim myButton = CType(sender, Button)
If myButton.Text <> "" Then
'Can't play here.
Exit Sub
Else
'Whose turn is it?
myButton.Text = "X"
End If
CheckFirstLine()
End Sub
End Class