继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

c语言——控制台?esayx图形界面?------2048

路过的小白成长ing
关注TA
已关注
手记 13
粉丝 51
获赞 518
/************************************************************************************************************************
文件名称:main.c
文件描述:实现控制台的2048逻辑代码
编译环境:vs2015
最后修改:
<2017.1.8> <最后一次修改23:38 依然存在算法上的问题,在上下左右当中> <修改者:PorYoung>
<最近修改:2017.1.9 算法问题待定,另发现生成2和4时存在bug,整体有待优化><修改者:PorYoung>
<优化思路:①代码优化,减少函数个数;②功能优化:增加撤销功能,自动走一步功能;③算法优化:生成算法优化、移动、加法优化>
*************************************************************************************************************************/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

void Add(int dir);
void Print();
int map[4][1] = { { 0,0,0,0 }, };
void MoveRight();
void MoveLeft();
void MoveUp();
void MoveDown();

void Print()
{
    printf("\t\t\t\t*Welcome to 2048*\n");
    printf("\t\t*use     to play; press \"esc\" to reset*\n");
    printf("\t\t\t┏━━━┳━━━┳━━━┳━━━┓\n");
    printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[0][0], map[0][2], map[0][3], map[0][4]);
    printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
    printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
    printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
    printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[1][0], map[1][5], map[1][6], map[1][7]);
    printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
    printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
    printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[2][0], map[2][8], map[2][9], map[2][10]);
    printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
    printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
    printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[3][0], map[3][11], map[3][12], map[3][13]);
    printf("\t\t\t┗━━━┻━━━┻━━━┻━━━┛\n");

}

void Add(int dir) //第一次进入界面 dir=0; 上 dir=1; 下 dir=2; 左 dir=3; 右 dir=4
{
    srand((unsigned int)time(NULL));    //随机数
    int num, flag;  //2,4
    int row, col;
    row = rand() % 4;   //0-3
    col = rand() % 4;   //0-3
    flag = rand() % 2;  //flag 0 1
    if (flag == 0)
        num = 2;
    else
        num = 4;
    switch (dir)
    {
    case 0:
        map[row][col] = num; break;
    case 1://上
        if (map[3][col] == 0)
        {
            map[3][col] = num;
        }
        else
        {
            for (col = 0; col < 4; col)
                if (map[3][col] == 0) break;
        }
        break;
    case 2://下
        if (map[0][col] == 0)
        {
            map[0][col] = num;
        }
        else
        {
            for (col = 0; col < 4; col++)
            {
                if (map[0][col] == 0) break;
            }
        }
        break;
    case 3://左
        if (map[row][14] == 0)
        {
            map[row][15] = num;
        }
        else
        {
            for (row = 0; row < 4; row++)
            {
                if (map[row][16] == 0) break;         /*有疑问!!!*/
            }
        }
        break;
    case 4://右
        if (map[row][0] == 0)
        {
            map[row][0] = num;
        }
        else
        {
            for (row = 0; row < 4; row++)
            {
                if (map[row][0] == 0) break;
            }
        }
        break;
    }
}

//控制函数
void Move()
{
    char ch;
    int row, col;
    ch = _getch();
    switch (ch)
    {
    case 72://上
        MoveUp();
        break;
    case 80://下
        MoveDown();
        break;
    case 75://左
        MoveLeft();
        break;
    case 77://右
        MoveRight();
        break;
    case 27://重置
    {
        for (row = 0; row < 4; row++)
        {
            for (col = 0; col < 4; col++)
            {
                map[row][col] = 0;
            }
        }
        Add(0);
    }
    }
    system("cls");
}

void MoveUp()
{
    int temp, row, col;
    for (col = 0; col < 4; col++)
    {
        int n = 4;
        while (n--)
        {
            for (row = 0; row < 3; row++)
            {
                if (map[row][col] == 0)
                {
                    for (temp = row; temp < 3; temp++)
                    {
                        map[temp][col] = map[temp + 1][col];
                        map[temp + 1][col] = 0;
                    }
                }
            }
        }
        //实现加法
        for (row = 0; row<3; row++)
        {
            if (map[row][col] == map[row + 1][col])
            {
                map[row][col] = map[row][col] * 2;
                map[row + 1][col] = 0;
                for (temp = row + 1; temp<3; temp++)
                {
                    map[temp][col] = map[temp + 1][col];
                    map[temp + 1][col] = 0;
                }
            }
        }
    }
    Add(1);
}

void MoveDown()
{
    int row, col, temp;
    for (col = 0; col<4; col++)
    {
        int n = 4;
        while (n--)
        {
            for (row = 3; row>0; row--)
            {
                if (map[row][col] == 0)
                {
                    for (temp = row; temp>0; temp--)
                    {
                        map[temp][col] = map[temp - 1][col];
                        map[temp - 1][col] = 0;
                    }

                }
            }
        }
        //加法操作
        for (row = 3; row > 0; row--)
        {
            if (map[row][col] == map[row - 1][col])
            {
                map[row][col] = map[row][col] * 2;
                map[row - 1][col] = 0;
                for (temp = row - 1; temp > 0; temp--)
                {
                    map[temp][col] = map[temp - 1][col];
                    map[temp - 1][col] = 0;
                }
            }
        }
    }

    Add(2);
}

void MoveLeft()
{
    int row, col, temp;
    for (row = 0; row<4; row++)
    {
        int n = 4;
        while (n--)
        {
            for (col = 0; col < 4; col++)
            {
                if (map[row][col] == 0)
                {
                    for (temp = col; temp < 3; temp++)
                    {
                        map[row][temp] = map[row][temp + 1];
                        map[row][temp + 1] = 0;
                    }
                }
            }
        }
        //加法操作
        for (col = 0; col < 3; col++)
        {
            if (map[row][col] == map[row][col + 1])
            {
                map[row][col] = map[row][col] * 2;
                map[row][col + 1] = 0;
                for (temp = col + 1; temp < 3; temp++)
                {
                    map[row][temp] = map[row][temp + 1];
                    map[row][temp + 1] = 0;
                }
            }
        }
    }
    Add(3);
}

void MoveRight()
{
    int row, col, temp;
    for (row = 0; row<4; row++)
    {
        int n = 4;
        while (n--)
        {
            for (col = 3; col > 0; col--)
            {
                if (map[row][col] == 0)
                {
                    for (temp = col; temp>0; temp--)
                    {
                        map[row][temp] = map[row][temp - 1];
                        map[row][temp - 1] = 0;
                    }
                }
            }
        }
        //加法操作
        for (col = 3; col > 0; col--)
        {
            if (map[row][col] == map[row][col - 1])
            {
                map[row][col] = map[row][col] * 2;
                map[row][col - 1] = 0;
                for (temp = col - 1; temp > 0; temp--)
                {
                    map[row][temp] = map[row][temp - 1];
                    map[row][temp - 1] = 0;
                }
            }
        }
    }
    Add(4);
}

int main(void)
{
    printf("程序正在启动,请稍后......\n");
    Add(0);
    printf("按任意键开始游戏\n");
    _getch();
    Print();
    while (1)
    {
        Move();
        Print();
    }
    return 0;
}

效果图
效果图2
效果图3

/*************************************
工程名称:2048图形界面版
创建时间:2017.1.19
编译环境:VS2015
创建者:PorYoung
**************************************/

#include "graphics.h"
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "windows.h"
#include "resource.h" 
#include "stdio.h"

IMAGE bkImg;
int maps[4][4] = { 0 };
char str[4];
int temp = 0;       //游戏状态  

void gameShow();
int numShow();  //随机生成2或4
void gamePlay();
//void judge();

//4*4界面
void gameShow()
{
    if (temp == 0)
    {
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                maps[i][j] = 0;
        int x, y;
        x = rand() % 10 % 4;
        y = rand() % 10 % 4;
        maps[x][y] = numShow();
        temp = 1;
    }
    BeginBatchDraw();
    //putimage(0, 0, &bkImg);
    for (int i = 1; i < 4; i++)
    {
        setlinecolor(RGB(125, 125, 125));
        line(86, 40 + 80 * i, 415, 40 + 80 * i);
        line(88 + 82*i, 40, 88 + 82*i, 358);
    }
    for (int i = 0, x = 106, y = 60; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            sprintf(str, "%d", maps[i][j]);
            setbkmode(TRANSPARENT);
            settextcolor(RGB(25, 65, 125));
            settextstyle(40, 10, "微软雅黑");
            outtextxy(x, y, str);
            x += 80;
            if (x >= 400)
            {
                y += 82;
                x = 106;
            }
        }
    }
    EndBatchDraw();
}

int numShow()
{
    int r = rand() % 10, num;
    if (r < 5)  num = 2;
    else num = 4;
    return num;
}

void gamePlay()
{
    void upPlay();
    void downPlay();
    void leftPlay();
    void rightPlay();
    Sleep(100);
    char ch = _getch();
    switch (ch)
    {
        case 72:
            upPlay();
            break;  //上
        case 80:
            downPlay();
            break;  //下
        case 75:
            leftPlay();
            break;  //左
        case 77:
            rightPlay();
            break;  //右
        case 'r':
        case 'R':
            temp = 0;
            break;
    }
}

void upPlay()
{
    int row, col;

    //移动    
    for (col = 0; col < 4; col++)
    {
        int i = 3;
        while (i--)
        {
            for (row = 0; row < 3; row++)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row + 1][col];
                    maps[row + 1][col] = 0;
                }
            }
        }
    }
    //相加
    for (col = 0; col < 4; col++)
    {
        int i = 3;
        while (i--)
        {
            for (row = 0; row < 3; row++)
            {
                if (maps[row][col] == maps[row + 1][col])
                {
                    maps[row][col] *= 2;
                    maps[row + 1][col] = 0;
                }
            }
        }
    }
    //移动
    for (col = 0; col < 4; col++)
    {
        int i = 3;
        while (i--)
        {
            for (row = 0; row < 3; row++)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row + 1][col];
                    maps[row + 1][col] = 0;
                }
            }
        }
    }
    //新数字
    row = 3;        //加不加无所谓
    do
    {
        col = rand() % 10 % 4;
    } while (maps[row][col] != 0);
    maps[row][col] = numShow();
}

void downPlay()
{
    int row, col;

    //移动    
    for (col = 0; col < 4; col++)
    {
        int i = 3;
        while (i--)
        {
            for (row = 3; row > 0; row--)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row - 1][col];
                    maps[row - 1][col] = 0;
                }
            }
        }
    }
    //相加
    for (col = 0; col < 4; col++)
    {
        int i = 3;
        while (i--)
        {
            for (row = 3; row > 0; row--)
            {
                if (maps[row][col] == maps[row - 1][col])
                {
                    maps[row][col] *= 2;
                    maps[row - 1][col] = 0;
                }
            }
        }
    }
    //移动
    for (col = 0; col < 4; col++)
    {
        int i = 3;
        while (i--)
        {
            for (row = 3; row > 0; row--)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row - 1][col];
                    maps[row - 1][col] = 0;
                }
            }
        }
    }
    //新数字
    row = 0;
    do
    {
        col = rand() % 10 % 4;
    } while (maps[row][col] != 0);
    maps[row][col] = numShow();
}

void leftPlay()
{
    int row, col;

    //移动    
    for (row = 0; row < 4; row++)
    {
        int i = 3;
        while (i--)
        {
            for (col = 0; col < 3; col++)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row][col + 1];
                    maps[row][col + 1] = 0;
                }
            }
        }
    }
    //相加
    for (row = 0; row < 4; row++)
    {
        int i = 3;
        while (i--)
        {
            for (col = 0; col < 3; col++)
            {
                if (maps[row][col] == maps[row][col + 1])
                {
                    maps[row][col] *= 2;
                    maps[row][col + 1] = 0;
                }
            }
        }
    }
    //移动
    for (row = 0; row < 4; row++)
    {
        int i = 3;
        while (i--)
        {
            for (col = 0; col < 3; col++)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row][col + 1];
                    maps[row][col + 1] = 0;
                }
            }
        }
        //新数字
        col = 3;        //加不加无所谓
        do
        {
            row = rand() % 10 % 4;
        } while (maps[row][col] != 0);
        maps[row][col] = numShow();
    }
}

void rightPlay()
{
    int row, col;

    //移动    
    for (row = 0; row < 4; row++)
    {
        int i = 3;
        while (i--)
        {
            for (col = 3; col > 0; col--)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row][col - 1];
                    maps[row][col - 1] = 0;
                }
            }
        }
    }
    //相加
    for (row = 0; row < 4; row++)
    {
        int i = 3;
        while (i--)
        {
            for (col = 3; col > 0; col--)
            {
                if (maps[row][col] == maps[row][col - 1])
                {
                    maps[row][col] *= 2;
                    maps[row][col - 1] = 0;
                }
            }
        }
    }
    //移动
    for (row = 0; row < 4; row++)
    {
        int i = 3;
        while (i--)
        {
            for (col = 3; col > 0; col--)
            {
                if (maps[row][col] == 0)
                {
                    maps[row][col] = maps[row][col - 1];
                    maps[row][col - 1] = 0;
                }
            }
        }
    }
    //新数字
    col = 0;
    do
    {
        row = rand() % 10 % 4;
    } while (maps[row][col] != 0);
    maps[row][col] = numShow();
}

/*void judge()
{
    //判断是否结束
    int row, col, num = 0;
    for (row = 0; row < 4; row += 3)
        for (col = 0; col < 4; col++)
        {
            if (maps[row][col] != 0) num++;
        }
    for (col = 0; col < 4; col += 3)
        for (row = 0; row < 4; row++)
        {
            if (maps[row][col] != 0) num++;
        }
    if (num == 16)
    {
        setbkcolor(RGB(255, 255, 255));
        clearrectangle(88, 48, 412, 358);
        settextcolor(RGB(255, 100, 155));
        outtextxy(200, 170, "游戏结束!");
        Sleep(3000);
        temp = 0;
        clearrectangle(88, 48, 412, 358);
    }
}*/                //有bug,可不要

int main()
{
    initgraph(500, 400);
    loadimage(&bkImg, "IMAGE", MAKEINTRESOURCE(IDR_IMAGE1), 500, 400);
    putimage(0, 0, &bkImg);
    srand((unsigned int)time(NULL));
    while (1)
    {
        gameShow();
        gamePlay();
        setbkcolor(RGB(255, 255, 255));
        clearrectangle(88, 48, 412, 358);
        setlinecolor(RED);
        rectangle(235, 365, 260, 385);  //准备增加点击事件
    }
}

唯一遗憾的是算法没有模块化,代码没有精简,先把存货放出来,之后有时间再慢慢修改完善吧

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

大神啊 看了你的似乎懂了点什么

大神啊 看了你的似乎懂了点什么

查看全部评论