人工智能---alphabeta剪枝算法实现AI井子棋

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;
}
点赞