Backgammon simple algorithm - primary articles

Backgammon simple algorithm - primary articles

Foreword

Five children is one of the ancient Chinese chess, it is entertainment for all ages. Man-machine game is the easiest category, compared to Go, Chess less change, the algorithm is relatively simple to implement.

Backgammon just get one hundred percent winning percentage in theory, so there have been some gradual ban on hand in the development of rules to limit the upper hand advantage. But these are for professional players, for the average player does not need so many restrictions, can be simple. An algorithm is here, too, since it is simple backgammon, therefore, not so much to consider restrictions.

Design ideas

As to design algorithms, we have principles behind backgammon mining. Chess game can be classified into question. Game two is that the interests of a battle, then the final result of the game to see who can get the most out of both sides benefit.

Deep analysis of our first chess divided into offensive and defensive mode. For example, at this time we have three sub-connection, connect the next one to four, which is offensive; there when the other three sub-connection, we're going to stop the four sub-Fang Liancheng, which is defensive .

According to the offensive and defensive way of thinking, we need to weigh how to get the maximum benefit under. A simple method is to quantify the benefits as a fraction, according to each position to weigh Lazi high and low scores.

Thus the establishment of a fair grading system is very important. Depending on the number and son, and son, and both sides establish whether the other party Lazi score sheet as follows (X enemies son, O my son, _ for the vacancy):

layout No child child twin Three sons Four sons Five children
Two anti XX XOX compete XOOOX XOOOOX XOOOOOX
A proof X_ XO_ XOO_ XOOO_ XOOOO_ XOOOOO_
No proof _ O AND OOO OOOO ooooo
fraction No child child twin Three sons Four sons Five children
Two anti 0 0 0 0 0 10000
A proof 0 0 20 100 500 10000
No proof 0 20 100 500 2500 10000

Even if the number is greater than five sub-sub, five children will be calculated.
OK, we can start writing the program.

Algorithm

Standard backgammon is generally 15 * 15 lattice, so first establish a board, and the agreement on behalf of sunspots 1, -1 albino.

vector<vector<int>> topo(15, vector<int>(15, 0));

According to the above score table, we have to write a program rating for each position:

//米字型搜索
//[—, | , / , \]四个移动方向
static const int inX[] = { 1,0,1,1 };
static const int inY[] = { 0,1,1,-1 };

//评分表
static const int Score[3][6] = {
	{ 0, 0,  0,  0,   0,10000 },//防守2子
    { 0, 0, 20,100, 500,10000 },//防守1子
    { 0,20,100,500,2500,10000 } //防守0子
};

//topo:棋盘
//x,y:棋盘位置
//color:落子颜色(黑色:1;白色:-1)
int getScore(vector<vector<int>> topo,
             const int x, const int y,
             const int color) {
	//返回评分值
	int re = 0;

	//向 [—,|, /, \]四个方向搜索,对应inX,inY
	for (int i = 0; i < 4; ++i) {
		//k记录连子两侧空位的数目(非墙,非敌方落子)
		int k = 0;
		//记录连子的数目,初始值为1因为假设在当前位置落子
		int count = 1;

		//[—,|, /, \]四个方向的正负方向
		for (int j = -1; j < 2; j += 2) {
			int dx = x + j * inX[i];
			int dy = y + j * inY[i];

			//判断是否超出棋盘边界
			while (dx >= 0 && dx < topo.size() &&
			       dy >= 0 && dy < topo[0].size()) {
				//假如遇到颜色相同的子,count+1,反之则退出循环,并判断此时有无空位
				if (color*topo[dx][dy] > 0) {
					++count;
				}
				else {
					if (color*topo[dx][dy] == 0) ++k;
					break;
				}

				dx += j * inX[i];
				dy += j * inY[i];
			}
		}

		//假如连子大于5,使之等于5
		if (count > 5) count = 5;

		//加上该方向所得评分
		re += Score[k][count];
	}

	return re;
}

Scoring function written, we began to design AI players, according to previous thinking, we only need to traverse all empty, and then find the maximum points to obtain benefits. as follows:

void AiChesser(int color,
	           int &x, int &y, 
	           vector<vector<int>> topo) {
	int max = INT_MIN;
	vector<int> X;
	vector<int> Y;

	for (int i = 0; i < topo.size(); ++i) {
		for (int j = 0; j < topo[i].size(); ++j) {
			//判断是否为空位
			if (topo[i][j] == 0) {
				//进攻还是防守,寻找利益最大值
				int score = max(getScore(topo, i, j, color),
				                getScore(topo, i, j, -color));

				//以防有多个相同的最大值
				if (score >= max) {
					if (score > max) {
						X.clear();
						Y.clear();
						max = score;
					}
					X.push_back(i);
					Y.push_back(j);
				}
			}
		}
	}

	//在最大值中任取一个返回,大部分情况只有一个
	int r = rand() % X.size();
	x = X[r];
	y = Y[r];
}

After words

Of course, this stuff is not a program also can run specific UI design is a matter of opinion, but this is mainly explained algorithm. Easyx library design provides a complete backgammon project as a reference to attach executable exe file.

Here Insert Picture Description

This algorithm in fact only reached the level of junior players, but enough to ordinary people and a fight.
There are many places still worth algorithm optimized for code easier to understand, in many parts of simple design.

Advanced Design

The design of our AI is fairly simple, but the real master can not only see the eyes, they tend to step down is to step back a lot of services. Therefore, we hope to further improve our algorithm, it will need to consider a lot of steps from the back, and not only see the immediate step. Here is necessary to introduce Minimax algorithm and the alpha-beta pruning algorithm, I will explain these in a future article.

Published 63 original articles · won praise 73 · views 70000 +

Guess you like

Origin blog.csdn.net/jjwwwww/article/details/84593137