【问题标题】:Problems with Buttons in Visual BasicVisual Basic 中的按钮问题
【发布时间】:2014-04-09 11:25:26
【问题描述】:

我正在尝试用 Visual Basic 编写一个在用户和计算机之间播放的井字游戏程序。该程序使用模块中的二维数组来跟踪游戏的状态。这是我到目前为止的代码。

Public Class Form1

Private _currentPlayer As String = "O"
Private _gameArray As String

Private Property gameArray(p1 As Integer, p2 As Integer) As String
    Get
        Return _gameArray
    End Get
    Set(value As String)
        _gameArray = value
    End Set
End Property

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MessageBox.Show("Welcome to Tic Tac Toe! Would you like to play a game?")
End Sub

Private Function nextPlayer() As String

    If _currentPlayer.Equals("O") Then
        _currentPlayer = "X"
        Return "O"
    Else
        _currentPlayer = "O"
        Return "X"
    End If

End Function

Private Sub printView()
    Console.WriteLine(btn1.Text & "|" & btn2.Text & "|" & btn3.Text)
    Console.WriteLine("----------------")
    Console.WriteLine(btn4.Text & "|" & btn5.Text & "|" & btn6.Text)
    Console.WriteLine("----------------")
    Console.WriteLine(btn7.Text & "|" & btn8.Text & "|" & btn9.Text)
End Sub

Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
    Console.WriteLine(e.KeyChar)

    Select Case e.KeyChar
        Case "1"
            btn1.Text = "X"
        Case "2"
            btn2.Text = "X"
        Case "3"
            btn3.Text = "X"
        Case "4"
            btn4.Text = "X"
        Case "5"
            btn5.Text = "X"
        Case "6"
            btn6.Text = "X"
        Case "7"
            btn7.Text = "X"
        Case "8"
            btn8.Text = "X"
        Case "9"
            btn9.Text = "X"
    End Select
    printView()
End Sub

Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    If Not String.IsNullOrEmpty(btn1.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn1.Text = nextPlayer()
        gameArray(0, 0) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
    If Not String.IsNullOrEmpty(btn2.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn2.Text = nextPlayer()
        gameArray(0, 1) = nextPlayer()
        checkwin()
    End If

End Sub

Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
    If Not String.IsNullOrEmpty(btn3.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn3.Text = nextPlayer()
        gameArray(0, 2) = nextPlayer()
        checkwin()
    End If

End Sub

Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
    If Not String.IsNullOrEmpty(btn4.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn4.Text = nextPlayer()
        gameArray(1, 0) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
    If Not String.IsNullOrEmpty(btn5.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn5.Text = nextPlayer()
        gameArray(1, 1) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
    If Not String.IsNullOrEmpty(btn6.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn6.Text = nextPlayer()
        gameArray(1, 2) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
    If Not String.IsNullOrEmpty(btn7.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn7.Text = nextPlayer()
        gameArray(2, 0) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
    If Not String.IsNullOrEmpty(btn8.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn8.Text = nextPlayer()
        gameArray(2, 1) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
    If Not String.IsNullOrEmpty(btn9.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn9.Text = nextPlayer()
        gameArray(2, 2) = nextPlayer()
        checkwin()
    End If
End Sub

Private Sub checkwin()

    If (btn1.Text = "X") And (btn2.Text = "X") And (btn3.Text = "X") Then MsgBox("X WINS!")
    If (btn4.Text = "X") And (btn5.Text = "X") And (btn6.Text = "X") Then MsgBox("X WINS!")
    If (btn7.Text = "X") And (btn8.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
    If (btn1.Text = "X") And (btn4.Text = "X") And (btn7.Text = "X") Then MsgBox("X WINS!")
    If (btn2.Text = "X") And (btn5.Text = "X") And (btn8.Text = "X") Then MsgBox("X WINS!")
    If (btn3.Text = "X") And (btn6.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
    If (btn1.Text = "X") And (btn5.Text = "X") And (btn9.Text = "X") Then MsgBox("X WINS!")
    If (btn3.Text = "X") And (btn5.Text = "X") And (btn7.Text = "X") Then MsgBox("X WINS!")

    If (btn1.Text = "O") And (btn2.Text = "O") And (btn3.Text = "O") Then MsgBox("O WINS!")
    If (btn4.Text = "O") And (btn5.Text = "O") And (btn6.Text = "O") Then MsgBox("O WINS!")
    If (btn7.Text = "O") And (btn8.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
    If (btn1.Text = "O") And (btn4.Text = "O") And (btn7.Text = "O") Then MsgBox("O WINS!")
    If (btn2.Text = "O") And (btn5.Text = "O") And (btn8.Text = "O") Then MsgBox("O WINS!")
    If (btn3.Text = "O") And (btn6.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
    If (btn1.Text = "O") And (btn5.Text = "O") And (btn9.Text = "O") Then MsgBox("O WINS!")
    If (btn3.Text = "O") And (btn5.Text = "O") And (btn7.Text = "O") Then MsgBox("O WINS!")
End Sub

End Class

在编写代码时,程序实际上并没有轮流运行,只是简单地在我按下的任何按钮上放一个 O。

我还应该让用户既可以单击他们想要的方块,也可以使用键盘上的 1-9 键来选择他们想要的框。我无法将按键分配给相应的按钮。

任何帮助将不胜感激。

【问题讨论】:

  • 在其中一个按钮单击事件处使用断点...单击按钮并查看 btnX.Text 是什么...很可能它不是 NullOrEmpty...

标签: vb.net button multidimensional-array tic-tac-toe


【解决方案1】:

还可以尝试以这种方式更改您的代码,这样您就不会使用 NOT 子句....

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click

    If String.IsNullOrEmpty(btn2.Text) Then
        MessageBox.Show("This position has already been taken, please select an empty square.")
    Else
        btn2.Text = nextPlayer()
        gameArray(0, 1) = nextPlayer()
    End If

End Sub

【讨论】:

  • 谢谢这解决了我当前的问题,但是程序实际上并没有轮流,它只是在我单击的每个按钮中放置一个 O。另外,知道如何在每次移动后检查获胜位置吗?
  • 好的,你真的在​​调用你的 checkwin() 子吗?我在你的代码中看不到这一点。每次更改按钮的文本时都需要调用此函数。这是合乎逻辑的步骤。用户移动...显示该移动...检查获胜或移动到下一个玩家。
  • 我意识到,当我按照您的建议移动代码时。我现在已经编写了代码,所以它调用了 checkwin() 并且这部分工作正常。但是,该程序实际上仍然没有像预期的那样轮流。另外,您是否知道编写此代码的简单方法,以便玩家 2 (X) 是计算机而不是另一个人类玩家?
  • OK.. 看看你上面的代码......我不确定你是否已经改变了它,但目前它总是会在按键上放置一个 X 而不是 0... 来实现第二个播放器,您需要使用 nextPlayer() 函数。如果下一个玩家是“计算机”,那么您需要实现一个子程序,该子程序将检查所有可用的空白“按钮”并查看它是否可以完成一行……如果可能的话,那就是它的移动位置 O按钮运行 checkwin() 否则检查是否有任何“空格”,如果更改为“X”将完成一行,然后放置“O”来阻止,否则将“O”放置在任何空白处。
  • 好吧,听起来很复杂哈哈。我在 nextPlayer() 函数中使用了几个断点,看起来它永远不会将 O 更改为 X,因此 O 始终是按钮中发布的内容。
【解决方案2】:

再次查看您的代码....假设 _currentPlayer = O

Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If Not String.IsNullOrEmpty(btn2.Text) Then
    MessageBox.Show("This position has already been taken, please select an empty square.")
Else
    btn2.Text = nextPlayer()    ' this will return O and _currentPlayer is now X
    gameArray(0, 1) = nextPlayer() 'Your calling it again so return will be X and _currentPlayer in now O again... 
    checkwin()
End If

因为这是一个班级项目...看看你能不能先自己解决这个问题。

【讨论】:

    猜你喜欢
    • 2012-04-23
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    相关资源
    最近更新 更多