人工智能---alphabeta剪枝算法实现AI井子棋
来源:互联网 发布:阿里云机房等级标准 编辑:程序博客网 时间:2024/05/27 16:43
AI的智能程度是跟你剪枝的层数相关的,越深越智能
#include <iostream>#include <cstring>#include <cmath>#include <cstdio>#include <cstdlib>#include <stack>#include <list>#include <iomanip>#include <algorithm> using namespace std;const int empty = 0;const int human = 1;const int comp = 2;int board[3][3];int counthad;int x, y;int pos;bool iswin(int value, int x, int y) //是否构成线取胜 { if ((board[x][0] == value && board[x][1] == value && board[x][2] == value) || (board[0][y] == value && board[1][y] == value && board[2][y] == value ) || (board[0][0] == value && board[1][1] == value && board[2][2] == value) || (board[0][2] == value && board[1][1] == value && board[2][0] == value)) return true; else return false;}int aicaozuo(int value, int alpha, int beta, int &pos, int human_pos) //扫描并决定下点 { int win = 1, draw = 0, result, tmp; int other = 3 - value; if (human_pos != -1 && iswin(other, human_pos / 3, human_pos % 3)) //对手赢 return -win; if (counthad == 9) //9步走完平局开始 return draw; for (int i = 0; i < 9; ++i) //开始向下寻找知道alpha>=beta,AI下的点 { if (alpha >= beta) break; if (board[i / 3][i % 3]) continue; board[i / 3][i % 3] = value; counthad++; result = -aicaozuo(other, -beta, -alpha, tmp, i); //递归调用,向下一层搜索 if (result > alpha){ alpha = result; pos = i; } board[i / 3][i % 3] = empty; //每次重新把找到的点置零 counthad--; } return alpha;}//显示棋盘 void display(){ for (int i = 0; i<3; i++){ for (int j = 0; j<3; j++){ if (board[i][j] == empty) cout << "+"; else if (board[i][j] == human) cout << "o"; else cout << "x"; } cout << endl; }}void handhuman(){ //int x, y; cin >> x >> y; while (1) { if (!(x < 0 || x >= 3 || y <0 || y >= 3 || board[x][y])) break; cout << "该点无效,重新输入\n"; cin >> x >> y; } board[x][y] = 1;}void handcomp(){ //int pos; aicaozuo(2, -1, 1, pos, -1); //扫描并选择下子 pos board[pos / 3][pos % 3] = 2; counthad++; display();}int main(){ while (1) { cout << "game start!\n"; cout << "please input:\n"; for (int i = 0; i < 3; ++i)//清空棋盘 for (int j = 0; j < 3; ++j) board[i][j] = empty; counthad = 0; while (1) { handhuman(); if (iswin(1, x, y)) //返回真,则玩家赢 { cout << "You win!\n"; break; } counthad++; if (counthad == 9){ cout << "Draw!\n";//平局 break; } handcomp(); if (iswin(2, pos / 3, pos % 3)){ //返回真,则电脑赢 cout << "You lose!\n"; break; } } } return 0;}
0 0
- 人工智能---alphabeta剪枝算法实现AI井子棋
- AlphaBeta剪枝算法
- AlphaBeta剪枝算法
- AlphaBeta剪枝算法
- AlphaBeta剪枝算法
- AlphaBeta剪枝算法
- 常见棋类AI人工智能算法
- AI人工智能-Python实现人机对话
- 人工智能之最大最小值算法+剪枝优化(算法 + C++实现)
- 五子棋AI算法-Alpha Beta剪枝
- 基于极大极小算法和alpha-beta剪枝实现AI井字棋
- 人工智能(AI)
- 人工智能(AI)
- 地主AI算法实现
- 地主AI算法实现
- 地主AI算法实现
- 五子棋AI算法第三篇-Alpha Beta剪枝
- Unity人工智能学习—确定性AI算法之随机运动
- bzoj3652 大新闻(数位DP)
- safari浏览器使用js进行页面跳转
- 查看MYSQL数据库中所有用户及拥有权限
- Html div上下左右居中显示
- MySQL中使用SET类型快速插入中文字符串
- 人工智能---alphabeta剪枝算法实现AI井子棋
- 【DWT笔记】傅里叶变换与小波变换
- 正则表达式入门
- Java中,public class与class,以及main函数的一些说明
- popwindow和spinner的用法
- Java基于注解和反射导入导出Excel(Bean转Excel,Excel转Bean)
- Spark 生产开发环境搭建 Git + Maven + IDEA
- Java中对List集合内的元素进行顺序、倒序、随机排序的示例代码
- 分布式存储Ceph