Old tune, using SDK to achieve intelligent square

xiaoxiao2021-03-06  67

There are many algorithms for achieving five daughters, such as using rule, recursive law, and game tree law to achieve five daughters. The last time I wrote an article using SDK to realize the maze algorithm. This time, the same topic, old tune , I use the rule method to achieve the intelligence of the five daughter. But I personally think that the game tree is still simple. If the reader is interested in the game tree, you can reread the implementation of the tree structure in the data structure! This is the use of the SDK implementation of the five-donor program run interface:

Here I talked about my achievement: The first step, calculate all the victory combinations of a board of five. In the second step, calculate the player's chess state, the computer will take offense or defense according to the player's state. In the third step, according to the operation of the second step, three results, players win, computers winning, and bureaus. In the first step, the combination of calculations From the figure, it can be seen from the figure that as long as the five chess pieces are continuous, it can be victorious, so that we can calculate all the victory combinations according to such rules, and use the combination operation to calculate a 10 * 10 There can be 192 victory combinations of chessboards. That is, the calculation is calculated, there are ten grids in each line, calculate the combination of continuous five grids, and there is no matter how four is continuous in 10, and each of the remaining six grids will be a continuous combination. Therefore, C6.1 = 6, a total of 6 kinds of victory combinations, 10 lines have a total of 60 victories, and there will be 60 victories in 10 columns, and the diagonal is calculated, starting from the positive diagonal, its combination The straight lines can have 10, 9, 8, 7, 6, 5, the remaining deficiency five grid, it is impossible to make a victory combination C (10 - 4) .1 = 6, c (9 - 4) .1 = 5 , C (8 - 4) .1 = 4, c (7 - 4) .1 = 3, c (6 - 4) .1 = 2, c (5 - 5) .1 = 1, due to symmetry, ie The angle has 6 (5 4 3 2 1) * 2 = 36, the opposition angle is also 36. All victory combines 36 * 2 60 * 2 = 192 victory combinations, all winners of players and computers will be in these combinations.

/ / Know all the victory combinations, we can define a group to record these combinations.

Bool BarrPlayerwin [10] [10] [192] = {false};

Bool Barrcomwin [10] [10] [192] = {false};

This is a three-dimensional array that records all the victories in the chessboard. If the player is in the position of the chessboard 1 line 1, there are three victory portions, as shown: Barrplayer [1] [1] [3] = TRUE , BarrPlayer [1] [1] [6] = true, barrplayer [1] [1] [9] = True If the computer puts a chess piece in the place labeled green, destroy this winning combination, players are not It may be won in this combination, so set its value to false, the computer can put the chess pieces here, and like, the player can destroy the computers of the computer. Calculate all the combinations of victory in the initwinstatus () function, these code is simple, so don't explain, please readers have understood these codes!

Voidinstatus ()

{

INT ncount = 0;

// set the vertical combinations winning status.for (int i = 0; i <10; i )

For (int J = 0; j <6; j )

{

For (int K = 0; k <5; k )

{

BarrPlayerwin [J K] [I] [NCOUNT] = TRUE;

Barrcomwin [J K] [I] [NCOUNT] = True;

}

NCOUNT ;

}

// Vertical Has 60 Winning Status.

Assert (ncount == 60);

// set The Horizontal Combinations Winning Status.

For (i = 0; i <10; i )

For (int J = 0; j <6; j )

{

For (int K = 0; k <5; k )

{

BarrPlayerwin [I] [J K] [NCOUNT] = True;

Barrcomwin [I] [J K] [NCOUNT] = TRUE

}

NCOUNT ;

}

// Horizontal Has 60 Winning Status

Assert (ncount == 120);

// set The Positive Diagonal Winning Status.

FOR (i = 0; i <6; i )

For (int J = 0; j <6; j )

{

For (int K = 0; k <5; k )

{

BarrPlayerwin [J K] [i K] [NCOUNT] = TRUE;

Barrcomwin [J K] [i K] [NCOUNT] = True;

}

NCOUNT ;

}

// Positive Diagonal Has 36 Winning Status.

Assert (ncount == 156);

// set The Negative Diagonal Winning Status.

FOR (i = 0; i <6; i )

For (int J = 9; J> 3; J -)

{

For (int K = 0; k <5; k )

{

BarrPlayerwin [J - K] [i K] [NCOUNT] = True;

Barrcomwin [J - K] [i K] [NCOUNT] = True;

}

NCOUNT ;

}

// NEGATIVE DIAGONAL HAS 36 WINNING STATUS.

Assert (ncount == 192);

// Who is the first?

IF (rand ()% 2 == 0)

BPLAYERDO = TRUE;

Else

BComputerDO = True;

}

In the second step, the computer calculates the player's chess state to take the attack or defensive strategy, which is the key to the five sorrows, which gives the pseudo code realized. / / Calculate the status of the player.

For (int i = 0; i <10; i )

For (int J = 0; j <10; j )

IF (Narrboard [I] [J] == NOBALL)

{

// Record the score of this position by using a variable.

NarrPlayergrades [i] [j] = 0;

For (int K = 0; k <192; k )

// This location is in the winning combination.

IF (BarrPlayerwin [i] [j] [k])

Switch (NARRWINNER [NPLAYER] [K])

// calculate the score based on the number of pieces placed in the combination.

The more the number of chess, the higher the score.

}

/ / Calculate the status of the computer, the code is the same.

.........

// judgment

IF (Narrcomgrades [i] [j] [j])

/ / May lose in the victory combination

attack;

Else

Defense;

In the third step, in the second step, you can stop the defense in the second step, you can have three results, the power, players, or the bureau.

For (int i = 0; i <= 1; i )

For (int J = 0; j <192; j )

{

IF (Narrwinner [i] [j] == 5)

IF (i == nplayer)

{

BPLAYERWIN = True;

Bover = TRUE;

Break;

}

Else

{

BComputerwin = True;

Bover = TRUE;

Break;

}

IF (bover)

Break;

}

Traverse all victory portfolios, if the combination has already five chess pieces, there is a game end! This is just simply explaining all the processes I implemented. All the source code of the five dumps in the attachment, you have modified these code, add functions. The conclusion can be learned from here, and the implementation of AI is to calculate all the circumstances, and then take corresponding measures according to the situation. However, the implementation here is limited to a simple five-child chess rule, which calculates all the victories. For complicated situations such as chess, Go, we still need to use a game tree to make all victory portions. I hope you will like this game! Code: http://www.vckbase.com/code/downloadcode.asp? Id = 2483

转载请注明原文地址:https://www.9cbs.com/read-112832.html

New Post(0)