2009-07-09 10 views
2

私のゲームに2つの問題があります。Visual Basic 2005でTic Tac Toeのゲームを修正しました

1.)マルチプレイヤーを選択すると、初期化時にボタンはまだ無効になっています。私はそれを動作させるためにリフレッシュする必要があります。

2.)私がシングルプレイヤーをしているとき、私のAIは正しく動作していません。 AIが四角形を選択すると、あたかも私がマルチプレイヤーゲームをしているかのようにAIのシンボルで四角形を選択することができます。私は、AIのターンが完了したときにターン1になるAIサブクラスに指定していても、それは自分の順番でなければなりません。私はバットをオフに気づい

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

4つの問題:

  • あなたAI()方法は、以前に選択された正方形を上書きすることができます。それはまだプレイヤー2の番だのようにAIが行く後
If computer = 1 Then 
     btnOne.Text = "O" 
     turn = 1 
 btnOne.Enabled = False 
    End If
  • は、あなたがテキストをリセットしていないので、それはなります
  • はおそらくAIが同様に選択し、ボタンを無効にしたいです:
If computer = 1 Then 
     btnOne.Text = "O" 
     turn = 1 
 txtSummary.Text = "Player 1's Turn" 
 btnOne.Enabled = False 
    End If
  • あなたはAIがすべてのTIを手放すしています私は勝利条件を最初に確認することなく。最も単純なのは、Win()をbooleanを返す関数に変更し、ゲームオーバーの場合はtrueを返し、そうでない場合はfalseを返します。そして、次のおAI()の呼び出しを変更:
If Not Win() Then 
     If rdoSinglePlayer.Checked Then Call AI() 
    End If

他の提案は、次のとおりです。

  • あなたが繰り返しのコードの多くを持っています。それを複数回呼び出される単一のメソッドにリファクタリングすることを考えてみましょう。これにより、コードの保守が非常に簡単になり、読みやすくなります。 たとえば、あなたは(勝利は()上記のようなブール値を返すと仮定した場合)かもしれない:
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

は、このいずれかに私を信頼しています。ゲームに2つ以上の問題があります。

(1)に関しては、マルチプレーヤーボタンのハンドラーは表示されません。シングルプレイヤーボタンの場合は、再度ボタンを有効にしますが、マルチプレイヤーの場合は無効にします。ハンドラを追加し、マルチプレイヤーをセットアップします。

(2)に関しては、コンピュータが選択するときにボタンを無効にする必要があります。そのままでは、ボタンはユーザがクリックしたときにのみ無効になります。 AIルーチンでは、ボタンのテキストを変更した後、無効にします。

思考を開始するには、実際には8つの可能な「勝ち」のポジションがあることを考慮してください。中央には4つ、左右にはサイドセンターの四角形が1つあります。これらの組み合わせのいずれかが、組み合わせに含まれる3つのすべての四角形に対して同じボタンテキストを持つかどうかをテストする必要があります。移動するたびにチェックすると、現在の移動のメーカーが勝者になったほうがよいので、現在の移動メーカーと一致しない組み合わせで四角を見つけるまで、実際に確認するだけです。これにより、Win()メソッドの速度が大幅に向上します。