# A simple way to realize ai in man-machine combat with gobang game in c language

## A simple way to realize ai in man-machine combat with gobang game in c language

#### Idea:

First, select the color of the computer's chess pieces in the main function and call different functions.
When it's the computer's turn, the basic idea is to traverse every empty space on the chessboard, calculate the value one by one, find the position with the largest value, and pass this position back to the start game function to perform the computer's sub game.
Every time you find a gap, first check whether there are chessmen in the eight directions of up, down, left, right, upper left, lower left, upper right and lower right.
For example, if there are no pieces in the upper direction of the gap, the skip value is zero. If there are pieces in the lower left, continue to check several pieces, starting from whether there is a piece with the same color until there are four pieces, and accumulate the value one by one. Each time, judge whether the color of these pieces is the same as that of the computer itself. There are two cases of the same and different, and the value superimposed by them is different, Then judge whether there are pieces in the next position of the line composed of several pieces with the same color. There are three cases: the same color, different color and no pieces, and the superimposed value of the three is different.
In addition, in order to make the difference of value greater and easier to control, different weights will be added after judging different numbers of continuous pieces. The more the number, the weight will increase exponentially. In addition, in order to distinguish the two special cases of living three and even four, a great amount of value is added separately for them to facilitate computer judgment.

#### Code implementation:

1.AI function

```typedef struct computerchessposition
{
int x;
int y;
} comcp;//Record the position of the computer
```

The comcp structure is used to return the x and y coordinates of the position of the drop selected by the computer

```comcp* AI(comcp* head, int Chessboard[][15], int ln,int color) // Go through the chessboard and calculate the value if there is an empty point, and take the maximum value point
{
int  k, max = 0, x=0, y=0;

int i = 0, j = 0;

for (i = 0; i < ln; i++)
for (j = 0; j < ln; j++)
if (Chessboard[i][j] == SPA)
{      // Go through the chessboard and calculate the value if there is an empty point, and take the maximum value point.
k = calculatevalue(i, j, Chessboard, color);//Calculate the value of eight directions respectively
if (k >= max)
{
x = i;
y = j;
max = k;
}

}

}
```

comcp* AI(comcp* head, int Chessboard[][15], int ln,int color)； This function is used to calculate the maximum value of the chessboard

Description of parameters passed in:
comcp* head: the structure used to return the x and y coordinates of the position selected by the computer
Int chess board [] [15]: used to record the two-dimensional array of chessboard. 15 represents the size of chessboard 15x15
int ln: the size of the chessboard (the maximum value of the horizontal and vertical coordinates) (you can also pass it to 15)
int color: the color of the pieces held by the computer in this game (black is 1, white is - 1)

Return value Description:
return head;: Returns the structure comcp used to return the x and y coordinates of the drop position selected by the computer

2.calculatevalue function

```int calculatevalue(int p, int q, int Chessboard[][15],int color)//Calculate the value of eight directions respectively
{
int k = 0;
int check;
int spare;
k += checkpos(p, q, Chessboard, color, 0, -1);//Left
k += checkpos(p, q, Chessboard, color, 0, 1);//right
k += checkpos(p, q, Chessboard, color, -1, 0);//upper
k += checkpos(p, q, Chessboard, color, 1, 0);//lower
k += checkpos(p, q, Chessboard, color, -1, -1);//Upper left
k += checkpos(p, q, Chessboard, color, 1, -1);//Lower left
k += checkpos(p, q, Chessboard, color, -1, 1);//Upper right
k += checkpos(p, q, Chessboard, color, 1, 1);//lower right
return k;
}
```

int calculatevalue(int p, int q, int Chessboard[][15],int color) is used to calculate the values of eight directions respectively

Description of parameters passed in:
int p: x value traversed in AI function
int q: y value traversed in AI function
int Chessboard[][15]: the same as above. It is used to record the two-dimensional array of chessboard. 15 represents the size of chessboard 15x15
int color: the same as above, the color of the chess pieces held by the computer in this game (black is 1, white is - 1)

Return value Description:
return k: k is the cumulative value of the value of the traversed position, that is, the final value of the position

3.checkpos function

```int checkpos(int p, int q, int Chessboard[][15], int color, int pos_x, int pos_y)//How many subsystems are there in each direction
{
int k = 0;
int check;
int spare;
if (checkaround(p, q, Chessboard, pos_x, pos_y))//Judge whether the selected direction of each vacancy has adjacent sub positions
{
check = ai_judga_line(Chessboard, 15, p + pos_x, q + pos_y, pos_x, pos_y, 1, color);//Judge whether a child connected into a line is the same
k += checkline(1, check, p, q, Chessboard, color);//1

check = ai_judga_line(Chessboard, 15, p+ pos_x, q + pos_y, pos_x, pos_y, 2, color);//Judge whether the two children connected into a line are the same
k += checkline(2, check, p, q, Chessboard, color);//2

check = ai_judga_line(Chessboard, 15, p + pos_x, q + pos_y, pos_x, pos_y, 3, color);//Judge whether the three children connected into a line are the same
k += checkline(3, check, p, q, Chessboard, color);//3

check = ai_judga_line(Chessboard, 15, p + pos_x, q + pos_y, pos_x, pos_y, 4, color);//Judge whether the four children connected into a line are the same
k += checkline(4, check, p, q, Chessboard, color);//4

}
return k;
}
```

int checkpos(int p, int q, int Chessboard[][15], int color, int pos_x, int pos_y) this function is used to judge the number of adjacent sub nodes for each direction

Description of parameters passed in:
int p: same as above, x value traversed in AI function
int q: same as above, y value traversed in AI function
int Chessboard[][15]: the same as above. It is used to record the two-dimensional array of chessboard. 15 represents the size of chessboard 15x15
int color: the same as above, the color of the chess pieces held by the computer in this game (black is 1, white is - 1)
int pos_x: Ordinate used to represent direction
int pos_y: Abscissa used to indicate direction
For example, (0, - 1) is left and (- 1, 1) is upper right

Return value Description:
return k: k is the cumulative value of the value of the traversed position

4.checkaround function

```int checkaround(int p, int q, int Chessboard[][15], int pos_p, int pos_q)//Judge whether the selected direction of each vacancy has adjacent sub positions
{
if (Chessboard[p + pos_p][q + pos_q] != 0)
{
return 1;
}
return 0;
}
```

int checkaround(int p, int q, int Chessboard[][15], int pos_p, int pos_q) is a function used to judge whether there are adjacent children in the selected direction of each vacancy

Description of parameters passed in:
int p: same as above, x value traversed in AI function
int q: same as above, y value traversed in AI function
int Chessboard[][15]: the same as above. It is used to record the two-dimensional array of chessboard. 15 represents the size of chessboard 15x15
int pos_x: Same as above, used to indicate the ordinate of the direction
int pos_y: Same as above, abscissa used to indicate direction
For example, (0, - 1) is left and (- 1, 1) is upper right

Return value Description:
return 1 if there are pieces around, otherwise return 0

5.ai_judga_line function

```int ai_judga_line(int Chessboard[][15], int ln, int XS, int YS, int dx, int dy,int num,int color)//Judge whether the num children connected into a line are the same
{
assert((Chessboard != NULL) && (ln > 0));
if ((XS < ln) && (YS < ln) && (XS >= 0) && (YS >= 0) && (dx != 0 || dy != 0))        //The starting point coordinate is in the chessboard / / the coordinate increment is not 0 at the same time
{

if (((XS + dx * (num-1)) > ln) || ((XS + dx * (num - 1)) < 0) || ((YS + dy * (num - 1)) > ln) || ((YS + dy * (num - 1)) < 0) || (0 == Chessboard[XS][YS]))//Determine the end coordinates / / outside the chessboard
{
return 0;  //Not in the chessboard, or the starting point is no son
}
else
{
int i = 0;
for (i = 1; i < num; ++i)
{
if (Chessboard[XS][YS] != Chessboard[XS + (dx * i)][YS + (dy * i)])
{
return 0;  //If it's not consecutive num, it's the same
}//end if3
}//end for1

if (Chessboard[XS][YS] != color)
{
return 1;  //num pieces are all opponent's pieces and are all on the chessboard
}
if (Chessboard[XS][YS] == color)
{
return 2;  //num pieces are all their own pieces and are all on the chessboard
}

}//end if 2
}
return 0;  //Other situations
}
```

int ai_judga_line(int Chessboard[][15], int ln, int XS, int YS, int dx, int dy,int num,int color) this function is used to judge whether the num children connected into a line are the same

Description of parameters passed in:
int Chessboard[][15]: the same as above. It is used to record the two-dimensional array of chessboard. 15 represents the size of chessboard 15x15
int ln: the same as above, the size of the chessboard (the maximum value of the abscissa and ordinate) (also pass 15)
int XS: equivalent to p+ pos_x. The position of a piece in a certain direction around the traversed position
int YS: equivalent to q + pos_y. The position of a piece in a certain direction around the traversed position
int dx: int pos_x: Ordinate used to represent direction
int dy: int pos_y: Abscissa used to indicate direction
int num: judge whether the num children connected into a line are the same
int color: the same as above, the color of the chess pieces held by the computer in this game (black is 1, white is - 1)

Return value Description:
If the same pieces are connected into num pieces in this direction, return 1, otherwise return 0

6.checkline function

```int checkline(int linenum,int check,int p, int q, int Chessboard[][15], int color)//Calculate the value according to the number of fallen seeds
{
int num = linenum + 1;
int k = 0;
int value=1;
for (; linenum > 0; linenum--)//Weighted weight
{
value = value * 10;
}

if (check)
{
k += value;
if (check == 1)//Opponent's chess pieces
{
k += 20;
}
if (check == 2)//Own chess pieces
{
k += 10;
}
if (q - num >= 0)//The next position of these connected pieces is still in the chessboard
{
if (Chessboard[p][q - num] == 0)//The next position is empty
{
k += 10;
if (linenum == 3)//Live three
{
k += 10000000;
}
}
if (Chessboard[p][q - num] == color)//The next position is your own chess piece
{
if (linenum == 4)//Four in a row
{
k += 100000000;
}

}
if (Chessboard[p][q - num] == (0 - color))//The next position is the opponent's chess piece
{
k += 20;
}
}

}
return k;
}
```

int checkline(int linenum,int check,int p, int q, int Chessboard[][15], int color) is a function used to calculate the value according to the number of falling seeds

Description of parameters passed in:
int linenum: pieces of the same color connected into linenum
int check: if there are pieces in this direction, it is 1, otherwise it is 0
int p: same as above, x value traversed in AI function
int q: same as above, y value traversed in AI function
int Chessboard[][15]: the same as above. It is used to record the two-dimensional array of chessboard. 15 represents the size of chessboard 15x15
int color: the same as above, the color of the chess pieces held by the computer in this game (black is 1, white is - 1)

Return value Description:
return k: k is the cumulative value of the value of the traversed position

#### Last words:

This is a piece of code in the homework assigned by the teacher when I just came into contact with c language. Due to my inexperience, the naming of functions and variables needs to be improved. And because I still have many questions about how to determine the weighted weight in artificial intelligence, the weight added in this code is not perfect. Please forgive me

Tags: C Game Development

Posted by jdc44 on Tue, 17 May 2022 10:34:32 +0300