简单五子棋算法——初级篇

简单五子棋算法——初级篇

前言

五子是中国古老的棋类之一,是老少咸宜的娱乐项目。也是人机博弈中最简单的一类,相较于围棋、象棋变化更少,算法实现起来就相对比较简单。

五子棋先手胜率理论上是百分之百的,因此在发展中逐渐出现了一些禁手规则来限制先手优势。但是这些都是对于职业棋手而言,对于普通玩家来说就不需要这么多的限制,简简单单即可。这里的算法也是如此,既然是简单五子棋,因此并不考虑那么多的限制。

设计思路

既然要设计算法,我们就要挖掘五子棋背后的原理。下棋都可以归类到博弈问题。二者博弈,就是一场利益争夺战,那么最终结果就看博弈双方谁能够获得最大的利益。

我们由浅入深的分析,首先下棋分为进攻和防守模式。例如此时我方已有三颗子连线,在下一颗就四颗连线了,这是进攻;又有对方三颗子连线时,我们要去阻止对方连成四颗子,这是防守。

根据进攻和防守的思路,我们需要权衡怎么下才能获得最大利益。一个简单的方法就是将利益量化为分数,根据每个位置落子的分数高低来权衡。

如此一来建立一个公正的评分制度就很重要。根据连子的数目和连子两侧有无对方落子设立评分表如下(X为敌子,O为我子,_为空位):

布局 无子 一子 二子 三子 四子 五子
二防 XX XOX XOOX XOOOX XOOOOX XOOOOOX
一防 X_ XO_ XOO_ XOOO_ XOOOO_ XOOOOO_
无防 _ O OO OOO OOOO OOOOO
分数 无子 一子 二子 三子 四子 五子
二防 0 0 0 0 0 10000
一防 0 0 20 100 500 10000
无防 0 20 100 500 2500 10000

如果有连子数大于五子,将按五子计算。
OK,我们可以开始写程序了。

算法实现

标准的五子棋一般是15*15的格子,因此先建立棋盘,并约定1代表黑子,-1代表白子。

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

根据上面的评分表,我们来写每个位置的评分程序:

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

//评分表
static const int Score[3]
  • 31
    点赞
  • 146
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于C语言的五子棋蒙特卡洛树搜索(MCTS)示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define BOARD_SIZE 15 #define MAX_PLAYS 225 #define UCB_C 1.96 typedef struct { int x; int y; } Point; typedef struct Node Node; struct Node { int wins; int plays; Point move; Node *parent; Node *children[MAX_PLAYS]; }; typedef struct { int board[BOARD_SIZE][BOARD_SIZE]; int next_player; } GameState; int opposite(int player) { return 3 - player; } int legal_moves(GameState *state, Point moves[]) { int count = 0; for (int x = 0; x < BOARD_SIZE; x++) { for (int y = 0; y < BOARD_SIZE; y++) { if (state->board[x][y] == 0) { moves[count].x = x; moves[count].y = y; count++; } } } return count; } int winner(GameState *state) { int lines[BOARD_SIZE][BOARD_SIZE][5][2] = {{{{0}}}}; int counts[3] = {0}; for (int x = 0; x < BOARD_SIZE; x++) { for (int y = 0; y < BOARD_SIZE; y++) { int p = state->board[x][y]; if (p == 0) { continue; } for (int i = 0; i < 5; i++) { if (x + i < BOARD_SIZE) { lines[x][y][i][0] = state->board[x + i][y]; } if (y + i < BOARD_SIZE) { lines[x][y][i][1] = state->board[x][y + i]; } } counts[p]++; } } if (counts[1] > counts[2] + 1) { return 2; } if (counts[2] > counts[1]) { return 1; } for (int x = 0; x < BOARD_SIZE; x++) { for (int y = 0; y < BOARD_SIZE; y++) { for (int i = 0; i < 5; i++) { int j; for (j = 0; j < 5; j++) { if (lines[x][y][j][0] != (i + j < 5 ? 0 : lines[x + j - i][y][i + j - 5][0])) { break; } } if (j == 5) { return lines[x][y][0][0]; } for (j = 0; j < 5; j++) { if (lines[x][y][j][1] != (i + j < 5 ? 0 : lines[x][y + j - i][i + j - 5][1])) { break; } } if (j == 5) { return lines[x][y][0][1]; } } } } if (counts[1] + counts[2] == BOARD_SIZE * BOARD_SIZE) { return 0; } return -1; } void play_move(GameState *state, Point move) { state->board[move.x][move.y] = state->next_player; state->next_player = opposite(state->next_player); } Node *new_node(Node *parent, Point move) { Node *node = (Node *) malloc(sizeof(Node)); node->wins = 0; node->plays = 0; node->move = move; node->parent = parent; for (int i = 0; i < MAX_PLAYS; i++) { node->children[i] = NULL; } return node; } void free_tree(Node *node) { for (int i = 0; i < MAX_PLAYS; i++) { if (node->children[i] != NULL) { free_tree(node->children[i]); } } free(node); } int random_playout(GameState *state) { GameState *copy = (GameState *) malloc(sizeof(GameState)); *copy = *state; int result = -1; while (result == -1) { Point moves[MAX_PLAYS]; int count = legal_moves(copy, moves); if (count == 0) { break; } Point move = moves[rand() % count]; play_move(copy, move); result = winner(copy); } free(copy); if (result == -1) { return 0; } if (result == 0) { return 1; } return result == 1 ? -1 : 1; } Node *best_child(Node *node) { double best_score = -1; Node *best_child = NULL; for (int i = 0; i < MAX_PLAYS; i++) { Node *child = node->children[i]; if (child == NULL) { continue; } double score = (double) child->wins / child->plays + UCB_C * sqrt(log(node->plays) / child->plays); if (score > best_score) { best_score = score; best_child = child; } } return best_child; } Node *tree_policy(GameState *state, Node *node) { while (winner(state) == -1) { Point moves[MAX_PLAYS]; int count = legal_moves(state, moves); if (count == 0) { break; } int unexplored = 0; for (int i = 0; i < count; i++) { Point move = moves[i]; int found = 0; for (int j = 0; j < MAX_PLAYS; j++) { if (node->children[j] != NULL && node->children[j]->move.x == move.x && node->children[j]->move.y == move.y) { node = node->children[j]; found = 1; break; } } if (!found) { unexplored = 1; break; } } if (unexplored) { Point move = moves[rand() % count]; node->children[node->plays] = new_node(node, move); node = node->children[node->plays]; } else { node = best_child(node); } play_move(state, node->move); node->plays++; } return node; } void backpropagate(Node *node, int result) { while (node != NULL) { node->wins += result; node->plays++; node = node->parent; result = -result; } } Point select_move(GameState *state, int iterations) { Node *root = new_node(NULL, (Point) {0, 0}); root->plays = 1; for (int i = 0; i < iterations; i++) { GameState *copy = (GameState *) malloc(sizeof(GameState)); *copy = *state; Node *node = tree_policy(copy, root); int result = random_playout(copy); backpropagate(node, result); free(copy); } double best_score = -1; Point best_move = (Point) {-1, -1}; for (int i = 0; i < MAX_PLAYS; i++) { Node *child = root->children[i]; if (child == NULL) { continue; } double score = (double) child->wins / child->plays; if (score > best_score) { best_score = score; best_move = child->move; } } free_tree(root); return best_move; } void print_board(GameState *state) { for (int y = 0; y < BOARD_SIZE; y++) { for (int x = 0; x < BOARD_SIZE; x++) { printf("%c", state->board[x][y] == 1 ? 'X' : state->board[x][y] == 2 ? 'O' : '-'); } printf("\n"); } } int main() { srand(time(NULL)); GameState state = {0}; while (winner(&state) == -1) { print_board(&state); if (state.next_player == 1) { Point move; printf("Enter move: "); scanf("%d %d", &move.x, &move.y); play_move(&state, move); } else { Point move = select_move(&state, 10000); printf("Computer plays: %d %d\n", move.x, move.y); play_move(&state, move); } } print_board(&state); int w = winner(&state); printf("%s wins\n", w == 0 ? "Draw" : w == 1 ? "X" : "O"); return 0; } ``` 该示例代码实现了五子棋蒙特卡洛树搜索,包括游戏状态表示、棋盘的打印、对局的进行(玩家输入和AI自动选择)、胜负判断等功能。其核心是通过蒙特卡洛树搜索来选择AI的下一步棋子位置,具体实现包括: - `new_node`:创建一个新的节点。 - `legal_moves`:获取当前状态下所有合法的落子位置。 - `winner`:判断当前状态下的胜负情况。 - `play_move`:在当前状态下落子。 - `random_playout`:执行一次随机模拟,返回胜负结果。 - `best_child`:找到当前节点中最优的子节点。 - `tree_policy`:根据当前状态和节点,选择下一个要扩展的节点,并返回最终的节点。 - `backpropagate`:在树中回溯更新节点的胜负统计信息。 - `select_move`:根据当前状态,执行多次蒙特卡洛树搜索,返回AI的落子位置。 该示例代码使用了UCB算法来计算节点选择的得分,即使用节点的胜率和置信度上限来决定节点的优先级。经过大量的模拟对局,蒙特卡洛树搜索可以搜索到更深的状态空间,从而提高AI的胜率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值