Make it look like a computer thinking in TicTacToe

here is a snippet of code that I wrote on 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 ie 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 ie 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 that I try to make it look as if the computer should “think” before making its move. So I tried to make this call System.Threading.Sleep () in different places in the above code.

The problem is that instead of making the computer look like its thinking, the program waits and then combines X and O at the same time. Can someone help me make it so that the program puts X wherever I click, AND WHAT TO WAIT BEFORE IT PLACES O?

Edit: in case one of you asks a question, I understand that the AI ​​computer is ridiculously dumb, but its just a mess right now. Later I will implement a serious AI ... I hope.

+4
source share
3 answers

Since Greg said that I would use a timer, I would first take the logic from your ClickTurn event on the computer and create a method, you could use a random number generator to make it seem like how much time the computer thought varied, then you can For example, change the cursor to a wait cursor. Something like that:

 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 
+1
source

In general, the solution for this kind of task is to set a timer for the subsequent display of the calculated computer. The problem is that (in general) VB.NET does not allow the screen to refresh while your code is running. Therefore, when you add a dream, even if you already told the display to show player X, the screen does not actually reflect this until your function returns after sleep.

You will need to see how to use the timer object in VB.NET (I don't know the details from the top of my head). Since there will be time between the player’s movement and the computer’s movement, you will also want to make sure that the player cannot move twice before the computer gets a chance (so make a variable that changes depending on what it is in turn).

+1
source

Separation of problems.

Create a method that tells you what the computer is driving. Ask this method to do it as best and effective as possible, and return the entire result back to the presentation code. This is up to the level of presentation, in order to later decide what to do with this movement, which may include waiting or reviving something to make it seem like the computer is thinking.

0
source

Source: https://habr.com/ru/post/1441022/


All Articles