这是一个井字棋

· · 休闲·娱乐

#include<bits/stdc++.h>
#include<bits/extc++.h>
#include<windows.h>
using namespace std;
#define red "\e[31m"
#define green "\e[32m"
#define blue "\e[34m"
#define yellow "\e[33m"
#define black "\e[0m"
#define sky "\e[36m"
#define purple "\e[35m"
#define normal "\e[0m"
#define cls aaa();

struct node
{
    char c='&';
    int cnt=0;
};
node a[5][5]= {'&',0};

void old_ai();
struct xy
{
    int x,y;
};
xy checkdie()
{
    for(int i=1; i<=3; i++)
    {
        bool flag=0;
        int cnt=0;
        int x,y;
        for(int j=1; j<=3; j++)
        {
            if(a[i][j].c!='#')
            {
                flag=1;
                x=i,y=j;
            }
            else cnt++;
        }
        if(flag&&a[x][y].cnt==4)return {x,y};
    }
    for(int i=1; i<=3; i++)
    {
        bool flag=0;
        int cnt=0;
        int x,y;
        for(int j=1; j<=3; j++)
        {
            if(a[j][i].c!='#')
            {
                flag=1;
                x=i,y=j;
            }
            else cnt++;
        }
        if(flag&&a[x][y].cnt==6)return {x,y};
    }
    if(a[1][1].c=='#'&&a[2][2].c=='#'&&a[3][3].c!='#'&&a[3][3].cnt==5)return {3,3};
    if(a[1][1].c=='#'&&a[2][2].c!='#'&&a[3][3].c=='#'&&a[2][2].cnt==5)return {2,2};
    if(a[1][1].c!='#'&&a[2][2].c=='#'&&a[3][3].c=='#'&&a[1][1].cnt==5)return {1,1};
    if(a[1][3].c!='#'&&a[2][2].c=='#'&&a[3][1].c=='#'&&a[1][3].cnt==5)return {1,3};
    if(a[1][3].c=='#'&&a[2][2].c!='#'&&a[3][1].c=='#'&&a[2][2].cnt==5)return {2,2};
    if(a[1][3].c=='#'&&a[2][2].c=='#'&&a[3][1].c!='#'&&a[3][1].cnt==5)return {3,1};
    return {0,0};
}
string loading[13]=
{
    {
        "     \n     \n     \n     \n     "
    },
    {
        " ### \n    #\n    #\n     \n     "
    },
    {
        "  ## \n    #\n    #\n    #\n     "
    },
    {
        "   # \n    #\n    #\n    #\n   # "
    },
    {
        "     \n    #\n    #\n    #\n  ## "
    },
    {
        "     \n     \n    #\n    #\n ### "
    },
    {
        "     \n     \n     \n#   #\n ### "
    },
    {
        "     \n     \n#    \n#    \n ### "
    },
    {
        "     \n#    \n#    \n#    \n ##  "
    },
    {
        " #   \n#    \n#    \n#    \n #   "
    },
    {
        " ##  \n#    \n#    \n#    \n     "
    },
    {
        " ### \n#    \n#    \n     \n     "
    },
    {
        " ### \n#   #\n     \n     \n     "
    },
};
void aaa()
{
    COORD topLeft = {0, 0};
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(console, topLeft);
    for(int i=1; i<=20; i++)
    {
        for(int f=1; f<=40; f++)
            cout<<" ";
        cout<<"\n";
    }
    SetConsoleCursorPosition(console, topLeft);
}
void load()
{
    system("cls");
    for(int v=1; v<=10; v++)
    {
        for(int i=1; i<=12; i++)
        {
            cout<<loading[i];
            cout<<"加载中...\n";
            Sleep(10);
            system("cls");
        }
    }
}
void printinfo(bool flag)
{
    if(flag)
        MessageBox(NULL,"*代表你的棋子\n#代表人机的棋子\n.表示空白棋盘\n黄色的棋子在下一步时就会消失\n","Tips",MB_OK|MB_ICONINFORMATION);
    if(!flag)
        MessageBox(NULL,"*代表红方的棋子\n#代表蓝方的棋子\n.表示空白棋盘\n黄色的棋子在下一步时就会消失\n","Tips",MB_OK|MB_ICONINFORMATION);
}

void pause(double second)
{
    Sleep(second*500);
}
void print(string test,string color="normal")
{
    if(color=="red")cout<<red;
    else if(color=="red")cout<<red;
    else if(color=="blue")cout<<blue;
    else if(color=="purple")cout<<purple;
    else if(color=="yellow")cout<<yellow;
    else if(color=="sky")cout<<sky;
    else if(color=="black")cout<<black;
    for(int i=0; i<test.length(); i++)
        cout<<test[i];
    cout<<normal;
}
void write()
{
    cout<<"╔═══╦═══╦═══╗\n";
    for(int i=1; i<=3; i++)
    {
        cout<<"║";
        for(int j=1; j<=3; j++)
        {
            if(a[i][j].cnt==4)cout<<yellow<<" "<<a[i][j].c<<normal;
            else if(a[i][j].c=='#')
                cout<<blue<<" "<<a[i][j].c<<normal;
            else if(a[i][j].c=='*')
                cout<<red<<" "<<a[i][j].c<<normal;
            else cout<<" "<<a[i][j].c;
            cout<<" ║";
        }
        cout<<"\n";
        if(i!=3)cout<<"╠═══╬═══╬═══╣\n";
    }
    cout<<"╚═══╩═══╩═══╝\n";
}
long long sj(int maxx)
{
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<> dis(1,maxx);
    long long f = dis(gen);
    return f;
}
int check()
{
    if(a[1][1].c=='*'&&a[1][2].c=='*'&&a[1][3].c=='*')return 1;
    if(a[2][1].c=='*'&&a[2][2].c=='*'&&a[2][3].c=='*')return 1;
    if(a[3][1].c=='*'&&a[3][2].c=='*'&&a[3][3].c=='*')return 1;
    if(a[1][1].c=='*'&&a[2][1].c=='*'&&a[3][1].c=='*')return 1;
    if(a[1][2].c=='*'&&a[2][2].c=='*'&&a[3][2].c=='*')return 1;
    if(a[1][3].c=='*'&&a[2][3].c=='*'&&a[3][3].c=='*')return 1;
    if(a[1][3].c=='*'&&a[2][2].c=='*'&&a[3][1].c=='*')return 1;
    if(a[1][1].c=='*'&&a[2][2].c=='*'&&a[3][3].c=='*')return 1;
    if(a[1][1].c=='#'&&a[1][2].c=='#'&&a[1][3].c=='#')return 2;
    if(a[2][1].c=='#'&&a[2][2].c=='#'&&a[2][3].c=='#')return 2;
    if(a[3][1].c=='#'&&a[3][2].c=='#'&&a[3][3].c=='#')return 2;
    if(a[1][1].c=='#'&&a[2][1].c=='#'&&a[3][1].c=='#')return 2;
    if(a[1][2].c=='#'&&a[2][2].c=='#'&&a[3][2].c=='#')return 2;
    if(a[1][3].c=='#'&&a[2][3].c=='#'&&a[3][3].c=='#')return 2;
    if(a[1][3].c=='#'&&a[2][2].c=='#'&&a[3][1].c=='#')return 2;
    if(a[1][1].c=='#'&&a[2][2].c=='#'&&a[3][3].c=='#')return 2;
    return 0;
}
void reddown()
{
    cout<<"\n\n请红方下子(输入棋子的行和列):\n";
    int x,y;
    cin>>x>>y;
    if(x<1||y>3||x>3||y<1||a[x][y].c!='.'&&a[x][y].cnt!=4)
    {
        cout<<"坐标不合法!\n";
        pause(0.5);
        cls
        write();
        reddown();
        return;
    }
    else
        a[x][y].c='*';
}
void bluedown()
{
    cout<<"\n\n请蓝方下子(输入棋子的行和列):\n";
    int x,y;
    cin>>x>>y;
    if(x<1||y>3||x>3||y<1||a[x][y].c!='.')
    {
        cout<<"坐标不合法!\n";
        pause(0.5);
        cls
        write();
        bluedown();
        return;
    }
    else
        a[x][y].c='#';
}
void aidown(int x,int y)
{
    a[x][y].c='#';
}
int dx[4]= {0,1,0,-1};
int dy[4]= {1,0,-1,0};
bool ok(int x, int y, char player)
{
    node temp = a[x][y];
    a[x][y].c = player;
    bool result = check() == (player == '*' ? 1 : 2);
    a[x][y] = temp;
    return result;
}
bool find_ai(char player, int& x, int& y)
{
    for(int i = 1; i <= 3; i++)
    {
        for(int j = 1; j <= 3; j++)
        {
            if(a[i][j].c == '.')
            {
                if(ok(i, j, player))
                {
                    x = i;
                    y = j;
                    return 1;
                }
            }
        }
    }
    return 0;
}
void sjx()
{
    vector<pair<int, int>> ec;
    for(int i = 1; i <= 3; i++)
        for(int j = 1; j <= 3; j++)
            if(a[i][j].c == '.')
                ec.emplace_back(i, j);
    if(!ec.empty())
    {
        int i = sj(ec.size()) - 1;
        aidown(ec[i].first, ec[i].second);
    }
}
int renji(bool xxx, int d= 0, int maxd = 3)
{
    int sc = check();
    if(sc == 2) return 1;
    if(sc == 1) return -1;
    bool flag = 1;
    for(int i = 1; i <= 3; i++)
        for(int j = 1; j <= 3; j++)
            if(a[i][j].c == '.')
            {
                flag = 0;
                break;
            }
    if(flag || d >= maxd) return 0;
    if(xxx)
    {
        int maxn = -500;
        for(int i = 1; i <= 3; i++)
        {
            for(int j = 1; j <= 3; j++)
            {
                if(a[i][j].c == '.')
                {
                    a[i][j].c = '#';
                    int sc = renji(0, d + 1, maxd);
                    a[i][j].c = '.';
                    maxn = max(sc, maxn);
                }
            }
        }
        return maxn;
    }
    else
    {
        int maxn = 500;
        for(int i = 1; i <= 3; i++)
        {
            for(int j = 1; j <= 3; j++)
            {
                if(a[i][j].c == '.')
                {
                    a[i][j].c = '*';
                    int sc = renji(1, d + 1, maxd);
                    a[i][j].c = '.';
                    maxn = min(sc, maxn);
                }
            }
        }
        return maxn;
    }
}

void ai()
{
    cout<<"\n人机正在下子...\n";
    pause(1);
    int x, y;
    if(find_ai('#', x, y))
    {
        aidown(x, y);
        return;
    }
    if(find_ai('*', x, y))
    {
        aidown(x, y);
        return;
    }
    int maxn = -500;
    for(int i = 1; i <= 3; i++)
    {
        for(int j = 1; j <= 3; j++)
        {
            if(a[i][j].c == '.'||a[i][j].c!='#'&&a[i][j].c!='.'&&a[i][j].cnt==4)
            {
                a[i][j].c = '#';
                int sc = renji(0);
                a[i][j].c = '.';
                if(sc > maxn)
                {
                    maxn = sc;
                    x = i;
                    y = j;
                }
            }
        }
    }
    aidown(x, y);
}
void add()
{
    for(int i = 1; i <= 3; ++i)
    {
        for(int j = 1; j <= 3; ++j)
        {
            if(a[i][j].c != '.')
            {
                if(++a[i][j].cnt==5)
                {
                    a[i][j].cnt = 0;
                    a[i][j].c = '.';
                }
            }
        }
    }
}
void vs_ai()
{
    printinfo(1);
    cout<<"请选择难度:\n1:简单\n2:困难\n";
    int flag;
    cin>>flag;
    for(int i=1; i<4; i++)
        for(int j=1; j<4; j++)
        {
            a[i][j].c='.';
            a[i][j].cnt=0;
        }
    system("pause");
    system("cls");
    while(!check())
    {
        write();
        reddown();
        add();
        cls
        write();
        pause(0.5);
        if(flag==1)old_ai();
        else ai();
        add();
        cls
    }
    write();
    pause(1);
    cout<<"\n";
    cls
    write();
    if(check()!=1)system("color 8");
    if(check()==1)
    {
        cls
        for(int i=1; i<=10; i++)
        {
            write();
            cout<<"\n";
            cout<<"你赢了!\n";
            pause(0.5);
            cls
            pause(0.5);
        }
    }
    else cout<<"你输了!\n";
    pause(3);
    system("color f");
    cls
}
void pvp()
{
    for(int i=1; i<4; i++)
        for(int j=1; j<4; j++)
        {
            a[i][j].c='.';
            a[i][j].cnt=0;
        }
    printinfo(0);
    system("pause");
    system("cls");
    while(!check())
    {
        add();
        write();
        reddown();
        if(check()==1)break;
        pause(0.5);
        cls
        add();
        write();
        bluedown();
        if(check()==2)break;
        pause(0.5);
        cls
    }
    pause(1);
    cls
    write();
    if(check()==1)cout<<"红方赢了!\n";
    else cout<<"蓝方赢了!\n";
    cls
    for(int i=1; i<=8; i++)
    {
        write();
        if(check()==1)cout<<"红方赢了!\n";
        else cout<<"蓝方赢了!\n";
        pause(0.5);
        cls
        pause(0.5);
    }
    cls
}
void translate(bool flag)
{
    cls
    load();
    cls
    if(flag)vs_ai();
    else pvp();
}
void old_ai()
{
    cout<<"\n人机正在下子...\n";
    pause(1);
    int x, y;
    if(find_ai('#', x, y))
    {
        aidown(x, y);
        return;
    }
    if(find_ai('*', x, y))
    {
        aidown(x, y);
        return;
    }
    sjx();
}
int main()
{
    while(1)
    {
        cout<<"人机对战:1\n双人:2\n";
        int i;
        cin>>i;
        if(i==1)translate(1);
        else if(i==2)translate(0);
        else
        {
            cout<<"输入不合法!\n";
            pause(0.5);
            cls
            continue;
        }
    }
    return 0;
}
没写注释见凉