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;}
原创粉丝点击