C语言实现小游戏2048

2048游戏

棋盘的运算

//初始化全部棋子的位置
int setshess(struct chess setchessp[][4])
{
    line(40, 50, 320, 330);
    int add, pdd;
    int x = 55;
    int y = 70;
    //设定位置
    for (add = 0; add < 4; add++)
    {
        for (pdd = 0; pdd < 4; pdd++)
        {
            setchessp[add][pdd].site.x = x;
            setchessp[add][pdd].site.y = y;
            x += 70;
            //调试用 显示所有棋子
            //setchessp[add][pdd].isfull = 1;
        }
        x = 55;
        y += 70;
    }
    return 0;
}
//遍历全部棋子遇空就返回棋子的号码 无空则退出程序
int jud_exit(struct chess allchess[][4])
{
    int add;
    int pdd;
    int men = 1;

    int met = 0;
    for (add = 0; add < 4; add++)
    {
        for (pdd = 0; pdd < 4; pdd++)
        {
            if (allchess[add][pdd].isfull == 1)
                met++;
        }
    }
    for (add = 0; add < 4; add++)
    {
        for (pdd = 0; pdd < 3; pdd++)
        {
            if (allchess[add][pdd].chessnum == allchess[add][pdd + 1].chessnum)
            {
                men = 0;
                break;
            }
        }
    }
    for (add = 0; add < 3; add++)
    {
        for (pdd = 0; pdd < 4; pdd++)
        {
            if (allchess[add][pdd].chessnum == allchess[add+1][pdd].chessnum)
            {
                men = 0; 
                break;
            }
        }
    }

    if (met == 16&&men)
        return 1;
    else
        return 0;
}
//随机2或4的数字 供随机棋子调用 随机个数也用到此函数
int randomnum()
{
    int number;
    srand((unsigned)time(NULL));
    do
    {
        number = rand() % 4 + 1;
    } while (number!=4&&number!=2);
    //最坏的情况可能导致程序假死
    return number;
}
//随机棋子 随机一个位置,棋子为空就付给数字
int randomsite(struct chess chesssite[][4])
{
    /*
    if (jud_exit(chesssite))
    {
        MessageBox(NULL, TEXT("GANE OVER"), TEXT("2048"), MB_OK);
        exit(1);
    }
    */
    int site_x;
    int site_y;
    srand((unsigned)time(NULL));
    do
    {
        site_x = rand() % 4;
        site_y = rand() % 4;
    } while (chesssite[site_x][site_y].isfull!=0);
    //随机一个位置并赋值
    chesssite[site_x][site_y].chessnum = randomnum();
    chesssite[site_x][site_y].isfull = 1;

    return 1;
}
//棋子相加
//此处要计数显示分数
int addchessnum(struct chess * addchess0,struct chess * addchess1)
{
    //先来累积一下分数

    //数字加到第一个形参
    (addchess0->chessnum) += (addchess1->chessnum);
    score += (addchess0->chessnum);
    addchess1->chessnum = 0;
    addchess1->isfull = 0;
    addchess0->isfull = 1;

    return 1;
}
//移动非空棋子到空棋子
int movechess(struct chess * mchess0, struct chess * mchess1)
{
    mchess0->chessnum = mchess1->chessnum;
    mchess1->chessnum = 0;
    mchess0->isfull = 1;
    mchess1->isfull = 0;

    return 1;
}
/*下面的这一段都是垃圾,不过是大段的复用*/
//可以改为按下一次整个棋盘按方向完成工作
//判断一行
int jud_line(struct chess chess_line[][4], int line,int keyword)
{
    int add;
    int pdd;

    //w 87 a 65 s 83 d 68
    switch (keyword)
    {
    case 65:
        //左滑 序号小的棋子为第一参数
        for (add = 0; add < 3; add++)
        {
            for (pdd = add + 1; pdd < 4; pdd++)
            {
                if ((chess_line[line][add].chessnum != chess_line[line][pdd].chessnum) && chess_line[line][pdd].isfull&&chess_line[line][add].isfull)
                {
                    //中间棋子不为空,无法相加
                    break;
                }
                else if (chess_line[line][add].chessnum == chess_line[line][pdd].chessnum&&chess_line[line][add].isfull == 1)
                {
                    addchessnum((chess_line[line]) + add, (chess_line[line]) + pdd);
                    //找出可加的棋子 并完成相加
                    jud = 1;
                }
            }
        }
        //相加后出现空格以及原本存在的空格
        //移动棋子至相邻
        for (add = 0; add < 3;add++)
        {
            if (chess_line[line][add].isfull == 0)
            {
                for (pdd = add + 1; pdd < 4; pdd++)
                {
                    if (chess_line[line][pdd].isfull != 0)
                    {
                        movechess((chess_line[line]) + add, (chess_line[line]) + pdd);
                        jud = 1;
                        break;
                    }
                }
            }
        }
        break;
    case 68:
        for (add = 3; add >0 ; add--)
        {
            for (pdd = add - 1; pdd >= 0; pdd--)
            {
                if (chess_line[line][add].chessnum != chess_line[line][pdd].chessnum&&chess_line[line][pdd].isfull&&chess_line[line][pdd].isfull)
                {
                    //中间棋子不为空,无法相加
                    break;
                }
                else if (chess_line[line][add].chessnum == chess_line[line][pdd].chessnum&&chess_line[line][add].isfull)
                {
                    addchessnum((chess_line[line]) + pdd, (chess_line[line]) + add);
                    //找出可加的棋子 并完成相加
                    jud = 1;
                }
            }
        }
        //相加后出现空格以及原本存在的空格
        //移动棋子至相邻
        for (add = 3; add > 0; add--)
        {
            if (chess_line[line][add].isfull == 0)
            {
                for (pdd = add - 1; pdd >= 0; pdd--)
                {
                    if (chess_line[line][pdd].isfull != 0)
                    {
                        movechess((chess_line[line]) + add, (chess_line[line]) + pdd);
                        jud = 1;
                        break;
                    }
                }
            }
        }
        break;
    }

    return jud;
}
//判断一列并完成棋子的相加和移动
int jud_row(struct chess chess_row[][4], int row, int keyword)
{
    int add;
    int pdd;

    //w 87 a 65 s 83 d 68
    switch (keyword)
    {
    case 87:
        //上滑 序号小的棋子为第一参数
        for (add = 0; add < 3; add++)
        {
            for (pdd = add + 1; pdd < 4; pdd++)
            {
                if (chess_row[add][row].chessnum != chess_row[pdd][row].chessnum&&chess_row[pdd][row].isfull&&chess_row[add][row].isfull)
                {
                    //中间棋子不为空,无法相加
                    break;
                }
                else if (chess_row[add][row].chessnum == chess_row[pdd][row].chessnum&&chess_row[add][row].isfull == 1)
                {
                    addchessnum((chess_row[add]) + row, (chess_row[pdd]) + row);
                    //找出可加的棋子 并完成相加
                    jud = 1;
                }
            }
        }
        //相加后出现空格以及原本存在的空格
        for (add = 0; add < 3; add++)
        {
            if (chess_row[add][row].isfull == 0)
            {
                for (pdd = add + 1; pdd < 4; pdd++)
                {
                    if (chess_row[pdd][row].isfull != 0)
                    {
                        movechess(chess_row[add] + row, chess_row[pdd] + row);
                        jud = 1;
                        break;
                    }
                }
            }
        }
        break;
    case 83:
        for (add = 3; add >0; add--)
        {
            for (pdd = add - 1; pdd >= 0; pdd--)
            {
                if (chess_row[add][row].chessnum != chess_row[pdd][row].chessnum&&chess_row[pdd][row].isfull&&chess_row[add][row].isfull)
                {
                    //中间棋子不为空,无法相加
                    break;
                }
                else if (chess_row[add][row].chessnum == chess_row[pdd][row].chessnum&&chess_row[add][row].isfull == 1)
                {
                    addchessnum((chess_row[add]) + row, (chess_row[pdd]) + row);
                    //找出可加的棋子 并完成相加
                    jud = 1;
                }
            }
        }
        //相加后出现空格以及原本存在的空格
        //移动棋子至相邻
        for (add = 3; add > 0; add--)
        {
            if (chess_row[add][row].isfull == 0)
            {
                for (pdd = add - 1; pdd >= 0; pdd--)
                {
                    if (chess_row[pdd][row].isfull != 0)
                    {
                        movechess(chess_row[add] + row, chess_row[pdd] + row);
                        jud = 1;
                        break;
                    }
                }
            }
        }
        break;
    }

    return jud;
}
/*上面一段是垃圾,偷懒的复用*/

棋盘绘出

#include"2048paint.h"

//画棋盘的
int paintchessboard()
{
    beginPaint();
    //line(40, 50, 320, 330);
    int width = 70;

    line(40, 50, 40, 330);
    line(40, 50, 320, 50);
    line(320, 330, 40, 330);
    line(320, 330, 320, 50);
    //上面是棋盘范围
    //下面是棋盘的横竖线
    line(40 + width, 50, 40 + width, 330);
    line(40 + 2 * width, 50, 40 + 2 * width, 330);
    line(40 + 3 * width, 50, 40 + 3 * width, 330);
    line(40, 50 + width, 320, 50 + width);
    line(40, 50 + 2 * width, 320, 50 + 2 * width);
    line(40, 50 + 3 * width, 320, 50 + 3 * width);
    endPaint();

    //生成成功返回1
    return 1;

}
//画单个棋子 供重绘函数调用
int paintchess(struct chess * pchess)
{
    char buffer[10];
    sprintf_s(buffer, 9,"%d",pchess->chessnum);
    //画出棋子
    switch (pchess->isfull)
    {
    case 1:
        beginPaint();
        setTextSize(25);
        paintText((pchess->site).x, (pchess->site).y,buffer);
        endPaint();
        break;

    case 0:
        break;
    }

    //执行返回1
    return 1;
}
//显示分数 供重绘函数调用
int showscore(int s)
{
    char num[20];
    sprintf_s(num ,6, "%d" , s);

    beginPaint();
    setTextSize(30);
    paintText(170, 10, num);
    endPaint();

    return 1;
}
//刷新 供重绘函数调用
int refresh()
{
    beginPaint();
    clearDevice();
    endPaint();

    return 1;
}
//重绘画面 包括棋子 分数
int repaint(struct chess chesspoint[][4])
{
    int add;
    int pdd;
    //重画一副画面
    refresh();
    paintchessboard();
    showscore(score);
    jud = 0;
    //画出全部棋子
    for (add = 0; add < 4; add++)
    {
        for (pdd = 0; pdd < 4; pdd++)
        {
            paintchess(chesspoint[add]+pdd);

        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值