C++实现图形界面五子棋游戏源码

C++图形界面五子棋, Shell控制台实现五子棋的基础上引用了图形库源码。

如需安装c++或远程调试,可加QQ905733049由专业技术人员远程协助!

下面给出代码:

#include
#include
#include
#pragma warning(disable:4996)
void menu();
void play();
void display();
void playgame();
void again(int index);
int a[9][9] = { 0 };
 
void main()
{
    initgraph(600, 600, SHOWCONSOLE);
    menu();
    getch();
}
 
void menu()
{
    IMAGE img;
    setaspectratio(1.1, 1);
    loadimage(&img, "IMAGE", "tur", 0, 0, true);
    putimage(-10, 0, &img, SRCPAINT);
    settextstyle(50, 20, "宋体");
    outtextxy(200, 400, "开始游戏");
    MOUSEMSG m;
    while (true)
    {
        m = GetMouseMsg();
        if (m.uMsg == WM_MOUSEMOVE && m.x >= 200 && m.x <= 360 && m.y >= 400 && m.y <= 450)
        {
            setlinecolor(YELLOW);
            rectangle(200, 400, 360, 450);
 
        }
        if (m.uMsg == WM_LBUTTONDOWN && m.x >= 200 && m.x <= 360 && m.y >= 400 && m.y <= 450)
        {
            cleardevice();
            Sleep(500);
            play();
            break;
        }
        if (!(m.x >= 200 && m.x <= 360 && m.y >= 400 && m.y <= 450))
        {
            setlinecolor(BLACK);
            rectangle(200, 400, 360, 450);
        }
    }
}
 
void play()
{
    graphdefaults();
    setlinecolor(WHITE);
    settextstyle(15, 10, "宋体");
    settextcolor(WHITE);
    outtextxy(460, 80, "play1:红子:");
    outtextxy(460, 100, "步数: ");
    outtextxy(460, 160, "play2:黄子");
    outtextxy(460, 180, "步数: ");
    display();
}
 
void display()
{
    for (int i = 50; i <= 450; i += 50)
    {
        line(i, 50, i, 450);
        line(50, i, 450, i);
    }
    playgame();
}
void playgame()
{
    char str1[5];
    char str2[5];
    int bushu1 = 0, bushu2 = 0;
    memset(a, 0, sizeof(a));
    int play1 = 1;
    int i = 0, j = 0;
    int win = 0;
    MOUSEMSG msg;
    while (win == 0)
    {
        msg = GetMouseMsg();
        if (msg.uMsg == WM_LBUTTONDOWN)
        {
            for (int lie = 50; lie <= 450; lie += 50)
            {
                if (msg.x <= lie + 10 && msg.x >= lie - 10)
                {
                    for (int hang = 50; hang <= 450; hang += 50)
                    {
                        if (msg.y <= hang + 10 && msg.y >= hang - 10)
                        {
                            if (play1 == 1 && a[hang / 50 - 1][lie / 50 - 1] == 0)
                            {
                                setfillcolor(RED);
                                solidcircle(lie, hang, 12);
                                a[hang / 50 - 1][lie / 50 - 1] = 1;
                                play1 = 0;
                                bushu1++;
                                sprintf(str1, "%i", bushu1);
                                outtextxy(520, 100, str1);
                                break;
                            }
                            if (play1 == 0 && a[hang / 50 - 1][lie / 50 - 1] == 0)
                            {
                                setfillcolor(YELLOW);
                                solidcircle(lie, hang, 12);
                                a[hang / 50 - 1][lie / 50 - 1] = 2;
                                play1 = 1;
                                bushu2++;
                                sprintf(str2, "%i", bushu2);
                                outtextxy(520, 180, str2);
                                break;
                            }
                        }
                    }
                }
            }
        }
        //判断是否赢
        for (j = 0; j<9 && (win == 0); j++)
        {
            for (i = 0; i<6; i++)
            {
 
                if ((a[j][i] == 1 && a[j][i + 1] == 1 && a[j][i + 2] == 1 && a[j][i + 3] == 1 && a[j][i + 4] == 1) 
                    || (a[i][j] == 1 && a[i + 1][j] == 1 && a[i + 2][j] == 1 && a[i + 3][j] == 1 && a[i + 4][j] == 1))//横纵是5个子play1 win
                {
                    win = 1;
                    Sleep(100);
                    break;
                }
                if ((a[j][i] == 2 && a[j][i + 1] == 2 && a[j][i + 2] == 2 && a[j][i + 3] == 2 && a[j][i + 4] == 2)
                    || (a[i][j] == 2 && a[i + 1][j] == 2 && a[i + 2][j] == 2 && a[i + 3][j] == 2 && a[i + 4][j] == 2))//横纵是5个子play2 win
                {
                    win = 2;
                    Sleep(100);
                    break;
                }
            }
        }
        for (j = 0; j<6 && (win == 0); j++)
        {
            for (i = 0; i<6; i++)
            {
                if (a[j][i] == 1 && a[j + 1][i + 1] == 1 && a[j + 2][i + 2] == 1 && a[j + 3][i + 3] == 1 && a[j + 4][i + 4] == 1)//向右倾斜时候play1 win
                {
                    win = 1;
                    Sleep(100);
                    break;
 
                }
                if (a[j][i] == 2 && a[j + 1][i + 1] == 2 && a[j + 2][i + 2] == 2 && a[j + 3][i + 3] == 2 && a[j + 4][i + 4] == 2)//向右倾斜时候play2 win
                {
                    win = 2;
                    Sleep(100);
                    break;
                }
            }
            for (i = 4; i<9 && (win == 0); i++)
            {
                if (a[j][i] == 1 && a[j + 1][i - 1] == 1 && a[j + 2][i - 2] == 1 && a[j + 3][i - 3] == 1 && a[j + 4][i - 4] == 1)//向左倾斜时候play1 win
                {
                    win = 1;
                    Sleep(100);
                    break;
                }
                if (a[j][i] == 2 && a[j + 1][i - 1] == 2 && a[j + 2][i - 2] == 2 && a[j + 3][i - 3] == 2 && a[j + 4][i - 4] == 2)//向左倾斜时候play2 win
                {
                    win = 2;
                    Sleep(100);
                    break;
                }
            }
        }
    }
    again(win);
}
void again(int index)
{
    settextstyle(20, 20, "宋体");
    settextcolor(YELLOW);
    if (index == 1)
    {
        outtextxy(220, 500, "play1 win!");
    }
    else
    {
        outtextxy(220, 500, "play2 win!");
    }
    int quit = MessageBox(NULL, "是否继续游戏", "ArchyLi", MB_YESNO);
    if (IDYES == quit)
    {
        cleardevice();
        play();
    }
    else
    {
        cleardevice();
        settextstyle(50, 30, "宋体");
        settextcolor(YELLOW);
        outtextxy(200, 300, "欢迎下次再来");
        Sleep(1000);
        return;
    }
}

 

实现效果如图:

如需远程调试,可加QQ905733049由专业技术人员远程协助!

你可能感兴趣的:(C++实现图形界面五子棋游戏源码)