4

here is a slice of code that i've written in 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

Everything works smoothly, except i'm trying to make it seem like the computer has to "think" before making its move. So what i've tried to do is place a System.Threading.Sleep() call in different places in the code above.

The problem is that instead of making the computer look like its thinking, the program waits and then puts the X and O together at the same time. Can someone help me make it so that the program puts an X wherever i click AND THEN wait before it places an O?

Edit: in case any of you are wondering, i realize that the computers AI is ridiculously dumb, but its just to mess around right now. Later on i will implement a serious AI..hopefully.

4

3 回答 3

1

一般来说,此类问题的解决方案是设置一个计时器,以便稍后显示计算出的计算机移动。问题是(通常)VB.NET 不允许在代码运行时更新屏幕。因此,当您添加睡眠时,即使您已经告诉显示器显示玩家 X,屏幕实际上也不会反映这一点,直到您的函数在睡眠之后返回。

您必须查看如何在 VB.NET 中使用计时器对象(我不知道我头脑中的细节)。因为玩家移动和计算机移动之间会有时间,所以您还需要确保玩家在计算机获得机会之前不能移动两次(所以要根据轮到谁来创建一个变量) .

于 2012-10-20T23:06:00.347 回答
1

正如 Greg 所说我会使用 Timer,我会首先从您的 computerTurn Click 事件中取出逻辑并创建一个 Method,您可以使用随机数生成器使其看起来像是计算机认为变化的时间量,然后例如,您可以将光标更改为等待光标。像这样的东西:

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
于 2012-10-20T23:17:16.457 回答
0

关注点分离。

建立一种方法,告诉您计算机想要进行什么移动。让该方法尽可能高效地完成工作,并将整个结果返回给您的演示代码。然后由演示级别决定如何处理该动作,这可能包括等待或制作动画以使其看起来像是计算机正在思考。

于 2012-10-20T23:20:25.767 回答