Vb. NET realizes Gobang Artificial intelligence (1)

Source: Internet
Author: User
Tags define array integer reset valid
Ai is the so-called AI (Artificial Intelligence), it is a very abstract technology, AI program writing does not need to be based on any established thinking mode or rules. In particular, the AI in the game can be made entirely according to the logic of the programmer's own thinking. I personally think that the core of AI should be to enable computers to have automatic handling of events, and all of our research should be centered in this direction. What we're talking about today is the strategy-class AI.

Strategy class AI can be said to be a more complex AI, the most common strategy-type AI game is a chessboard game. In this type of game, the usual strategy AI program is to make the computer judge all the available chess and possible wins in the current situation, and calculate the winning score for the moves step or the player can moves step, and then decide on the best way to go. Let's first introduce the AI idea of Gobang.

The AI conception of Gobang

There is a saying "lookers-on, the spectators." ", but this phrase is not tenable on computer players controlled by AI, because the computer must know the winning ways and calculate the winning probability of each next move to the chessboard, that is, a complete Gobang AI concept must:
 
1, can know all the winning combination;

2, the establishment and use of the winning table;

3, set the winning score;

4, so that the computer has the ability to attack and defense;

One, to seek the winning combination of Gobang

In a Gobang game, the computer must know the winning combination of those, so we have to get the total number of winning combinations. We assume that the current chessboard is 10*10.

(1) Calculate the horizontal direction of the winning combination number, each column of the winning combination is: 6, a total of 10 columns, so the horizontal direction of the winning combination number: 6*10=60

(2) Calculate the total number of wins in the vertical direction, the winning combination of each row is: 6, a total of 10 rows, then the vertical direction of the winning combination number: 6*10=60

(3) To calculate the total number of winning combinations in the positive diagonal direction, the total number of winning combinations on the diagonal is 6+ (5+4+3+2+1) *2=36

(4) Calculate the total number of winning combinations in the inverse diagonal direction, the total number of winning combinations on the inverse diagonal is 6+ (5+4+3+2+1) *2=36, so that all the winning combinations are: 60+60+36+36=192

Ii. establishment and use of the winning form

We have calculated that a 10*10 Gobang disk will have 192 winning ways, so we can use the array to build the winning table, the main function of the winning table is: 1, to determine whether the current winning mode is effective, 2, to determine how many of the current winning methods in the winning combination. The detailed use you will see in the later program.

Third, the setting of the score

In the game in order for the computer to be able to determine the next best way to go, you must first calculate the computer down to the board of a space on the score, and the highest score is the computer next best way to go.

Principle: We determine the current discussion of the space and the current discussion points have several ways to win, there are several of these spaces add a few. It doesn't sound like it's going to start at first, it doesn't matter, you'll understand the decision principle when you understand the program behind us.

There are some drawbacks to this decision, because if the computer or player has three of them in the first place, the computer will not be able to determine that some other space is the best position for the current win without attacking or defending it. It's okay. We can completely change the current score by a reinforcement algorithm, that is to say, when a computer or a player has three or four children connected to a line, we can make up for this shortcoming by enhancing the algorithm to increase the current value of the spaces associated with three or four children.

Iv. Attack and defense

The above way, in fact, the computer only calculates the best attack position, in order to defend we should also calculate the current player's best attack position. What's the use of that? The reason is very simple, if the player's best attack position score is greater than the computer's best attack position on the score, then the computer will be the next piece in the player's best attack position to prevent the player's attack, or the computer will be under the chess pieces in their own best attack position to attack.

In fact, this AI idea is very powerful if you are not a great gobang, you may soon be defeated by the computer. I am a mid-level chess player in the audience, and the odds are not very high when playing with this idea.

Write Gobang using vb.net

I. Preparation BEFORE preparation:

1. Describe the whole process of chess with the thought of computer

Consider the steps:

(1) In order to be simple, we can first move the computer first, the computer every step will be sealed off a lot of players to win the possible situation.

(2) When the player moves, we should first consider the legality of the player moves.

(3) If it is legal, the player will also be able to seal off many of the computer's chances of winning.

(4) The computer's thinking path: First to determine whether the current player and computer all the winning combinations need to be enhanced assignment,
is to make an enhanced assignment, otherwise the normal assignment.

(5) Compare the current player and computer who have the biggest score. Take the point of the most points as the next step of the computer.

2, the use of vb.net forms and graphics tools to build Gobang board interface

(1) Add a PictureBox control

Role: Use the PictureBox control to draw pieces and a chessboard

(2) Add a Label control

Function: Displays the current winning sign, that is, when a party wins or draw the label.

(3) Add a MainMenu control

Function: To control the beginning or end of a game

(4) Add a Mediaplay component

Function: Enables programs to play music.

3, set the overall box price

We take the 10*10 board as the main platform. Use an array to define the entire board table, using an array to define the winning combination and the winning logo.

Two, declaring global arrays and variables

To define a virtual desktop:

Dim Table (9, 9) as Integer

Defines the fraction of the current player's desktop space:

Dim Pscore (9, 9) as Integer

Define the fraction of the current computer desktop space:

Dim Cscore (9, 9) as Integer

Define the player's winning combination:

Dim Pwin (9, 9, 191) as Boolean

Define the winning combination of the computer:

Dim Cwin (9, 9, 191) as Boolean

Define the player's winning combination flag:

Dim Pflag (191) as Boolean

Define a computer's winning combination flag:
Dim Cflag (191) as Boolean

Define Game Valid Flags:

Dim Theplayflag as Boolean

Third, initialization of the game

'*****************************************************************************
' * * Module name: initplayenvironment
'**
' * * Description: This function has the following main functions:
' * * 1. Set up background music.
' * * 2. Setting the game state is valid.
' * * 3. Initializes the Game Status tab.
' * * 4. Directly specify the first step of the computer to go.
' * * 5. Initializes the basic scoring desktop.
' * * 6. Computer and player WINS flag initialization.
' * * 7. Initializes all winning combinations.
' * * 8. Reset the player's winning logo.
'**
'*****************************************************************************
Sub initplayenvironment ()
Player. FileName = ". \music\zhyu01.mid"
Player. Play ()
Theplayflag = True
' Game effective
Label1.visible = False
' Game Status tab does not show
Picturebox1.refresh ()
' Empty the contents of the PictureBox1
Yuandian (130, 130)
' Call the drawing function to draw the current computer's first position
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
' Win sign initialization
Table (4, 4) = 1
' Because we set the computer to the first tempo, and down 4, 4 digits, so set its value to 1
"' ******** initialize the 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
Cwin (i, j + M, n) = True
Next
n = n + 1
Next
Next
For i = 0 to 5
For j = 0 to 5
For m = 0 to 4
Pwin (j + m, i + M, n) = True
Cwin (j + m, i + M, n) = True
Next
n = n + 1
Next
Next
For i = 0 to 5
For j = 9 to 4 Step-1
For m = 0 to 4
Pwin (j-m, i + M, n) = True
Cwin (j-m, i + M, n) = True
Next
n = n + 1
Next
Next
"' ******** initialization wins combination end ********
For i = 0 to 191
If Pwin (4, 4, i) = True Then
PFLAG (i) = False
End If
Next
' Because the computer has been down 4, 4, so we need to reset the player's winning logo
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.