C++双人五子棋

· · 学习·文化课

C++五子棋

版本号:v1.4

已经有初步的下棋与判断输赢功能

new:新增AI功能+重置

// ● ■
#include <windows.h>                            //GetAsyncKeyState所需头文件
#include <bits/stdc++.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)  //用来检测按键的点击事件

using namespace std;
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
int Map[55][55],use[55][55];
int u[4][2]={{1,0},{1,1},{0,1},{-1,1}};
int map_use[2][5][2]={{{1,1},{10,5},{40,30},{6000,60},{40000,40000}},{{2,2},{10,5},{50,25},{10000,55},{1000000,1000000}}};
int game_mo=2;
struct node{
    int x,y;
};

//----------移动光标----------
void gotoxy(int x, int y) {
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
    SetConsoleCursorPosition(hOut, pos);//两个参数分别是指定哪个窗体,具体位置
}

//----------隐藏光标----------
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
//----------设置字体颜色----------
void color(int BackC, int ForgC) {
    WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void ChuShiHua()
{
    //----------随机数种子----------
    srand(time(0)); 
    //----------移除快速编辑模式(对于win10用户)----------
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin, &mode);
    mode &= ~ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(hStdin, mode);
    //---------更改窗口标题-----------------------
    HWND hWnd = GetConsoleWindow();
    SetWindowText(hWnd, "五子棋");
    //---------恢复数据---------------------------
    for(int i=0;i<55;i++)
        for(int j=0;j<55;j++)
            Map[i][j]=0; 
    return ;
}
bool mapcheak(int x,int y)
{
    return x>=0&&y>=0&&x<19&&y<19;
}
bool cheak(int x,int y,int f)
{
    int sum=1;
    for(int i=0;i<4;i++)
    {
        int s=1,nx=x,ny=y;
        while(mapcheak(nx+u[i][0],ny+u[i][1])&&Map[nx+u[i][0]][ny+u[i][1]]==f)
            s++,nx+=u[i][0],ny+=u[i][1];
        nx=x,ny=y;
        while(mapcheak(nx-u[i][0],ny-u[i][1])&&Map[nx-u[i][0]][ny-u[i][1]]==f)
            s++,nx-=u[i][0],ny-=u[i][1];
        sum=max(sum,s);
    }
    return sum>=5;
}
void welcome()
{
    gotoxy(40,24); 
    printf("欢迎来到");
    Sleep(1000);
    gotoxy(40,25);
    printf("C++五子琪大赛");
    Sleep(1000);
    gotoxy(40,26);
    printf("接下来,有请AI与你大战");
    Sleep(2000);
    system("mode con cols=100 lines=50");
    system("color 60");
}
void draw_this(int x,int y)
{
    gotoxy(x*2,y);
    if(x==0)
    {
        if(y==0)
            cout<<"┌ ";
        else if(y==18)
            cout<<"└ ";
        else
            cout<<"├ ";
    }
    else if(y==0)
    {
        if(x==18)
            cout<<"┐ ";
        else
            cout<<"┬ ";
    }
    else if(y==18)
    {
        if(x==18)
            cout<<"┘ ";
        else
            cout<<"┴ ";
    }
    else if(x==18)
        cout<<"┤ ";
    else
    {
        if((x==3||x==9||x==15)&&(y==3||y==9||y==15))
            cout<<"╋ ";
        else
            cout<<"┼ ";
    }
}
void draw_map()
{
    color(6,0);
    for(int i=0;i<19;i++)
    {
        for(int j=0;j<19;j++)
            draw_this(j,i);
        Sleep(2);
    }
}
void victory(int player)
{
    color(6,0);
    gotoxy(0,0);
    printf("获胜者是:");
    gotoxy(0,1);
    printf((player%2?"     黑子玩家":"     白子玩家"));
    Sleep(1000);
    gotoxy(0,2);
    printf("按下鼠标左键重新开始");
    return ;
}
node AI()
{
    Sleep((rand()%15+10)*100);
    vector<node>ans;
    int mx=0;
    for(int x=0;x<19;x++)
    {
        for(int y=0;y<19;y++)
        {
            use[x][y]=0;
            if(Map[x][y])continue;
            int x1,y1,x2,y2;
            for(int f=1;f<=2;f++)
                for(int i=0;i<4;i++)
                {
                    int s=0,nx=x,ny=y;
                    while(mapcheak(nx+u[i][0],ny+u[i][1])&&Map[nx+u[i][0]][ny+u[i][1]]==f)
                        s++,nx+=u[i][0],ny+=u[i][1];
                    x1=nx,y1=ny,nx=x,ny=y;
                    while(mapcheak(nx-u[i][0],ny-u[i][1])&&Map[nx-u[i][0]][ny-u[i][1]]==f)
                        s++,nx-=u[i][0],ny-=u[i][1];
                    x2=nx,y2=ny;
                    if(s>4)s=4;
                    if(mapcheak(x1+u[i][0],y1+u[i][1])&&Map[x1+u[i][0]][y1+u[i][1]]==0&&
                        mapcheak(x2-u[i][0],y2-u[i][1])&&Map[x2-u[i][0]][y2-u[i][1]]==0)
                        use[x][y]+=map_use[f-1][s][0];
                    else if(mapcheak(x1+u[i][0],y1+u[i][1])&&Map[x1+u[i][0]][y1+u[i][1]]==0||
                        mapcheak(x2-u[i][0],y2-u[i][1])&&Map[x2-u[i][0]][y2-u[i][1]]==0)
                        use[x][y]+=map_use[f-1][s][1];
                }
            mx=max(mx,use[x][y]);
        }
    }
    for(int x=0;x<19;x++)
    {
        for(int y=0;y<19;y++)
        {
            if(use[x][y]==mx){
                node _={x,y};
                ans.push_back(_);
            }
        }
    }
    return ans[rand()%ans.size()];
}
void solve()
{
    system("mode con cols=38 lines=19");
    system("color 60");
    ChuShiHua();
    HideTheCursor();                            //隐藏光标
    draw_map();
    //welcome();
    int i=0;//
    while(1) {
        POINT p;
        GetCursorPos(&p);
        ScreenToClient(h,&p);               //获取鼠标在窗口上的位置
        GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //获取字体信息
        int x=p.x/consoleCurrentFont.dwFontSize.X/2;
        int y=p.y/consoleCurrentFont.dwFontSize.Y;
        while(!mapcheak(x,y))
        {
            GetCursorPos(&p);
            ScreenToClient(h,&p);               //获取鼠标在窗口上的位置
            GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont); //获取字体信息
            x=p.x/consoleCurrentFont.dwFontSize.X/2;
            y=p.y/consoleCurrentFont.dwFontSize.Y;
            Sleep(10);
        }
        if(!Map[x][y]&&KEY_DOWN(VK_LBUTTON)) {              //鼠标左键按下
            gotoxy(x*2,y);
            i++;
            int col=(i%2?0:15);
            Map[x][y]=(i+1)%2+1;             
            color(6,col);
            printf("■");                      //在鼠标位置输出
            while(KEY_DOWN(VK_LBUTTON))
                Sleep(10);
            if(cheak(x,y,(i+1)%2+1))
            {
                victory(i);
                break;
            }
            if(game_mo==2)
            {
                i++;
                node new_node=AI();
                int AI_x=new_node.x,AI_y=new_node.y;
                gotoxy(AI_x*2,AI_y);
                int col=(i%2?0:15);
                Map[AI_x][AI_y]=(i+1)%2+1;       
                color(6,col);
                printf("■");                      //在鼠标位置输出
                if(cheak(AI_x,AI_y,(i+1)%2+1))
                {
                    victory(0);
                    break;
                }
            }
        }
        gotoxy(x*2,y);
        if(!Map[x][y])
        {
            color(2,0);
            draw_this(x,y);
        }else{
            color(4,(Map[x][y]==1?0:15));
            printf("■");
        }
        Sleep(10);                              //延时
        gotoxy(x*2,y);
        if(!Map[x][y])
        {
            color(6,0);
            draw_this(x,y);
        }else{
            color(6,(Map[x][y]==1?0:15));
            printf("■");
        }
    }
    return ;
 } 
int main() {
    while(1)
    {
        solve();
        while(!KEY_DOWN(VK_LBUTTON))
            Sleep(10);
        while(KEY_DOWN(VK_LBUTTON))
            Sleep(10);
    }
    return 0;
}