五子棋

#include <graphics.h>  //图形库头文件
#include <stdio.h>
#include <mmsystem.h>  //
#pragma comment(lib,"winmm.lib")   //加载静态库--下一行


IMAGE mm;

void initGame();           //初始图像窗口
void playChess();        //下棋
int judge(int a, int b);     //判断胜负

int flag = 0;   //标记轮到谁下棋
int board[20][20] = { 0 };  //0表示没有下棋的状态

int main()   //主函数窗口
{
    initGame();
    playChess();

    while (1); //防止闪屏
    return 0;
}


void initGame()
{
    initgraph(600, 500);
    loadimage(&mm, "1.jpg", 600, 500);
    putimage(0, 0, &mm);
    mciSendString("open D:\\vs project\\趣味五子棋\\趣味五子棋1.mp3 alias music ",0, 0, 0);
    //MCIERROR mciError;
    /* buf[50];
    mciError = mciSendString("open D:\\vs project\\趣味五子棋\\趣味五子棋\\3.mp3 alias music ", buf, strlen(buf), NULL);

    if (mciError)

    {
        HWND hwnd;
        hwnd = GetHWnd();

        mciGetErrorString(mciError, buf, strlen(buf));

        MessageBox(hwnd, "打开音乐文件出错,请考虑以下选择","五子棋", MB_OK);

        return;

    }*/
    mciSendString("play music repeat",NULL, 0, NULL);

    
    //绘制棋盘
    setlinecolor(BLACK);
    for (int i = 1; i < 20; i++)
    {
        line(0, i * 25, 500, i * 25);
        line(i * 25, 0, i * 25, 500);
    }
    setlinestyle(PS_SOLID, 2);  //设置线条为实线,宽度为2
    line(500, 0, 500, 500);
    outtextxy(512, 60, "luoshiyong:黑棋");
    outtextxy(512, 100, "daijunyi:白棋");
}


void playChess()
{
    int x = 0, y = 0;
    int a = 0, b = 0;
    MOUSEMSG m;  //定义鼠标消息
    HWND hwnd;
    hwnd = GetHWnd();  //设置窗口句柄
    while (1)
    {
        m = GetMouseMsg();  //获取鼠标信息

        //求距离绝对值
        for (int i = 1; i < 20; i++)
        {
            for (int j = 1; j < 20; j++)
            {
                if (abs(m.x - i * 25) < 12 && abs(m.y - j * 25) < 12)
                {
                    a = i;
                    b = j;
                    x = i * 25;
                    y = j * 25;
                }
            }
        }

        if (m.uMsg == WM_LBUTTONDOWN)   //L左  button 按钮  down下
        {
            if (board[a][b] != 0)
            {
                MessageBox(hwnd, "别任性", "五子棋", MB_OK);
                continue;
            }
            if (flag % 2 == 0)
            {
                setfillcolor(BLACK);
                solidcircle(x, y, 10);
                board[a][b] = 1;
            }
            else
            {
                setfillcolor(WHITE);
                solidcircle(x, y, 10);
                board[a][b] = 2;
            }
            flag++;
        }
        if (judge(a, b))
        {
            if (1 == flag % 2)
            {
                MessageBox(hwnd,"玩家1胜利", "游戏结束", MB_OK);
                return;
            }
            else
            {
                MessageBox(hwnd, "玩家2胜利", "游戏结束", MB_OK);
                return;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值