2009-07-09 71 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()方法的速度。