C++ 黑白棋AI minimax+alphabeta剪枝
没事写着玩玩,通过debian上的黑白棋测试,搜了10层,打hard应该问题不大
#include <cstdio>
#include <cstring>
using namespace std;
template <typename T> T min(const T &x, const T &y) { return x < y ? x : y; }
template <typename T> T max(const T &x, const T &y) { return x > y ? x : y; }
const int weight[10][10] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 65, -3, 6, 4, 4, 6, -3, 65, 0,
0, -3, -29, 3, 1, 1, 3, -29, -3, 0,
0, 6, 3, 5, 3, 3, 5, 3, 6, 0,
0, 4, 1, 3, 1, 1, 3, 1, 4, 0,
0, 4, 1, 3, 1, 1, 3, 1, 4, 0,
0, 6, 3, 5, 3, 3, 5, 3, 6, 0,
0, -3, -29, 3, 1, 1, 3, -29, -3, 0,
0, 65, -3, 6, 4, 4, 6, -3, 65, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};//苟的debian黑白棋AI的权值表
const int d[8][2] =
{
-1, 0, 1, 0, 0, -1, 0, 1,
-1, -1, -1, 1, 1, -1, 1, 1
};
#define re register
int state[10][10], ansx, ansy;
//black:1 white:-1
int cur[10][10];
inline int alpha_beta(int dep, int player, int alpha, int beta, int cnt) //0 for me 1 for it
{
if(dep > 9 || cnt > 64) //dep是搜索的层数
{
int ret = 0;
for(re int i = 1; i <= 8; ++i)
for(re int j = 1; j <= 8; ++j)
ret += weight[i][j]*state[i][j];
return ret;
}
int temp[10][10], tot = 0;
memcpy(temp, state, sizeof state);
if(!player)
for(re int id = 1, x, y, i, j; id <= 64; ++id)
{
i = (id-1>>3)+1, j = (id-1)%8+1;
if(state[i][j] == 0)
{
int flag = 0, t;
for(re int k = 0; k < 8; ++k)
{
t = 0, x = i+d[k][0], y = j+d[k][1];
while(state[x][y] == -1) t++, x += d[k][0], y += d[k][1];
if(t && state[x][y] == 1)
{
do
{
x -= d[k][0], y -= d[k][1];
state[x][y] = 1;
} while(x != i || y != j);
flag = 1;
}
}
if(flag)
{
tot++;
int v = alpha_beta(dep+1, player^1, alpha, beta, cnt+1);
if(v > alpha)
{
if(!dep) ansx = i, ansy = j, memcpy(cur, state, sizeof state);
alpha = v;
}
memcpy(state, temp, sizeof temp);
if(beta <= alpha) break;
}
}
}
if(!tot) player = 1;
if(player)
{
for(re int id = 1, x, y, tx, ty, i, j; id <= 64; ++id)
{
i = (id-1>>3)+1, j = (id-1)%8+1;
if(state[i][j] == 0)
{
int flag = 0, t;
for(re int k = 0; k < 8; ++k)
{
t = 0, x = i+d[k][0], y = j+d[k][1];
while(state[x][y] == 1) t++, x += d[k][0], y += d[k][1];
if(t && state[x][y] == -1)
{
do
{
x -= d[k][0], y -= d[k][1];
state[x][y] = -1;
} while(x != i || y != j);
flag = 1;
}
}
if(flag)
{
int v = alpha_beta(dep+1, player^1, alpha, beta, cnt+1);
if(beta > v) beta = v;
memcpy(state, temp, sizeof temp);
if(beta <= alpha) break;
}
}
}
}
return !player ? alpha : beta;
}
int main()
{
memset(cur, 0, sizeof cur);
cur[4][4] = cur[5][5] = -1, cur[4][5] = cur[5][4] = 1;
for(int i = 0; i < 10; ++i) cur[0][i] = cur[9][i] = -2;
for(int i = 1; i < 9; ++i) cur[i][0] = cur[i][9] = -2;
for(int i = 0, tx, ty; i < 32; ++i)
{
memcpy(state, cur, sizeof cur);
ansx = -1, ansy = -1;
alpha_beta(0, 0, -1000000000, 1000000000, i*2);
if(ansx != -1)
{
printf("----------\n");
printf("you should put here:%d %d\n", ansx, ansy);
for(int p = 1; p <= 8; ++p)
{
for(int q = 1; q <= 8; ++q) printf("%d", cur[p][q] < 0 ? 2 : cur[p][q]);
printf("\n");
}
printf("----------\n");
}
printf("Enter your competitor's move(If he can't even move,just type -1 -1):"); scanf("%d%d", &tx, &ty);
if(tx == -1) continue;
for(int k = 0, x, y, t; k < 8; ++k)
{
t = 0, x = tx+d[k][0], y = ty+d[k][1];
while(cur[x][y] == 1) t++, x += d[k][0], y += d[k][1];
if(t && cur[x][y] == -1)
{
do
{
x -= d[k][0], y -= d[k][1];
cur[x][y] = -1;
} while(x != tx || y != ty);
}
}
}
return 0;
}
C++ 黑白棋AI minimax+alphabeta剪枝的更多相关文章
- 新手立体四子棋AI教程(3)——极值搜索与Alpha-Beta剪枝
上一篇我们讲了评估函数,这一篇我们来讲讲立体四子棋的搜索函数. 一.极值搜索 极值搜索是game playing领域里非常经典的算法,它使用深度优先搜索(因为限制最大层数,所以也可以称为迭代加深搜索) ...
- 基于Alpha-Beta剪枝的欢乐斗地主残局辅助
2019年4月17日更新: 将搜索主函数优化为局部记忆化搜索,再次提高若干倍搜索速度 更新了main和player,helper无更新 #include "Player-v3.0.cpp&q ...
- 算法笔记--极大极小搜索及alpha-beta剪枝
参考1:https://www.zhihu.com/question/27221568 参考2:https://blog.csdn.net/hzk_cpp/article/details/792757 ...
- python3+tkinter实现的黑白棋,代码完整 100%能运行
今天分享给大家的是采用Python3+tkinter制作而成的小项目--黑白棋 tkinter是Python内置的图形化模块,简单易用,一般的小型UI程序可以快速用它实现,具体的tkinter相关知识 ...
- 用Dart写的黑白棋游戏
2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...
- [CareerCup] 8.8 Othello Game 黑白棋游戏
8.8 Othello is played as follows: Each Othello piece is white on one side and black on the other. Wh ...
- 黑白棋游戏 (codevs 2743)题解
[问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...
- bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)
黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...
- C#黑白棋制作~
前些天自己复习一下C#语言 做了个黑白棋,望大家看一下,可能有些bug嘿嘿 链接如下 http://files.cnblogs.com/files/flyingjun/%E9%BB%91%E7%99% ...
随机推荐
- 网络爬虫BeautifulSoup库的使用
使用BeautifulSoup库提取HTML页面信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup url='htt ...
- UDK Stat命令
Stat命令(chs en)提供了游戏和引擎各个方面的实时统计信息,输入不同参数会在屏幕HUD上显示对应统计数据. 非Shipping版的UDK才会启用STATS宏,统计逻辑才会编译进exe,才能使 ...
- git之命令git checkout
git checkout 最常用的就是切换分支,最近又发现一种新的用法: 有时候,在看代码的时候,不小心改动了部分代码,但跟项目没啥关系,这个时候,想不去提交这些代码,怎么处理呢? 使用git che ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- 数据结构学习之栈求解n皇后问题
数据结构学习之栈求解n皇后问题 0x1 目的 深入掌握栈应用的算法和设计 0x2 内容 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后 ...
- 017_python常用小技巧
一.进行十六进制运算 print(hex(int("6500000001", 16) - int("640064c6e7",16))) 0xff9b391a
- asp.net core 排序过滤分页组件:sieve(2)表达式树的复习
在Sieve组件中使用了很多关于表达式树的知识,但在我们日常的工作中写表达式树的机会是非常少的,至少在我的编程生涯中没怎么写过表达式树(可能也就是3,4次).所以,为了能够看懂Sieve里面的源代码, ...
- UI自动化测试之Jenkins配置
前一段时间帮助团队搭建了UI自动化环境,这里将Jenkins环境的一些配置分享给大家. 背景: 团队下半年的目标之一是实现自动化测试,这里要吐槽一下,之前开发的测试平台了,最初的目的是用来做接口自动化 ...
- Java BigDecimal类型的 加减乘除运算
原文: https://blog.csdn.net/xuwei_net/article/details/81253471 加法:add 减法:subtract 乘法:multiply 除法:divid ...
- codeforces#1139E. Maximize Mex(逆处理,二分匹配)
题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...