这是我在VB.net中编写的一段代码
Private Sub L00_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles L00.Click, L01.Click, L02.Click, L03.Click, L10.Click, L11.Click, L12.Click, L13.Click, L20.Click, L21.Click, L22.Click, L23.Click, L30.Click, L31.Click, L32.Click, L33.Click
Dim ticTac As Label = CType(sender, Label)
Dim strRow As String
Dim strCol As String
'Once a move is made, do not allow user to change whether player/computer goes first, because it doesn't make sense to do so since the game has already started.
ComputerFirstStripMenuItem.Enabled = False
PlayerFirstToolStripMenuItem.Enabled = False
'Check to make sure clicked tile is a valid tile i.e and empty tile.
If (ticTac.Text = String.Empty) Then
ticTac.Text = "X"
ticTac.ForeColor = ColorDialog1.Color
ticTac.Tag = 1
'After the player has made his move it becomes the computers turn.
computerTurn(sender, e)
Else
MessageBox.Show("Please pick an empty tile to make next move", "Invalid Move")
End If
End Sub
Private Sub computerTurn(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call Randomize()
row = Int(4 * Rnd())
col = Int(4 * Rnd())
'Check to make sure clicked tile is a valid tile i.e and empty tile.
If Not ticTacArray(row, col).Tag = 1 And Not ticTacArray(row, col).Tag = 4 Then
ticTacArray(row, col).Text = "O"
ticTacArray(row, col).ForeColor = ColorDialog2.Color
ticTacArray(row, col).Tag = 4
checkIfGameOver(sender, e)
Else
'Some good ole pseudo-recursion(doesn't require a base case(s)).
computerTurn(sender, e)
End If
End Sub
一切顺利,除了我试图让计算机在移动之前必须“思考”.所以我试图做的是在上面的代码中的不同位置放置一个System.Threading.Sleep()调用.
问题是,程序不是让计算机看起来像它的想法,而是等待,然后将X和O放在一起.有人可以帮助我做到这一点,以便程序在我点击的任何地方放一个X然后在它放置O之前等待吗?
编辑:如果你们中的任何人想知道,我意识到计算机AI是荒谬的愚蠢,但它现在只是乱七八糟.稍后我将实施一个严重的AI ..希望如此.
最佳答案 正如格雷格所说我会使用一个Timer,我会首先从你的computerTurn Click事件中取出逻辑并创建一个方法,你可以使用一个随机数生成器来使它看起来像是计算机思维变化的时间,然后例如,您可以将Cursor更改为Wait Cursor.像这样的东西:
Public Class Form1
Dim rnd As Random = New Random(1)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.Cursor = Cursors.WaitCursor
Timer1.Interval = CInt(rnd.NextDouble * 1000)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
computerTurn()
End Sub
Private Sub computerTurn()
Me.Cursor = Cursors.Default
'Your Move Logic Here
End Sub
End Class