This paper describes an example of VC to realize Gobang game, the algorithm uses Minimax pruning game algorithm, interested readers can modify and improve the imperfect part of the program.
This design mainly includes: data structure, valuation function, victory and defeat judgment, search algorithm
The program running interface is as follows:
The following steps are implemented:
1. Data structure
Record each move, you can establish a linked list for Undo, back (this procedure is not implemented)
struct step
{
int x,y;//Pawn coordinates
int ball;
;
Record board situation, for search process
class cboardsituation
{public
:
int narrboard[15][15];//chessboard situation
struct step Machinestep; AI is the next step
Long value;//disk hit the score
};
Current chessboard, for checkerboard display
int narrboard[15][15];
2. Valuation function
Analysis of the current chessboard black and white on both sides of the chess type: Five companies, live four, four, two live three, single live three, sleep three, live two, sleep two, and then according to Gobang rules to the board scoring,
The specific score can be determined by experience. In this procedure: five even =9999 (extreme value), live four = 9990, punching four = 9980, double live three = 9970, live three plus 2000,
Single live three plus 200, each sleep three plus 10, each live two plus 4, each sleep two plus 1. The scoring is also based on the current chess players to score.
3, Victory and defeat judgment
This is relatively simple, according to the final drop situation from the horizontal, vertical, left oblique, right oblique four direction check whether there are five consecutive pieces can be.
4. Search algorithm
The
algorithm uses the maximum minimum game algorithm, its main idea is: the prediction of the N-Step chess situation, the prediction of the latter N board scoring. When it's time to play chess, you pick the biggest, and your turn is the smallest. Choose what we think is best as the next step. This procedure algorithm basic idea is as follows (algorithm language representation):
void Dfai () {long value=-maxint;//value Assignment Cboardsituation Currentboard to the initial root node;
Gets the current machine new chess face condition 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 some of the best dismount (greedy method)-->countlist;
Getseveralgoodplace (¤tboard,white);
Countlist.removeall (); POSITION pos=templist.
GetHeadPosition (); for (int j=0;j {countlist.addtail (templist).
GetNext (POS));
} pos=countlist.getheadposition ();
Cboardsituation *pboard;
Make further depth searches for these disks for (I=0;i {pboard= & (POS));
Pboard->value=search (pboard,black,value,0); Value=select (Value,pboard->value,white);
Find the maximum score}//Back to the pos=countlist.getheadposition ();
For (I=0;i {pboard= & (Countlist.getnext (POS));
if (value==pboard->value)//Find the disk with the highest score {value=pboard->value; Gnrow=pboard->machinestep.x;
gncolumn=pboard->machinestep.y; Bplayerdo=true;
The current side changed to the person break; and//Other processing} The search () function is as follows://algorithm search function Long search (cboardsituation *board,int mode,long &oldvalue, int depth) {CLIs
T m_deeplist;
Long value;
if (Depthnarrboard,mode)) <8000) {value= (mode==white)?-maxint:maxint;
Select several of the best search target 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 continue depth search (pruning): Minimax 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);//Chess face rating} return 0; }