2009-07-09 74 views
2

我的游戏有2个问题。在Visual Basic 2005中修复我的Tic Tac Toe游戏

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 

回答

2

四个问题,我注意到了蝙蝠:

  • AI()方法可以覆盖先前选定的正方形。
  • 你可能想禁用按钮的AI选择,以及:
If computer = 1 Then 
     btnOne.Text = "O" 
     turn = 1 
 btnOne.Enabled = False 
    End If
  • 你不进行重置文本,所以它看起来它仍然是玩家2的回合AI走后:
If computer = 1 Then 
     btnOne.Text = "O" 
     turn = 1 
 txtSummary.Text = "Player 1's Turn" 
 btnOne.Enabled = False 
    End If
  • 你让AI去每一个TI我没有首先检查胜利条件。最简单的方法是将Win()更改为返回布尔值的函数,如果游戏结束则返回true,否则返回false。那么你的AI()调用更改为以下:
If Not Win() Then 
     If rdoSinglePlayer.Checked Then Call AI() 
    End If

其他建议包括:

  • 你有很多重复的代码。考虑将其重构为一次多次调用的方法。这使得您的代码更容易维护并且更具可读性。对于一个例如,你可以有(假设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
3

相信我在这一个。你的游戏有两个以上的问题。

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

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

让你开始思考:考虑到实际上只有8个可能的“胜利”位置:4个涉及中心,每个边上有1个侧边正方形。您只需测试这些组合中是否包含组合中涉及的所有三个方格的相同按钮文本。如果您在每次移动后进行检查,那么当前移动的制造商最好是赢家,因此您只需检查,直到找到与当前移动制造商不匹配的组合中的方形。这将大大提高您的Win()方法的速度。