A wuziqi Algorithm

Source: Internet
Author: User
I learned what I wrote during algorithm analysis and design, mainly for self-entertainment. The algorithm uses a very small pruning game algorithm. Artificial Intelligence is still compatible, but there are a lot of bugs and there is time to change.

The design mainly includes: data structure, valuation function, winning and losing judgment, Search Algorithm

VC: http://goc.ac.cn/liuag/html/software_fivechess.html

1. Data Structure

// Record every step of the game. You can create a linked list for repentance and rollback (not implemented in this program)
Struct step
{
Int X, Y; // the coordinate of the pawn.
Int ball; // indicates the sub-party {black, white}
};

// Record the Board information for the search process
Class cboardsituation
{
Public:
Int narrboard [15] [15]; // board Condition
Struct step machinestep; // The step under AI
Long value; // the score of the disk.
};

// The current chessboard for display.
Int narrboard [15] [15];

2. Valuation Functions

Analyze the black and white sides of the current Board: five connections, four active, four active, three active, three sleeping, two active, two sleeping, then score the Board according to the rules of the five-game,
The specific score can be determined based on experience. In this program: five connections = 9999 (extreme value), four active values = 9990, four rushed = 9980, three active values = 9970, three active values plus 2000,
One active three plus 200, each sleeping three plus 10, each active two plus 4, each sleeping two plus 1. The score should also be based on the current chess player situation.

3. Victory and defeat judgment

This is relatively simple. check whether there are five consecutive pawns in four directions: horizontal, vertical, left oblique, and right oblique based on the final sub-condition.

4. Search Algorithms

The algorithm uses the extremely minimal value game algorithm. Its main idea is to predict the next n steps of playing chess and score the last n boards. It's your turn to play the game with the highest score.

The minimum value is selected for the peer account. Select what we think is the best as the next step. The basic idea of this program's algorithm is as follows (algorithm language Representation ):

Void dfai ()
{
Long value =-maxint; // value assigned to the initial Root Node

Cboardsituation currentboard;

// Obtain the new view information of the Current Machine
For (INT I = 0; I <15; I ++)
For (Int J = 0; j <15; J ++)
Currentboard. narrboard [I] [J] = narrboard [I] [J];
Currentboard. machinestep. Ball = computerball;
Currentboard. machinestep. x = gnrow;
Currentboard. machinestep. Y = gncolumn;
Currentboard. value = eveluate (currentboard. narrboard, black );

// Select the best methods (Greedy) --> countlist;
Getseveralgoodplace (effectboard, white );
Countlist. removeall ();

Position Pos = templist. getheadposition ();
For (Int J = 0; J {
Countlist. addtail (templist. getnext (POS ));
}

Pos = countlist. getheadposition ();
Cboardsituation * pboard;
// Perform further in-depth search on these disks
For (I = 0; I {
Pboard = & (countlist. getnext (POS ));
Pboard-> value = search (pboard, black, value, 0 );
Value = select (value, pboard-> value, white); // you can specify the maximum value.
}

// Return to the linked list Header
Pos = countlist. getheadposition ();
For (I = 0; I {
Pboard = & (countlist. getnext (POS ));
If (value = pboard-> value) // locate the disc with the highest score
{
Value = pboard-> value;
Gnrow = pboard-> machinestep. X;
Gncolumn = pboard-> machinestep. Y;
Bplayerdo = true; // the current sub-party is changed to a person
Break;
}
}
// Other processing
}

The search () function is as follows:
// Algorithm search function
Long Search (cboardsituation * board, int mode, long & oldvalue, int depth)
{
Clist m_deeplist;
Long value;
If (depthnarrboard, mode) <8000)
{
Value = (mode = white )? -Maxint: maxint;
// Select the best search targets
Getseveralgoodplace (board, mode );
Position Pos = templist. getheadposition ();
For (Int J = 0; J {
M_deeplist.addtail (templist. getnext (POS ));
}
Pos = m_deeplist.getheadposition ();
Cboardsituation successorboard;
For (INT I = 0; I {
Successorboard = m_deeplist.getnext (POS );
// Do You Want To Perform continuous deep search (pruning): The maximum minimum value method
If (mode = White & value <= oldvalue) | (mode = Black & value> = oldvalue ))
{
If (mode = white)
Value = select (value, search (& successorboard, black, value, depth + 1), White );
Else // mode = black
Value = select (value, search (& successorboard, white, value, depth + 1), black );
}
}
Return value;
}
Else // search end Condition
{
Return eveluate (board-> narrboard, mode); // score
}
Return 0;
}

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.