Alpha-beta剪枝-井字棋
来源:互联网 发布:js valueof 比较大小 编辑:程序博客网 时间:2024/05/16 08:40
#include<iostream>#include<windows.h>#include<vector>#include"board.h"using namespace std;void gameBody();void placeChess(int pos);int winner();bool isDraw();// for negaMaxint evaluate(int playerNow);// for minMaxint evaluate();void GenerateMoves(vector<int> &steps);int negaMax(int depth, int playerNow);int maxMin(int depth, int playerNow);// 加入剪枝int negaMax(int depth, int playerNow, int alpha, int beta);namespace { Board board; int playerNow = 1; int bestMove; int depth = 9;}void gameBody() { while (winner()==0 && !isDraw()) { if (playerNow > 0) { cout << board; cout << std::endl << "--------请输入您要落子的位置(0-8)------------" << std::endl; int pos; cin >> pos; placeChess(pos); depth--; } else { maxMin(depth, playerNow); //negaMax(depth, playerNow); placeChess(bestMove); depth--; } } cout << board;}void placeChess(int pos) { board.board[pos]= playerNow; playerNow = -playerNow;}int winner(){ for (int i = 0; i < 3; i++) { if (board.getCol(i) == -3 || board.getRow(i) == -3||board.getMainLine()==-3||board.getCountLine()==-3) { return -1; } if (board.getCol(i) == 3 || board.getRow(i) == 3 || board.getMainLine() == 3 || board.getCountLine() == 3) { return 1; } } return 0;}bool isDraw() { for (int i = 0; i < 9; i++) { if (board.board[i] == 0) { return false; } } return (winner() == 0)&& true;}void GenerateMoves(vector<int> &steps) { for (int i = 0; i < 9; i++) { if (board.board[i] == 0) { steps.push_back(i); } }}int evaluate(int playerNow) { if (winner() ==1) { return 10000* playerNow; } else if (winner() == -1) { return -10000* playerNow; } else { return 0; }}int evaluate() { if (winner() == 1) { return 10000 ; } else if (winner() == -1) { return -10000; } else { return 0; }}int maxMin(int depth, int playerNow) { if (winner()!=0 || depth == 0) { return evaluate(); } int bestValue = 0; if (playerNow == -1) { bestValue = 10000; } else { bestValue = -10000; } vector<int> steps; vector<int>& stepVector = steps; GenerateMoves(stepVector);// 获得走步 while(stepVector.size()>0) { int mv = stepVector[stepVector.size() - 1]; stepVector.pop_back(); board.board[mv] = playerNow;// makeMove int value = maxMin(depth - 1, -playerNow); board.board[mv] = 0;// unMakeMove if (playerNow == 1) { if (value > bestValue) { bestValue = value; if (depth == ::depth) { bestMove = mv; } } } else { if (value < bestValue) { bestValue = value; if (depth == ::depth) { bestMove = mv; } } } } return bestValue;}/*int negaMax(int depth,int playerNow) { int best = -100000; if (depth == 0 || winner() != 0) { return evaluate(playerNow); } else { vector<int> steps; vector<int>& stepVector = steps; GenerateMoves(stepVector);// 获得走步 while (stepVector.size()>0) { int mv = stepVector[stepVector.size()-1]; stepVector.pop_back(); board.board[mv] = playerNow;// makeMove int value = -negaMax(depth - 1,-playerNow); board.board[mv] = 0;// unMakeMove if (value >= best) { best = value; if (depth == ::depth) { bestMove = mv; } } } } return best;}*/// 加入alpha-beta剪枝int negaMax(int depth, int playerNow) { negaMax(depth,playerNow,-10000000,10000000); return 0;}int negaMax(int depth,int playerNow,int alpha,int beta) { int best = -100000; if (depth == 0 || winner() != 0) { return evaluate(playerNow); } else { vector<int> steps; vector<int>& stepVector = steps; GenerateMoves(stepVector);// 获得走步 while (stepVector.size()>0) { int mv = stepVector[stepVector.size()-1]; stepVector.pop_back(); board.board[mv] = playerNow;// makeMove int value = -negaMax(depth - 1,-playerNow,-beta,-alpha); board.board[mv] = 0;// unMakeMove if (value >= beta) { if (depth == ::depth) { bestMove = mv; } return beta; } if (value > alpha) { if (depth == ::depth) { bestMove = mv; } alpha = value; } } } return alpha;}int main() { gameBody(); system("pause"); return 0;}
阅读全文
0 0
- Alpha-beta剪枝-井字棋
- alpha-beta剪枝搜索
- 例析Alpha-Beta剪枝
- Alpha-Beta 剪枝算法
- alpha beta 剪枝算法
- alpha-beta剪枝
- Alpha-Beta剪枝算法
- Alpha-beta剪枝算法
- 例析Alpha-Beta剪枝
- Alpha-Beta剪枝(Alpha Beta Pruning)
- Alpha-Beta剪枝算法(Alpha Beta Pruning)
- 对抗博弈 alpha-beta剪枝
- 浅谈"对抗搜索"的alpha-beta 剪枝
- Alpha-beta剪枝算法实例分析
- alpha-beta剪枝的代码实现
- 五子棋(人机)-alpha-beta剪枝
- 极小极大值方法以及alpha-Beta剪枝
- Minimax算法及其剪枝优化Alpha-beta
- 第一个小爬虫--爬取图片并保存
- Android中的visibility属性
- spring--自定义ReturnValueHandler
- C语言:可变参数宏以及##和#符的使用
- win32开发(绘制bitmap)
- Alpha-beta剪枝-井字棋
- Codeforces Round #453 (Div. 2) A
- java多线程[9]:线程池(ExecutorService)
- 汇编语言的执行过程
- 离合器预减振超载造成变速箱怠速异响matlab仿真分析
- 【Laravel】数据库事务
- random模块有关
- 坚持#第247天~sed和awk进阶
- 【ASM】Oracleasm命令