Vb. NET realizes Gobang Artificial intelligence (2)

Source: Internet
Author: User
Tags exit comparison integer valid
Four, handle mouse events

'*****************************************************************************
' * * Module name: Themousedown
'**
' * * Description: This function mainly implements the following functions:
' * * 1. Determines whether the current game flag is valid.
' * * 2. Converts the actual coordinates into virtual coordinates.
' * * 3. Draws a player's pawn.
' * * 4. Perform check wins function.
' * * 5. Perform computer algorithm functions.
'**
'*****************************************************************************
Sub Themousedown (ByVal x As Integer, ByVal y As Integer)
If Theplayflag = False Then
Exit Sub
End If
' Check that the game status is valid
Dim I, J as Integer
Dim Zhx, Zhy as Integer
ZHX = Int ((x-10)/30)
Zhy = Int ((y-10)/30)
For i = 0 to 9
For j = 0 to 9
If table (ZHX, Zhy) > 0 Then
Exit Sub
End If
Next
Next
' Check if the current mouse clicks are valid
Dim MyColor as Color
Dim G as System.Drawing.Graphics
g = Picturebox1.creategraphics
MyColor = Color.White
Dim brush1 as System.Drawing.Brush = New SolidBrush (mycolor)
G.fillellipse (BRUSH1, ZHX * +, Zhy * 30 + 10, 30, 30)
' Draw the player's pieces
Table (ZHX, zhy) = 2
For i = 0 to 191
If Cwin (ZHX, zhy, i) = True Then
Cflag (i) = False
End If
Next
' Reset the computer's winning sign
Checkwin ()
' Check if the current player wins
Diannao ()
' Call the computer algorithm
End Sub

V. Win check algorithm.

'*****************************************************************************
' * * Module name: Checkwin
'**
' * * Description: This module performs the following functions:
' * * 1. Check if draw.
' * * 2. Check to see if the computer wins.
' * * 3. Check to see if the player wins.
'**
'*****************************************************************************
Sub Checkwin ()
Dim I, J, K, M, N as Integer
Dim CA as Integer
Dim PA as Integer
Dim cnormal as Integer = 0
For i = 0 to 191
If Cflag (i) = False Then
Cnormal = cnormal + 1
End If
Next
If Cnormal = 190 Then
Label1.visible = True
Label1.Text = "Draw, please start again!" "
Picturebox1.refresh ()
Theplayflag = False
Exit Sub
End If
' Set draw rules
For i = 0 to 191
If Cflag (i) = True Then
CA = 0
For j = 0 to 9
For k = 0 to 9
If table (j, k) = 1 Then
If Cwin (j, k, i) = True Then
CA = CA + 1
End If
End If
Next
Next
If CA = 5 Then
Label1.visible = True
Label1.Text = "Computer wins, please start Again"
Picturebox1.refresh ()
Theplayflag = False
Exit Sub
End If
End If
Next
' Check if the computer wins
For i = 0 to 191
If pflag (i) = True Then
PA = 0
For j = 0 to 9
For k = 0 to 9
If table (j, k) = 2 Then
If Pwin (j, k, i) = True Then
PA = pa + 1
End If
End If
Next
Next
If PA = 5 Then
Label1.visible = True
Label1.Text = "Player wins, please start Again"
Picturebox1.refresh ()
Theplayflag = False
Exit Sub
End If
End If
Next
' Check if the player wins
End Sub

Six, computer algorithms

'*****************************************************************************
' * * Module name: Diannao
'**
' * * Description: This program mainly performs the following functions:
' * * 1. Initialize the assignment system.
' * * 2. Assignment enhancement algorithm.
' * * 3. Compute the best attack bit for the computer and the player.
' * * 4. Compare computer and player's best attack bit and determine the best strategy for your computer.
' * * 5. Perform check wins function.
'**
'*****************************************************************************

Sub Diannao ()
Dim I, J, K, M, N as Integer
Dim DC as Integer
Dim Cab as Integer
Dim PAB as Integer
For i = 0 to 9
For j = 0 to 9
Pscore (i, j) = 0
Cscore (i, j) = 0
Next
Next
' Initialize an array of assignments
"' ******** Computer enhancement algorithm ********
For i = 0 to 191
If Cflag (i) = True Then
Cab = 0
For j = 0 to 9
For k = 0 to 9
If table (j, k) = 1 Then
If Cwin (j, k, i) = True Then
CAB = cab + 1
End If
End If
Next
Next
Select Case Cab
Case 3
For m = 0 to 9
For n = 0 to 9
If table (m, n) = 0 Then
If Cwin (M, n, i) = True Then
Cscore (m, n) = Cscore (M, N) + 5
End If
End If
Next
Next
Case 4
For m = 0 to 9
For n = 0 to 9
If table (m, n) = 0 Then
If Cwin (M, n, i) = True Then
Yuandian (M * +, N * 30 + 10)
Table (m, n) = 1
For dc = 0 to 191
If Pwin (M, n, DC) = True Then
PFLAG (DC) = False
Checkwin ()
Exit Sub
End If
Next
End If
End If
Next
Next
End Select
End If
Next
For i = 0 to 191
If pflag (i) = True Then
PAB = 0
For j = 0 to 9
For k = 0 to 9
If table (j, k) = 2 Then
If Pwin (j, k, i) = True Then
PAB = PAB + 1
End If
End If
Next
Next
Select Case PAB
Case 3
For m = 0 to 9
For n = 0 to 9
If table (m, n) = 0 Then
If Pwin (M, n, i) = True Then
Pscore (m, n) = Pscore (M, N) + 30
End If
End If
Next
Next
Case 4
For m = 0 to 9
For n = 0 to 9
If table (m, n) = 0 Then
If Pwin (M, n, i) = True Then
Yuandian (M * +, N * 30 + 10)
Table (m, n) = 1
For dc = 0 to 191
If Pwin (M, n, DC) = True Then
PFLAG (DC) = False
Checkwin ()
Exit Sub
End If
Next
End If
End If
Next
Next
End Select
End If
Next
"' ******** computer-enhanced algorithm to end ********

' ******** Assignment System ********
For i = 0 to 191
If Cflag (i) = True Then
For j = 0 to 9
For k = 0 to 9
If table (j, k) = 0 Then
If Cwin (j, k, i) = True Then
For m = 0 to 9
For n = 0 to 9
If table (m, n) = 1 Then
If Cwin (M, n, i) = True Then
Cscore (j, k) = Cscore (j, K) + 1
End If
End If
Next
Next
End If
End If
Next
Next
End If
Next
For i = 0 to 191
If pflag (i) = True Then
For j = 0 to 9
For k = 0 to 9
If table (j, k) = 0 Then
If Pwin (j, k, i) = True Then
For m = 0 to 9
For n = 0 to 9
If table (m, n) = 2 Then
If Pwin (M, n, i) = True Then
Pscore (j, k) = Pscore (j, K) + 1
End If
End If
Next
Next
End If
End If
Next
Next
End If
Next
"' ******** Assignment system end ********
"'" ******** score comparison algorithm ********
Dim A, B, C, D as Integer
Dim cs as Integer = 0
Dim ps as Integer = 0
For i = 0 to 9
For j = 0 to 9
If Cscore (i, J) > CS Then
CS = Cscore (i, J)
A = I
B = J
End If
Next
Next
For i = 0 to 9
For j = 0 to 9
If Pscore (i, J) > PS Then
PS = Pscore (i, J)
c = i
D = J
End If
Next
Next
IF CS > PS Then
Yuandian (A * +, b * 30 + 10)
Table (A, b) = 1
For i = 0 to 191
If Pwin (A, b, i) = True Then
PFLAG (i) = False
End If
Next
Else
Yuandian (c * +, D * 30 + 10)
Table (c, D) = 1
For i = 0 to 191
If Pwin (c, D, i) = True Then
PFLAG (i) = False
End If
Next
End If
"'" ******** score comparison algorithm end ********
Checkwin ()
End Sub

Seven, Draw pieces

'*****************************************************************************
' * * Module name: Yuandian
'**
' * * Description: This function is mainly for the drawing of computer pieces.
'**
'*****************************************************************************

Sub Yuandian (ByVal x As Integer, ByVal y As Integer)
Dim MyColor as Color
Dim G as System.Drawing.Graphics
g = Picturebox1.creategraphics
Dim Zhx, Zhy as Integer
ZHX = Int ((x-10)/30)
Zhy = Int ((y-10)/30)
MyColor = Color.Black
Dim brush1 as System.Drawing.Brush = New SolidBrush (mycolor)
G.fillellipse (BRUSH1, ZHX * +, Zhy * 30 + 10, 30, 30)
End Sub


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.