Artificial intelligence of Wuzi
---- Using strategy type AI and VB.NET to implement five daughters
Author: Tommy
introduction
Artificial intelligence is the so-called AI (Artificial Intelligence), it is an abstract technology, and the writing of the AI program does not need to be based on any established thinking or rule. In particular, the AI in the game can be completely based on the logical logic of the program designer itself. I personally think that the core of artificial intelligence should be to make the computer's ability to handle events, and all our research should also surround this direction. What we discuss today is the artificial intelligence of strategy.
Strategic manual intelligence can be said to be a more complicated in AI, the most common policy AI game is a chessboard game. In such games, the usual policy class AI program is to make the computer to judge all the canned chess and possible winning status, and calculate the current computer that can take a winning fractal of the game or win the player. Score, then decide a best way to walk. Let's first introduce the AI concept of the five son.
First part of Wuzi's AI concept
There is a saying that "the authority fan, the bystander clear.", But this sentence is not established on the computer player controlled by the AI, because the computer must know that there are those winning ways, and calculate every one of the next chess board Winning a few probability, that is, a complete five-child AI concept must: 1, you can know all the winners, 2, build and use the winning table, 3, set the score of winning, 4, make the computer attack and defensive ability.
First, seek the winning combination of Wuzi chess
In a five-child game, the computer must know that there are those winners, so we must seek
The total number of victories. We assume that the current checkerboard is 10 * 10.
(1), the number of winning combinations in the horizontal direction is calculated, and the winning combination of each column is: 6, a total of 10 columns, so the number of wins in the horizontal direction is: 6 * 10 = 60
(2), calculate the total number of portions of the vertical direction, the winning combination of each line is: 6, a total of 10 lines, the number of vertical wins is: 6 * 10 = 60
(3), calculate the total number of wins in the direction of the positive diagonal direction, the total number of wins on the positive diagonal is 6 (5 4 3 2 1) * 2 = 36
(4), calculate the total number of winning combinations in the direction of the opposition angle, the total number of wins on the opposition angle is 6 (5 4 3 2 1) * 2 = 36
Thus all the winning combinations are: 60 60 36 36 = 192
Second, establish and use the winning table
We have calculated a 10 * 10 second part using VB.NET to write some squares.
First, prepare before preparing: 1. Describe the process of the entire play with the idea of the computer. Considering the steps: (1) In order to simprance, we can first let the computer first step, the computer will seal off many players' victory. (2) When the player travels, we should first consider the legality of the player's chess. (3) If legal, the player will also seal the possible situation of winning many computers. (4) Computer thinking path: First determine whether all the winners of the current player and computer need to strengthen assignment, it is to strengthen assignment, otherwise ordinary assignment. (5) Compares the biggest values of players and computers. Points the maximum score as the next step of the computer. 2, use the VB.NET form and graphics tool to establish a board of chess board. (1) Add a PictureBox control. Role: Draw your chess pieces and board (2) to add a Label control using the Picturebox control. Role: Show the current winning flag, that is, when a party wins or shows this label when it is wins or chess. (3) Add a MainMenu control. Role: Control the start or end of the game. (4) Add a MediaPlay component. Role: Make the program to play music. 3, set the overall box price to take 10 * 10 chessboards as the main platform. Using an array to define the entire board desktop, use the array to define the winning combination and the winning flag. Second, the declaration global array and variable definition virtual desktop: DIM TABLE (9, 9) AS integer Defines the score of the current player desktop space:
DIM PSCore (9, 9) AS Integer
Define the score of the current computer desktop space:
DIM CSCore (9, 9) AS INTEGER
Define the player's winning combination:
DIM PWIN
9, 9, 19
1) as boolean
Define the winning combination of the computer:
DIM CWIN
9, 9, 19
1) as boolean
Define the player's winning symbol:
DIM PFLAG (191) as boolean
Define the winning symbol of the computer:
DIM CFLAG (191) as boolean
Define game valid markers:
DIM theplayflag as boolean
Third, initialization game
'********************************************************** *****************************
'** Module Name: InitPlayenvironment
'**
'** Description: This function is as follows:
'** 1. Set background music.
'** 2. Set the game status.
'** 3. Initialize the game status label.
'** 4. Directly specify the first step of the computer.
'** 5. Initialize the basic score desktop.
'** 6. Computer and players win sign initialization.
'** 7. Initialize all winning combinations.
'** 8. Reset the player's winning flag.
'**
'********************************************************** ********************************************** SUB INITPLAYENVIRONMENT ()
Player.FileName = ".music/zhyu01.mid"
Player.Play ()
TheplayFlag = TRUE
'
Label1.visible = false
'Game Status Tags are not displayed
Picturebox1.refresh ()
'Clearing the content of PictureBox1
Yuandian (130, 130)
'Call the drawing function to draw the current computer first walk
DIM I, J, M, N AS Integer
For i = 0 TO 9
For j = 0 to 9
Table (i, j) = 0
NEXT
NEXT
'Desktop initialization
For i = 0 to 191
Pflag (i) = true
Cflag (i) = true
NEXT
'Winning flag initialization
TABLE (4, 4) = 1
'Since we set up the computer first, it is set to 1 for 4, 4.
'' ******** Initialization winning combination ********
N = 0
For i = 0 TO 9
For j = 0 to 5
For m = 0 to 4
PWIN (J M, I, N) = TRUE
CWIN (J M, I, N) = TRUE
NEXT
n = n 1
NEXT
NEXT
For i = 0 TO 9
For j = 0 to 5
For m = 0 to 4
PWIN (i, j m, n) = true four, handling mouse event '***************************************** ***************************************************
'** Module Name: Themousedown
'**
'** Description: This function is mainly implemented:
'** 1. Judgment whether the current game flag is valid.
'** 2. Transform the actual coordinates into virtual coordinates.
'** 3. Draw a player's chess.
'** 4. Execute the check win function.
'** 5. Perform a computer algorithm function.
'**
'********************************************************** *****************************
Sub themousedown (byval x askER, BYVAL Y AS INTEGER)
IF theplayflag = false kil
EXIT SUB
END IF
'Check if the game status is effective
DIM I, J AS Integer
DIM ENX, 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 click on the lattice
DIM MyColor As Color
Dimg as system.drawing.graphics
g = picturebox1.creategraphics
mycolor = color.white
DIM brush1 as system.drawing.brush = new solidbrush (MyColor)
g.fillellipse (brush1, zhx * 30 10, zhy * 30
10, 30, 30
)
'Draw players' chess pieces
Table (zhx, zhy) = 2
For i = 0 to 191
IF CWIN (ENX, ZHY, I) = TRUE THEN
Cflag (i) = false
END IF
NEXT
'Re-set the winning sign
Checkwin ()
'Check if the current player wins
Diannao ()
'Call computer algorithm
End Sub V. Winning Check Algorithm. '********************************************************** *****************************
'** Module Name: Checkwin
'**
'** Description: This module performs the following features:
'** 1. Check if it is.
'** 2. Check if the computer wins.
'** 3. Check 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 kil
CNORMAL = CNORMAL 1
END IF
NEXT
IF cnormal = 190 THEN
Label1.visible = true
Label1.text = "and chess, please start again!"
Picturebox1.refresh ()
TheplayFlag = false
EXIT SUB
END IF
'Settings and chess rules
For i = 0 to 191
IF cflag (i) = true kil
CA = 0
For j = 0 to 9
Fork = 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
6. Computer algorithm (1)
'********************************************************** *****************************
'** Module Name: Diannao
'**
'** Description: This program mainly performs the following features:
'** 1. Initialization assignment system. '** 2. Assignment enhancement algorithm.
'** 3. Calculate the best attack position of your computer and players.
'** 4. Compare the best attack between the computer and the player and determine the best strategy of the computer.
'** 5. Execute the check win function.
'**
'********************************************************** *****************************
Sub Dianna ()
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
'Initialization assignment array
'' ******** Computer strengthening algorithm ********
For i = 0 to 191
IF cflag (i) = true kil
CAB = 0
For j = 0 to 9
Fork = 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 CAS 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 * 30 10, 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 kil
PAB = 0
For j = 0 to 9
Fork = 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
Six Computer Algorithm (2)
'' ******** Assignment system ******** for i = 0 to 191
IF cflag (i) = true kil
For j = 0 to 9
Fork = 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 kil
For j = 0 to 9
Fork = 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 ends ********
'' ******** Differential 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 kil
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 kil
PS = pscore (i, j)
c = i
D = J
END IF
NEXT
NEXT
IF CS> PS THEN
YUANDIAN (A * 30 10, 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