Five son-player interaction
The program is divided into two parts. Core program 2. Windows graphical interface ç
This is only introduced here. Below, we look at the side.
/ / ======== Define file part =============================
#define max_length 19 // Chess board
#define computer 1 // Computer chess pieces
#define Player 2 // Player chess pieces
INT QP [MAX_LENGTH] [MAX_LENGTH]; / / Define Checkete
Int iswin; // lose winning mark, 1 = computer win, 2 = players win, 0 = unblocated;
#define randint (n) (rand ()% n) // random number macro
Struct List
{
Int ID;
Struct List * Next;
} * PList = null, * clist = null; // Define the list, PLIST is the player chess link list pointer, used to block, the clist is the computer chess link list pointer for attack
/ / ========= core program code part =========================
/ / This function adds data add to the list and sequencing the data from large to small
// Add low as a checkerboard location, the high position represents the priority in the current chess game
Static int AddList (struct list ** list, int appl)
{
Struct List * TMP = * list;
Struct List ** temp = list;
INT ATEMP = Add >> 16;
Int ID;
Struct List * newlist = malloc (SIZEOF (* newlist));
IF (! newlist)
Return 1;
While (TMP)
{
ID = (TMP-> ID) >> 16;
IF (id <= ATEMP)
Break;
IF (id> ATEMP)
{
Temp = & (TMP-> Next);
TMP = TMP-> Next;
}
}
NEWLIST-> ID = add;
NEWLIST-> next = TMP;
* TEMP = NewList;
Return 0;
}
/ / Function Gets the value of the maximum priority in the specified chain
Static int getmax (struct list * list)
{
IF (list)
Return (List-> ID >> 16);
Return 0;
}
// Function gets the link header data in the specified chain
Static int getLast (struct list ** list)
{
IF (* list)
{
int R;
Struct List * TEMP;
RET = (* list) -> ID & 0xfffff); // Take a low byte board position data
Temp = * list;
* List = (* list) -> next;
Free (TEMP);
Return Ret;
}
Return 0;
}
#define destroylist (l) while (getLast (& l)) // macro - destruction
// Function detects the priority of the board position specified by the parameter TMP and adds the corresponding linked list.
// Any point to the function scanned, all of which are empty positions on the board. We will analyze the position in the player game here.
// TMP = y * max_length x
Static int AddTTO_LIST (int TMP)
{
Int temp;
INT PY, PX;
PY = TMP / MAX_LENGTH;
PX = TMP% max_length;
// Detect the priority of the computer in this location
QP [py] [px] = computer;
Temp = scan (px, py, 0); // The last parameter must be 0, otherwise it will enter the dead cycle.
AddList (& Clist, (Temp << 16) TMP);
// Detect the priority of players in this location
QP [PY] [PX] = Player;
Temp = scan (px, py, 0); //.
AddList (& PLIST, (Temp << 16) TMP);
QP [py] [px] = 0; // Restore the annual state.
Return 0;
}
// Function format all-round scan on the specified chessboard position and returns the priority of this location in the chess game
// Format scan can reduce scanning redundancy and improve scan efficiency. This is a big board, such as 19 * 19
Particularly applicable.
// Mode control scanning method, 0 = Testive scan, does not add the scan results to the list. 1 = actual scanning, scanning
Join the list. The program can use the data in the linked list to perform the decision of the next chess.
// PX, PY is repeatedly used in the loop, we add a Const tag to prevent unnecessary trouble.
Static Int Scan (Const Int PX, Const Int Py, INT MODE)
{
Register INT i;
INT play = qp [py] [px]; // Get this position
INT RET = 0; RTEMP = 0; // Return value
Int Temp [4];
Int base;
Base = randint (8); // Generate random number, use different starting points, make the chess game with certain randomness
/ / Scan for eight directions of the chess
For (i = 0; i <8; i )
{
INT x = PX;
INT Y = Py;
INT SIDE = 0; // Boundary marker
Register J;
Switch ((Base I)% 8)
{
Case 0: // x -
{
/ / Format, first position to the first position that is not the same as Play.
// This way, in this direction, we can only consider the position of the four-grained chessboard, which greatly reduces unnecessary overhead.
DO
{
X-;
} while (x> = 0 && QP [y] == play);
// This direction has no sufficient position in front, and the marking is 1. In this way, we can ineffective detection.
IF (x 5> max_length)
{
SIDE = 1;
}
Else
{
X = 2; // In this direction, skip the first position equal to Play, record the four positions in front
For (j = 0; j <4; j )
{
Temp [j] = (max_length * y x); // Save the board position synthesis integration data
X ;
}
}
Break;
}
Case 1: // x--; y -;
{
DO
{
X-;
Y--
} while (x> = 0 && y> = 0 && QP [y] [x] == play);
IF (x 5> max_length || y 5> max_length)
{
SIDE = 1;
}
Else
{
X = 2;
Y = 2;
For (j = 0; j <4; j )
{
Temp [J] = (MAX_LENGTH * Y X);
X ;
y ;
}
}
Break;
}
Case 2: // y -;
{
DO
{
Y--
WHILE (Y> = 0 && QP [Y] == Play;
IF (Y 5> max_length)
{
SIDE = 1;
}
Else
{
Y = 2;
For (j = 0; j <4; j )
{
Temp [J] = (MAX_LENGTH * Y X);
y ;
}
}
Break;
}
Case 3: // x--; y ;
{
DO
{
X-;
y ;
} while (x> = 0 && y IF (x 5> max_length || y-5 <0) { SIDE = 1; } Else { X = 2; Y- = 2; For (j = 0; j <4; j ) { Temp [J] = (MAX_LENGTH * Y X); X ; Y-- } } Break; } Case 4: // x ; { DO { X ; WHILE (x IF (x-5 <0) { SIDE = 1; } Else { X- = 2; For (j = 0; j <4; j ) { Temp [J] = (MAX_LENGTH * Y X); X-; } } Break; } Case 5: // x ; y -; { DO { X ; Y-- WHILE (x IF (x-5 <0 || y 5> max_length) { SIDE = 1; } Else { X- = 2; Y = 2; For (j = 0; j <4; j ) { Temp [J] = (MAX_LENGTH * Y X); X-; y ; } } Break; } Case 6: // x ; y ; { DO { X ; y ; WHILE (x IF (x-5 <0 || Y-5 <0) { SIDE = 1; } Else { X- = 2; Y- = 2; For (j = 0; j <4; j ) { Temp [J] = (MAX_LENGTH * Y X); X-; Y-- } } Break; } Case 7: // y ; { DO { y ; WHILE (Y IF (Y-5 <0) { SIDE = 1; } Else { Y- = 2; For (j = 0; j <4; j ) { Temp [J] = (MAX_LENGTH * Y X); Y-- } } Break; } } IF (! Side) { / / This section is a key portion of the priority. This example only uses a simple priority determination scheme, which can make the program more intelligent by modifying the part. INT T = 0; INT K [4] = {8, 4, 2, 1}; INT KT [16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; / / Test the record of the record For (j = 0; j <4; j ) { INT jx = temp [j]% max_length; INT JY = Temp [J] / Max_Length INT P = QP [JY] [jx]; IF (p == play) T = K [J]; ELSE IF (P == 0) // Available location { IF (Mode) AddTo_List (Temp [J]); // Detection record on the available position } ELSE // There is an barrier { T -; // It is possible to make T = -1, because t is used for array subscripts, so we must exclude it. IF (t <0) T = 0; Break; } } IF (t == 0xf && mode) // If the five sons are connected together, return zero Return 0; IF (RET } Return (RET RTEMP); // Current situation} // Initialize the board Void Initqp (Void) { Register INT i; Register Int J; For (i = 0; i For (j = 0; j QP [i] [j] = 0; ISWIN = 0; SRAND (Time (NULL)); // Initialization Random Generator } / / The main function, detect the position of the player, return to the computer where the computer is located // PX, PY represents the player's piece of play. INT FIVE (int PX, int Py) { Struct List ** list; Int TMP; IF (qp [py] [px]! = 0) // This location already exists { Return -1; } // Scan the player's chess pieces QP [PY] [PX] = Player; IF (! SCAN (PX, PY, 1)) { // players win DESTROYLIST (PLIST); DESTROYLIST (CLIST); ISWIN = Player; Return 0; } IF (GetMax (PLIST)> getMax (CLIST)) / / Determine Attack List = & plist; // defense Else List = & clist; // attack While (tmp = getlast (list)) // Get the maximum priority board position { PX = TMP% max_length; PY = TMP / MAX_LENGTH; IF (QP [Py] [PX]! = 0) // This location does not continue to loop { IF (GetMax (PLIST)> getmax (clist)) / / re-determine the attack List = & plist; Else List = & clist; CONTINUE; } Break; } If (! tmp) // does not find data in the linked list to generate random data { DO { PX = randint (MAX_LENGTH); PY = randint; MAX_LENGTH; } while (QP [py] [px]! = 0); } // Computer walk QP [py] [px] = computer; IF (! SCAN (PX, PY, 1)) { // Computer win DESTROYLIST (PLIST); DESTROYLIST (CLIST); Iswin = computer; } Return ((py * max_length) px); // Return to walk position } The core program part is so much, the graphical interface part only needs to call the function initqp () to initialize the board, and then constantly call the function FIVE (INT PX, int Py).