我的遊戲有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