【问题标题】:Fix my Tic Tac Toe game in Visual Basic 2005在 Visual Basic 2005 中修复我的井字游戏
【发布时间】:2012-05-05 21:49:55
【问题描述】:

我的游戏有 2 个问题。

1.) 初始化时,如果我选择多人游戏,按钮仍处于禁用状态。我必须刷新才能让它工作。

2.) 当我玩单人游戏时,我的 AI 无法正常工作。当 AI 选择它的方格时,它仍然让我选择一个带有 AI 符号的方格,就好像我在玩多人游戏一样。我什至在 AI 子类中指定轮到 AI 时 turn = 1,这意味着轮到我了。

Public Class frmTicTacToe

Dim turn As Integer
Dim computer As Integer
Private Sub AI()
    Call Win()
    If turn <> 1 Then
        computer = Int(9 * Rnd()) + 1
    End If

    If computer = 1 Then
        btnOne.Text = "O"
        turn = 1
    End If

    If computer = 2 Then
        btnTwo.Text = "O"
        turn = 1
    End If

    If computer = 3 Then
        btnThree.Text = "O"
        turn = 1
    End If

    If computer = 4 Then
        btnFour.Text = "O"
        turn = 1
    End If

    If computer = 5 Then
        btnFive.Text = "O"
        turn = 1
    End If

    If computer = 6 Then
        btnSix.Text = "O"
        turn = 1
    End If

    If computer = 7 Then
        btnSeven.Text = "O"
        turn = 1
    End If

    If computer = 8 Then
        btnEight.Text = "O"
        turn = 1
    End If

    If computer = 9 Then
        btnNine.Text = "O"
        turn = 1
    End If

End Sub
Private Sub Win()
    If btnOne.Text = "X" And btnTwo.Text = "X" And btnThree.Text = "X" Then
        txtSummary.Text = "Player 1 Wins"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnOne.Text = "X" And btnFour.Text = "X" And btnSeven.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnOne.Text = "X" And btnFive.Text = "X" And btnNine.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnThree.Text = "X" And btnSix.Text = "X" And btnNine.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnSeven.Text = "X" And btnEight.Text = "X" And btnNine.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnFour.Text = "X" And btnFive.Text = "X" And btnSix.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnTwo.Text = "X" And btnFive.Text = "X" And btnEight.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()

    ElseIf btnThree.Text = "X" And btnFive.Text = "X" And btnSeven.Text = "X" Then
        txtSummary.Text = "Player 1 Wins!"
        MsgBox("Player 1 Wins")
        Call disablebuttons()
    End If

    If btnOne.Text = "O" And btnTwo.Text = "O" And btnThree.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnOne.Text = "O" And btnFour.Text = "O" And btnSeven.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnOne.Text = "O" And btnFive.Text = "O" And btnNine.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnThree.Text = "O" And btnSix.Text = "O" And btnNine.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnSeven.Text = "O" And btnEight.Text = "O" And btnNine.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnFour.Text = "O" And btnFive.Text = "O" And btnSix.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnTwo.Text = "O" And btnFive.Text = "O" And btnEight.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()

    ElseIf btnThree.Text = "O" And btnFive.Text = "O" And btnSeven.Text = "O" Then
        txtSummary.Text = "Player 2 Wins!"
        MsgBox("Player 2 Wins")
        Call disablebuttons()
    End If
End Sub
Private Sub disablebuttons()
    btnOne.Enabled = False
    btnTwo.Enabled = False
    btnThree.Enabled = False
    btnFour.Enabled = False
    btnFive.Enabled = False
    btnSix.Enabled = False
    btnSeven.Enabled = False
    btnEight.Enabled = False
    btnNine.Enabled = False
End Sub

Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
    If turn = 1 Then
        btnOne.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnOne.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnOne.Enabled = False

End Sub

Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
    If turn = 1 Then
        btnTwo.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnTwo.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnTwo.Enabled = False
End Sub

Private Sub btnThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click
    If turn = 1 Then
        btnThree.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnThree.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnThree.Enabled = False
End Sub

Private Sub btnFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFour.Click
    If turn = 1 Then
        btnFour.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnFour.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnFour.Enabled = False
End Sub

Private Sub btnFive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFive.Click
    If turn = 1 Then
        btnFive.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnFive.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnFive.Enabled = False
End Sub

Private Sub btnSix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSix.Click
    If turn = 1 Then
        btnSix.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnSix.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnSix.Enabled = False
End Sub

Private Sub btnSeven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeven.Click
    If turn = 1 Then
        btnSeven.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnSeven.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnSeven.Enabled = False
End Sub

Private Sub btnEight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEight.Click
    If turn = 1 Then
        btnEight.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnEight.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnEight.Enabled = False
End Sub

Private Sub btnNine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNine.Click
    If turn = 1 Then
        btnNine.Text = "X"
        txtSummary.Text = "Player 2's Turn"
    Else
        btnNine.Text = "O"
        txtSummary.Text = "Player 1's Turn"
    End If
    turn += 1
    If turn > 2 Then
        turn = 1
    End If

    If rdoSinglePlayer.Checked Then Call AI()
    Call Win()
    btnNine.Enabled = False
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    btnOne.Text = ""
    btnOne.Enabled = True
    btnTwo.Text = ""
    btnTwo.Enabled = True
    btnThree.Text = ""
    btnThree.Enabled = True
    btnFour.Text = ""
    btnFour.Enabled = True
    btnFive.Text = ""
    btnFive.Enabled = True
    btnSix.Text = ""
    btnSix.Enabled = True
    btnSeven.Text = ""
    btnSeven.Enabled = True
    btnEight.Text = ""
    btnEight.Enabled = True
    btnNine.Text = ""
    btnNine.Enabled = True
    rdoSinglePlayer.Checked = False
    rdoMultiplayer.Checked = False
    If turn = 1 Then
        txtSummary.Text = "Player 1's Turn"
    Else
        txtSummary.Text = "Player 2's Turn"
    End If
End Sub

Private Sub frmTicTacToe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    txtSummary.Text = "Select Single Player or Multiplayer"

    If rdoSinglePlayer.Checked = False And rdoMultiplayer.Checked = False Then
        Call disablebuttons()
    End If

    If rdoSinglePlayer.Checked = True Or rdoMultiplayer.Checked = True Then
        turn = 1
    End If

End Sub
Private Sub Start()
    btnOne.Text = ""
    btnOne.Enabled = True
    btnTwo.Text = ""
    btnTwo.Enabled = True
    btnThree.Text = ""
    btnThree.Enabled = True
    btnFour.Text = ""
    btnFour.Enabled = True
    btnFive.Text = ""
    btnFive.Enabled = True
    btnSix.Text = ""
    btnSix.Enabled = True
    btnSeven.Text = ""
    btnSeven.Enabled = True
    btnEight.Text = ""
    btnEight.Enabled = True
    btnNine.Text = ""
    btnNine.Enabled = True
    If turn = 1 Then
        txtSummary.Text = "Player 1's Turn"
    Else
        txtSummary.Text = "Player 2's Turn"
    End If
End Sub
Private Sub rdoSinglePlayer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoSinglePlayer.CheckedChanged
    Call Start()
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
End Sub

Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
    Dim AboutBox1 As New AboutBox1
    AboutBox1.Show()
End Sub

Private Sub ResetToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetToolStripMenuItem.Click
    btnOne.Text = ""
    btnOne.Enabled = True
    btnTwo.Text = ""
    btnTwo.Enabled = True
    btnThree.Text = ""
    btnThree.Enabled = True
    btnFour.Text = ""
    btnFour.Enabled = True
    btnFive.Text = ""
    btnFive.Enabled = True
    btnSix.Text = ""
    btnSix.Enabled = True
    btnSeven.Text = ""
    btnSeven.Enabled = True
    btnEight.Text = ""
    btnEight.Enabled = True
    btnNine.Text = ""
    btnNine.Enabled = True
    rdoSinglePlayer.Checked = False
    rdoMultiplayer.Checked = False
    If turn = 1 Then
        txtSummary.Text = "Player 1's Turn"
    Else
        txtSummary.Text = "Player 2's Turn"
    End If
End Sub
End Class

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    相信我这一点。您的游戏有超过 2 个问题。

    关于 (1),我没有看到多人游戏按钮的处理程序。对于单人游戏按钮,您可以重新启用按钮,但对于多人游戏,您不需要。添加一个处理程序并在其中设置多人游戏。

    关于(2),您需要在计算机选择按钮时禁用该按钮。实际上,该按钮仅在用户单击时才被禁用。在 AI 例程中,更改按钮文本后,将其禁用。

    让你开始思考:考虑一下实际上只有 8 个可能的“获胜”位置:4 个涉及中心,1 个涉及每边的边中心正方形。您只需要测试这些组合中的任何一个对于组合中涉及的所有三个方块是否具有相同的按钮文本。如果你在每一步之后都检查,如果有一个,那么当前移动的制造者最好是赢家,所以你真的只需要检查,直到你在组合中找到一个与当前移动制造者不匹配的方块。这将大大提高您的 Win() 方法的速度。

    【讨论】:

      【解决方案2】:

      我立即注意到了四个问题:

      • 您的AI() 方法可以覆盖以前选择的方块。
      • 您可能还想禁用 AI 选择的按钮:
          If computer = 1 Then
              btnOne.Text = "O"
              turn = 1
              btnOne.Enabled = False
          End If
      • 你没有重置文本,所以它看起来在 AI 离开后仍然轮到玩家 2:
          If computer = 1 Then
              btnOne.Text = "O"
              turn = 1
              txtSummary.Text = "Player 1's Turn"
              btnOne.Enabled = False
          End If
      • 您每次都让 AI 离开而没有先检查获胜条件。最简单的方法是将Win() 更改为返回布尔值的函数,如果游戏结束则返回true,否则返回false。然后将您的 AI() 调用更改为以下内容:
          If Not Win() Then
               If rdoSinglePlayer.Checked Then Call AI()
          End If

      其他建议包括:

      • 你有很多重复的代码。考虑将其重构为一个被多次调用的方法。这使您的代码更易于维护且更具可读性。对于 one 的例子,你可以有(假设 Win() 返回一个像上面一样的布尔值):
          Private Sub NextTurn()
              If Not Win() Then
                  If turn = 1 Then
                      txtSummary.Text = "Player 2's Turn"
                      turn = 2
                      If rdoSinglePlayer.Checked Then Call AI()
                  Else
                      txtSummary.Text = "Player 1's Turn"
                      turn = 1
                  End
              End If
          End Sub
      
          Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
              If turn = 1 Then
                  btnOne.Text = "X"
                  txtSummary.Text = "Player 2's Turn"
              Else
                  btnOne.Text = "O"
                  txtSummary.Text = "Player 1's Turn"
              End If
              turn += 1
              If turn > 2 Then
                  turn = 1
              End If
      
              If rdoSinglePlayer.Checked Then Call AI()
              Call Win()
              btnOne.Enabled = False
              Call NextTurn()
          End Sub

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-06
        • 2020-02-01
        • 2015-06-12
        • 1970-01-01
        相关资源
        最近更新 更多